@nativescript-community/sqlite
Advanced tools
Comparing version
@@ -0,1 +1,2 @@ | ||
import { InanoSQLInstance } from '@nano-sql/core/lib/interfaces'; | ||
import { nanoSQLMemoryIndex } from '@nano-sql/core/lib/adapters/memoryIndex'; | ||
@@ -11,3 +12,3 @@ import { SQLiteAbstract } from '@nano-sql/core/lib/adapters/webSQL'; | ||
name: 'Akylas NativeScript SQLite Adapter', | ||
version: 2.37, | ||
version: 2.37 | ||
}; | ||
@@ -24,2 +25,3 @@ this._ai = {}; | ||
this._sqlite.createAI(() => { | ||
// this._db.resultType(NSSQLite.RESULTSASOBJECT); | ||
complete(); | ||
@@ -26,0 +28,0 @@ }, error); |
{ | ||
"name": "@nativescript-community/sqlite", | ||
"version": "3.4.1", | ||
"version": "3.4.2", | ||
"description": "SQLite for Nativescript", | ||
@@ -8,3 +8,10 @@ "main": "sqlite", | ||
"sideEffects": false, | ||
"scripts": {}, | ||
"scripts": { | ||
"build": "npm run tsc", | ||
"build.all": "npm run build", | ||
"build.angular": "ng-packagr -p ../../src/sqlite/angular/ng-package.json -c ../../src/sqlite/angular/tsconfig.json", | ||
"readme": "readme generate -c ../../tools/readme/blueprint.json", | ||
"tsc": "cpy '**/*.d.ts' '../../packages/sqlite' --parents --cwd=../../src/sqlite && tsc -skipLibCheck -d", | ||
"clean": "rimraf ./*.d.ts ./*.js ./*.js.map" | ||
}, | ||
"nativescript": { | ||
@@ -23,4 +30,4 @@ "platforms": { | ||
"author": { | ||
"name": "Josep Bergay", | ||
"email": "josep.bergay@gmail.com" | ||
"name": "Martin Guillon", | ||
"email": "martin@akylas.fr" | ||
}, | ||
@@ -41,3 +48,3 @@ "bugs": { | ||
}, | ||
"gitHead": "5ddc3926ece1924df17ac203595a45bf451a20b4" | ||
"gitHead": "f7c54e1965d61a17d1037facc81f88785c52908f" | ||
} |
{ | ||
"uses": [ | ||
"android.database:Cursor*", | ||
"android.database.sqlite:SQLiteClosable*", | ||
"android.database.sqlite:SQLiteDatabase*" | ||
"android.database:Cursor", | ||
"android.database.sqlite:SQLiteClosable", | ||
"android.database.sqlite:SQLiteDatabase", | ||
"android.database.sqlite:SQLiteDatabase.CursorFactory" | ||
] | ||
} |
@@ -1,12 +0,54 @@ | ||
# Nativescript: Akylas Sqlite | ||
<!-- ⚠️ This README has been generated from the file(s) "blueprint.md" ⚠️--> | ||
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
DO NOT EDIT THIS READEME DIRECTLY! Edit "bluesprint.md" instead. | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> | ||
<h1 align="center">@nativescript-community/sqlite</h1> | ||
<p align="center"> | ||
<a href="https://npmcharts.com/compare/@nativescript-community/sqlite?minimal=true"><img alt="Downloads per month" src="https://img.shields.io/npm/dm/@nativescript-community/sqlite.svg" height="20"/></a> | ||
<a href="https://www.npmjs.com/package/@nativescript-community/sqlite"><img alt="NPM Version" src="https://img.shields.io/npm/v/@nativescript-community/sqlite.svg" height="20"/></a> | ||
</p> | ||
[](https://www.npmjs.com/package/@nativescript-community/sqlite) | ||
[](https://www.npmjs.com/package/@nativescript-community/sqlite) | ||
<p align="center"> | ||
<b>SQLite for Nativescript</b></br> | ||
<sub><sub> | ||
</p> | ||
<br /> | ||
[](#table-of-contents) | ||
## Table of Contents | ||
* [Installation](#installation) | ||
* [Usage](#usage) | ||
[](#installation) | ||
## Installation | ||
Run the following command from the root of your project: | ||
``` | ||
ns plugin add @nativescript-community/sqlite | ||
``` | ||
`ns plugin add @nativescript-community/sqlite` | ||
[](#usage) | ||
## Usage | ||
@@ -17,3 +59,3 @@ | ||
```typescript | ||
import { openOrCreate, deleteDatabase } from "nativescript-akylas-sqlite"; | ||
import { openOrCreate, deleteDatabase } from "@nativescript-community/sqlite"; | ||
@@ -20,0 +62,0 @@ const sqlite = openOrCreate("path/to/db"); |
module.exports = function ($logger, projectData, injector, hookArgs) { | ||
// const prepareData = hookArgs.prepareData; | ||
// prepareData.env = prepareData.env || {}; | ||
// prepareData.env.alias = prepareData.env.alias || {}; | ||
// Object.assign(prepareData.env.alias, { | ||
// '../driver/oracle/OracleDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './oracle/OracleDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/cockroachdb/CockroachDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './cockroachdb/CockroachDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './cordova/CordovaDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './react-native/ReactNativeDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/react-native/ReactNativeDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './nativescript/NativescriptDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/nativescript/NativescriptDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './mysql/MysqlDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/mysql/MysqlDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './postgres/PostgresDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/postgres/PostgresDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './expo/ExpoDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './aurora-data-api/AuroraDataApiDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/aurora-data-api/AuroraDataApiDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './sqlite/SqliteDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/sqljs/SqljsDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './sqljs/SqljsDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/sqlserver/SqlServerDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './sqlserver/SqlServerDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// './mongodb/MongoDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/mongodb/MongoDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/cordova/CordovaDriver': '@nativescript-community/sqlite/typeorm/NativescriptDriver', | ||
// '../driver/better-sqlite3/BetterSqlite3Driver': '@nativescript-community/sqlite/typeorm/NativescriptDriver' | ||
// }); | ||
}; | ||
//# sourceMappingURL=before-prepare.js.map |
@@ -0,4 +1,5 @@ | ||
import { SQLiteDatabase as ISQLiteDatabase } from './sqlite.common'; | ||
import { SQLiteDatabaseBase } from './sqlitedatabase.android'; | ||
export declare class SQLiteDatabase extends SQLiteDatabaseBase { | ||
open(): Promise<boolean>; | ||
export declare class SQLiteDatabase extends SQLiteDatabaseBase implements ISQLiteDatabase { | ||
open(): boolean; | ||
} | ||
@@ -5,0 +6,0 @@ export declare const openOrCreate: (filePath: string, options?: { |
@@ -0,1 +1,2 @@ | ||
import { paramsToStringArray, throwError } from './sqlite.common'; | ||
import { Application } from '@nativescript/core'; | ||
@@ -5,9 +6,7 @@ import { SQLiteDatabaseBase } from './sqlitedatabase.android'; | ||
if (dbName === ':memory:') { | ||
//noinspection JSUnresolvedVariable | ||
return android.database.sqlite.SQLiteDatabase.create(flags); | ||
} | ||
if (dbName.indexOf('/') >= 0) { | ||
return android.database.sqlite.SQLiteDatabase.openDatabase(dbName, null, flags !== undefined | ||
? flags | ||
: android.database.sqlite.SQLiteDatabase.CREATE_IF_NECESSARY | | ||
android.database.sqlite.SQLiteDatabase.NO_LOCALIZED_COLLATORS); | ||
return android.database.sqlite.SQLiteDatabase.openDatabase(dbName, null, flags !== undefined ? flags : android.database.sqlite.SQLiteDatabase.CREATE_IF_NECESSARY | android.database.sqlite.SQLiteDatabase.NO_LOCALIZED_COLLATORS); | ||
} | ||
@@ -20,3 +19,3 @@ else { | ||
export class SQLiteDatabase extends SQLiteDatabaseBase { | ||
async open() { | ||
open() { | ||
if (!this.db) { | ||
@@ -23,0 +22,0 @@ this.db = createDb(this.filePath, this.flags); |
@@ -9,6 +9,6 @@ export declare type SqliteParam = null | number | string | ArrayBuffer | any; | ||
export interface SQLiteDatabase { | ||
getVersion(): Promise<number>; | ||
setVersion(version: number): Promise<void>; | ||
getVersion(): any; | ||
setVersion(version: number): any; | ||
isOpen: boolean; | ||
close(): Promise<void>; | ||
close(): any; | ||
select(query: string, params?: SqliteParams): Promise<SqliteRow[]>; | ||
@@ -20,3 +20,3 @@ selectArray(query: string, params?: SqliteParams): Promise<SqliteParam[][]>; | ||
transaction<T = any>(action: (cancel?: () => void) => Promise<T>): Promise<T>; | ||
each(query: string, params: SqliteParams, callback: (error: Error, result: SqliteRow) => void, complete: (error: Error, count: number) => void): Promise<void>; | ||
each(query: string, params: SqliteParams, callback: (error: Error, result: SqliteRow) => void, complete: (error: Error, count: number) => void): Promise<number>; | ||
} | ||
@@ -23,0 +23,0 @@ export declare function isNothing(x: any): boolean; |
@@ -54,2 +54,3 @@ export function isNothing(x) { | ||
if (p.hasOwnProperty('length') && !Array.isArray(p)) { | ||
// native array | ||
return p; | ||
@@ -56,0 +57,0 @@ } |
@@ -1,3 +0,3 @@ | ||
import { SqliteParams, SqliteRow } from './sqlite.common'; | ||
export declare class SQLiteDatabase { | ||
import { SQLiteDatabase as ISQLiteDatabase, SqliteParams, SqliteRow } from './sqlite.common'; | ||
export declare class SQLiteDatabase implements ISQLiteDatabase { | ||
filePath: string; | ||
@@ -12,6 +12,6 @@ db: FMDatabase; | ||
isOpen: boolean; | ||
open(): Promise<boolean>; | ||
close(): Promise<void>; | ||
setVersion(version: number): Promise<void>; | ||
getVersion(): Promise<number>; | ||
open(): boolean; | ||
close(): void; | ||
setVersion(version: number): void; | ||
getVersion(): number; | ||
execute(query: string, params?: SqliteParams): Promise<void>; | ||
@@ -18,0 +18,0 @@ get(query: string, params?: SqliteParams, transformBlobs?: boolean): Promise<SqliteRow>; |
@@ -1,17 +0,12 @@ | ||
import { paramsToStringArray, throwError, } from './sqlite.common'; | ||
import { paramsToStringArray, throwError } from './sqlite.common'; | ||
const iosProperty = (_self, property) => { | ||
if (typeof property === 'function') { | ||
// xCode < 8 | ||
return property.call(_self); | ||
} | ||
else { | ||
// xCode >= 8 | ||
return property; | ||
} | ||
}; | ||
const toCharPtr = (str) => { | ||
const objcStr = NSString.stringWithString(str); | ||
const size = strlen(objcStr.UTF8String) + 1; | ||
const buffer = interop.alloc(size); | ||
objcStr.getCStringMaxLengthEncoding(buffer, size, NSUTF8StringEncoding); | ||
return buffer; | ||
}; | ||
const getNewCursorStatement = (statement) => ({ | ||
@@ -21,5 +16,77 @@ statement, | ||
columns: [], | ||
count: undefined, | ||
count: undefined | ||
}); | ||
// const getValuesAsString = ( | ||
// statement: interop.Reference<any>, | ||
// column: number | ||
// ) => { | ||
// const type = sqlite3_column_type(statement, column); | ||
// switch (type) { | ||
// case 1: // Int | ||
// case 2: // Float | ||
// case 3: // Text | ||
// return NSString.stringWithUTF8String( | ||
// sqlite3_column_text(statement, column) | ||
// ).toString(); | ||
// case 4: // Blob | ||
// return NSData.dataWithBytesLength( | ||
// sqlite3_column_blob(statement, column), | ||
// sqlite3_column_bytes(statement, column) | ||
// ); | ||
// case 5: // Null | ||
// return null; | ||
// default: | ||
// throwError(`unknown.type: ${type}`); | ||
// } | ||
// }; | ||
// const getValues = (statement: FMResultSet, column: number) => { | ||
// const type = statement.; | ||
// switch (type) { | ||
// case 1: // Int | ||
// return sqlite3_column_int64(statement, column); | ||
// case 2: // Float | ||
// return sqlite3_column_double(statement, column); | ||
// case 3: // Text | ||
// return NSString.stringWithUTF8String( | ||
// sqlite3_column_text(statement, column) | ||
// ).toString(); | ||
// case 4: // Blob | ||
// return NSData.dataWithBytesLength( | ||
// sqlite3_column_blob(statement, column), | ||
// sqlite3_column_bytes(statement, column) | ||
// ); | ||
// case 5: // Null | ||
// return null; | ||
// default: | ||
// throwError(`unknown.type: ${type}`); | ||
// return null; | ||
// } | ||
// }; | ||
// const getColumns = ( | ||
// statement: interop.Reference<any>, | ||
// cursorSt: CursorStatement, | ||
// asObject: boolean | ||
// ) => { | ||
// if (!cursorSt.built) { | ||
// cursorSt.count = sqlite3_column_count(statement); | ||
// if (asObject) { | ||
// for (let index = 0; index < cursorSt.count; index++) { | ||
// let colName = NSString.stringWithUTF8String( | ||
// sqlite3_column_name(statement, index) | ||
// ).toString(); | ||
// if (!colName || cursorSt.columns.indexOf(colName) >= 0) { | ||
// colName = `column[${index}]`; | ||
// } | ||
// cursorSt.columns = [...cursorSt.columns, colName]; | ||
// } | ||
// } | ||
// cursorSt.built = true; | ||
// } | ||
// return cursorSt.count; | ||
// }; | ||
function getResultsAsObject(cursorSt, transformBlobs) { | ||
// const count = cursorSt.columnCount; | ||
// if (!count) { | ||
// return null; | ||
// } | ||
const data = {}; | ||
@@ -34,5 +101,7 @@ const dict = cursorSt.resultDictionary; | ||
}); | ||
// for (let index = 0; index < count; index++) { | ||
// data[cursorSt.columnNameForIndex(index)] = getValues(cursorSt, index); | ||
// } | ||
return data; | ||
} | ||
; | ||
function getResultsAsArray(cursorSt, transformBlobs) { | ||
@@ -48,5 +117,7 @@ const data = []; | ||
}); | ||
// for (let index = 0; index < count; index++) { | ||
// data[cursorSt.columnNameForIndex(index)] = getValues(cursorSt, index); | ||
// } | ||
return data; | ||
} | ||
; | ||
function getRealPath(dbname, create = false) { | ||
@@ -57,7 +128,118 @@ if (dbname === ':memory:') { | ||
return dbname; | ||
// if (dbname !== "") { | ||
// let actualPath; | ||
// if (dbname.indexOf("/") === -1) { | ||
// actualPath = knownFolders.documents().path; | ||
// dbname = actualPath + "/" + dbname; | ||
// } else { | ||
// actualPath = dbname.substr(0, dbname.lastIndexOf("/") + 1); | ||
// } | ||
// // Create "databases" folder if it is missing. This causes issues on Emulators if it is missing | ||
// // So we create it if it is missing | ||
// // try { | ||
// // // noinspection JSUnresolvedVariable | ||
// // if (!File.exists(actualPath) && create) { | ||
// // //noinspection JSUnresolvedFunction | ||
// // const fileManager = iosProperty( | ||
// // NSFileManager, | ||
// // NSFileManager.defaultManager | ||
// // ); | ||
// // //noinspection JSUnresolvedFunction | ||
// // if ( | ||
// // !fileManager.createDirectoryAtPathWithIntermediateDirectoriesAttributesError( | ||
// // actualPath, | ||
// // true, | ||
// // null | ||
// // ) | ||
// // ) { | ||
// // console.warn( | ||
// // "SQLITE.CONSTRUCTOR - Creating DB Folder Error" | ||
// // ); | ||
// // } | ||
// // } | ||
// // } catch (err) { | ||
// // console.warn("SQLITE.CONSTRUCTOR - Creating DB Folder Error", err); | ||
// // } | ||
// return actualPath; | ||
// } | ||
} | ||
// function open(filePath: string, flags?: number, readOnly?: boolean): FMDatabase { | ||
// const db = new interop.Reference<any>(); | ||
// console.log("open", filePath, flags, readOnly, getRealPath(filePath)); | ||
// const result = sqlite3_open_v2( | ||
// toCharPtr(getRealPath(filePath)), | ||
// db, | ||
// (readOnly === true ? 65537 : 65542) | flags, | ||
// null | ||
// ); | ||
// if (result) { | ||
// throwError(`open: ${result}`); | ||
// } | ||
// return db.value; | ||
// } | ||
// function prepareStatement(db: FMDatabase, query: string) { | ||
// const statement = new interop.Reference<any>(); | ||
// const result = sqlite3_prepare_v2(db, query, -1, statement, null); | ||
// if (result) { | ||
// throwError(`prepareStatement: ${result}`); | ||
// } | ||
// return statement.value as interop.Reference<any>; | ||
// } | ||
// function step(statement: interop.Reference<any>) { | ||
// const result = sqlite3_step(statement); | ||
// if (result && result !== 100 && result !== 101) { | ||
// finalize(statement); | ||
// throwError(`step: ${result}`); | ||
// } | ||
// return result; | ||
// } | ||
// function bind(params: SqliteParams, statement: interop.Reference<any>) { | ||
// paramsToStringArray(params).forEach((param, i) => { | ||
// let result; | ||
// if (param === null) { | ||
// result = sqlite3_bind_null(statement, i + 1); | ||
// } else { | ||
// result = sqlite3_bind_text( | ||
// statement, | ||
// i + 1, | ||
// param, | ||
// -1, | ||
// sqlitehelper.getTrans() | ||
// ); | ||
// } | ||
// if (result) { | ||
// finalize(statement); | ||
// throwError(`bind: ${result}`); | ||
// } | ||
// }); | ||
// } | ||
// function finalize(statement: interop.Reference<any>) { | ||
// const result = sqlite3_finalize(statement); | ||
// if (result) { | ||
// throwError(`finalize: ${result}`); | ||
// } | ||
// } | ||
function getRaw(db, query, params, asObject, transformBlobs) { | ||
// const statement = prepareStatement(db, query); | ||
// const cursorSt = getNewCursorStatement(statement); | ||
// bind(params, statement); | ||
// const result = step(statement); | ||
// let data; | ||
// if (result === 100) { | ||
// data = asObject | ||
// ? getResultsAsObject(cursorSt) | ||
// : getResultsAsArray(cursorSt); | ||
// } | ||
// finalize(statement); | ||
// return data; | ||
const s = db.executeQueryWithArgumentsInArray(query, paramsToStringArray(params)); | ||
if (s) { | ||
return asObject ? getResultsAsObject(s, transformBlobs) : getResultsAsArray(s, transformBlobs); | ||
// while (s.next()) { | ||
// //retrieve values for each record | ||
// const row = getResults(s); | ||
// if (row) { | ||
// rows = [...rows, row]; | ||
// } | ||
// } | ||
} | ||
@@ -69,2 +251,6 @@ else { | ||
function eachRaw(db, query, params, asObject, callback, complete, transformBlobs) { | ||
// const statement = prepareStatement(db, query); | ||
// const cursorSt = getNewCursorStatement(statement); | ||
// bind(params, statement); | ||
// let rows = []; | ||
const getResults = asObject ? getResultsAsObject : getResultsAsArray; | ||
@@ -77,2 +263,3 @@ return Promise.resolve() | ||
while (s.next()) { | ||
//retrieve values for each record | ||
const row = getResults(s, transformBlobs); | ||
@@ -88,6 +275,22 @@ if (row) { | ||
} | ||
// while (true) { | ||
// const result = step(statement); | ||
// if (result === 100) { | ||
// const row = getResults(cursorSt); | ||
// if (row) { | ||
// count++; | ||
// callback(null, row); | ||
// } | ||
// } else if (result && result !== 101) { | ||
// finalize(statement); | ||
// throw new Error("db_error " + result); | ||
// } else { | ||
// break; | ||
// } | ||
// } | ||
// finalize(statement); | ||
complete && complete(null, count); | ||
return count; | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
const errorCB = complete || callback; | ||
@@ -99,4 +302,8 @@ if (errorCB) { | ||
}); | ||
// return rows; | ||
} | ||
function selectRaw(db, query, params, asObject, transformBlobs) { | ||
// const statement = prepareStatement(db, query); | ||
// const cursorSt = getNewCursorStatement(statement); | ||
// bind(params, statement); | ||
let rows = []; | ||
@@ -107,2 +314,3 @@ const getResults = asObject ? getResultsAsObject : getResultsAsArray; | ||
while (s.next()) { | ||
//retrieve values for each record | ||
const row = getResults(s, transformBlobs); | ||
@@ -117,7 +325,31 @@ if (row) { | ||
} | ||
// while (true) { | ||
// const result = step(statement); | ||
// if (result === 100) { | ||
// const row = getResults(cursorSt); | ||
// if (row) { | ||
// rows = [...rows, row]; | ||
// } | ||
// } else { | ||
// break; | ||
// } | ||
// } | ||
// finalize(statement); | ||
return rows; | ||
} | ||
function execRaw(db, query, params) { | ||
// const statement = prepareStatement(db, query); | ||
// bind(params, statement); | ||
// step(statement); | ||
// finalize(statement); | ||
const s = db.executeUpdateWithArgumentsInArray(query, paramsToStringArray(params)); | ||
if (!s) { | ||
// while (s.next()) { | ||
// //retrieve values for each record | ||
// const row = getResults(s); | ||
// if (row) { | ||
// rows = [...rows, row]; | ||
// } | ||
// } | ||
// } else { | ||
throw db.lastError(); | ||
@@ -159,3 +391,3 @@ } | ||
} | ||
async open() { | ||
open() { | ||
if (!this.db) { | ||
@@ -169,13 +401,19 @@ this.db = FMDatabase.databaseWithPath(getRealPath(this.filePath)); | ||
} | ||
async close() { | ||
close() { | ||
if (!this.isOpen) | ||
return; | ||
this.db.close(); | ||
// sqlite3_close_v2(db); | ||
this.db = null; | ||
this.isOpen = false; | ||
} | ||
async setVersion(version) { | ||
setVersion(version) { | ||
this.db.userVersion = version + 0; | ||
// const query = "PRAGMA user_version=" + (version + 0).toString(); | ||
// execRaw(this.db, query); | ||
} | ||
async getVersion() { | ||
getVersion() { | ||
// const query = "PRAGMA user_version"; | ||
// const result = this.getArray(query); | ||
// return result && (result[0] as number); | ||
return this.db.userVersion; | ||
@@ -187,15 +425,15 @@ } | ||
async get(query, params, transformBlobs) { | ||
return (getRaw(this.db, query, params, true, transformBlobs !== null && transformBlobs !== void 0 ? transformBlobs : this.transformBlobs) || null); | ||
return getRaw(this.db, query, params, true, transformBlobs ?? this.transformBlobs) || null; | ||
} | ||
async getArray(query, params, transformBlobs) { | ||
return (getRaw(this.db, query, params, false, transformBlobs !== null && transformBlobs !== void 0 ? transformBlobs : this.transformBlobs) || null); | ||
return (getRaw(this.db, query, params, false, transformBlobs ?? this.transformBlobs) || null); | ||
} | ||
async select(query, params, transformBlobs) { | ||
return selectRaw(this.db, query, params, true, transformBlobs !== null && transformBlobs !== void 0 ? transformBlobs : this.transformBlobs); | ||
return selectRaw(this.db, query, params, true, transformBlobs ?? this.transformBlobs); | ||
} | ||
async each(query, params, callback, complete, transformBlobs) { | ||
return eachRaw(this.db, query, params, true, callback, complete, transformBlobs !== null && transformBlobs !== void 0 ? transformBlobs : this.transformBlobs); | ||
return eachRaw(this.db, query, params, true, callback, complete, transformBlobs ?? this.transformBlobs); | ||
} | ||
async selectArray(query, params, transformBlobs) { | ||
return selectRaw(this.db, query, params, false, transformBlobs !== null && transformBlobs !== void 0 ? transformBlobs : this.transformBlobs); | ||
return selectRaw(this.db, query, params, false, transformBlobs ?? this.transformBlobs); | ||
} | ||
@@ -202,0 +440,0 @@ async transaction(action) { |
@@ -1,2 +0,2 @@ | ||
import { SqliteParams, SqliteRow } from './sqlite.common'; | ||
import { SqliteParams } from './sqlite.common'; | ||
export declare function byteArrayToBuffer(value: any): Uint8Array; | ||
@@ -25,44 +25,14 @@ export declare class SQLiteDatabaseBase { | ||
lastId: number; | ||
sendMessageToWorker(nativeData: any, messageData: any, timeout?: number): Promise<{ | ||
id: number; | ||
nativeDatas?: { | ||
[k: string]: any; | ||
}; | ||
[k: string]: any; | ||
}>; | ||
sendMessageToWorker(nativeData: any, messageData: any, timeout?: number): Promise<any>; | ||
get isOpen(): boolean; | ||
close(): Promise<void>; | ||
close(): void; | ||
setVersion(version: number): Promise<void>; | ||
getVersion(): Promise<number>; | ||
execute(query: string, params?: SqliteParams): Promise<void | { | ||
[k: string]: any; | ||
id: number; | ||
nativeDatas?: { | ||
[k: string]: any; | ||
}; | ||
}>; | ||
get(query: string, params?: SqliteParams, transformBlobs?: boolean): Promise<SqliteRow>; | ||
getArray(query: string, params?: SqliteParams, transformBlobs?: boolean): Promise<any[] | { | ||
[k: string]: any; | ||
id: number; | ||
nativeDatas?: { | ||
[k: string]: any; | ||
}; | ||
}>; | ||
select(query: string, params?: SqliteParams, transformBlobs?: boolean): Promise<SqliteRow[] | { | ||
[k: string]: any; | ||
id: number; | ||
nativeDatas?: { | ||
[k: string]: any; | ||
}; | ||
}>; | ||
selectArray(query: string, params?: SqliteParams, transformBlobs?: boolean): Promise<any[][] | { | ||
[k: string]: any; | ||
id: number; | ||
nativeDatas?: { | ||
[k: string]: any; | ||
}; | ||
}>; | ||
getVersion(): number; | ||
execute(query: string, params?: SqliteParams): Promise<any>; | ||
get(query: string, params?: SqliteParams, transformBlobs?: boolean): Promise<any>; | ||
getArray(query: string, params?: SqliteParams, transformBlobs?: boolean): Promise<any>; | ||
select(query: string, params?: SqliteParams, transformBlobs?: boolean): Promise<any>; | ||
selectArray(query: string, params?: SqliteParams, transformBlobs?: boolean): Promise<any>; | ||
each(query: string, params: SqliteParams, callback: (error: Error, result: any) => void, complete: (error: Error, count: number) => void, transformBlobs?: boolean): Promise<number>; | ||
transaction<T = any>(action: (cancel?: () => void) => Promise<T>): Promise<T>; | ||
} |
@@ -133,3 +133,3 @@ import { paramsToStringArray, throwError } from './sqlite.common'; | ||
this.threading = options && options.threading === true; | ||
this.flags = options === null || options === void 0 ? void 0 : options.flags; | ||
this.flags = options?.flags; | ||
this.transformBlobs = !options || options.transformBlobs !== false; | ||
@@ -149,2 +149,3 @@ } | ||
} | ||
// } | ||
}); | ||
@@ -165,6 +166,8 @@ delete messagePromises[id]; | ||
timeoutTimer = setTimeout(() => { | ||
// we need to try catch because the simple fact of creating a new Error actually throws. | ||
// so we will get an uncaughtException | ||
try { | ||
reject(new Error('timeout')); | ||
} | ||
catch (_a) { } | ||
catch { } | ||
delete messagePromises[id]; | ||
@@ -184,3 +187,3 @@ }, timeout); | ||
}, | ||
nativeDataKeys: keys, | ||
nativeDataKeys: keys | ||
}, messageData); | ||
@@ -193,3 +196,3 @@ this.worker.postMessage(mData); | ||
} | ||
async close() { | ||
close() { | ||
if (!this.isOpen) | ||
@@ -199,3 +202,3 @@ return; | ||
this.worker.postMessage({ | ||
type: 'terminate', | ||
type: 'terminate' | ||
}); | ||
@@ -205,2 +208,3 @@ this.worker = null; | ||
this.db.close(); | ||
// sqlite3_close_v2(db); | ||
this.db = null; | ||
@@ -211,3 +215,3 @@ } | ||
} | ||
async getVersion() { | ||
getVersion() { | ||
return this.db.getVersion(); | ||
@@ -218,6 +222,6 @@ } | ||
return this.sendMessageToWorker({ | ||
db: this.db, | ||
db: this.db | ||
}, { | ||
callName: 'execute', | ||
args: [query, params], | ||
args: [query, params] | ||
}); | ||
@@ -230,9 +234,9 @@ } | ||
return this.sendMessageToWorker({ | ||
db: this.db, | ||
db: this.db | ||
}, { | ||
callName: 'get', | ||
args: [query, params], | ||
args: [query, params] | ||
}); | ||
} | ||
return rawSql(dataFromCursor)(this.db, transformBlobs !== null && transformBlobs !== void 0 ? transformBlobs : this.transformBlobs)(query, params)[0] || null; | ||
return rawSql(dataFromCursor)(this.db, transformBlobs ?? this.transformBlobs)(query, params)[0] || null; | ||
} | ||
@@ -242,9 +246,9 @@ async getArray(query, params, transformBlobs) { | ||
return this.sendMessageToWorker({ | ||
db: this.db, | ||
db: this.db | ||
}, { | ||
callName: 'getArray', | ||
args: [query, params], | ||
args: [query, params] | ||
}); | ||
} | ||
return rawSql(arrayFromCursor)(this.db, transformBlobs !== null && transformBlobs !== void 0 ? transformBlobs : this.transformBlobs)(query, params)[0] || null; | ||
return rawSql(arrayFromCursor)(this.db, transformBlobs ?? this.transformBlobs)(query, params)[0] || null; | ||
} | ||
@@ -254,9 +258,9 @@ async select(query, params, transformBlobs) { | ||
return this.sendMessageToWorker({ | ||
db: this.db, | ||
db: this.db | ||
}, { | ||
callName: 'select', | ||
args: [query, params], | ||
args: [query, params] | ||
}); | ||
} | ||
return rawSql(dataFromCursor)(this.db, transformBlobs !== null && transformBlobs !== void 0 ? transformBlobs : this.transformBlobs)(query, params); | ||
return rawSql(dataFromCursor)(this.db, transformBlobs ?? this.transformBlobs)(query, params); | ||
} | ||
@@ -266,12 +270,12 @@ async selectArray(query, params, transformBlobs) { | ||
return this.sendMessageToWorker({ | ||
db: this.db, | ||
db: this.db | ||
}, { | ||
callName: 'selectArray', | ||
args: [query, params], | ||
args: [query, params] | ||
}); | ||
} | ||
return rawSql(arrayFromCursor)(this.db, transformBlobs !== null && transformBlobs !== void 0 ? transformBlobs : this.transformBlobs)(query, params); | ||
return rawSql(arrayFromCursor)(this.db, transformBlobs ?? this.transformBlobs)(query, params); | ||
} | ||
async each(query, params, callback, complete, transformBlobs) { | ||
return eachRaw(dataFromCursor)(this.db, transformBlobs !== null && transformBlobs !== void 0 ? transformBlobs : this.transformBlobs)(query, params, callback, complete); | ||
return eachRaw(dataFromCursor)(this.db, transformBlobs ?? this.transformBlobs)(query, params, callback, complete); | ||
} | ||
@@ -278,0 +282,0 @@ async transaction(action) { |
import { BaseConnectionOptions } from '@nativescript-community/typeorm/browser/connection/BaseConnectionOptions'; | ||
export declare function installMixins(): void; | ||
/** | ||
* NativeScript-specific connection options. | ||
*/ | ||
export interface NativescriptConnectionOptions extends BaseConnectionOptions { | ||
/** | ||
* Database type. | ||
*/ | ||
readonly type: 'nativescript'; | ||
/** | ||
* Database name. | ||
*/ | ||
readonly database: string; | ||
/** | ||
* The driver object | ||
* Default is `require('nativescript-sqlite') | ||
*/ | ||
readonly driver?: any; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { Connection } from '@nativescript-community/typeorm/browser/connection/Connection'; | ||
import { NativescriptDriver } from './NativescriptDriver'; | ||
@@ -2,0 +3,0 @@ let installed = false; |
@@ -7,7 +7,24 @@ import { Connection } from '@nativescript-community/typeorm/browser/connection/Connection'; | ||
import { NativescriptConnectionOptions } from './index'; | ||
/** | ||
* Organizes communication with sqlite DBMS within Nativescript. | ||
*/ | ||
export declare class NativescriptDriver extends AbstractSqliteDriver { | ||
/** | ||
* Connection options. | ||
*/ | ||
options: NativescriptConnectionOptions; | ||
/** | ||
* Nativescript driver module | ||
* this is most likely `nativescript-sqlite` | ||
* but user can pass his own | ||
*/ | ||
driver: any; | ||
constructor(connection: Connection); | ||
/** | ||
* Closes connection with database. | ||
*/ | ||
disconnect(): Promise<any>; | ||
/** | ||
* Creates a query runner used to execute database queries. | ||
*/ | ||
createQueryRunner(mode?: 'master' | 'slave'): QueryRunner; | ||
@@ -20,4 +37,21 @@ normalizeType(column: { | ||
}): string; | ||
/** | ||
* Creates connection with the database. | ||
* | ||
* nativescript-sqlite driver constructor and execSQL()/all() methods return Promise | ||
* Also as mentioned in nativescript-sqlite documentation: | ||
* | ||
* "You should choose either to use a promise or a callback; | ||
* you can use whichever you are most comfortable with | ||
* however, you CAN use both if you want; | ||
* but side effects WILL occur with some functions." | ||
* | ||
* We choose Promise. So we should use Promises, when create connection and when make query | ||
* Let's use async/await | ||
*/ | ||
sqlite: typeof NSQlite; | ||
protected createDatabaseConnection(): Promise<NSQlite.SQLiteDatabase>; | ||
/** | ||
* If driver dependency is not given explicitly, then try to load it via "require". | ||
*/ | ||
protected loadDependencies(): void; | ||
@@ -24,0 +58,0 @@ } |
@@ -0,6 +1,14 @@ | ||
import { Connection } from '@nativescript-community/typeorm/browser/connection/Connection'; | ||
import { AbstractSqliteDriver } from '@nativescript-community/typeorm/browser/driver/sqlite-abstract/AbstractSqliteDriver'; | ||
import { DriverOptionNotSetError } from '@nativescript-community/typeorm/browser/error/DriverOptionNotSetError'; | ||
import { DriverPackageNotInstalledError } from '@nativescript-community/typeorm/browser/error/DriverPackageNotInstalledError'; | ||
import * as NSQlite from '../sqlite'; | ||
import { NativescriptQueryRunner } from './NativescriptQueryRunner'; | ||
/** | ||
* Organizes communication with sqlite DBMS within Nativescript. | ||
*/ | ||
export class NativescriptDriver extends AbstractSqliteDriver { | ||
// ------------------------------------------------------------------------- | ||
// Constructor | ||
// ------------------------------------------------------------------------- | ||
constructor(connection) { | ||
@@ -12,7 +20,15 @@ super(connection); | ||
this.driver = this.options.driver; | ||
// validate options to make sure everything is set | ||
if (!this.options.database) { | ||
throw new DriverOptionNotSetError('database'); | ||
} | ||
// load sqlite package | ||
this.loadDependencies(); | ||
} | ||
// ------------------------------------------------------------------------- | ||
// Public Methods | ||
// ------------------------------------------------------------------------- | ||
/** | ||
* Closes connection with database. | ||
*/ | ||
async disconnect() { | ||
@@ -22,2 +38,5 @@ this.queryRunner = undefined; | ||
} | ||
/** | ||
* Creates a query runner used to execute database queries. | ||
*/ | ||
createQueryRunner(mode = 'master') { | ||
@@ -37,3 +56,7 @@ if (!this.queryRunner) { | ||
try { | ||
const db = this.sqlite.openOrCreate(this.options.database, this.options.extra ? Object.assign({}, this.options.extra) : {}); | ||
const db = this.sqlite.openOrCreate(this.options.database, this.options.extra ? { ...this.options.extra } : {}); | ||
// use object mode to work with TypeORM | ||
// db.resultType(this.sqlite.RESULTSASOBJECT); | ||
// we need to enable foreign keys in sqlite to make sure all foreign key related features | ||
// working properly. this also makes onDelete work with sqlite. | ||
await db.execute('PRAGMA foreign_keys = ON;', []); | ||
@@ -46,2 +69,5 @@ return db; | ||
} | ||
/** | ||
* If driver dependency is not given explicitly, then try to load it via "require". | ||
*/ | ||
loadDependencies() { | ||
@@ -56,2 +82,3 @@ try { | ||
} | ||
// fake export class for webpack clean up with aliases | ||
export class MongoDriver { | ||
@@ -58,0 +85,0 @@ } |
import { ObjectLiteral } from '@nativescript-community/typeorm/browser/common/ObjectLiteral'; | ||
import { AbstractSqliteQueryRunner } from '@nativescript-community/typeorm/browser/driver/sqlite-abstract/AbstractSqliteQueryRunner'; | ||
import { NativescriptDriver } from './NativescriptDriver'; | ||
/** | ||
* Runs queries on a single sqlite database connection. | ||
*/ | ||
export declare class NativescriptQueryRunner extends AbstractSqliteQueryRunner { | ||
/** | ||
* Database driver used by connection. | ||
*/ | ||
driver: NativescriptDriver; | ||
constructor(driver: NativescriptDriver); | ||
query(query: string, parameters?: any[]): Promise<void | import("../sqlite.common").SqliteRow[] | { | ||
[k: string]: any; | ||
id: number; | ||
nativeDatas?: { | ||
[k: string]: any; | ||
}; | ||
}>; | ||
/** | ||
* Executes a given SQL query. | ||
*/ | ||
query(query: string, parameters?: any[]): Promise<any>; | ||
/** | ||
* Parametrizes given object of values. Used to create column=value queries. | ||
*/ | ||
protected parametrize(objectLiteral: ObjectLiteral, startIndex?: number): string[]; | ||
} |
@@ -5,3 +5,11 @@ import { QueryRunnerAlreadyReleasedError } from '@nativescript-community/typeorm/browser/error/QueryRunnerAlreadyReleasedError'; | ||
import { Broadcaster } from '@nativescript-community/typeorm/browser/subscriber/Broadcaster'; | ||
import { NativescriptDriver } from './NativescriptDriver'; | ||
import * as NSQlite from '../sqlite'; | ||
/** | ||
* Runs queries on a single sqlite database connection. | ||
*/ | ||
export class NativescriptQueryRunner extends AbstractSqliteQueryRunner { | ||
// ------------------------------------------------------------------------- | ||
// Constructor | ||
// ------------------------------------------------------------------------- | ||
constructor(driver) { | ||
@@ -13,2 +21,5 @@ super(); | ||
} | ||
/** | ||
* Executes a given SQL query. | ||
*/ | ||
async query(query, parameters) { | ||
@@ -24,3 +35,3 @@ if (this.isReleased) { | ||
const queryStartTime = +new Date(); | ||
const result = isInsertQuery | ||
const result = isInsertQuery // when isInsertQuery == true, result is the id | ||
? await db.execute(query, parameters) | ||
@@ -31,2 +42,3 @@ : await db.select(query, parameters); | ||
const maxQueryExecutionTime = connection.options.maxQueryExecutionTime; | ||
// log slow queries if maxQueryExecution time is set | ||
if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime) { | ||
@@ -42,2 +54,8 @@ connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this); | ||
} | ||
// ------------------------------------------------------------------------- | ||
// Protected Methods | ||
// ------------------------------------------------------------------------- | ||
/** | ||
* Parametrizes given object of values. Used to create column=value queries. | ||
*/ | ||
parametrize(objectLiteral, startIndex = 0) { | ||
@@ -44,0 +62,0 @@ return Object.keys(objectLiteral).map((key, index) => `"${key}"` + '=?'); |
const context = self; | ||
import { SQLiteDatabaseBase } from './sqlitedatabase'; | ||
// function nativeArray(array) { | ||
// if (global.isAndroid) { | ||
// const nArray = Array.create(java.lang.Object, array.length); | ||
// for (let index = 0; index < array.length; index++) { | ||
// nArray[index] = array[index]; | ||
// } | ||
// return nArray; | ||
// } | ||
// return array; | ||
// } | ||
// export default class DBWorker { | ||
// processing = false; | ||
// } | ||
// const worker = new DBWorker(); | ||
const fakeDatabase = new SQLiteDatabaseBase(null); | ||
@@ -8,2 +22,4 @@ context.onmessage = (async (event) => { | ||
case 'terminate': | ||
// worker.log('terminating worker'); | ||
// worker.stop(); | ||
context.close(); | ||
@@ -23,5 +39,6 @@ break; | ||
const result = await fakeDatabase[call].apply(fakeDatabase, args); | ||
// console.log('Worker.onmessage done ', id, call, !!result); | ||
global.postMessage({ | ||
id, | ||
result, | ||
result | ||
}); | ||
@@ -32,5 +49,17 @@ } | ||
id, | ||
error: err.toString(), | ||
error: err.toString() | ||
})); | ||
// console.log('error in worker', err.toString()); | ||
} | ||
// if (worker.processing) { | ||
// console.log('ignoring processing'); | ||
// (global as any).postMessage( | ||
// Object.assign(data, { | ||
// type: 'error', | ||
// id, | ||
// error: 'processing', | ||
// }) | ||
// ); | ||
// return; | ||
// } | ||
break; | ||
@@ -37,0 +66,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
120430
31.29%1738
55.6%73
135.48%42
-12.5%