import { Injectable } from '@angular/core'; import { DbSvc } from '../db/db'; import { Migration } from '../migration/migration'; @Injectable() export class MigrationSql { constructor(public db: DbSvc, public migration: Migration) { } public list = []; public tableName = "app_migrationsql"; public idColumn = "app_migrationsql_uu"; public init() { let promise = new Promise((resolve, reject) => { this.db.query("Select * from " + this.tableName).then((result) => { this.list = this.db.fetchAll(result); resolve(this.list); }, (reject) => { reject(reject); }); }); return promise; } public all() { return this.list; } public remove(obj) { let sql = "Delete from " + this.tableName + " WHERE " + this.idColumn + " = ? "; this.db.query(sql, [obj[this.idColumn]]).then((result) => { this.list.splice(this.list.indexOf(obj), 1); }); } public get(id) { for (let i = 0; i < this.list.length; i++) { if (this.list[i][this.idColumn] == id) { return this.list[i]; } } return null; } public add(row) { var promise = new Promise((resolve, reject) => { this.db.addTableRow(this.tableName, row, true).then((result) => { //because of updation as default commit 21 pass true as demo or avoid error this.list.push(row); resolve(result); }, (res) => { reject(res); }); }); return promise; } public update(row) { var promise = new Promise((resolve, reject) => { let obj = row if (obj) { this.db.updateTableRow(this.tableName, obj).then((result) => { for (let i = 0; i < this.list.length; i++) { if (this.list[i][this.idColumn] == obj[this.idColumn]) { this.list[i] = obj; break; } } resolve(result); }, (res) => { reject(res); }); } }); return promise; } public addCustomFields(string) { let temp = null; if (!string.match('app_updated') && !string.match('app_synced')) { return temp = (string.replace(');', " , 'app_updated' timestamp , 'app_synced' timestamp);")); } else { if (!string.match('app_updated')) { return temp = (string.replace(');', " , 'app_updated' timestamp);")); } if (!string.match('app_synced')) { return temp = (string.replace(');', " , 'app_synced' timestamp);")); } } } public applySQLByVersion(version, def) { let promise = new Promise((resolve, reject) => { let app_migration_uu = this.migration.getmigrationByVersion(version); let loopPromises = []; this.list.forEach(obj => { if (obj.app_migration_uu == app_migration_uu && obj.isapplied != "Y") { let prom = new Promise((resolve, reject) => { this.db.query(this.addCustomFields(obj.sql), []).then((result) => { obj.isapplied = "Y"; this.update(obj); resolve(); }, (res) => { reject(); }); loopPromises.push(prom); }); } }); Promise.all(loopPromises).then((res) => resolve(res), (err) => reject(err)); }); return promise; } public applySQL(obj) { this.db.query(obj.Sql, []).then((result) => { obj.isApplied = "Y"; this.update(obj); }); } public addArray(objList) { var promise = new Promise((resolve, reject) => { let loopPromises = []; objList.forEach(value => { var prom = new Promise((resolve, reject) => { if (this.get(value[this.idColumn]) == null) { this.add(value).then((res) => { resolve(); }, (res) => { reject(); }); } else { this.update(value).then((res) => { resolve(); }, (res) => { reject(); }); } }); loopPromises.push(prom); }); Promise.all(loopPromises).then((res) => resolve(resolve), (err) => resolve(err)); }); return promise; } }