Created
October 1, 2010 21:09
-
-
Save mattlanham/606869 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This JS file will hold all functions relating to people | |
Ti.include('includes/phpjs.js'); | |
function Person() { | |
// Class variables | |
this.table = 'PEOPLE'; | |
this.db = 'gifterapp'; | |
this.className = 'Person'; | |
this.columns = ['id', 'name', 'budget', 'created', 'updated']; | |
this.row = []; | |
this.rows = []; | |
/* | |
getById | |
This will return a person based on their ID | |
*/ | |
this.getById = function(id) { | |
// Open the DB | |
var db = Titanium.Database.open(this.db); | |
// Get the person based on id | |
var result = db.execute("SELECT * FROM " + this.table + " WHERE ID = ? LIMIT 1", id); | |
if(result.getRowCount() == 1){ | |
// Set all of this classes variables to the data | |
for ( var i=0, len=this.columns.length; i<len; ++i ){ | |
this[this.columns[i]] = result.fieldByName(this.columns[i]); | |
} | |
// Close the DB | |
db.close(); | |
// Return result | |
return true; | |
}else{ | |
// Close the DB | |
db.close(); | |
// Return result | |
return false; | |
} | |
// Close the DB | |
db.close(); | |
}; | |
/* | |
getAll | |
This will return all people in the DB | |
*/ | |
this.getAll = function() { | |
// Set an array | |
var rows = []; | |
var rowCount = 0; | |
// Open the DB | |
var db = Titanium.Database.open(this.db); | |
// Get all rows | |
var result = db.execute("SELECT * FROM " + this.table); | |
// Loop through results | |
if(result.getRowCount() > 0){ | |
while (result.isValidRow()) | |
{ | |
eval('var thisRow = new ' + this.className + '();'); | |
// Set all of this classes variables to the data | |
for ( var i=0, len=this.columns.length; i<len; ++i ){ | |
thisRow[this.columns[i]] = result.fieldByName(this.columns[i]); | |
} | |
rows[rowCount] = thisRow; | |
rowCount++; | |
result.next(); | |
} | |
// Close the DB | |
db.close(); | |
// Return the result | |
return rows; | |
}else{ | |
// Close the DB | |
db.close(); | |
// Return the result | |
return false; | |
} | |
}; | |
/* | |
countAll | |
This function will simply return the integer of how many rows there are | |
in the DB | |
*/ | |
this.countAll = function() { | |
// Open the DB | |
var db = Titanium.Database.open(this.db); | |
// Get the person based on id | |
var result = db.execute("SELECT * FROM " + this.table); | |
// Return number of rows | |
return result.getRowCount(); | |
}; | |
/* | |
Save | |
This function will save the information to the database | |
*/ | |
this.save = function() { | |
Ti.API.info('Function save()'); | |
// Open the DB | |
var db = Titanium.Database.open(this.db); | |
if(typeof(this.id) == 'undefined') { | |
Ti.API.info('Uhuh...'); | |
} | |
if(typeof this.id == 'undefined'){ | |
Ti.API.info('Started writing SQL'); | |
// This is a new user | |
this.created = date('Y-m-d H:i:s'); | |
this.updated = date('Y-m-d H:i:s'); | |
var sql = "INSERT INTO " + this.table + " ("; | |
// Set all of this classes variables to the data | |
for ( var i=0, len=this.columns.length; i<len; ++i ){ | |
if(this.columns[i] != 'id'){ | |
sql += this.columns[i].toUpperCase() + ","; | |
} | |
} | |
sql = sql.substr(0, sql.length - 1); | |
sql += ") VALUES ("; | |
// Set all of this classes variables to the data | |
for ( var ii=0, leni=this.columns.length; ii<leni; ++ii ){ | |
if(this.columns[ii] != 'id'){ | |
sql += "'" + this[this.columns[ii]] + "',"; | |
} | |
} | |
sql = sql.substr(0, sql.length - 1); | |
sql += ")"; | |
Ti.API.info(sql); | |
db.execute(sql); | |
// Close the DB | |
db.close(); | |
}else{ | |
Ti.API.info('Update existing record'); | |
// Update existing record | |
this.updated = date('Y-m-d H:i:s'); | |
var updateSQL = "UPDATE " + this.table + " SET "; | |
// Set all of this classes variables to the data | |
for ( var x=0, l=this.columns.length; x<l; ++x ){ | |
if(this.columns[x] != 'id'){ | |
if(this.columns[x] != 'created'){ | |
updateSQL += this.columns[x].toUpperCase() + " = '" + this[this.columns[x]] + "',"; | |
} | |
}else{ | |
updateSQLID = " WHERE ID = " + this[this.columns[x]] + ""; | |
} | |
} | |
updateSQL = updateSQL.substr(0, updateSQL.length - 1); | |
updateSQL+= updateSQLID; | |
Ti.API.info(updateSQL); | |
db.execute(updateSQL); | |
// Close the DB | |
db.close(); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment