-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-sqlite-creator.js
84 lines (47 loc) · 1.55 KB
/
node-sqlite-creator.js
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
var sqlite = require('sqlite');
var TableCreator = exports.TableCreator = function(db){
this.db = db;
};
TableCreator.prototype.create = function(tableName, columns){
return this._exec_sql(this.create_sql(tableName,columns));
};
TableCreator.prototype.drop = function(tableName){
return this._exec_sql(this.drop_sql(tableName));
};
TableCreator.prototype.rename = function(tableName, newTableName){
return this._exec_sql(this.rename_sql(tableName, newTableName));
};
TableCreator.prototype.add_columns = function(tableName, columns){
return this._exec_sql(this.add_columns_sql(tableName, columns));
};
TableCreator.prototype.create_sql = function(tableName, columns){
var sql = 'CREATE TABLE IF NOT EXISTS ' + tableName + '(';
var cols = [];
for(c in columns){
cols.push(c + ' ' + columns[c]);
}
sql += cols.join(',') + ');';
return sql;
};
TableCreator.prototype.drop_sql = function(tableName){
return 'DROP TABLE IF EXISTS ' + tableName + ';';
};
TableCreator.prototype.rename_sql = function(tableName, newTableName){
return 'ALTER TABLE ' + tableName + ' RENAME TO ' + newTableName;
};
TableCreator.prototype.add_columns_sql = function(tableName, columns){
var sql = '';
for(c in columns){
sql += 'ALTER TABLE ' + tableName + ' ADD COLUMN ' + c + ' ' + columns[c] + ';';
}
return sql;
};
TableCreator.prototype._exec_sql = function(script){
this.db.executeScript(script, function(error){
if (error){
console.log('This sql did not work:\r\n' + script);
return false;
}
});
return true;
};