import { ForeignKey } from '../triggers.js'; import { QualifiedTablename, SqlValue, Statement } from '../../util/index.js'; export type Dialect = 'SQLite' | 'Postgres'; export declare abstract class QueryBuilder { abstract readonly dialect: Dialect; abstract readonly paramSign: '?' | '$'; abstract readonly defaultNamespace: 'main' | 'public'; /** * The autoincrementing integer primary key type for the current SQL dialect. */ abstract readonly AUTOINCREMENT_PK: string; /** * The type to use for BLOB for the current SQL dialect. */ abstract readonly BLOB: string; /** * Queries the version of SQLite/Postgres we are using. */ abstract readonly getVersion: string; /** * Depending on the dialect, defers or disables foreign key checks for the duration of the transaction. */ abstract readonly deferOrDisableFKsForTx: string; /** * Returns the given query if the current SQL dialect is PostgreSQL. */ abstract pgOnly(query: string): string; /** * Returns the given query if the current SQL dialect is SQLite. */ abstract sqliteOnly(query: string): string; /** * Makes the i-th positional parameter, * e.g. '$3' For Postgres when `i` is 3 * and always '?' for SQLite */ abstract makePositionalParam(i: number): string; /** * Checks if the given table exists. */ abstract tableExists(table: QualifiedTablename): Statement; /** * Counts tables whose name is included in `tableNames`. */ abstract countTablesIn(tableNames: string[]): Statement; /** * Converts a column value to a hexidecimal string. */ abstract toHex(column: string): string; /** * Converts a hexidecimal string to a hex value. */ abstract hexValue(hexString: string): string; /** * Create an index on a table. */ abstract createIndex(indexName: string, onTable: QualifiedTablename, columns: string[]): string; /** * Fetches the names of all tables that are not in `notIn`. */ abstract getLocalTableNames(notIn?: string[]): Statement; /** * Fetches information about the columns of a table. * The information includes all column names, their type, * whether or not they are nullable, and whether they are part of the PK. */ abstract getTableInfo(table: QualifiedTablename): Statement; /** * Insert a row into a table, ignoring it if it already exists. */ abstract insertOrIgnore(table: QualifiedTablename, columns: string[], values: SqlValue[]): Statement; /** * Insert a row into a table, replacing it if it already exists. */ abstract insertOrReplace(table: QualifiedTablename, columns: string[], values: Array, conflictCols: string[], updateCols: string[]): Statement; /** * Insert a row into a table. * If it already exists we update the provided columns `updateCols` * with the provided values `updateVals` */ abstract insertOrReplaceWith(table: QualifiedTablename, columns: string[], values: Array, conflictCols: string[], updateCols: string[], updateVals: SqlValue[]): Statement; /** * Inserts a batch of rows into a table, replacing them if they already exist. */ abstract batchedInsertOrReplace(table: QualifiedTablename, columns: string[], records: Array>, conflictCols: string[], updateCols: string[], maxSqlParameters: number): Statement[]; /** * Drop a trigger if it exists. */ abstract dropTriggerIfExists(triggerName: string, table: QualifiedTablename): string; /** * Create a trigger that prevents updates to the primary key. */ abstract createNoFkUpdateTrigger(table: QualifiedTablename, pk: string[]): string[]; /** * Creates or replaces a trigger that prevents updates to the primary key. */ createOrReplaceNoFkUpdateTrigger(table: QualifiedTablename, pk: string[]): string[]; /** * Modifies the trigger setting for the table identified by its tablename and namespace. */ abstract setTriggerSetting(table: QualifiedTablename, value: 0 | 1): string; /** * Create a trigger that logs operations into the oplog. */ abstract createOplogTrigger(opType: 'INSERT' | 'UPDATE' | 'DELETE', table: QualifiedTablename, newPKs: string, newRows: string, oldRows: string): string[]; createOrReplaceOplogTrigger(opType: 'INSERT' | 'UPDATE' | 'DELETE', table: QualifiedTablename, newPKs: string, newRows: string, oldRows: string): string[]; /** * Creates or replaces a trigger that logs insertions into the oplog. */ createOrReplaceInsertTrigger: (table: QualifiedTablename, newPKs: string, newRows: string, oldRows: string) => string[]; /** * Creates or replaces a trigger that logs updates into the oplog. */ createOrReplaceUpdateTrigger: (table: QualifiedTablename, newPKs: string, newRows: string, oldRows: string) => string[]; /** * Creates or replaces a trigger that logs deletions into the oplog. */ createOrReplaceDeleteTrigger: (table: QualifiedTablename, newPKs: string, newRows: string, oldRows: string) => string[]; /** * Creates a trigger that logs compensations for operations into the oplog. */ abstract createFkCompensationTrigger(opType: 'INSERT' | 'UPDATE', table: QualifiedTablename, childKey: string, fkTable: QualifiedTablename, joinedFkPKs: string, foreignKey: ForeignKey): string[]; createOrReplaceFkCompensationTrigger(opType: 'INSERT' | 'UPDATE', table: QualifiedTablename, childKey: string, fkTable: QualifiedTablename, joinedFkPKs: string, foreignKey: ForeignKey): string[]; /** * Creates a trigger that logs compensations for insertions into the oplog. */ createOrReplaceInsertCompensationTrigger: (table: QualifiedTablename, childKey: string, fkTable: QualifiedTablename, joinedFkPKs: string, foreignKey: ForeignKey) => string[]; /** * Creates a trigger that logs compensations for updates into the oplog. */ createOrReplaceUpdateCompensationTrigger: (table: QualifiedTablename, childKey: string, fkTable: QualifiedTablename, joinedFkPKs: string, foreignKey: ForeignKey) => string[]; /** * For each affected shadow row, set new tag array, unless the last oplog operation was a DELETE */ abstract setTagsForShadowRows(oplogTable: QualifiedTablename, shadowTable: QualifiedTablename): string; /** * Deletes any shadow rows where the last oplog operation was a `DELETE` */ abstract removeDeletedShadowRows(oplogTable: QualifiedTablename, shadowTable: QualifiedTablename): string; /** * Prepare multiple batched insert statements for an array of records. * * Since SQLite only supports a limited amount of positional `?` parameters, * we generate multiple insert statements with each one being filled as much * as possible from the given data. All statements are derived from same `baseSql` - * the positional parameters will be appended to this string. * * @param baseSql base SQL string to which inserts should be appended * @param columns columns that describe records * @param records records to be inserted * @param maxParameters max parameters this SQLite can accept - determines batching factor * @param suffixSql optional SQL string to append to each insert statement * @returns array of statements ready to be executed by the adapter */ prepareInsertBatchedStatements(baseSql: string, columns: string[], records: Record[], maxParameters: number, suffixSql?: string): Statement[]; /** * Prepare multiple batched DELETE statements for an array of records. * * Since SQLite only supports a limited amount of positional `?` parameters, * we generate multiple delete statements with each one being filled as much * as possible from the given data. This function only supports column equality checks * * @param baseSql base SQL string to which inserts should be appended * @param columns columns that describe records * @param records records to be inserted * @param maxParameters max parameters this SQLite can accept - determines batching factor * @param suffixSql optional SQL string to append to each insert statement * @returns array of statements ready to be executed by the adapter */ prepareDeleteBatchedStatements(baseSql: string, columns: Array, records: T[], maxParameters: number, suffixSql?: string): Statement[]; makeQT(tablename: string): QualifiedTablename; }