Skip to content

Commit fded8f8

Browse files
author
fernandocode
committed
v.0.4.0-alpha.1
Implemented transaction managed and test for new feature.
1 parent f256924 commit fded8f8

File tree

13 files changed

+380
-119
lines changed

13 files changed

+380
-119
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ npm-debug.log
4040
yarn-error.log
4141
testem.log
4242
/typings
43+
debug.log
4344

4445
# System Files
4546
.DS_Store

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
"@angular/platform-browser-dynamic": "~7.2.0",
2424
"@angular/router": "~7.2.0",
2525
"core-js": "^2.5.4",
26-
"database-builder": "^0.5.0-alpha.3",
26+
"database-builder": "^0.5.0-alpha.8",
2727
"moment": "^2.22.0",
2828
"rxjs": "6.4.0",
29-
"tslib": "^1.9.0",
29+
"tslib": "^1.10.0",
3030
"zone.js": "~0.8.26"
3131
},
3232
"devDependencies": {
@@ -35,9 +35,9 @@
3535
"@angular/cli": "~7.3.5",
3636
"@angular/compiler-cli": "~7.2.0",
3737
"@angular/language-service": "~7.2.0",
38-
"@types/node": "~8.9.4",
3938
"@types/jasmine": "~2.8.8",
4039
"@types/jasminewd2": "~2.0.3",
40+
"@types/node": "~8.9.4",
4141
"codelyzer": "~4.5.0",
4242
"jasmine-core": "~2.99.1",
4343
"jasmine-spec-reporter": "~4.2.1",
@@ -50,7 +50,6 @@
5050
"protractor": "~5.4.0",
5151
"ts-node": "~7.0.0",
5252
"tsickle": ">=0.34.0",
53-
"tslib": "^1.9.0",
5453
"tslint": "~5.11.0",
5554
"typescript": "~3.2.2"
5655
}

projects/ionic-database-builder/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ionic-database-builder",
3-
"version": "0.3.6-alpha.0",
3+
"version": "0.4.0-alpha.1",
44
"description": "Extended library from database-builder to assist in creating and maintaining SQL commands. Allowing integrate execute commands with SQLite ('@ionic-native/sqlite'), Web Sql, etc. Through the interface injection 'DatabaseCreatorContract' returning an implementation of 'DatabaseObject'.",
55
"repository": {
66
"type": "git",
Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,9 @@
11
import { Injectable } from '@angular/core';
22
import { WebSqlDatabaseAdapter } from 'database-builder';
33

4-
// /**
5-
// * @deprecated use WebSqlDatabaseService
6-
// */
7-
// @Injectable()
8-
// export class DatabaseBrowserService extends WebSqlDatabaseAdapter {
9-
// constructor() {
10-
// super((window as any));
11-
// }
12-
// }
13-
144
@Injectable()
155
export class WebSqlDatabaseService extends WebSqlDatabaseAdapter {
166
constructor() {
177
super((window as any));
188
}
199
}
20-
21-
// export class DatabaseBrowserService implements DatabaseCreatorContract {
22-
23-
// create(config: DatabaseConfig): Promise<DatabaseObject> {
24-
// console.warn("Use WebSQL only for testing, some browsers no longer support WebSQL! \\o/");
25-
// return new Promise<DatabaseObject>((resolve, reject) => {
26-
// // o método OpenDatabase precisa de 4 parametros; o nome do banco de dados, a versão, a descrição e o tamanho estimado (em bytes)
27-
// const db = (window as any).openDatabase(config.name, "1.0", config.name, 200000);
28-
29-
// // de qualquer forma, sempre teste que o objeto foi instanciado direito antes de usá-lo
30-
// if (!db) {
31-
// reject("Não foi possivel iniciar o banco de dados no Browser!");
32-
// }
33-
// resolve(<DatabaseObject>{
34-
// executeSql: (statement: string, params: any): Promise<DatabaseResult> => {
35-
// return new Promise<DatabaseResult>((resolve, reject) => {
36-
// if (
37-
// statement.toUpperCase().indexOf("TRANSACTION") > -1
38-
// ||
39-
// statement.toUpperCase().indexOf("COMMIT") > -1
40-
// ) {
41-
// console.warn(`command sql ignored: '${statement}'`);
42-
// resolve({} as DatabaseResult);
43-
// return;
44-
// }
45-
// db.transaction((transaction: any) => {
46-
// transaction.executeSql(statement, Array.isArray(params) ? params : [],
47-
// (s: any, r: any) => resolve(r),
48-
// (e: any, err: any) => {
49-
// reject(err)
50-
// });
51-
// });
52-
// });
53-
// },
54-
// transaction:
55-
// (fn: (transaction: DatabaseBaseTransaction) => void): Promise<any> => {
56-
// return db.transaction((transiction: any) => {
57-
// fn({
58-
// executeSql: (sql: string, values: any): Promise<DatabaseResult> => {
59-
// return new Promise<DatabaseResult>((resolve, reject) => {
60-
// transiction.executeSql(sql, Array.isArray(values) ? values : [],
61-
// (s: any, r: any) => {
62-
// resolve(r)
63-
// },
64-
// (e: any, err: any) => {
65-
// reject(err)
66-
// });
67-
// });
68-
// }
69-
// });
70-
// });
71-
// }
72-
// });
73-
// });
74-
// }
75-
// }

projects/ionic-database-builder/src/lib/utils/buildable-database-manager.ts

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DatabaseBaseTransaction, DatabaseResult } from 'database-builder';
1+
import { DatabaseBaseTransaction, DatabaseResult, DatabaseBuilderError } from 'database-builder';
22
import { DatabaseManager } from './database-manager';
33
import { Crud, DatabaseObject, Ddl, ExecutableBuilder, GetMapper, Query, QueryCompiled } from 'database-builder';
44
import { DatabaseFactoryContract } from './database-factory-contract';
@@ -54,18 +54,12 @@ export abstract class BuildableDatabaseManager extends DatabaseManager {
5454
}
5555

5656
public managedTransaction(): Observable<ManagedTransaction> {
57-
return from(this.databaseInstance()).pipe(mergeMap(database => of(database.managedTransaction())));
58-
// return new Observable<ManagedTransaction>((observer) => {
59-
// const database = await this.databaseInstance();
60-
// // this.newTransaction(successTransaction)
61-
// // .subscribe((transaction) => {
62-
// // observer.next(new Crud(transaction, this._mapper, this.enableLog));
63-
// // observer.complete();
64-
// // }, error => {
65-
// // observer.error(error);
66-
// // observer.complete();
67-
// // });
68-
// });
57+
return from(this.databaseInstance()).pipe(mergeMap(database => {
58+
if (!database.managedTransaction) {
59+
throw new DatabaseBuilderError('Managed Transaction not supported in current middleware!');
60+
}
61+
return of(database.managedTransaction());
62+
}));
6963
}
7064

7165
/**
@@ -149,28 +143,6 @@ export abstract class BuildableDatabaseManager extends DatabaseManager {
149143
});
150144
}
151145

152-
// public batch(compiled: QueryCompiled[]): Observable<DatabaseResult[]> {
153-
// return new Observable((observer: Observer<DatabaseResult[]>) => {
154-
// this.databaseInstance()
155-
// .then(database => {
156-
// const executable = new ExecutableBuilder(this.enableLog);
157-
// console.log('batch ::: ', database);
158-
// executable.executeBatch(compiled, database)
159-
// .subscribe((cursor: DatabaseResult[]) => {
160-
// observer.next(cursor);
161-
// observer.complete();
162-
// }, err => {
163-
// observer.error(err);
164-
// observer.complete();
165-
// });
166-
// })
167-
// .catch(err => {
168-
// observer.error(err);
169-
// observer.complete();
170-
// });
171-
// });
172-
// }
173-
174146
public sql(sql: string, params: any[] = []): Observable<DatabaseResult> {
175147
return new Observable((observer: Observer<DatabaseResult>) => {
176148
this.databaseInstance()

src/app/database/data-to-test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { GuidClazz } from './models/guid-clazz';
2+
3+
export const guidClazz = {
4+
description: 'Condicao Pagamento 25'
5+
} as GuidClazz;

src/app/database/mapper/table-mapper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Classificacao } from '../models/classificacao';
1010
import { MapperBase } from 'database-builder';
1111
import { PrimaryKeyType } from 'database-builder/src/core/enums/primary-key-type';
1212
import { DatabaseHelperService } from 'ionic-database-builder';
13+
import { GuidClazz } from '../models/guid-clazz';
1314

1415
@Injectable()
1516
export class TableMapper extends MapperBase {
@@ -24,6 +25,9 @@ export class TableMapper extends MapperBase {
2425
}
2526
);
2627

28+
this.mapper(GuidClazz)
29+
.key(x => x.guid, PrimaryKeyType.Guid, String)
30+
.column(x => x.description, String);
2731
this.autoMapper(TestClazzRef, x => x.id, PrimaryKeyType.AutoIncrement);
2832
this.autoMapper(TestClazz, x => x.id, PrimaryKeyType.AutoIncrement);
2933
this.autoMapper(Regiao, x => x.codeImport, PrimaryKeyType.AutoIncrement);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class GuidClazz {
2+
get guid(): string {
3+
return this._guid;
4+
}
5+
set guid(theBar: string) {
6+
this._guid = theBar;
7+
}
8+
9+
constructor(
10+
private _guid?: string,
11+
public description?: string
12+
) {
13+
14+
}
15+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// import { Injectable } from '@angular/core';
2+
// import { WebSqlObjectInterface, DatabaseResult, WebSqlTransactionInterface } from 'database-builder';
3+
// import { WebSqlDatabaseService } from 'ionic-database-builder';
4+
5+
// @Injectable()
6+
// export class TestWebSqlDatabaseService extends WebSqlDatabaseService {
7+
8+
// // protected convertToExecuteSql(
9+
// // databaseNative: WebSqlObjectInterface
10+
// // ): (sql: string, values: any) => Promise<DatabaseResult> {
11+
// // return (sql: string, values: any): Promise<DatabaseResult> => {
12+
// // return new Promise<DatabaseResult>((executeSqlResolve, executeSqlReject) => {
13+
// // if (
14+
// // sql.toUpperCase().indexOf('TRANSACTION') > -1
15+
// // ||
16+
// // sql.toUpperCase().indexOf('COMMIT') > -1
17+
// // ||
18+
// // sql.toUpperCase().indexOf('ROLLBACK') > -1
19+
// // ) {
20+
// // this.ignoreExecuteSql(sql, values)
21+
// // .then(result => executeSqlResolve(result))
22+
// // .catch(err => executeSqlReject(err));
23+
// // } else {
24+
// // databaseNative.transaction(transaction => {
25+
// // return this.executeSql(transaction, sql, values)
26+
// // .then(result => executeSqlResolve(result))
27+
// // .catch(err => executeSqlReject(err));
28+
// // });
29+
// // }
30+
// // });
31+
// // };
32+
// // }
33+
34+
// // protected convertToSqlBatch(
35+
// // databaseNative: WebSqlObjectInterface
36+
// // ): (sqlStatements: Array<(string | string[] | any)>) => Promise<DatabaseResult[]> {
37+
// // return (sqlStatements: any[]) => {
38+
// // return (this as any).batch(databaseNative, sqlStatements, true);
39+
// // };
40+
// // }
41+
42+
// // protected batch(
43+
// // database: WebSqlObjectInterface,
44+
// // sqlStatements: Array<string | string[] | any>,
45+
// // runInTransaction: boolean
46+
// // ): Promise<DatabaseResult[]> {
47+
// // if (!sqlStatements || sqlStatements.constructor !== Array) {
48+
// // throw Error('sqlBatch expects an array');
49+
// // }
50+
// // const batchList = [];
51+
// // for (const st of sqlStatements) {
52+
// // if (st.constructor === Array) {
53+
// // if (st.length === 0) {
54+
// // throw Error('sqlBatch array element of zero (0) length');
55+
// // }
56+
// // batchList.push(
57+
// // {
58+
// // sql: st[0],
59+
// // params: st.length === 0 ? [] : st[1]
60+
// // }
61+
// // );
62+
// // } else {
63+
// // batchList.push({
64+
// // sql: st,
65+
// // params: []
66+
// // });
67+
// // }
68+
// // }
69+
// // return this.executeBatchs(database, batchList, runInTransaction);
70+
// // }
71+
72+
// protected transaction(databaseNative: WebSqlObjectInterface) {
73+
// return new Promise<WebSqlTransactionInterface>((resolve, reject) => {
74+
// try {
75+
// databaseNative.transaction(async transaction => {
76+
// resolve(transaction);
77+
// });
78+
// } catch (error) {
79+
// reject(error);
80+
// }
81+
// });
82+
// }
83+
84+
// protected async executeBatchs(
85+
// databaseNative: WebSqlObjectInterface,
86+
// batchs: Array<{ sql: string, params: any[] }>,
87+
// runInTransaction: boolean
88+
// ): Promise<DatabaseResult[]> {
89+
// const result: DatabaseResult[] = [];
90+
// const transaction = await this.transaction(databaseNative);
91+
// for (const batch of batchs) {
92+
// result.push(await this.executeSql(transaction, batch.sql, batch.params));
93+
// }
94+
// return result;
95+
// }
96+
97+
// // quando atualizar o database-builder pode remover isso
98+
// protected executeSql(transaction: WebSqlTransactionInterface, sql: string, values: any): Promise<DatabaseResult> {
99+
// return new Promise<DatabaseResult>((resolve, reject) => {
100+
// transaction.executeSql(
101+
// sql,
102+
// Array.isArray(values) ? values : [],
103+
// (_t: WebSqlTransactionInterface, r: any) => resolve(r),
104+
// (_t: WebSqlTransactionInterface, err: any) => {
105+
// reject(err);
106+
// // It is need to return truely to rollback in the transaction
107+
// // https://stackoverflow.com/a/21993115/2290538
108+
// return true;
109+
// }
110+
// );
111+
// });
112+
// }
113+
114+
// }

0 commit comments

Comments
 (0)