1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

orm,sqlite,mysql,pg: cleanup import v.ast, using typeof[Type]()

This commit is contained in:
Delyan Angelov 2022-12-07 12:26:41 +02:00
parent 4eb81d2f05
commit 458e68e196
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
5 changed files with 49 additions and 54 deletions

View File

@ -1,7 +1,6 @@
import orm import orm
import mysql import mysql
import time import time
import v.ast
struct TestCustomSqlType { struct TestCustomSqlType {
id int [primary; sql: serial] id int [primary; sql: serial]
@ -51,7 +50,7 @@ fn test_mysql_orm() {
db.create('Test', [ db.create('Test', [
orm.TableField{ orm.TableField{
name: 'id' name: 'id'
typ: ast.int_type_idx typ: typeof[int]().idx
attrs: [ attrs: [
StructAttribute{ StructAttribute{
name: 'primary' name: 'primary'
@ -66,12 +65,12 @@ fn test_mysql_orm() {
}, },
orm.TableField{ orm.TableField{
name: 'name' name: 'name'
typ: ast.string_type_idx typ: typeof[string]().idx
attrs: [] attrs: []
}, },
orm.TableField{ orm.TableField{
name: 'age' name: 'age'
typ: ast.int_type_idx typ: typeof[int]().idx
}, },
]) or { panic(err) } ]) or { panic(err) }
@ -84,11 +83,11 @@ fn test_mysql_orm() {
table: 'Test' table: 'Test'
has_where: true has_where: true
fields: ['id', 'name', 'age'] fields: ['id', 'name', 'age']
types: [ast.int_type_idx, ast.string_type_idx, ast.i64_type_idx] types: [typeof[int]().idx, typeof[string]().idx, typeof[i64]().idx]
}, orm.QueryData{}, orm.QueryData{ }, orm.QueryData{}, orm.QueryData{
fields: ['name', 'age'] fields: ['name', 'age']
data: [orm.Primitive('Louis'), i64(101)] data: [orm.Primitive('Louis'), i64(101)]
types: [ast.string_type_idx, ast.i64_type_idx] types: [typeof[string]().idx, typeof[i64]().idx]
is_and: [true, true] is_and: [true, true]
kinds: [.eq, .eq] kinds: [.eq, .eq]
}) or { panic(err) } }) or { panic(err) }

View File

@ -1,39 +1,38 @@
module orm module orm
import time import time
import v.ast
pub const ( pub const (
num64 = [ast.i64_type_idx, ast.u64_type_idx] num64 = [typeof[i64]().idx, typeof[u64]().idx]
nums = [ nums = [
ast.i8_type_idx, typeof[i8]().idx,
ast.i16_type_idx, typeof[i16]().idx,
ast.int_type_idx, typeof[int]().idx,
ast.u8_type_idx, typeof[u8]().idx,
ast.u16_type_idx, typeof[u16]().idx,
ast.u32_type_idx, typeof[u32]().idx,
ast.bool_type_idx, typeof[bool]().idx,
] ]
float = [ float = [
ast.f32_type_idx, typeof[f32]().idx,
ast.f64_type_idx, typeof[f64]().idx,
] ]
type_string = ast.string_type_idx type_string = typeof[string]().idx
time = -2 time = -2
serial = -1 serial = -1
type_idx = { type_idx = {
'i8': ast.i8_type_idx 'i8': typeof[i8]().idx
'i16': ast.i16_type_idx 'i16': typeof[i16]().idx
'int': ast.int_type_idx 'int': typeof[int]().idx
'i64': ast.i64_type_idx 'i64': typeof[i64]().idx
'u8': ast.u8_type_idx 'u8': typeof[u8]().idx
'u16': ast.u16_type_idx 'u16': typeof[u16]().idx
'u32': ast.u32_type_idx 'u32': typeof[u32]().idx
'u64': ast.u64_type_idx 'u64': typeof[u64]().idx
'f32': ast.f32_type_idx 'f32': typeof[f32]().idx
'f64': ast.f64_type_idx 'f64': typeof[f64]().idx
'bool': ast.bool_type_idx 'bool': typeof[bool]().idx
'string': ast.string_type_idx 'string': typeof[string]().idx
} }
string_max_len = 2048 string_max_len = 2048
) )

View File

@ -1,5 +1,4 @@
import orm import orm
import v.ast
fn test_orm_stmt_gen_update() { fn test_orm_stmt_gen_update() {
query, _ := orm.orm_stmt_gen('Test', "'", .update, true, '?', 0, orm.QueryData{ query, _ := orm.orm_stmt_gen('Test', "'", .update, true, '?', 0, orm.QueryData{
@ -121,7 +120,7 @@ fn test_orm_table_gen() {
query := orm.orm_table_gen('test_table', "'", true, 0, [ query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{ orm.TableField{
name: 'id' name: 'id'
typ: ast.int_type_idx typ: typeof[int]().idx
default_val: '10' default_val: '10'
attrs: [ attrs: [
StructAttribute{ StructAttribute{
@ -137,11 +136,11 @@ fn test_orm_table_gen() {
}, },
orm.TableField{ orm.TableField{
name: 'test' name: 'test'
typ: ast.string_type_idx typ: typeof[string]().idx
}, },
orm.TableField{ orm.TableField{
name: 'abc' name: 'abc'
typ: ast.i64_type_idx typ: typeof[i64]().idx
default_val: '6754' default_val: '6754'
}, },
], sql_type_from_v, false) or { panic(err) } ], sql_type_from_v, false) or { panic(err) }
@ -150,7 +149,7 @@ fn test_orm_table_gen() {
alt_query := orm.orm_table_gen('test_table', "'", true, 0, [ alt_query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{ orm.TableField{
name: 'id' name: 'id'
typ: ast.int_type_idx typ: typeof[int]().idx
default_val: '10' default_val: '10'
attrs: [ attrs: [
StructAttribute{ StructAttribute{
@ -166,11 +165,11 @@ fn test_orm_table_gen() {
}, },
orm.TableField{ orm.TableField{
name: 'test' name: 'test'
typ: ast.string_type_idx typ: typeof[string]().idx
}, },
orm.TableField{ orm.TableField{
name: 'abc' name: 'abc'
typ: ast.i64_type_idx typ: typeof[i64]().idx
default_val: '6754' default_val: '6754'
}, },
], sql_type_from_v, true) or { panic(err) } ], sql_type_from_v, true) or { panic(err) }
@ -179,7 +178,7 @@ fn test_orm_table_gen() {
unique_query := orm.orm_table_gen('test_table', "'", true, 0, [ unique_query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{ orm.TableField{
name: 'id' name: 'id'
typ: ast.int_type_idx typ: typeof[int]().idx
default_val: '10' default_val: '10'
attrs: [ attrs: [
StructAttribute{ StructAttribute{
@ -195,7 +194,7 @@ fn test_orm_table_gen() {
}, },
orm.TableField{ orm.TableField{
name: 'test' name: 'test'
typ: ast.string_type_idx typ: typeof[string]().idx
attrs: [ attrs: [
StructAttribute{ StructAttribute{
name: 'unique' name: 'unique'
@ -204,7 +203,7 @@ fn test_orm_table_gen() {
}, },
orm.TableField{ orm.TableField{
name: 'abc' name: 'abc'
typ: ast.i64_type_idx typ: typeof[i64]().idx
default_val: '6754' default_val: '6754'
}, },
], sql_type_from_v, false) or { panic(err) } ], sql_type_from_v, false) or { panic(err) }
@ -213,7 +212,7 @@ fn test_orm_table_gen() {
mult_unique_query := orm.orm_table_gen('test_table', "'", true, 0, [ mult_unique_query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{ orm.TableField{
name: 'id' name: 'id'
typ: ast.int_type_idx typ: typeof[int]().idx
default_val: '10' default_val: '10'
attrs: [ attrs: [
StructAttribute{ StructAttribute{
@ -229,7 +228,7 @@ fn test_orm_table_gen() {
}, },
orm.TableField{ orm.TableField{
name: 'test' name: 'test'
typ: ast.string_type_idx typ: typeof[string]().idx
attrs: [ attrs: [
StructAttribute{ StructAttribute{
name: 'unique' name: 'unique'
@ -241,7 +240,7 @@ fn test_orm_table_gen() {
}, },
orm.TableField{ orm.TableField{
name: 'abc' name: 'abc'
typ: ast.i64_type_idx typ: typeof[i64]().idx
default_val: '6754' default_val: '6754'
attrs: [ attrs: [
StructAttribute{ StructAttribute{

View File

@ -2,7 +2,6 @@ module main
import orm import orm
import pg import pg
import v.ast
import time import time
struct TestCustomSqlType { struct TestCustomSqlType {
@ -51,7 +50,7 @@ fn test_pg_orm() {
db.create('Test', [ db.create('Test', [
orm.TableField{ orm.TableField{
name: 'id' name: 'id'
typ: ast.string_type_idx typ: typeof[string]().idx
is_time: false is_time: false
default_val: '' default_val: ''
is_arr: false is_arr: false
@ -72,7 +71,7 @@ fn test_pg_orm() {
}, },
orm.TableField{ orm.TableField{
name: 'name' name: 'name'
typ: ast.string_type_idx typ: typeof[string]().idx
is_time: false is_time: false
default_val: '' default_val: ''
is_arr: false is_arr: false
@ -80,7 +79,7 @@ fn test_pg_orm() {
}, },
orm.TableField{ orm.TableField{
name: 'age' name: 'age'
typ: ast.i64_type_idx typ: typeof[i64]().idx
is_time: false is_time: false
default_val: '' default_val: ''
is_arr: false is_arr: false
@ -104,7 +103,7 @@ fn test_pg_orm() {
primary: 'id' primary: 'id'
has_offset: false has_offset: false
fields: ['id', 'name', 'age'] fields: ['id', 'name', 'age']
types: [ast.int_type_idx, ast.string_type_idx, ast.i64_type_idx] types: [typeof[int]().idx, typeof[string]().idx, typeof[i64]().idx]
}, orm.QueryData{}, orm.QueryData{ }, orm.QueryData{}, orm.QueryData{
fields: ['name', 'age'] fields: ['name', 'age']
data: [orm.Primitive('Louis'), orm.Primitive(101)] data: [orm.Primitive('Louis'), orm.Primitive(101)]

View File

@ -1,6 +1,5 @@
import orm import orm
import sqlite import sqlite
import v.ast
import time import time
struct TestCustomSqlType { struct TestCustomSqlType {
@ -35,7 +34,7 @@ fn test_sqlite_orm() {
db.create('Test', [ db.create('Test', [
orm.TableField{ orm.TableField{
name: 'id' name: 'id'
typ: ast.int_type_idx typ: typeof[int]().idx
attrs: [ attrs: [
StructAttribute{ StructAttribute{
name: 'primary' name: 'primary'
@ -50,12 +49,12 @@ fn test_sqlite_orm() {
}, },
orm.TableField{ orm.TableField{
name: 'name' name: 'name'
typ: ast.string_type_idx typ: typeof[string]().idx
attrs: [] attrs: []
}, },
orm.TableField{ orm.TableField{
name: 'age' name: 'age'
typ: ast.i64_type_idx typ: typeof[i64]().idx
}, },
]) or { panic(err) } ]) or { panic(err) }
@ -68,11 +67,11 @@ fn test_sqlite_orm() {
table: 'Test' table: 'Test'
has_where: true has_where: true
fields: ['id', 'name', 'age'] fields: ['id', 'name', 'age']
types: [ast.int_type_idx, ast.string_type_idx, ast.i64_type_idx] types: [typeof[int]().idx, typeof[string]().idx, typeof[i64]().idx]
}, orm.QueryData{}, orm.QueryData{ }, orm.QueryData{}, orm.QueryData{
fields: ['name', 'age'] fields: ['name', 'age']
data: [orm.Primitive('Louis'), i64(100)] data: [orm.Primitive('Louis'), i64(100)]
types: [ast.string_type_idx, ast.i64_type_idx] types: [typeof[string]().idx, typeof[i64]().idx]
is_and: [true, true] is_and: [true, true]
kinds: [.eq, .eq] kinds: [.eq, .eq]
}) or { panic(err) } }) or { panic(err) }