@@ -7,13 +7,6 @@ const tableNameFormat = async (req: Request, dbDataSource: DataSource) => {
7
7
const { db_type, username } = req . session ;
8
8
const { tableName } = req . body ;
9
9
10
- // let newTableName = '';
11
- // if (tableName.substring(0, 7) === '.public') {
12
- // newTableName = tableName.slice(7)
13
- // } else {
14
- // newTableName = tableName
15
- // };
16
-
17
10
let tableNameFormat = '' ;
18
11
switch ( db_type ) {
19
12
case 'oracle' :
@@ -194,7 +187,7 @@ export const updateRow: RequestHandler = async (req: Request, _res: Response, ne
194
187
195
188
await dbDataSource . destroy ( ) ;
196
189
console . log ( 'Database has been disconnected' ) ;
197
- return dbUpdatedRow ;
190
+ return dbUpdatedRow ;
198
191
199
192
} catch ( err : unknown ) {
200
193
console . log ( 'Error occurred in the updatedRow middleware: ' , err ) ;
@@ -209,49 +202,25 @@ export const updateRow: RequestHandler = async (req: Request, _res: Response, ne
209
202
export const deleteRow : RequestHandler = async ( req : Request , _res : Response , next : NextFunction , ) => {
210
203
const dbDataSource = await dbConnect ( req ) ;
211
204
const { db_type } = req . session ;
212
- const { primaryKey , value, deletedRow } = req . body
205
+ const { value } = req . body
213
206
214
207
try {
215
- // let tableNameDelete = '';
216
- // switch (db_type) {
217
- // case 'oracle':
218
- // tableNameDelete = `"${(username as string).toUpperCase()}"."${tableName}"`;
219
- // break;
220
- // case 'mssql':
221
- // const schemaName: {[SchemaName: string]: string}[] = await dbDataSource.query(`SELECT SCHEMA_NAME() AS SchemaName;`);
222
- // tableNameDelete = `${schemaName[0].SchemaName}.${tableName}`;
223
- // break;
224
- // default:
225
- // tableNameDelete = tableName;
226
- // break;
227
- // };
228
-
229
208
const tableNameDelete = await Promise . resolve ( tableNameFormat ( req , dbDataSource ) ) ;
230
209
231
- if ( primaryKey ) {
232
- // Deleting a row that has a PK
233
- await dbDataSource . query ( `
234
- DELETE FROM ${ tableNameDelete }
235
- WHERE ${ db_type === 'oracle' ? `"${ primaryKey } "` : primaryKey } = ${ db_type === 'oracle' || db_type === 'mysql' ? `'${ value } '` : value }
236
- ` )
237
-
238
- } else {
239
- // Deleting a row that does NOT have a PK
240
- const deleteEntries = Object . entries ( deletedRow ) . filter ( ( [ _key , value ] ) => value !== null ) ;
241
- const deleteKeys = deleteEntries . map ( ( [ key , _value ] ) => key ) ;
242
- const deleteValues = deleteEntries . map ( ( [ _key , value ] ) => value ) ;
243
-
244
- let oracleKeyValueString = '' ;
245
- for ( let i = 0 ; i < deleteKeys . length ; i ++ ) {
246
- oracleKeyValueString += `"${ deleteKeys [ i ] } " = '${ deleteValues [ i ] } '${ i < deleteKeys . length - 1 ? ' AND ' : '' } `
247
- } ;
248
- const keyValueString = oracleKeyValueString . replace ( / " / g, '' ) ;
210
+ const deleteEntries = Object . entries ( value ) . filter ( ( [ _key , value ] ) => value !== null ) ;
211
+ const deleteKeys = deleteEntries . map ( ( [ key , _value ] ) => key ) ;
212
+ const deleteValues = deleteEntries . map ( ( [ _key , value ] ) => value ) ;
249
213
250
- await dbDataSource . query ( `
251
- DELETE FROM ${ tableNameDelete }
252
- WHERE ${ db_type === 'oracle' ? oracleKeyValueString : keyValueString }
253
- ` )
214
+ let oracleKeyValueString = '' ;
215
+ for ( let i = 0 ; i < deleteKeys . length ; i ++ ) {
216
+ oracleKeyValueString += `"${ deleteKeys [ i ] } " = '${ deleteValues [ i ] } '${ i < deleteKeys . length - 1 ? ' AND ' : '' } `
254
217
} ;
218
+ const keyValueString = oracleKeyValueString . replace ( / " / g, '' ) ;
219
+
220
+ await dbDataSource . query ( `
221
+ DELETE FROM ${ tableNameDelete }
222
+ WHERE ${ db_type === 'oracle' ? oracleKeyValueString : keyValueString }
223
+ ` )
255
224
256
225
dbDataSource . destroy ( ) ;
257
226
console . log ( 'Database has been disconnected' ) ;
@@ -279,26 +248,33 @@ export const addNewDbColumn: RequestHandler = async (req: Request, _res: Respons
279
248
console . log ( 'tableNameAddColumn: ' , tableNameAddColumn )
280
249
281
250
let keyValueString : string = '' ;
282
- let repeatingKeyValueString : string = '' ;
283
251
let newColumnString : string = ''
284
252
285
253
columnData . forEach ( ( el : NewColumn ) => {
286
- if ( db_type === 'mssql' || db_type === 'oracle' ) {
287
- repeatingKeyValueString += `ALTER TABLE ${ tableNameAddColumn } ADD "${ el . name } " ${ el . type === 'AUTO_INCREMENT' ? 'INT' : el . type } ${ el . isPrimary ? ' PRIMARY KEY' : '' } ${ el . isNullable ? '' : ' NOT NULL' } ${ el . defaultValue ? ` DEFAULT ${ el . defaultValue } ` : '' } ${ el . type === 'AUTO_INCREMENT' ? ' AUTO_INCREMENT' : '' } ; `
288
- } else {
254
+ if ( db_type === 'mssql' ) {
255
+ keyValueString += `ALTER TABLE ${ tableNameAddColumn } ADD "${ el . name } " ${ el . type === 'AUTO_INCREMENT' ? 'INT' : el . type } ${ el . isPrimary ? ' PRIMARY KEY' : '' } ${ el . isNullable ? '' : ' NOT NULL' } ${ el . defaultValue ? ` DEFAULT ${ el . defaultValue } ` : '' } ${ el . type === 'AUTO_INCREMENT' ? ' AUTO_INCREMENT' : '' } ; `
256
+ } else if ( db_type === 'oracle' ) {
257
+ let number : string = '' ;
258
+ if ( el . type . includes ( 'VARCHAR' ) ) {
259
+ const regex = / \( ( \d + ) \) / ;
260
+ const match = el . type . match ( regex )
261
+ number = ( match as RegExpMatchArray ) [ 1 ]
262
+ }
263
+ keyValueString += `ALTER TABLE ${ tableNameAddColumn } ADD(${ el . name } ${ el . type . includes ( 'VARCHAR' ) ? `VARCHAR2(${ + number } )` : el . type } ${ el . isPrimary ? ' PRIMARY KEY' : '' } ${ el . isNullable ? '' : ' NOT NULL' } ${ el . defaultValue ? ` DEFAULT ${ el . defaultValue } ` : '' } ${ el . type === 'AUTO_INCREMENT' ? ' AUTO_INCREMENT' : '' } ))`
264
+ } else {
289
265
keyValueString += `ADD${ db_type === 'postgres' ? ' COLUMN' : '' } ${ db_type === 'mysql' ? `${ el . name } ` : `"${ el . name } "` } ${ el . type === 'AUTO_INCREMENT' ? 'INT' : el . type } ${ el . isPrimary ? ' PRIMARY KEY' : '' } ${ el . isNullable ? '' : ' NOT NULL' } ${ el . defaultValue ? ` DEFAULT ${ el . defaultValue } ` : '' } ${ el . type === 'AUTO_INCREMENT' ? ' AUTO_INCREMENT' : '' } , `
290
266
} ;
291
267
} ) ;
292
268
293
269
if ( db_type === 'mssql' || db_type === 'oracle' ) {
294
- newColumnString = repeatingKeyValueString . slice ( 0 , - 1 ) ;
270
+ newColumnString = keyValueString . slice ( 0 , - 1 ) ;
295
271
} else {
296
272
newColumnString = keyValueString . slice ( 0 , - 2 ) ;
297
- }
273
+ } ;
298
274
299
- if ( db_type === 'mssql' ) {
275
+ if ( db_type === 'mssql' || db_type === 'oracle' ) {
300
276
const addedNewColumn : Promise < unknown > = await dbDataSource . query ( `
301
- ${ repeatingKeyValueString }
277
+ ${ newColumnString }
302
278
` ) ;
303
279
304
280
await dbDataSource . destroy ( ) ;
@@ -329,26 +305,22 @@ export const addNewDbColumn: RequestHandler = async (req: Request, _res: Respons
329
305
export const updateDbColumn : RequestHandler = async ( req : Request , _res : Response , next : NextFunction , ) => {
330
306
const dbDataSource = await dbConnect ( req ) ;
331
307
const { db_type, username } = req . session ;
308
+ const { tableName, columnName, schemaData, columnData } = req . body ;
332
309
333
310
try {
334
- const updateColumnData : { [ key : string ] : string } = req . body ;
335
-
336
- const schemaName = db_type === 'mssql' ? await dbDataSource . query ( `SELECT SCHEMA_NAME() AS SchemaName;` ) : '' ;
337
-
338
- const slicedTableName = updateColumnData . tableName . slice ( 7 , updateColumnData . tableName . length + 1 ) ;
339
- const tableName : string = db_type === 'oracle' ? `"${ ( username as string ) . toUpperCase ( ) } "."${ slicedTableName } "` :
340
- db_type === 'mssql' ? `${ schemaName [ 0 ] . SchemaName } .${ slicedTableName } ` : updateColumnData . tableName ;
311
+ const tableNameUpdateColumn = await Promise . resolve ( tableNameFormat ( req , dbDataSource ) ) ;
312
+ console . log ( 'schemaData: ' , schemaData )
313
+ console . log ( 'columnData: ' , columnData )
341
314
342
-
343
- const updatedColumn : Promise < unknown > = await dbDataSource . query ( `
344
- ALTER TABLE ${ tableName }
345
- ${ db_type === 'postgres' || db_type === 'microsoft' ? 'ALTER COLUMN' : 'MODIFY' } "${ updateColumnData . columnName } " ${ updateColumnData . dataType } ${ db_type === 'postgres' ? updateColumnData . constraintName : null } ${ updateColumnData . constraintExpression }
346
- ` ) ;
315
+ // const updatedColumn: Promise<unknown> = await dbDataSource.query(`
316
+ // ALTER TABLE ${tableNameUpdateColumn}
317
+ // ${db_type === 'postgres' || db_type === 'microsoft' ? 'ALTER COLUMN' : 'MODIFY' } "${columnName}" ${db_type} ${db_type === 'postgres' ? updateColumnData.constraintName : null} ${updateColumnData.constraintExpression}
318
+ // `);
347
319
348
320
dbDataSource . destroy ( ) ;
349
321
console . log ( 'Database has been disconnected' ) ;
350
- console . log ( 'addedForeignKey in helper: ' , updatedColumn ) ;
351
- return updatedColumn ;
322
+ // console.log('addedForeignKey in helper: ', updatedColumn);
323
+ // return updatedColumn;
352
324
353
325
} catch ( err : unknown ) {
354
326
console . log ( 'Error occurred in the addedForeignKey middleware: ' , err ) ;
@@ -364,13 +336,13 @@ export const deleteColumn: RequestHandler = async (req: Request, _res: Response,
364
336
const dbDataSource = await dbConnect ( req )
365
337
const { db_type } = req . session
366
338
const { columnName } = req . body
367
-
339
+ console . log ( 'we are in the helper functions: ' , req . body )
368
340
try {
369
341
const columnTableNameDelete = await Promise . resolve ( tableNameFormat ( req , dbDataSource ) ) ;
370
342
371
343
const deletedColumn : Promise < unknown > = await dbDataSource . query ( `
372
344
ALTER TABLE ${ columnTableNameDelete }
373
- DROP${ db_type !== 'mysql' ? ' COLUMN' : null } ${ columnName }
345
+ DROP${ db_type !== 'mysql' ? ' COLUMN' : '' } ${ columnName }
374
346
` )
375
347
376
348
dbDataSource . destroy ( ) ;
0 commit comments