1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
import orm
fn test_orm_stmt_gen_update() {
query := orm.orm_stmt_gen('Test', "'", .update, true, '?', 0, orm.QueryData{
fields: ['test', 'a']
data: []
types: []
kinds: []
}, orm.QueryData{
fields: ['id', 'name']
data: []
types: []
kinds: [.ge, .eq]
})
assert query == "UPDATE 'Test' SET 'test' = ?0, 'a' = ?1 WHERE 'id' >= ?2 AND 'name' = ?3;"
}
fn test_orm_stmt_gen_insert() {
query := orm.orm_stmt_gen('Test', "'", .insert, true, '?', 0, orm.QueryData{
fields: ['test', 'a']
data: []
types: []
kinds: []
}, orm.QueryData{})
assert query == "INSERT INTO 'Test' ('test', 'a') VALUES (?0, ?1);"
}
fn test_orm_stmt_gen_delete() {
query := orm.orm_stmt_gen('Test', "'", .delete, true, '?', 0, orm.QueryData{
fields: ['test', 'a']
data: []
types: []
kinds: []
}, orm.QueryData{
fields: ['id', 'name']
data: []
types: []
kinds: [.ge, .eq]
})
assert query == "DELETE FROM 'Test' WHERE 'id' >= ?0 AND 'name' = ?1;"
}
fn get_select_fields() []string {
return ['id', 'test', 'abc']
}
fn test_orm_select_gen() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
}, "'", true, '?', 0, orm.QueryData{})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' ORDER BY 'id' ASC;"
}
fn test_orm_select_gen_with_limit() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
has_limit: true
}, "'", true, '?', 0, orm.QueryData{})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' ORDER BY 'id' ASC LIMIT ?0;"
}
fn test_orm_select_gen_with_where() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
has_where: true
}, "'", true, '?', 0, orm.QueryData{
fields: ['abc', 'test']
kinds: [.eq, .gt]
is_and: [true]
})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' WHERE 'abc' = ?0 AND 'test' > ?1 ORDER BY 'id' ASC;"
}
fn test_orm_select_gen_with_order() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
has_order: true
order_type: .desc
}, "'", true, '?', 0, orm.QueryData{})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' ORDER BY '' DESC;"
}
fn test_orm_select_gen_with_offset() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
has_offset: true
}, "'", true, '?', 0, orm.QueryData{})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' ORDER BY 'id' ASC OFFSET ?0;"
}
fn test_orm_select_gen_with_all() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
has_limit: true
has_order: true
order_type: .desc
has_offset: true
has_where: true
}, "'", true, '?', 0, orm.QueryData{
fields: ['abc', 'test']
kinds: [.eq, .gt]
is_and: [true]
})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' WHERE 'abc' = ?0 AND 'test' > ?1 ORDER BY '' DESC LIMIT ?2 OFFSET ?3;"
}
fn test_orm_table_gen() {
query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{
name: 'id'
typ: 7
default_val: '10'
attrs: [
StructAttribute{
name: 'primary'
},
StructAttribute{
name: 'sql'
has_arg: true
arg: 'serial'
kind: .plain
},
]
},
orm.TableField{
name: 'test'
typ: 18
},
orm.TableField{
name: 'abc'
typ: 8
default_val: '6754'
},
], sql_type_from_v, false) or { panic(err) }
assert query == "CREATE TABLE IF NOT EXISTS 'test_table' ('id' SERIAL DEFAULT 10, 'test' TEXT, 'abc' INT64 DEFAULT 6754, PRIMARY KEY('id'));"
alt_query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{
name: 'id'
typ: 7
default_val: '10'
attrs: [
StructAttribute{
name: 'primary'
},
StructAttribute{
name: 'sql'
has_arg: true
arg: 'serial'
kind: .plain
},
]
},
orm.TableField{
name: 'test'
typ: 18
},
orm.TableField{
name: 'abc'
typ: 8
default_val: '6754'
},
], sql_type_from_v, true) or { panic(err) }
assert alt_query == "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='test_table' and xtype='U') CREATE TABLE 'test_table' ('id' SERIAL DEFAULT 10, 'test' TEXT, 'abc' INT64 DEFAULT 6754, PRIMARY KEY('id'));"
}
fn sql_type_from_v(typ int) ?string {
return if typ in orm.nums {
'INT'
} else if typ in orm.num64 {
'INT64'
} else if typ in orm.float {
'DOUBLE'
} else if typ == orm.string {
'TEXT'
} else if typ == -1 {
'SERIAL'
} else {
error('Unknown type $typ')
}
}
|