@@ -9,12 +9,12 @@ import {Broadcaster} from "../../subscriber/Broadcaster";
99 * Runs queries on a single sqlite database connection.
1010 */
1111export class CordovaQueryRunner extends AbstractSqliteQueryRunner {
12-
12+
1313 /**
1414 * Database driver used by connection.
1515 */
1616 driver : CordovaDriver ;
17-
17+
1818 // -------------------------------------------------------------------------
1919 // Constructor
2020 // -------------------------------------------------------------------------
@@ -54,7 +54,7 @@ export class CordovaQueryRunner extends AbstractSqliteQueryRunner {
5454 for ( let i = 0 ; i < result . rows . length ; i ++ ) {
5555 resultSet . push ( result . rows . item ( i ) ) ;
5656 }
57-
57+
5858 ok ( resultSet ) ;
5959 }
6060 } , ( err : any ) => {
@@ -98,6 +98,48 @@ export class CordovaQueryRunner extends AbstractSqliteQueryRunner {
9898 });
9999 }*/
100100
101+ /**
102+ * Would start a transaction but this driver does not support transactions.
103+ */
104+ async startTransaction ( ) : Promise < void > {
105+ throw new Error ( 'Transactions are not supported by the Cordova driver' )
106+ }
107+
108+ /**
109+ * Would start a transaction but this driver does not support transactions.
110+ */
111+ async commitTransaction ( ) : Promise < void > {
112+ throw new Error ( 'Transactions are not supported by the Cordova driver' )
113+ }
114+
115+ /**
116+ * Would start a transaction but this driver does not support transactions.
117+ */
118+ async rollbackTransaction ( ) : Promise < void > {
119+ throw new Error ( 'Transactions are not supported by the Cordova driver' )
120+ }
121+
122+ /**
123+ * Removes all tables from the currently connected database.
124+ * Be careful with using this method and avoid using it in production or migrations
125+ * (because it can clear all your database).
126+ */
127+ async clearDatabase ( ) : Promise < void > {
128+ await this . query ( `PRAGMA foreign_keys = OFF;` ) ;
129+ try {
130+ const selectViewDropsQuery = `SELECT 'DROP VIEW "' || name || '";' as query FROM "sqlite_master" WHERE "type" = 'view'` ;
131+ const dropViewQueries : ObjectLiteral [ ] = await this . query ( selectViewDropsQuery ) ;
132+
133+ const selectTableDropsQuery = `SELECT 'DROP TABLE "' || name || '";' as query FROM "sqlite_master" WHERE "type" = 'table' AND "name" != 'sqlite_sequence'` ;
134+ const dropTableQueries : ObjectLiteral [ ] = await this . query ( selectTableDropsQuery ) ;
135+
136+ await Promise . all ( dropViewQueries . map ( q => this . query ( q [ "query" ] ) ) ) ;
137+ await Promise . all ( dropTableQueries . map ( q => this . query ( q [ "query" ] ) ) ) ;
138+ } finally {
139+ await this . query ( `PRAGMA foreign_keys = ON;` ) ;
140+ }
141+ }
142+
101143 // -------------------------------------------------------------------------
102144 // Protected Methods
103145 // -------------------------------------------------------------------------
@@ -108,4 +150,4 @@ export class CordovaQueryRunner extends AbstractSqliteQueryRunner {
108150 protected parametrize ( objectLiteral : ObjectLiteral , startIndex : number = 0 ) : string [ ] {
109151 return Object . keys ( objectLiteral ) . map ( ( key , index ) => `"${ key } "` + "=?" ) ;
110152 }
111- }
153+ }
0 commit comments