Lungo.Data.Sql.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * Wrapper for using WebSql (HTML5 feature)
  3. *
  4. * @namespace LUNGO.Data
  5. * @class Sql
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  9. */
  10. LUNGO.Data.Sql = (function(lng, undefined) {
  11. var CONFIG = {
  12. name: 'lungo_db',
  13. version: '1.0',
  14. size: 65536,
  15. schema: []
  16. };
  17. var db = null;
  18. /**
  19. * Initialize the SQLite storage (HTML5 Feature)
  20. *
  21. * @method init
  22. *
  23. * @param {object} Configuration for the Database
  24. */
  25. var init = function(db_config) {
  26. CONFIG = lng.Core.mix(CONFIG, db_config);
  27. db = openDatabase(CONFIG.name, CONFIG.version, CONFIG.name, CONFIG.size);
  28. if (db) {
  29. _createSchema();
  30. } else {
  31. lng.Core.log(3, 'lng.Data.Sql >> Failed to connect to database.');
  32. }
  33. };
  34. /**
  35. * Select a data set of a given table and based on a selection object
  36. *
  37. * @method select
  38. *
  39. * @param {string} Name of the table in the database
  40. * @param {object} [OPTIONAL] Object selection condition
  41. * @param {Function} Callback when the process is complete
  42. */
  43. var select = function(table, where_obj, callback) {
  44. var where = (where_obj) ? ' WHERE ' + _convertToSql(where_obj, 'AND') : '';
  45. execute('SELECT * FROM ' + table + where, function(rs) {
  46. var result = [];
  47. for (var i = 0, len = rs.rows.length; i < len; i++) {
  48. result.push(rs.rows.item(i));
  49. }
  50. _callbackResponse(callback, result);
  51. });
  52. };
  53. /**
  54. * Inserts a data set of a given table and based on a data object
  55. *
  56. * @method insert
  57. *
  58. * @param {string} Name of the table in the database
  59. * @param {object} Data object to insert in table
  60. */
  61. var insert = function(table, data_obj) {
  62. var fields = '';
  63. var values = '';
  64. for (var field in data_obj) {
  65. if (lng.Core.isOwnProperty(data_obj, field)) {
  66. var value = data_obj[field];
  67. fields += (fields) ? ', ' + field : field;
  68. if (values) values += ', ';
  69. values += (isNaN(value)) ? '"' + value + '"' : value;
  70. }
  71. }
  72. execute('INSERT INTO ' + table + ' (' + fields + ') VALUES (' + values + ')');
  73. };
  74. /**
  75. * Updates a data set of a given table and based on a data object and
  76. * an optional selection object
  77. *
  78. * @method update
  79. *
  80. * @param {string} Name of the table in the database
  81. * @param {object} Data object to update in table
  82. * @param {object} [OPTIONAL] Object selection condition
  83. */
  84. var update = function(table, data_obj, where_obj) {
  85. var sql = 'UPDATE ' + table + ' SET ' + _convertToSql(data_obj, ',');
  86. if (where_obj) sql += ' WHERE ' + _convertToSql(where_obj, 'AND');
  87. execute(sql);
  88. };
  89. /**
  90. * Delete a data set of a given table and based on a selection object
  91. *
  92. * @method drop
  93. *
  94. * @param {string} Name of the table in the database
  95. * @param {object} [OPTIONAL] Object selection condition
  96. */
  97. var drop = function(table, where_obj) {
  98. var where = (where_obj) ? ' WHERE ' + _convertToSql(where_obj, 'AND') : null;
  99. execute('DELETE FROM ' + table + where + ';');
  100. };
  101. /**
  102. * Executes a SQL statement in the SQLite storage
  103. *
  104. * @method execute
  105. *
  106. * @param {string} SQL statement
  107. * @param {Function} Callback when the process is complete
  108. */
  109. var execute = function(sql, callback) {
  110. lng.Core.log(1, 'lng.Data.Sql >> ' + sql);
  111. db.transaction(function(tx) {
  112. tx.executeSql(sql, [], function(tx, rs) {
  113. _callbackResponse(callback, rs);
  114. }, _throwError);
  115. });
  116. };
  117. var _createSchema = function() {
  118. var schema = CONFIG.schema;
  119. var schema_len = schema.length;
  120. if (!schema_len) return;
  121. for (var i = 0; i < schema_len; i++) {
  122. var current = schema[i];
  123. _regenerateTable(current);
  124. _createTable(current.name, current.fields);
  125. }
  126. };
  127. var _createTable = function(table, fields) {
  128. var sql_fields = '';
  129. for (var field in fields) {
  130. if (lng.Core.isOwnProperty(fields, field)) {
  131. if (sql_fields) sql_fields += ', ';
  132. sql_fields += field + ' ' + fields[field];
  133. }
  134. }
  135. execute('CREATE TABLE IF NOT EXISTS ' + table + ' (' + sql_fields + ');');
  136. };
  137. var _regenerateTable = function(table) {
  138. if (table.drop === true) {
  139. _dropTable(table.name);
  140. }
  141. };
  142. var _dropTable = function(table) {
  143. execute('DROP TABLE IF EXISTS ' + table);
  144. };
  145. var _convertToSql = function(fields, separator) {
  146. var sql = '';
  147. for (var field in fields) {
  148. if (lng.Core.isOwnProperty(fields, field)) {
  149. var value = fields[field];
  150. if (sql) sql += ' ' + separator + ' ';
  151. sql += field + '=';
  152. sql += (isNaN(value)) ? '"' + value + '"' : value;
  153. }
  154. }
  155. return sql;
  156. };
  157. var _callbackResponse = function(callback, response) {
  158. if (lng.Core.toType(callback) === 'function') {
  159. setTimeout(callback, 100, response);
  160. }
  161. };
  162. var _throwError = function(transaction, error) {
  163. lng.Core.log(3, 'lng.Data.Sql >> ' + error.code + ': ' + error.message);
  164. };
  165. return {
  166. init: init,
  167. select: select,
  168. insert: insert,
  169. update: update,
  170. drop: drop,
  171. execute: execute
  172. };
  173. })(LUNGO);