From 5c9b77c2db4f324ab2431f27b35a0b9c44383dbe Mon Sep 17 00:00:00 2001 From: George Wu Date: Fri, 11 Dec 2015 21:49:53 +0800 Subject: [PATCH 1/2] Added type definitions for sql.js. --- sql.js/sql.js-tests.ts | 81 ++++++++++++++++++++++++++++++++++++++++++ sql.js/sql.js.d.ts | 71 ++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 sql.js/sql.js-tests.ts create mode 100644 sql.js/sql.js.d.ts diff --git a/sql.js/sql.js-tests.ts b/sql.js/sql.js-tests.ts new file mode 100644 index 0000000000..eeba3f0809 --- /dev/null +++ b/sql.js/sql.js-tests.ts @@ -0,0 +1,81 @@ +/// +/// + +import fs = require("fs"); +import SQL = require("sql.js"); + +var DB_PATH = "data.db"; + +function createFile(path: string): void { + var fd = fs.openSync(path, "a"); + fs.closeSync(fd); +} + +// Open the database file. If it does not exist, create a blank database in memory. +var databaseData: Buffer; +databaseData = fs.existsSync(DB_PATH) ? fs.readFileSync(DB_PATH) : null; +var db = new SQL.Database(databaseData); + +// Create a new table 'test_table' in the database in memory. +var createTableStatement = + "DROP TABLE IF EXISTS test_table;" + + "CREATE TABLE test_table (id INTEGER PRIMARY KEY, content TEXT);"; +db.run(createTableStatement); + +// Insert 2 records for testing. +var insertRecordStatement = + "INSERT INTO test_table (id, content) VALUES (@id, @content);"; +db.run(insertRecordStatement, { + "@id": 1, + "@content": "Content 1" +}); +db.run(insertRecordStatement, { + "@id": 2, + "@content": "Content 2" +}); + +try { + // This query will throw exception: primary key constraint failed. + db.run(insertRecordStatement, { + "@id": 1, + "@content": "Content 3" + }); +} catch (ex) { + console.warn(ex); +} + +// A simple SELECT query. +var selectRecordStatement = + "SELECT * FROM test_table WHERE id = @id;" +var selectStatementObject = db.prepare(selectRecordStatement); +var results = selectStatementObject.get({ + "@id": 1 +}); +console.log(results); +selectStatementObject.free(); + +// Access the results one by one, asynchronously. +var selectRecordsStatement = + "SELECT * FROM test_table;"; +db.each( + selectRecordsStatement, + (obj: SQL.SQLValueObject): void => { + console.log(obj); + }, + (): void => { + console.info("Iteration done."); + dbAccessDone(); + }); + + +function dbAccessDone(): void { + // Save the database into SQLite version 3 format. + if (!fs.existsSync(DB_PATH)) { + createFile(DB_PATH); + } + var exportedData = db.export(); + fs.writeFileSync(DB_PATH, exportedData); + + // Finally, close the database connection and release the resources in memory. + db.close(); +} diff --git a/sql.js/sql.js.d.ts b/sql.js/sql.js.d.ts new file mode 100644 index 0000000000..5f2ddd0691 --- /dev/null +++ b/sql.js/sql.js.d.ts @@ -0,0 +1,71 @@ + +// Type definitions for sql.js (Sep. 6 2015 snapshot) +// Project: https://github.com/kripken/sql.js +// Definitions by: George Wu +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "sql.js" { + + type SQLValue = number | string | Uint8Array; + type KeyValueObject = { [key: string]: SQLValue }; + type SQLValueObject = { [columnName: string]: SQLValue }; + type DataRow = SQLValue[]; + + class Database { + constructor(data: Buffer); + constructor(data: Uint8Array); + constructor(data: number[]); + + run(sql: string): Database; + run(sql: string, params: KeyValueObject): Database; + run(sql: string, params: SQLValue[]): Database; + + exec(sql: string): QueryResults[]; + + each(sql: string, callback: (obj: SQLValueObject) => void, done: () => void): void; + each(sql: string, params: KeyValueObject, callback: (obj: SQLValueObject) => void, done: () => void): void; + each(sql: string, params: SQLValue[], callback: (obj: SQLValueObject) => void, done: () => void): void; + + prepare(sql: string): Statement; + prepare(sql: string, params: KeyValueObject): Statement; + prepare(sql: string, params: SQLValue[]): Statement; + + export(): Uint8Array; + + close(): void; + } + + class Statement { + bind(): boolean; + bind(values: KeyValueObject): boolean; + bind(values: SQLValue[]): boolean; + + step(): boolean; + + get(): DataRow; + get(params: KeyValueObject): DataRow; + get(params: SQLValue[]): DataRow; + + getColumnNames(): string[]; + + getAsObject(): SQLValueObject; + getAsObject(params: KeyValueObject): SQLValueObject; + getAsObject(params: SQLValue[]): SQLValueObject; + + run(): void; + run(values: KeyValueObject): void; + run(values: SQLValue[]): void; + + reset(): void; + + freemem(): void; + + free(): boolean; + } + + interface QueryResults { + columns: string[]; + values: DataRow[]; + } + +} From 98339951b7a45fe9679a83777d61bad70a037976 Mon Sep 17 00:00:00 2001 From: George Wu Date: Fri, 11 Dec 2015 22:09:48 +0800 Subject: [PATCH 2/2] Renewed code to follow DefinitelyTyped's contribution guidelines. --- sql.js/sql.js-tests.ts | 2 +- sql.js/sql.js.d.ts | 46 +++++++++++++++++++----------------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/sql.js/sql.js-tests.ts b/sql.js/sql.js-tests.ts index eeba3f0809..40fceb8fbd 100644 --- a/sql.js/sql.js-tests.ts +++ b/sql.js/sql.js-tests.ts @@ -59,7 +59,7 @@ var selectRecordsStatement = "SELECT * FROM test_table;"; db.each( selectRecordsStatement, - (obj: SQL.SQLValueObject): void => { + (obj: { [columnName: string]: number | string | Uint8Array }): void => { console.log(obj); }, (): void => { diff --git a/sql.js/sql.js.d.ts b/sql.js/sql.js.d.ts index 5f2ddd0691..d3f22afc1f 100644 --- a/sql.js/sql.js.d.ts +++ b/sql.js/sql.js.d.ts @@ -1,15 +1,11 @@ - -// Type definitions for sql.js (Sep. 6 2015 snapshot) +// Type definitions for sql.js // Project: https://github.com/kripken/sql.js // Definitions by: George Wu // Definitions: https://github.com/borisyankov/DefinitelyTyped -declare module "sql.js" { +/// - type SQLValue = number | string | Uint8Array; - type KeyValueObject = { [key: string]: SQLValue }; - type SQLValueObject = { [columnName: string]: SQLValue }; - type DataRow = SQLValue[]; +declare module "sql.js" { class Database { constructor(data: Buffer); @@ -17,18 +13,18 @@ declare module "sql.js" { constructor(data: number[]); run(sql: string): Database; - run(sql: string, params: KeyValueObject): Database; - run(sql: string, params: SQLValue[]): Database; + run(sql: string, params: { [key: string]: number | string | Uint8Array }): Database; + run(sql: string, params: (number | string | Uint8Array)[]): Database; exec(sql: string): QueryResults[]; - each(sql: string, callback: (obj: SQLValueObject) => void, done: () => void): void; - each(sql: string, params: KeyValueObject, callback: (obj: SQLValueObject) => void, done: () => void): void; - each(sql: string, params: SQLValue[], callback: (obj: SQLValueObject) => void, done: () => void): void; + each(sql: string, callback: (obj: { [columnName: string]: number | string | Uint8Array }) => void, done: () => void): void; + each(sql: string, params: { [key: string]: number | string | Uint8Array }, callback: (obj: { [columnName: string]: number | string | Uint8Array }) => void, done: () => void): void; + each(sql: string, params: (number | string | Uint8Array)[], callback: (obj: { [columnName: string]: number | string | Uint8Array }) => void, done: () => void): void; prepare(sql: string): Statement; - prepare(sql: string, params: KeyValueObject): Statement; - prepare(sql: string, params: SQLValue[]): Statement; + prepare(sql: string, params: { [key: string]: number | string | Uint8Array }): Statement; + prepare(sql: string, params: (number | string | Uint8Array)[]): Statement; export(): Uint8Array; @@ -37,24 +33,24 @@ declare module "sql.js" { class Statement { bind(): boolean; - bind(values: KeyValueObject): boolean; - bind(values: SQLValue[]): boolean; + bind(values: { [key: string]: number | string | Uint8Array }): boolean; + bind(values: (number | string | Uint8Array)[]): boolean; step(): boolean; - get(): DataRow; - get(params: KeyValueObject): DataRow; - get(params: SQLValue[]): DataRow; + get(): (number | string | Uint8Array)[]; + get(params: { [key: string]: number | string | Uint8Array }): (number | string | Uint8Array)[]; + get(params: (number | string | Uint8Array)[]): (number | string | Uint8Array)[]; getColumnNames(): string[]; - getAsObject(): SQLValueObject; - getAsObject(params: KeyValueObject): SQLValueObject; - getAsObject(params: SQLValue[]): SQLValueObject; + getAsObject(): { [columnName: string]: number | string | Uint8Array }; + getAsObject(params: { [key: string]: number | string | Uint8Array }): { [columnName: string]: number | string | Uint8Array }; + getAsObject(params: (number | string | Uint8Array)[]): { [columnName: string]: number | string | Uint8Array }; run(): void; - run(values: KeyValueObject): void; - run(values: SQLValue[]): void; + run(values: { [key: string]: number | string | Uint8Array }): void; + run(values: (number | string | Uint8Array)[]): void; reset(): void; @@ -65,7 +61,7 @@ declare module "sql.js" { interface QueryResults { columns: string[]; - values: DataRow[]; + values: (number | string | Uint8Array)[][]; } }