forked from jamespaulmuir/AppceleratorRecord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappcelerator_database.js
60 lines (48 loc) · 1.61 KB
/
appcelerator_database.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
var AppceleratorDatabase = function(){
var name;
var tableName;
var api = {};
this.db = null;
this.createSQL = null;
this.databaseExists = false;
this.initialize = function(args){
this.tableName = args.tableName;
this.name = args.name;
this.createSQL = args.createSQL;
this.indexes = args.indexes;
this.db = Ti.Database.open(this.name);
};
this.execute = function(SQL){
if( typeof(AppceleratorRecordConfig) != 'undefined' && AppceleratorRecordConfig.logging == true ){ Ti.API.notice(SQL); }
return this.db.execute(SQL);
};
this.createTable = function(){
var SQL = "CREATE TABLE IF NOT EXISTS " + this.tableName + ' ' + this.createSQL;
this.execute(SQL);
};
this.firstColumnValue = function(SQL){
var resultSet = this.execute(SQL);
var x = resultSet.field(0);
resultSet.close();
return x;
};
this.columnNames = function(){
var SQL = "SELECT * FROM sqlite_master WHERE tbl_name = '"+this.tableName+"' AND sql LIKE 'CREATE TABLE%'";
var resultSet = this.execute(SQL);
if( !resultSet.isValidRow() ){ Ti.API.notice("INVALID RESULTSET!!"); return; }
while (resultSet.isValidRow()) {
var h = {};
for(var i=0; i<resultSet.fieldCount(); i++){ h[resultSet.fieldName(i)] = resultSet.field(i); }
resultSet.next();
}
var columns = [];
h.sql.match(/\({1}.*\){1}/)[0].replace('(', '').replace(')','').split(/,/).each(function(c){
columns.push( c.match(/\s*(\w*)\s/)[0].match(/\s*([a-zA-Z_]*)/)[0].replace(' ', '') );
});
return columns;
};
this.columnExists = function(column){
return this.columnNames().indexOf(column) >= 0;
};
return this;
};