orm: fix time (#11026)

pull/11036/head
Louis Schmieder 2021-08-03 04:17:00 +02:00 committed by GitHub
parent 6dcf72fe9b
commit a55ba08fad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 15 deletions

View File

@ -256,7 +256,7 @@ fn mysql_type_from_v(typ int) ?string {
6, 10 {
'SMALLINT'
}
7, 11 {
7, 11, orm.time {
'INT'
}
8, 12 {

View File

@ -342,7 +342,7 @@ pub fn orm_table_gen(table string, para string, defaults bool, def_unique_len in
mut field_name := sql_field_name(field)
mut ctyp := sql_from_v(sql_field_type(field)) or {
field_name = '${field_name}_id'
sql_from_v(8) ?
sql_from_v(7) ?
}
if ctyp == '' {
return error('Unknown type ($field.typ) for field $field.name in struct $table')

View File

@ -1,6 +1,7 @@
// import os
// import pg
// import term
import time
import sqlite
struct Module {
@ -23,6 +24,11 @@ struct Foo {
age int
}
struct TestTime {
id int [primary; sql: serial]
create time.Time
}
fn test_orm_sqlite() {
db := sqlite.connect(':memory:') or { panic(err) }
db.exec('drop table if exists User')
@ -287,4 +293,24 @@ fn test_orm_sqlite() {
}
assert first.age == 60
sql db {
create table TestTime
}
tnow := time.now()
time_test := TestTime{
create: tnow
}
sql db {
insert time_test into TestTime
}
data := sql db {
select from TestTime where create == tnow
}
assert data.len == 1
}

View File

@ -181,7 +181,7 @@ fn pg_type_from_v(typ int) ?string {
6, 10 {
'SMALLINT'
}
7, 11 {
7, 11, orm.time {
'INT'
}
8, 12 {

View File

@ -140,7 +140,8 @@ fn (stmt Stmt) sqlite_select_column(idx int, typ int) ?orm.Primitive {
} else if typ == orm.string {
primitive = stmt.get_text(idx).clone()
} else if typ == orm.time {
primitive = time.unix(stmt.get_int(idx))
d := stmt.get_int(idx)
primitive = time.unix(d)
} else {
return error('Unknown type $typ')
}
@ -149,7 +150,7 @@ fn (stmt Stmt) sqlite_select_column(idx int, typ int) ?orm.Primitive {
}
fn sqlite_type_from_v(typ int) ?string {
return if typ in orm.nums || typ < 0 || typ in orm.num64 {
return if typ in orm.nums || typ < 0 || typ in orm.num64 || typ == orm.time {
'INTEGER'
} else if typ in orm.float {
'REAL'

View File

@ -7651,9 +7651,10 @@ fn (mut c Checker) sql_expr(mut node ast.SqlExpr) ast.Type {
info := sym.info as ast.Struct
fields := c.fetch_and_verify_orm_fields(info, node.table_expr.pos, sym.name)
mut sub_structs := map[int]ast.SqlExpr{}
for f in fields.filter(c.table.type_symbols[int(it.typ)].kind == .struct_
for f in fields.filter((c.table.type_symbols[int(it.typ)].kind == .struct_
|| (c.table.get_type_symbol(it.typ).kind == .array
&& c.table.get_type_symbol(c.table.get_type_symbol(it.typ).array_info().elem_type).kind == .struct_)) {
&& c.table.get_type_symbol(c.table.get_type_symbol(it.typ).array_info().elem_type).kind == .struct_))
&& c.table.get_type_name(it.typ) != 'time.Time') {
typ := if c.table.get_type_symbol(f.typ).kind == .struct_ {
f.typ
} else if c.table.get_type_symbol(f.typ).kind == .array {
@ -7752,9 +7753,10 @@ fn (mut c Checker) sql_stmt_line(mut node ast.SqlStmtLine) ast.Type {
info := table_sym.info as ast.Struct
fields := c.fetch_and_verify_orm_fields(info, node.table_expr.pos, table_sym.name)
mut sub_structs := map[int]ast.SqlStmtLine{}
for f in fields.filter((c.table.type_symbols[int(it.typ)].kind == .struct_)
for f in fields.filter(((c.table.type_symbols[int(it.typ)].kind == .struct_)
|| (c.table.get_type_symbol(it.typ).kind == .array
&& c.table.get_type_symbol(c.table.get_type_symbol(it.typ).array_info().elem_type).kind == .struct_)) {
&& c.table.get_type_symbol(c.table.get_type_symbol(it.typ).array_info().elem_type).kind == .struct_))
&& c.table.get_type_name(it.typ) != 'time.Time') {
typ := if c.table.get_type_symbol(f.typ).kind == .struct_ {
f.typ
} else if c.table.get_type_symbol(f.typ).kind == .array {

View File

@ -114,7 +114,11 @@ fn (mut g Gen) sql_create_table(node ast.SqlStmtLine, expr string, table_name st
sym := g.table.get_type_symbol(field.typ)
g.write('(orm__TableField){')
g.write('.name = _SLIT("$field.name"),')
g.write('.typ = ${int(field.typ)},')
mut typ := int(field.typ)
if sym.name == 'time.Time' {
typ = -2
}
g.write('.typ = $typ,')
g.write('.is_arr = ${sym.kind == .array}, ')
g.write('.is_time = ${int(g.table.get_type_name(field.typ) == 'time__Time')},')
g.write('.default_val = (string){.str = (byteptr) "$field.default_val", .is_lit = 1},')
@ -151,7 +155,7 @@ fn (mut g Gen) sql_insert(node ast.SqlStmtLine, expr string, table_name string,
for f in node.fields {
sym := g.table.get_type_symbol(f.typ)
if sym.kind == .struct_ {
if sym.kind == .struct_ && sym.name != 'time.Time' {
subs << node.sub_structs[int(f.typ)]
} else if sym.kind == .array {
mut f_key := ''
@ -206,12 +210,12 @@ fn (mut g Gen) sql_insert(node ast.SqlStmtLine, expr string, table_name string,
continue
}
mut sym := g.table.get_type_symbol(f.typ)
if sym.kind == .struct_ {
mut typ := sym.cname
if sym.kind == .struct_ && typ != 'time__Time' {
g.write('(*(orm__Primitive*) array_get($last_ids_arr, $structs)),')
structs++
continue
}
mut typ := sym.cname
if typ == 'time__Time' {
typ = 'time'
}
@ -571,6 +575,10 @@ fn (mut g Gen) sql_select(node ast.SqlExpr, expr string, left string) {
for field in select_fields {
g.write('_SLIT("${g.get_field_name(field)}"),')
sym := g.table.get_type_symbol(field.typ)
if sym.name == 'time.Time' {
types << -2
continue
}
if sym.kind == .struct_ {
types << int(ast.int_type)
continue
@ -664,7 +672,7 @@ fn (mut g Gen) sql_select(node ast.SqlExpr, expr string, left string) {
for i, field in fields {
sel := '(*(orm__Primitive*) array_get((*(Array_orm__Primitive*) array_get($res, $idx)), $i))'
sym := g.table.get_type_symbol(field.typ)
if sym.kind == .struct_ {
if sym.kind == .struct_ && sym.name != 'time.Time' {
mut sub := node.sub_structs[int(field.typ)]
mut where_expr := sub.where_expr as ast.InfixExpr
mut ident := where_expr.right as ast.Ident
@ -819,7 +827,7 @@ fn (mut g Gen) get_field_name(field ast.StructField) string {
}
}
sym := g.table.get_type_symbol(field.typ)
if sym.kind == .struct_ {
if sym.kind == .struct_ && sym.name != 'time.Time' {
name = '${name}_id'
}
return name