angular.module('odoo') /** * ----------------------------------------------------------------------------- * Description: Database tables automatically create * ----------------------------------------------------------------------------- */ .factory('databaseFactory', function ( odooFactory, asyncLoopFactory ) { var mappings = [ { model: 'res.partner', table: 'partner' }, { model: 'crm.lead', table: 'crm_lead' }, { model: 'crm.case.stage', table: 'crm_stage' } ]; /** * */ var buildCreateStatement = function (table, model) { var statement = 'CREATE TABLE IF NOT EXISTS ' + table + ' (\n'; for (var key in model) { if (model.hasOwnProperty(key) && typeof model[key] == 'object') { if (model[key].type == 'float') { statement += key + ' NUMERIC DEFAULT 0,\n'; } if (model[key].type == 'binary') { statement += key + ' BLOB DEFAULT NULL,\n'; } if (model[key].type == 'char' || model[key].type == 'text' || model[key].type == 'date' || model[key].type == 'datetime') { statement += key + ' TEXT DEFAULT NULL,\n'; } if (model[key].type == 'integer' || model[key].type == 'selection' || model[key].type == 'boolean' || model[key].type == 'many2one') { statement += key + ' INTEGER DEFAULT 0'; if (key == 'id') { statement += ' PRIMARY KEY AUTOINCREMENT'; } statement += ',\n'; } if (model[key].type == 'one2many' || model[key].type == 'many2many') { continue; } } } statement += 'remote_id INTEGER DEFAULT 0,\n' + 'modified INTEGER DEFAULT 0,\n' + 'modified_date TEXT DEFAULT CURRENT_TIMESTAMP\n'; return statement + ');' } /** * */ var fetchMaps = function (success, error) { var maps = []; asyncLoopFactory(mappings.length, function (loop) { var map = mappings[loop.iteration()]; odooFactory.fields(map.model, function (response) { maps.push({ table: map.table, model: response }); loop.next(); }, function (odooErr) { console.log(odooErr); loop.next(); }); }, function () { success(maps); }); } /** * */ var initTables = function (success, error) { fetchMaps(function (maps) { asyncLoopFactory(maps.length, function (loop) { var map = maps[loop.iteration()]; var sql = buildCreateStatement(map.table, map.model); db.executeSql(sql, [], function (result) { console.log(result); loop.next(); }, function (err) { console.log(err); loop.next(); }); }, function () { success(maps); }); }, function (err) { error(err); }); } return { fetchMaps: fetchMaps, initTables: initTables } });