database.factory.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. angular.module('odoo')
  2. /**
  3. * -----------------------------------------------------------------------------
  4. * Description: Database tables automatically create
  5. * -----------------------------------------------------------------------------
  6. */
  7. .factory('databaseFactory', function (
  8. odooFactory,
  9. asyncLoopFactory
  10. ) {
  11. var mappings = [
  12. {
  13. model: 'res.partner',
  14. table: 'partner'
  15. },
  16. {
  17. model: 'crm.lead',
  18. table: 'crm_lead'
  19. },
  20. {
  21. model: 'crm.case.stage',
  22. table: 'crm_stage'
  23. }
  24. ];
  25. /**
  26. *
  27. */
  28. var buildCreateStatement = function (table, model) {
  29. var statement = 'CREATE TABLE IF NOT EXISTS ' + table + ' (\n';
  30. for (var key in model) {
  31. if (model.hasOwnProperty(key) && typeof model[key] == 'object') {
  32. if (model[key].type == 'float') {
  33. statement += key + ' NUMERIC DEFAULT 0,\n';
  34. }
  35. if (model[key].type == 'binary') {
  36. statement += key + ' BLOB DEFAULT NULL,\n';
  37. }
  38. if (model[key].type == 'char' || model[key].type == 'text' || model[key].type == 'date' || model[key].type == 'datetime') {
  39. statement += key + ' TEXT DEFAULT NULL,\n';
  40. }
  41. if (model[key].type == 'integer' || model[key].type == 'selection' || model[key].type == 'boolean' || model[key].type == 'many2one') {
  42. statement += key + ' INTEGER DEFAULT 0';
  43. if (key == 'id') {
  44. statement += ' PRIMARY KEY AUTOINCREMENT';
  45. }
  46. statement += ',\n';
  47. }
  48. if (model[key].type == 'one2many' || model[key].type == 'many2many') {
  49. continue;
  50. }
  51. }
  52. }
  53. statement += 'remote_id INTEGER DEFAULT 0,\n' +
  54. 'modified INTEGER DEFAULT 0,\n' +
  55. 'modified_date TEXT DEFAULT CURRENT_TIMESTAMP\n';
  56. return statement + ');'
  57. }
  58. /**
  59. *
  60. */
  61. var fetchMaps = function (success, error) {
  62. var maps = [];
  63. asyncLoopFactory(mappings.length, function (loop) {
  64. var map = mappings[loop.iteration()];
  65. odooFactory.fields(map.model, function (response) {
  66. maps.push({ table: map.table, model: response });
  67. loop.next();
  68. }, function (odooErr) {
  69. console.log(odooErr);
  70. loop.next();
  71. });
  72. }, function () {
  73. success(maps);
  74. });
  75. }
  76. /**
  77. *
  78. */
  79. var initTables = function (success, error) {
  80. fetchMaps(function (maps) {
  81. asyncLoopFactory(maps.length, function (loop) {
  82. var map = maps[loop.iteration()];
  83. var sql = buildCreateStatement(map.table, map.model);
  84. db.executeSql(sql, [], function (result) {
  85. console.log(result);
  86. loop.next();
  87. }, function (err) {
  88. console.log(err);
  89. loop.next();
  90. });
  91. }, function () {
  92. success(maps);
  93. });
  94. }, function (err) {
  95. error(err);
  96. });
  97. }
  98. return {
  99. fetchMaps: fetchMaps,
  100. initTables: initTables
  101. }
  102. });