alter_table.anubis
9.6 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
*Project* Anubis
*Title* SQLite Alter table function
*Copyright* Copyright (c) Cédric Ricard 2007.
*Author* Cédric Ricard
*Created* 2007 02 17
*Satus* Released
*Compatibility* 1.7.10
read tools/basis.anubis
read data_base/db_tools.anubis
read data_base/alter_table.anubis
read system/logger.anubis
read system/string.anubis
public define Maybe(One)
alter_table
(
Database db,
String tableName,
String newTableQuery,
Logger log
).
--- That's all for the public part ! --------------------------------------------------
define String
to_String
(
DbEntityType type
) =
if type is
{
table then "table",
view then "view",
index then "index",
trigger then "trigger"
}.
define One
print_list
(
List(String) l
) =
if l is
{
[] then print(" <<<\n"),
[h . t] then print(h + ", "); print_list(t)
}.
define Maybe(One)
my_sql_query
(
Database db,
String sql,
Logger log
) =
if sql_query(db, sql) is
{
error(sql_error) then logError(log, "SQL ERROR: " + sql_error.text + "\n ...executing query [" + sql + "]"); failure,
ok(_,_,_) then success(unique)
}.
define Maybe(String)
get_original_query
(
Database db,
String dbName, // alias for attached database, 'main' for main database.
DbEntityType object_type,
String tableName,
Logger log
) =
if sql_query(db, "SELECT sql FROM " + dbName + ".sqlite_master where (type='"+to_String(object_type)+"') and tbl_name='" + tableName + "'") is
{
error(sql_error) then logError(log, "alter_table/get_original_query ERROR '" + sql_error.text + "'"); failure,
ok(_,table_cursor,_) then
if table_cursor(unique) is
{
error(sql_error) then logError(log, "alter_table/get_original_query ERROR '" + sql_error.text + "'"); failure,
no_more_row then failure, //can't find the table in sqlite_master, because the table is new
row(current) then success(text(current)(0))
}
}.
define Bool
has_rows
(
Database db,
String tableName,
Logger log
) =
if sql_query(db, "SELECT count(*) FROM " + tableName) is
{
error(sql_error) then logError(log, "has_rows query ERROR '" + sql_error.text + "'"); false,
ok(_,table_cursor,_) then
if table_cursor(unique) is
{
error(sql_error) then logError(log, "has_row run ERROR '" + sql_error.text + "'"); false,
no_more_row then false,
row(current) then db_integer(current)(0) /= 0
}
}.
define List(String)
alter_table_extract_columns_list_sub
(
Int -> SQLite3Datum row,
Int colIndex
) =
if row(colIndex) is text(name) then [name . alter_table_extract_columns_list_sub(row, colIndex+1)]
else [].
define List(String)
alter_table_extract_columns_list
(
SQLite3HeadersOrRow -> SQLite3Row table_cursor,
Logger log
) =
if table_cursor(headers) is
{
error(sql_error) then logError(log, "alter_table_extract_columns_list ERROR '" + sql_error.text + "'"); [],
no_more_row then [],
row(headers) then alter_table_extract_columns_list_sub(headers, 0)
}.
define String
alter_table_extract_column_string
(
One -> DbRow table_cursor,
List(String) oldColumns,
Logger log
) =
if table_cursor(unique) is
{
error(sql_error) then logError(log, "alter_table_extract_columns_list ERROR '" + sql_error.text + "'"); "",
no_more_row then /*logDebug(log, "Extract columns answers 'no more row'");*/ "",
row(row) then
if row(1) is db_text(name) then
with test = (String name2) |-> logDebug(log, "name: " + name + " / name2: " + name2); if name = name2 then true else false,
colString = alter_table_extract_column_string(table_cursor, oldColumns, log),
if find_element(oldColumns, test) is
{
failure then colString,
success(_) then logDebug(log, "found: " + name);
if is_empty(colString) then name else name + ", " + colString
}
else ""
}.
// false = different, true = same
define Bool
compare_creation_queries
(
String tableName,
String query1,
String query2,
) =
if find(tableName, query1, 0) is
{
failure then false,
success(p1) then
if find(tableName, query2, 0) is
{
failure then false,
success(p2) then
if sub_string(query1, p1, length(query1) - p1) is
{
failure then false,
success(s1) then
if sub_string(query2, p2, length(query2) - p2) is
{
failure then false,
success(s2) then s1 = s2
}
}
}
}.
public define Maybe(One)
alter_table
(
Database db,
String dbName, // alias for attached database, 'main' for main database.
String tableName,
String newTableQuery,
Logger log
) =
with fullTableName = dbName + "." + tableName,
if get_original_query(db, dbName, table, tableName, log) is
{
failure then
logInfo(log, "Creating table '" + fullTableName + "'... ");
if sql_query(db, newTableQuery) is error(sql_error)
then logError(log, "ERROR : " + sql_error.text); failure
else logInfo(log, " --> ok."); success(unique),
success(originalTableQuery) then
if compare_creation_queries(tableName, originalTableQuery, newTableQuery) = false then
logInfo(log, "Updating table '" + fullTableName + "' on database... ");
println(originalTableQuery);
println(newTableQuery);
if has_rows(db, fullTableName, log) is
{
false then
if my_sql_query(db, "DROP TABLE " + fullTableName, log) is failure
then failure
else if my_sql_query(db, newTableQuery, log ) is
{
failure then failure,
success(_) then logInfo(log, " --> ok."); success(unique)
},
true then
with tempNameTable = tableName + "_temp",
forget(sql_query(db, "DROP TABLE " + tempNameTable));
if my_sql_query(db, "CREATE TEMP TABLE " + tempNameTable + " AS SELECT * from " + fullTableName, log) is failure then failure else
if my_sql_query(db, "DROP TABLE " + fullTableName, log) is failure then failure else
if my_sql_query(db, newTableQuery , log) is failure then failure else
if sql_query(db, "SELECT * FROM " + tempNameTable) is
{
error(sql_error) then logError(log, "alter_table: select * tempTable ERROR '" + sql_error.text + "'"); failure,
ok(get_cols,table_cursor2,_) then
with oldColumns = get_cols(unique),
//print_list(oldColumns);
if sql_query(db, "PRAGMA " + dbName + ".table_info(" + tableName + ")") is
{
error(sql_error) then logError(log, "alter_table: pragma table_info() ERROR '" + sql_error.text + "'"); failure,
ok(_,table_cursor3,_) then
with newColumnsStr = alter_table_extract_column_string(table_cursor3, oldColumns, log),
sql = "INSERT INTO " + fullTableName + " (" + newColumnsStr + ") SELECT " + newColumnsStr + " FROM " + tempNameTable,
if sql_query(db, sql) is
{
error(sql_error) then logError(log, "alter_table ERROR '" + sql_error.text + "'\n\tquery = [" + sql + "]"); failure,
ok(_,_,_) then logInfo(log, " --> ok."); success(unique)
}
}
}
}
else
success(unique)
}.
public define Maybe(One)
alter_table
(
Database db,
String tableName,
String newTableQuery,
Logger log
) =
alter_table(db, "main", tableName, newTableQuery, log).
public define Maybe(One)
alter_view
(
Database db,
String dbName, // alias for attached database, 'main' for main database.
String viewName,
String newViewQuery,
Logger log
) =
with fullViewName = dbName + "." + viewName,
if get_original_query(db, dbName, view, viewName, log) is
{
failure then
logInfo(log, "Creating view '" + fullViewName + "'... ");
if sql_query(db, newViewQuery) is error(sql_error)
then logError(log, "ERROR : " + sql_error.text); failure
else logInfo(log, " --> ok."); success(unique),
success(originalViewQuery) then
if compare_creation_queries(viewName, originalViewQuery, newViewQuery) = false then
logInfo(log, "Updating view '" + fullViewName + "' on database... ");
println(originalViewQuery);
println(newViewQuery);
if my_sql_query(db, "DROP VIEW " + fullViewName, log) is failure
then failure
else if my_sql_query(db, newViewQuery, log ) is
{
failure then failure,
success(_) then logInfo(log, " --> ok."); success(unique)
}
else
success(unique)
}.
public define Maybe(One)
alter_view
(
Database db,
String viewName,
String newViewQuery,
Logger log
) =
alter_view(db, "main", viewName, newViewQuery, log).