Skip to content

Instantly share code, notes, and snippets.

@andergmartins
Created February 19, 2013 13:13
Show Gist options
  • Save andergmartins/4985745 to your computer and use it in GitHub Desktop.
Save andergmartins/4985745 to your computer and use it in GitHub Desktop.
Ti.App.Properties.setList('DB_queue', []);
Ti.App.Properties.setBool('DB_locked', false);
Ti.App.Properties.setBool('DB_queue_running', false);
Ti.include('db.js', 'examples.js');
var DB = Ti.Database.open('');
var Database = D = {
'queue': Ti.App.Properties.getList('DB_queue', []),
'locked': Ti.App.Properties.getBool('DB_locked', false),
'running': Ti.App.Properties.getBool('DB_queue_running', false),
'isValidRow': false,
'rows': [],
'fields': {
'*': [] // instead of ['all', 'fields', 'must', 'be', 'used', 'with', '*'] store them here and use D.fields['*'] for a shortcut
},
lockDB: function() {
this.locked = true;
Ti.App.Properties.setBool('DB_locked', true);
},
unlockDB: function() {
this.locked = false;
Ti.App.Properties.setBool('DB_locked', false);
},
addQueueItem: function(params) {
this.queue.push(params);
Ti.App.Properties.setList('DB_queue', this.queue);
this.runQueue();
},
runQueueItem: function(params) {
Ti.API.info('beforeRun -> DB_locked: '+this.locked);
this.lockDB();
Ti.API.info('onRun -> DB_locked: '+this.locked);
DB = Ti.Database.open('starleads_01');
this.isValidRow = false;
switch(params.type) {
case 'create':
DB.execute(params.sql);
break;
case 'select':
if(params.selectWhere) {
var rows = DB.execute(params.sql, params.args);
} else {
var rows = DB.execute(params.sql);
}
var results = [];
while(rows.isValidRow()) {
this.isValidRow = true;
var row = {};
for(var i=0,l=params.fields.length;i<l;i++) {
row[params.fields[i]] = rows.field(i);
}
results.push(row);
rows.next();
}
this.rows = results;
rows.close();
break;
case 'insert':
DB.execute(params.sql, params.args);
break;
case 'update':
DB.execute(params.sql, params.args);
break;
case 'delete':
DB.execute(params.sql, params.args);
break;
}
DB.close();
this.unlockDB();
Ti.API.info('afterRun - >DB_locked: '+this.locked);
},
runQueue: function() {
this.running = true;
Ti.App.Properties.setBool('DB_queue_running', true);
if(this.locked) {
Ti.API.info('locked -> runQueue');
this.runQueue();
} else {
Ti.API.info('unlocked -> que: '+ this.queue.length);
if(this.queue.length > 0) {
Ti.API.info('runQueueItem');
var queueItem = this.queue.shift();
this.runQueueItem(queueItem);
this.runQueue();
} else {
this.running = false;
Ti.App.Properties.setBool('DB_queue_running', false);
}
}
},
exec: function(params) {
if(this.locked) {
this.addQueueItem(params);
} else {
this.runQueueItem(params);
}
}
};
// CREATE
D.exec({
type: 'create',
sql: 'CREATE TABLE SYNTAX'
});
// INSERT
D.exec({
type: 'insert',
sql: 'INSERT INTO SYNTAX',
args: ['field1', 'field2']
});
// SELECT w/out WHERE
D.exec({
type: 'select',
sql: 'SELECT * FROM SYNTAX',
fields: ['all', 'fields', 'must', 'be', 'used', 'with', '*']
});
alert(D.rows);
D.exec({
type: 'select',
sql: 'SELECT field1, field2 FROM SYNTAX',
fields: ['field1', 'field2']
});
alert(D.rows);
// SELECT w/ WHERE
D.exec({
type: 'select',
selectWhere: true,
sql: 'SELECT * FROM table WHERE ARG = ? SYNTAX',
fields: ['all', 'fields', 'must', 'be', 'used', 'with', '*'],
args: [ARG]
});
alert(D.rows);
// UPDATE
D.exec({
type: 'update',
sql: 'UPDATE table SET field1 = ?, field2 = ? WHERE ARG = ?',
args: ['field1', 'field2', 'ARG']
});
// DELETE
D.exec({
type: 'delete',
sql: 'DELETE FROM table WHERE ARG = ?',
args: [ARG]
});
// ROW EXISTS
D.exec({
type: 'select',
selectWhere: true,
sql: 'SELECT * FROM table WHERE ARG = ?',
fields: ['all', 'fields', 'must', 'be', 'used', 'with', '*']
args: [ARG]
});
alert(D.isValidRow);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment