diff --git a/vlib/mysql/orm.v b/vlib/mysql/orm.v index c68be6330a..3a19a16092 100644 --- a/vlib/mysql/orm.v +++ b/vlib/mysql/orm.v @@ -256,7 +256,7 @@ fn mysql_type_from_v(typ int) ?string { 6, 10 { 'SMALLINT' } - 7, 11 { + 7, 11, orm.time { 'INT' } 8, 12 { diff --git a/vlib/orm/orm.v b/vlib/orm/orm.v index e86696096f..589031adf4 100644 --- a/vlib/orm/orm.v +++ b/vlib/orm/orm.v @@ -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') diff --git a/vlib/orm/orm_test.v b/vlib/orm/orm_test.v index 5be8fe274f..c5e4fe1a74 100644 --- a/vlib/orm/orm_test.v +++ b/vlib/orm/orm_test.v @@ -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 } diff --git a/vlib/pg/orm.v b/vlib/pg/orm.v index f40e643faa..b08992f115 100644 --- a/vlib/pg/orm.v +++ b/vlib/pg/orm.v @@ -181,7 +181,7 @@ fn pg_type_from_v(typ int) ?string { 6, 10 { 'SMALLINT' } - 7, 11 { + 7, 11, orm.time { 'INT' } 8, 12 { diff --git a/vlib/sqlite/orm.v b/vlib/sqlite/orm.v index a1df1c7793..f2e9310bed 100644 --- a/vlib/sqlite/orm.v +++ b/vlib/sqlite/orm.v @@ -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' diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 519b75159b..42c6b50c7d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 { diff --git a/vlib/v/gen/c/sql.v b/vlib/v/gen/c/sql.v index 68d053cd4c..ff9f6262c8 100644 --- a/vlib/v/gen/c/sql.v +++ b/vlib/v/gen/c/sql.v @@ -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