-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathschema.v
126 lines (115 loc) · 2.4 KB
/
schema.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
module vsql
// import database.sql
import dialect.pg
// create database
// status:done
pub fn (mut db DB) create_database(name string) []pg.Row {
db.stmt.typ = .create_database
db.stmt.db_name = name
return db.end()
}
// create table
// status:done
pub fn (mut db DB) create_table(table_name string) Table {
mut table := Table{
db: db
name: table_name
}
return table
}
// create table if not exists
// status:done
pub fn (mut db DB) create_table_if_not_exist(table_name string) []pg.Row {
if db.has_table(table_name) {
println('table $table_name is already exists')
} else {
return db.create_table(table_name).exec()
}
return []pg.Row{}
}
// alter table
// status: wip
pub fn (mut db DB) alter_table() &DB {
return db
}
// rename table
// status:done
pub fn (mut db DB) rename_table(old_name string, new_name string) []pg.Row {
db.stmt.typ = .rename_table
db.stmt.table_name = old_name
db.stmt.new_table_name = new_name
return db.end()
}
// drop table
// status:done
pub fn (mut db DB) drop_table(name string) []pg.Row {
db.stmt.typ = .drop_table
db.stmt.table_name = name
return db.end()
}
// drop table
// status:done
pub fn (mut db DB) drop_table_if_exist(name string) []pg.Row {
if db.has_table(name) {
return db.drop_table(name)
}
return []pg.Row{}
}
// has
// staut:wip
// only pg is ok
pub fn (mut db DB) has_table(name string) bool {
mut s := ''
match db.config.client {
'pg' {
s = "select count(*) from information_schema.tables where table_schema=\'public\' and table_name ='$name'"
}
'mysql' {
// todo
}
'sqlite' {
// todo
}
else {
panic('unknown database client')
}
}
res := db.exec(s)
if res[0].vals[0] == '1' {
return true
} else {
return false
}
}
// staut:wip
// ERROR: syntax error at or near "and column_name"
pub fn (mut db DB) has_column(table_name string, column_name string) bool {
mut s := ''
match db.config.client {
'pg' {
s = "select count(*) from information_schema.columns where (table_schema=\'public\') and (table_name ='$table_name') and (column_name='$column_name')"
}
'mysql' {
// todo
}
'sqlite' {
// todo
}
else {
panic('unknown database client')
}
}
res := db.exec(s)
if res[0].vals[0] == '1' {
return true
} else {
return false
}
}
// truncate table
// status:done
pub fn (mut db DB) truncate(name string) []pg.Row {
db.stmt.typ = .truncate_table
db.stmt.table_name = name
return db.end()
}