123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 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
- }
- });
|