-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathquery.v
210 lines (183 loc) · 4.57 KB
/
query.v
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
module vsql
// status:done
pub fn (mut db DB) table(name string) &DB {
// handle table alias,like:select * from person as p
table_name, table_alias := split_by_separator(name, 'as')
db.stmt.typ = .select_
db.stmt.table_name = table_name
db.stmt.table_alias = table_alias
return db
}
// status:done
pub fn (mut db DB) column(columns string) &DB {
if columns.trim_space() in ['', '*'] {
db.stmt.columns = []Column{}
} else {
column_array := columns.split(',')
for col in column_array {
// handle column and column alias,like:column('id,name as name2,age as age2')
name, alias := split_by_separator(col, 'as')
db.stmt.columns << Column{
name: name
alias: alias
}
}
}
return db
}
// the same with table()
// status:done
pub fn (mut db DB) from(name string) &DB {
return db.table(name)
}
// the same with column()
// status:done
pub fn (mut db DB) select_(columns string) &DB {
return db.column(columns)
}
// status:done
pub fn (mut db DB) first() &DB {
db.stmt.limit = 1
return db
}
// status:done
pub fn (mut db DB) limit(num int) &DB {
if num <= 0 {
panic('limit must great zero')
}
db.stmt.limit = num
return db
}
// status:done
pub fn (mut db DB) offset(num int) &DB {
if num <= 0 {
panic('offset must great zero')
}
db.stmt.offset = num
return db
}
// status:done
pub fn (mut db DB) distinct() &DB {
db.stmt.is_distinct = true
return db
}
// status:done
pub fn (mut db DB) group_by(column string) &DB {
db.stmt.group_by << column
return db
}
// status:done
pub fn (mut db DB) group_by_raw(raw string) &DB {
if db.stmt.group_by.len > 0 {
panic('when use group_by_raw,the group_by will be ignored,remove group_by first')
}
db.stmt.group_by_raw = raw
return db
}
// status:done
pub fn (mut db DB) order_by(column string) &DB {
col, mut order := split_by_space(column)
if order == '' {
order = 'asc'
}
if order !in ['asc', 'desc'] {
panic('order by must be asc or desc')
}
// check col is already in order_by array
for c in db.stmt.order_by {
if c.column == col {
panic('$col is already in order by')
}
}
db.stmt.order_by << OrderBy{
column: col
order: order
}
return db
}
// status:done
pub fn (mut db DB) order_by_raw(raw string) &DB {
if db.stmt.order_by.len > 0 {
panic('when use order_by_raw,the order_by will be ignored,remove order_by first')
}
db.stmt.order_by_raw = raw
return db
}
// status:done
pub fn (mut db DB) having(condition string) &DB {
db.stmt.having = condition
return db
}
// union statement
// status:done
pub fn (mut db DB) union_type(typ string, stmt string, other_stmts ...string) &DB {
db.stmt.union_type = typ
db.stmt.union_stmts << stmt
for s in other_stmts {
db.stmt.union_stmts << s
}
return db
}
// status:done
pub fn (mut db DB) union_(stmt string, other_stmts ...string) &DB {
return db.union_type('union', stmt, ...other_stmts)
}
// status:done
pub fn (mut db DB) union_all(stmt string, other_stmts ...string) &DB {
return db.union_type('union all', stmt, ...other_stmts)
}
// status:done
pub fn (mut db DB) intersect(stmt string, other_stmts ...string) &DB {
return db.union_type('intersect', stmt, ...other_stmts)
}
// status:done
pub fn (mut db DB) except(stmt string, other_stmts ...string) &DB {
return db.union_type('except', stmt, ...other_stmts)
}
// join statement
// status:done
pub fn (mut db DB) join(table string, join_condition string) &DB {
return db.join_type('join', table, join_condition)
}
// status:done
pub fn (mut db DB) inner_join(table string, join_condition string) &DB {
return db.join_type('inner join', table, join_condition)
}
// status:done
pub fn (mut db DB) left_join(table string, join_condition string) &DB {
return db.join_type('left join', table, join_condition)
}
// status:done
pub fn (mut db DB) right_join(table string, join_condition string) &DB {
return db.join_type('right join', table, join_condition)
}
// status:done
pub fn (mut db DB) outer_join(table string, join_condition string) &DB {
return db.join_type('full outer join', table, join_condition)
}
// status:done
// Cross join only supported in MySQL and SQLite3
pub fn (mut db DB) cross_join(table string) &DB {
return db.join_type('cross join', table, '')
}
// status:done
pub fn (mut db DB) join_raw(raw string) &DB {
db.stmt.join_raw = raw
return db
}
// status:done
fn (mut db DB) join_type(typ string, table string, join_condition string) &DB {
name, alias := split_by_separator(table, 'as')
db.stmt.join << Join{
typ: typ
table_name: name
table_alias: alias
join_condition: join_condition
}
return db
}
// result to struct
// status:wip
pub fn (mut db DB) to() &DB {
return db
}