@@ -11,8 +11,12 @@ import {
1111 CrateDBBulkResponse ,
1212 CrateDBRecord ,
1313 CrateDBErrorResponse ,
14+ OptimizeOptions ,
15+ ColumnDefinition ,
16+ TableOptions ,
1417} from './interfaces' ;
1518import { CrateDBError , DeserializationError , RequestError } from './utils/Error.js' ;
19+ import { StatementGenerator } from './StatementGenerator.js' ;
1620
1721// Configuration options with CrateDB-specific environment variables
1822const defaultConfig : CrateDBConfig = {
@@ -162,23 +166,6 @@ export class CrateDBClient {
162166 }
163167 }
164168
165- // Convenience methods for common SQL operations
166- private _generateInsertQuery ( tableName : string , keys : string [ ] , primaryKeys : string [ ] | null ) : string {
167- const placeholders = keys . map ( ( ) => '?' ) . join ( ', ' ) ;
168- let query = `INSERT INTO ${ tableName } (${ keys . map ( ( key ) => `"${ key } "` ) . join ( ', ' ) } ) VALUES (${ placeholders } )` ;
169-
170- if ( primaryKeys && primaryKeys . length > 0 ) {
171- const keysWithoutPrimary = keys . filter ( ( key ) => ! primaryKeys . includes ( key ) ) ;
172- const updates = keysWithoutPrimary . map ( ( key ) => `"${ key } " = excluded."${ key } "` ) . join ( ', ' ) ;
173- query += ` ON CONFLICT (${ primaryKeys . map ( ( key ) => `"${ key } "` ) . join ( ', ' ) } ) DO UPDATE SET ${ updates } ` ;
174- } else {
175- query += ' ON CONFLICT DO NOTHING' ;
176- }
177-
178- query += ';' ; // Ensure the query ends with a semicolon
179- return query ;
180- }
181-
182169 async insert (
183170 tableName : string ,
184171 obj : Record < string , unknown > ,
@@ -196,7 +183,7 @@ export class CrateDBClient {
196183 }
197184
198185 const keys = Object . keys ( obj ) ;
199- const query = this . _generateInsertQuery ( tableName , keys , primaryKeys ) ;
186+ const query = StatementGenerator . insert ( tableName , keys , primaryKeys ) ;
200187 const args = Object . values ( obj ) ;
201188
202189 // Execute the query
@@ -233,7 +220,7 @@ export class CrateDBClient {
233220 uniqueKeys . map ( ( key ) => ( Object . prototype . hasOwnProperty . call ( obj , key ) ? obj [ key ] : null ) )
234221 ) ;
235222
236- const query = this . _generateInsertQuery ( tableName , uniqueKeys , primaryKeys ) ;
223+ const query = StatementGenerator . insert ( tableName , uniqueKeys , primaryKeys ) ;
237224
238225 // Execute the query with bulk arguments
239226 const response = await this . executeMany ( query , bulkArgs ) ;
@@ -245,32 +232,69 @@ export class CrateDBClient {
245232
246233 async update ( tableName : string , options : Record < string , unknown > , whereClause : string ) : Promise < CrateDBResponse > {
247234 const { keys, values, args } = this . _prepareOptions ( options ) ;
248- const setClause = keys . map ( ( key , i ) => `${ key } =${ values [ i ] } ` ) . join ( ', ' ) ;
249- const query = `UPDATE ${ tableName } SET ${ setClause } WHERE ${ whereClause } ` ;
235+ const query = StatementGenerator . update ( tableName , options , whereClause ) ;
250236 return this . execute ( query , args ) ;
251237 }
252238
253239 async delete ( tableName : string , whereClause : string ) : Promise < CrateDBResponse > {
254- const query = `DELETE FROM ${ tableName } WHERE ${ whereClause } ` ;
240+ const query = StatementGenerator . delete ( tableName , whereClause ) ;
255241 return this . execute ( query ) ;
256242 }
257243
244+ /**
245+ * Drops a table if it exists in CrateDB.
246+ *
247+ * Constructs and executes a `DROP TABLE IF EXISTS` SQL statement.
248+ *
249+ * @param {string } tableName - The name of the table to drop.
250+ * @returns {Promise<CrateDBResponse> } A promise resolving to the response from CrateDB.
251+ */
258252 async drop ( tableName : string ) : Promise < CrateDBResponse > {
259- const query = `DROP TABLE IF EXISTS ${ tableName } ` ;
253+ const query = StatementGenerator . dropTable ( tableName ) ;
260254 return this . execute ( query ) ;
261255 }
262256
257+ async createTable (
258+ tableName : string ,
259+ schema : Record < string , ColumnDefinition > ,
260+ options ?: TableOptions
261+ ) : Promise < CrateDBResponse > {
262+ const query = StatementGenerator . createTable ( tableName , schema , options ) ;
263+ return this . execute ( query ) ;
264+ }
265+
266+ /**
267+ * Refreshes a given table by refreshing it in CrateDB.
268+ *
269+ * The `REFRESH TABLE` command makes recently committed changes available for querying
270+ * without waiting for automatic refresh intervals.
271+ *
272+ * @param {string } tableName - The name of the table to refresh.
273+ * @returns {Promise<CrateDBResponse> } A promise resolving to the response from CrateDB.
274+ */
263275 async refresh ( tableName : string ) : Promise < CrateDBResponse > {
264- const query = `REFRESH TABLE ${ tableName } ` ;
276+ const query = StatementGenerator . refresh ( tableName ) ;
265277 return this . execute ( query ) ;
266278 }
267279
268- async createTable ( schema : Record < string , Record < string , string > > ) : Promise < CrateDBResponse > {
269- const tableName = Object . keys ( schema ) [ 0 ] ;
270- const columns = Object . entries ( schema [ tableName ] )
271- . map ( ( [ col , type ] ) => `"${ col } " ${ type } ` )
272- . join ( ', ' ) ;
273- const query = `CREATE TABLE ${ tableName } (${ columns } )` ;
280+ /**
281+ * Optimizes a given table or specific partitions in CrateDB by merging table segments.
282+ *
283+ * The `OPTIMIZE TABLE` command reduces the number of segments in a table, improving
284+ * query performance and reducing storage overhead. It supports optimizing the entire table
285+ * or specific partitions and allows additional optimization parameters.
286+ *
287+ * @param {string } tableName - The name of the table to optimize.
288+ * @param {OptimizeOptions } [options] - Optional parameters for table optimization.
289+ * @param {Record<string, string | number> } [partitions] - Optional key-value pairs specifying partition columns and values.
290+ * @returns {Promise<CrateDBResponse> } A promise resolving to the response from CrateDB.
291+ */
292+ async optimize (
293+ tableName : string ,
294+ options ?: OptimizeOptions ,
295+ partitions ?: Record < string , string | number >
296+ ) : Promise < CrateDBResponse > {
297+ const query = StatementGenerator . optimize ( tableName , options , partitions ) ;
274298 return this . execute ( query ) ;
275299 }
276300
0 commit comments