1e11b7986SStanisław Chmiela// Definitions copied from `@types/websql` as we want
2e11b7986SStanisław Chmiela// to expose a custom version of the API that:
3e11b7986SStanisław Chmiela// - uses primitive `string` instead of `String`
4e11b7986SStanisław Chmiela// - excludes some methods that are not exposed by our API.
5e11b7986SStanisław Chmiela//
6e11b7986SStanisław Chmiela// Original definitions by: TeamworkGuy2 <https://github.com/TeamworkGuy2>
7e11b7986SStanisław Chmiela
8abded2bbSBartosz Kaszubowski// @docsMissing
9e11b7986SStanisław Chmielaexport interface Window {
10e11b7986SStanisław Chmiela  openDatabase?: (
11e11b7986SStanisław Chmiela    name: string,
12e11b7986SStanisław Chmiela    version: string,
13e11b7986SStanisław Chmiela    displayName: string,
14e11b7986SStanisław Chmiela    estimatedSize: number,
15e11b7986SStanisław Chmiela    creationCallback?: DatabaseCallback
16e11b7986SStanisław Chmiela  ) => Database;
17e11b7986SStanisław Chmiela}
18034d2790SSzymon20000
19abded2bbSBartosz Kaszubowski// @docsMissing
20abded2bbSBartosz Kaszubowskiexport type DatabaseCallback = (database: Database) => void;
21034d2790SSzymon20000
22abded2bbSBartosz Kaszubowski// @needsAudit @docsMissing
23abded2bbSBartosz Kaszubowski/**
24abded2bbSBartosz Kaszubowski * `Database` objects are returned by calls to `SQLite.openDatabase()`. Such an object represents a
25abded2bbSBartosz Kaszubowski * connection to a database on your device.
26abded2bbSBartosz Kaszubowski */
27034d2790SSzymon20000export interface Database {
28034d2790SSzymon20000  version: string;
29034d2790SSzymon20000
30abded2bbSBartosz Kaszubowski  /**
31abded2bbSBartosz Kaszubowski   * Execute a database transaction.
32abded2bbSBartosz Kaszubowski   * @param callback A function representing the transaction to perform. Takes a Transaction
33abded2bbSBartosz Kaszubowski   * (see below) as its only parameter, on which it can add SQL statements to execute.
34abded2bbSBartosz Kaszubowski   * @param errorCallback Called if an error occurred processing this transaction. Takes a single
35abded2bbSBartosz Kaszubowski   * parameter describing the error.
36abded2bbSBartosz Kaszubowski   * @param successCallback Called when the transaction has completed executing on the database.
37abded2bbSBartosz Kaszubowski   */
38034d2790SSzymon20000  transaction(
39034d2790SSzymon20000    callback: SQLTransactionCallback,
40034d2790SSzymon20000    errorCallback?: SQLTransactionErrorCallback,
41abded2bbSBartosz Kaszubowski    successCallback?: () => void
42034d2790SSzymon20000  ): void;
43034d2790SSzymon20000
44034d2790SSzymon20000  readTransaction(
45034d2790SSzymon20000    callback: SQLTransactionCallback,
46034d2790SSzymon20000    errorCallback?: SQLTransactionErrorCallback,
47abded2bbSBartosz Kaszubowski    successCallback?: () => void
48034d2790SSzymon20000  ): void;
49034d2790SSzymon20000}
50034d2790SSzymon20000
51abded2bbSBartosz Kaszubowski// @docsMissing
52abded2bbSBartosz Kaszubowskiexport type SQLTransactionCallback = (transaction: SQLTransaction) => void;
53034d2790SSzymon20000
54abded2bbSBartosz Kaszubowski// @docsMissing
55abded2bbSBartosz Kaszubowskiexport type SQLTransactionErrorCallback = (error: SQLError) => void;
56034d2790SSzymon20000
57abded2bbSBartosz Kaszubowski// @needsAudit
58abded2bbSBartosz Kaszubowski/**
59abded2bbSBartosz Kaszubowski * A `SQLTransaction` object is passed in as a parameter to the `callback` parameter for the
60abded2bbSBartosz Kaszubowski * `db.transaction()` method on a `Database` (see above). It allows enqueuing SQL statements to
61abded2bbSBartosz Kaszubowski * perform in a database transaction.
62abded2bbSBartosz Kaszubowski */
63034d2790SSzymon20000export interface SQLTransaction {
64abded2bbSBartosz Kaszubowski  /**
65abded2bbSBartosz Kaszubowski   * Enqueue a SQL statement to execute in the transaction. Authors are strongly recommended to make
66abded2bbSBartosz Kaszubowski   * use of the `?` placeholder feature of the method to avoid against SQL injection attacks, and to
67abded2bbSBartosz Kaszubowski   * never construct SQL statements on the fly.
68abded2bbSBartosz Kaszubowski   * @param sqlStatement A string containing a database query to execute expressed as SQL. The string
69abded2bbSBartosz Kaszubowski   * may contain `?` placeholders, with values to be substituted listed in the `arguments` parameter.
7092499077STyler Sheaffer   * @param args An array of values (numbers, strings or nulls) to substitute for `?` placeholders in the
71abded2bbSBartosz Kaszubowski   * SQL statement.
72abded2bbSBartosz Kaszubowski   * @param callback Called when the query is successfully completed during the transaction. Takes
73abded2bbSBartosz Kaszubowski   * two parameters: the transaction itself, and a `ResultSet` object (see below) with the results
74abded2bbSBartosz Kaszubowski   * of the query.
75abded2bbSBartosz Kaszubowski   * @param errorCallback Called if an error occurred executing this particular query in the
76abded2bbSBartosz Kaszubowski   * transaction. Takes two parameters: the transaction itself, and the error object.
77abded2bbSBartosz Kaszubowski   */
78034d2790SSzymon20000  executeSql(
79034d2790SSzymon20000    sqlStatement: string,
8092499077STyler Sheaffer    args?: (number | string | null)[],
81034d2790SSzymon20000    callback?: SQLStatementCallback,
82034d2790SSzymon20000    errorCallback?: SQLStatementErrorCallback
83034d2790SSzymon20000  ): void;
84034d2790SSzymon20000}
85034d2790SSzymon20000
86abded2bbSBartosz Kaszubowski// @docsMissing
87abded2bbSBartosz Kaszubowskiexport type SQLStatementCallback = (transaction: SQLTransaction, resultSet: SQLResultSet) => void;
88034d2790SSzymon20000
89abded2bbSBartosz Kaszubowski// @docsMissing
90abded2bbSBartosz Kaszubowskiexport type SQLStatementErrorCallback = (transaction: SQLTransaction, error: SQLError) => boolean;
91034d2790SSzymon20000
92abded2bbSBartosz Kaszubowski// @needsAudit
93abded2bbSBartosz Kaszubowskiexport type SQLResultSet = {
94abded2bbSBartosz Kaszubowski  /**
95abded2bbSBartosz Kaszubowski   * The row ID of the row that the SQL statement inserted into the database, if a row was inserted.
96abded2bbSBartosz Kaszubowski   */
97abded2bbSBartosz Kaszubowski  insertId?: number;
98abded2bbSBartosz Kaszubowski  /**
99abded2bbSBartosz Kaszubowski   * The number of rows that were changed by the SQL statement.
100abded2bbSBartosz Kaszubowski   */
101034d2790SSzymon20000  rowsAffected: number;
102034d2790SSzymon20000  rows: SQLResultSetRowList;
103abded2bbSBartosz Kaszubowski};
104034d2790SSzymon20000
105abded2bbSBartosz Kaszubowski// @needsAudit
106034d2790SSzymon20000export interface SQLResultSetRowList {
107abded2bbSBartosz Kaszubowski  /**
108abded2bbSBartosz Kaszubowski   * The number of rows returned by the query.
109abded2bbSBartosz Kaszubowski   */
110034d2790SSzymon20000  length: number;
111abded2bbSBartosz Kaszubowski  /**
112abded2bbSBartosz Kaszubowski   * Returns the row with the given `index`. If there is no such row, returns `null`.
113abded2bbSBartosz Kaszubowski   * @param index Index of row to get.
114abded2bbSBartosz Kaszubowski   */
115034d2790SSzymon20000  item(index: number): any;
116abded2bbSBartosz Kaszubowski  /**
117abded2bbSBartosz Kaszubowski   * The actual array of rows returned by the query. Can be used directly instead of
118abded2bbSBartosz Kaszubowski   * getting rows through rows.item().
119abded2bbSBartosz Kaszubowski   */
1201fb1afb4SBartłomiej Bukowski  _array: any[];
121034d2790SSzymon20000}
122034d2790SSzymon20000
123abded2bbSBartosz Kaszubowski// @docsMissing
124034d2790SSzymon20000export declare class SQLError {
125034d2790SSzymon20000  static UNKNOWN_ERR: number;
126034d2790SSzymon20000  static DATABASE_ERR: number;
127034d2790SSzymon20000  static VERSION_ERR: number;
128034d2790SSzymon20000  static TOO_LARGE_ERR: number;
129034d2790SSzymon20000  static QUOTA_ERR: number;
130034d2790SSzymon20000  static SYNTAX_ERR: number;
131034d2790SSzymon20000  static CONSTRAINT_ERR: number;
132034d2790SSzymon20000  static TIMEOUT_ERR: number;
133034d2790SSzymon20000
134034d2790SSzymon20000  code: number;
135034d2790SSzymon20000  message: string;
136034d2790SSzymon20000}
137034d2790SSzymon20000
1384a7bfa1dSKudo Chien/** @deprecated Use `SQLiteDatabase` instead. */
139034d2790SSzymon20000export interface WebSQLDatabase extends Database {
140034d2790SSzymon20000  exec(queries: Query[], readOnly: boolean, callback: SQLiteCallback): void;
1413f4e5fe5SKudo Chien
1423f4e5fe5SKudo Chien  /**
1433f4e5fe5SKudo Chien   * Close the database.
1443f4e5fe5SKudo Chien   */
1453f4e5fe5SKudo Chien  closeAsync(): void;
1463f4e5fe5SKudo Chien
1473f4e5fe5SKudo Chien  /**
1483f4e5fe5SKudo Chien   * Delete the database file.
1493f4e5fe5SKudo Chien   * > The database has to be closed prior to deletion.
1503f4e5fe5SKudo Chien   */
1513f4e5fe5SKudo Chien  deleteAsync(): Promise<void>;
152034d2790SSzymon20000}
153034d2790SSzymon20000
154abded2bbSBartosz Kaszubowski// @docsMissing
155034d2790SSzymon20000export type Query = { sql: string; args: unknown[] };
156034d2790SSzymon20000
157abded2bbSBartosz Kaszubowski// @docsMissing
158c52e83e3SKudo Chienexport interface ResultSetError {
159034d2790SSzymon20000  error: Error;
160c52e83e3SKudo Chien}
161abded2bbSBartosz Kaszubowski
162abded2bbSBartosz Kaszubowski// @needsAudit
163abded2bbSBartosz Kaszubowski/**
164abded2bbSBartosz Kaszubowski * `ResultSet` objects are returned through second parameter of the `success` callback for the
165abded2bbSBartosz Kaszubowski * `tx.executeSql()` method on a `SQLTransaction` (see above).
166abded2bbSBartosz Kaszubowski */
167c52e83e3SKudo Chienexport interface ResultSet {
168abded2bbSBartosz Kaszubowski  /**
169abded2bbSBartosz Kaszubowski   * The row ID of the row that the SQL statement inserted into the database, if a row was inserted.
170abded2bbSBartosz Kaszubowski   */
171034d2790SSzymon20000  insertId?: number;
172abded2bbSBartosz Kaszubowski  /**
173abded2bbSBartosz Kaszubowski   * The number of rows that were changed by the SQL statement.
174abded2bbSBartosz Kaszubowski   */
175034d2790SSzymon20000  rowsAffected: number;
1765ab2946bSWill Schurman  rows: { [column: string]: any }[];
177c52e83e3SKudo Chien}
178034d2790SSzymon20000
179abded2bbSBartosz Kaszubowski// @docsMissing
180034d2790SSzymon20000export type SQLiteCallback = (
181034d2790SSzymon20000  error?: Error | null,
1825ab2946bSWill Schurman  resultSet?: (ResultSetError | ResultSet)[]
183034d2790SSzymon20000) => void;
1844a7bfa1dSKudo Chien
1854a7bfa1dSKudo Chien/** A transaction object to perform SQL statements in async mode. */
1864a7bfa1dSKudo Chienexport interface SQLTransactionAsync {
1874a7bfa1dSKudo Chien  /** Executes a SQL statement in async mode. */
188*c4573fffSKudo Chien  executeSqlAsync(sqlStatement: string, args?: (number | string)[]): Promise<ResultSet>;
1894a7bfa1dSKudo Chien}
1904a7bfa1dSKudo Chien
1914a7bfa1dSKudo Chien/** A transaction callback with given `SQLTransactionAsync` object to perform SQL statements in async mode. */
1924a7bfa1dSKudo Chienexport type SQLTransactionAsyncCallback = (transaction: SQLTransactionAsync) => Promise<void>;
193