Guest User

Untitled

a guest
Jun 25th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. // This JS file will hold all functions relating to people
  2. Ti.include('includes/phpjs.js');
  3.  
  4. function Person() {
  5. // Class variables
  6. this.table = 'PEOPLE';
  7. this.db = 'gifterapp';
  8. this.className = 'Person';
  9. this.columns = ['id', 'name', 'budget', 'created', 'updated'];
  10. this.row = [];
  11. this.rows = [];
  12.  
  13. /*
  14. getById
  15. This will return a person based on their ID
  16. */
  17. this.getById = function(id) {
  18. // Open the DB
  19. var db = Titanium.Database.open(this.db);
  20. // Get the person based on id
  21. var result = db.execute("SELECT * FROM " + this.table + " WHERE ID = ? LIMIT 1", id);
  22. if(result.getRowCount() == 1){
  23. // Set all of this classes variables to the data
  24. for ( var i=0, len=this.columns.length; i<len; ++i ){
  25. this[this.columns[i]] = result.fieldByName(this.columns[i]);
  26. }
  27. // Close the DB
  28. db.close();
  29. // Return result
  30. return true;
  31. }else{
  32. // Close the DB
  33. db.close();
  34. // Return result
  35. return false;
  36. }
  37. // Close the DB
  38. db.close();
  39. };
  40. /*
  41. getAll
  42. This will return all people in the DB
  43. */
  44. this.getAll = function() {
  45. // Set an array
  46. var rows = [];
  47. var rowCount = 0;
  48. // Open the DB
  49. var db = Titanium.Database.open(this.db);
  50. // Get all rows
  51. var result = db.execute("SELECT * FROM " + this.table);
  52. // Loop through results
  53. if(result.getRowCount() > 0){
  54. while (result.isValidRow())
  55. {
  56. eval('var thisRow = new ' + this.className + '();');
  57. // Set all of this classes variables to the data
  58. for ( var i=0, len=this.columns.length; i<len; ++i ){
  59. thisRow[this.columns[i]] = result.fieldByName(this.columns[i]);
  60. }
  61. rows[rowCount] = thisRow;
  62. rowCount++;
  63. result.next();
  64. }
  65. // Close the DB
  66. db.close();
  67. // Return the result
  68. return rows;
  69. }else{
  70. // Close the DB
  71. db.close();
  72. // Return the result
  73. return false;
  74. }
  75.  
  76. };
  77.  
  78.  
  79. /*
  80. countAll
  81. This function will simply return the integer of how many rows there are
  82. in the DB
  83. */
  84. this.countAll = function() {
  85. // Open the DB
  86. var db = Titanium.Database.open(this.db);
  87. // Get the person based on id
  88. var result = db.execute("SELECT * FROM " + this.table);
  89. // Return number of rows
  90. return result.getRowCount();
  91. };
  92. /*
  93. Save
  94. This function will save the information to the database
  95. */
  96. this.save = function() {
  97. Ti.API.info('Function save()');
  98. // Open the DB
  99. var db = Titanium.Database.open(this.db);
  100.  
  101. if(typeof(this.id) == 'undefined') {
  102. Ti.API.info('Uhuh...');
  103. }
  104.  
  105. if(typeof this.id == 'undefined'){
  106. Ti.API.info('Started writing SQL');
  107.  
  108. // This is a new user
  109. this.created = date('Y-m-d H:i:s');
  110. this.updated = date('Y-m-d H:i:s');
  111.  
  112. var sql = "INSERT INTO " + this.table + " (";
  113. // Set all of this classes variables to the data
  114. for ( var i=0, len=this.columns.length; i<len; ++i ){
  115. if(this.columns[i] != 'id'){
  116. sql += this.columns[i].toUpperCase() + ",";
  117. }
  118. }
  119. sql = sql.substr(0, sql.length - 1);
  120.  
  121. sql += ") VALUES (";
  122.  
  123. // Set all of this classes variables to the data
  124. for ( var ii=0, leni=this.columns.length; ii<leni; ++ii ){
  125. if(this.columns[ii] != 'id'){
  126. sql += "'" + this[this.columns[ii]] + "',";
  127. }
  128. }
  129. sql = sql.substr(0, sql.length - 1);
  130. sql += ")";
  131.  
  132. Ti.API.info(sql);
  133.  
  134. db.execute(sql);
  135.  
  136. // Close the DB
  137. db.close();
  138. }else{
  139. Ti.API.info('Update existing record');
  140. // Update existing record
  141. this.updated = date('Y-m-d H:i:s');
  142.  
  143. var updateSQL = "UPDATE " + this.table + " SET ";
  144. // Set all of this classes variables to the data
  145. for ( var x=0, l=this.columns.length; x<l; ++x ){
  146. if(this.columns[x] != 'id'){
  147. if(this.columns[x] != 'created'){
  148. updateSQL += this.columns[x].toUpperCase() + " = '" + this[this.columns[x]] + "',";
  149. }
  150. }else{
  151. updateSQLID = " WHERE ID = " + this[this.columns[x]] + "";
  152. }
  153. }
  154. updateSQL = updateSQL.substr(0, updateSQL.length - 1);
  155. updateSQL+= updateSQLID;
  156.  
  157. Ti.API.info(updateSQL);
  158.  
  159. db.execute(updateSQL);
  160.  
  161. // Close the DB
  162. db.close();
  163. }
  164. };
  165. }
Add Comment
Please, Sign In to add comment