@ -748,13 +748,13 @@ export async function columnUpdate(req: Request, res: Response<TableType>) {
// MultiSelect to SingleSelect
// MultiSelect to SingleSelect
if ( column . uidt === UITypes . MultiSelect && colBody . uidt === UITypes . SingleSelect ) {
if ( column . uidt === UITypes . MultiSelect && colBody . uidt === UITypes . SingleSelect ) {
if ( driverType === 'mysql' || driverType === 'mysql2' ) {
if ( driverType === 'mysql' || driverType === 'mysql2' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = SUBSTRING_INDEX(??, ',', 1) WHERE ?? LIKE '%,%'; ` , [ table . table_name , column . titl e, column . titl e, column . titl e] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = SUBSTRING_INDEX(??, ',', 1) WHERE ?? LIKE '%,%'; ` , [ table . table_name , column . column_nam e, column . column_nam e, column . column_nam e] ) ;
} else if ( driverType === 'pg' ) {
} else if ( driverType === 'pg' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = split_part(??, ',', 1); ` , [ table . table_name , column . titl e, column . titl e] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = split_part(??, ',', 1); ` , [ table . table_name , column . column_nam e, column . column_nam e] ) ;
} else if ( driverType === 'mssql' ) {
} else if ( driverType === 'mssql' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = LEFT(cast(?? as varchar(max)), CHARINDEX(',', ??) - 1) WHERE CHARINDEX(',', ??) > 0; ` , [ table . table_name , column . titl e, column . titl e, column . titl e, column . titl e] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = LEFT(cast(?? as varchar(max)), CHARINDEX(',', ??) - 1) WHERE CHARINDEX(',', ??) > 0; ` , [ table . table_name , column . column_nam e, column . column_nam e, column . column_nam e, column . column_nam e] ) ;
} else if ( driverType === 'sqlite3' ) {
} else if ( driverType === 'sqlite3' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = substr(??, 1, instr(??, ',') - 1) WHERE ?? LIKE '%,%'; ` , [ table . table_name , column . titl e, column . titl e, column . titl e, column . titl e] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = substr(??, 1, instr(??, ',') - 1) WHERE ?? LIKE '%,%'; ` , [ table . table_name , column . column_nam e, column . column_nam e, column . column_nam e, column . column_nam e] ) ;
}
}
}
}
@ -764,16 +764,21 @@ export async function columnUpdate(req: Request, res: Response<TableType>) {
}
}
// Handle default values
// Handle default values
const optionTitles = colBody . colOptions . options . map ( el = > el . title ) ;
if ( colBody . cdf ) {
if ( colBody . cdf ) {
if ( colBody . uidt === UITypes . SingleSelect ) {
if ( driverType === 'mysql' || driverType === 'mysql2' ) {
if ( ! optionTitles . includes ( colBody . cdf ) ) {
NcError . badRequest ( ` Default value ' ${ colBody . cdf } ' is not a select option. ` ) ;
} else if ( driverType === 'pg' ) {
}
} else {
} else if ( driverType === 'mssql' ) {
for ( const cdf of colBody . cdf . split ( ',' ) ) {
if ( ! optionTitles . includes ( cdf ) ) {
} else if ( driverType === 'sqlite3' ) {
NcError . badRequest ( ` Default value ' ${ cdf } ' is not a select option. ` ) ;
}
}
}
if ( driverType === 'pg' ) {
colBody . cdf = ` ' ${ colBody . cdf } ' ` ;
}
}
}
}
@ -811,19 +816,19 @@ export async function columnUpdate(req: Request, res: Response<TableType>) {
}
}
if ( column . uidt === UITypes . SingleSelect ) {
if ( column . uidt === UITypes . SingleSelect ) {
if ( driverType === 'mssql' ) {
if ( driverType === 'mssql' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = NULL WHERE ?? LIKE ? ` , [ table . table_name , column . titl e, column . titl e, option . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = NULL WHERE ?? LIKE ? ` , [ table . table_name , column . column_nam e, column . column_nam e, option . title ] ) ;
} else {
} else {
await baseModel . bulkUpdateAll ( { where : ` ( ${ column . titl e} ,eq, ${ option . title } ) ` } , { [ column . titl e] : null } ) ;
await baseModel . bulkUpdateAll ( { where : ` ( ${ column . column_nam e} ,eq, ${ option . title } ) ` } , { [ column . column_nam e] : null } ) ;
}
}
} else if ( column . uidt === UITypes . MultiSelect ) {
} else if ( column . uidt === UITypes . MultiSelect ) {
if ( driverType === 'mysql' || driverType === 'mysql2' ) {
if ( driverType === 'mysql' || driverType === 'mysql2' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', ??, ','), CONCAT(',', ?, ','), ',')) WHERE FIND_IN_SET(?, ??) ` , [ table . table_name , column . titl e, column . titl e, option . title , option . title , column . titl e] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', ??, ','), CONCAT(',', ?, ','), ',')) WHERE FIND_IN_SET(?, ??) ` , [ table . table_name , column . column_nam e, column . column_nam e, option . title , option . title , column . column_nam e] ) ;
} else if ( driverType === 'pg' ) {
} else if ( driverType === 'pg' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = array_to_string(array_remove(string_to_array(??, ','), ?), ',') ` , [ table . table_name , column . titl e, column . titl e, option . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = array_to_string(array_remove(string_to_array(??, ','), ?), ',') ` , [ table . table_name , column . column_nam e, column . column_nam e, option . title ] ) ;
} else if ( driverType === 'mssql' ) {
} else if ( driverType === 'mssql' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = substring(replace(concat(',', ??, ','), concat(',', ?, ','), ','), 2, len(replace(concat(',', ??, ','), concat(',', ?, ','), ',')) - 2) ` , [ table . table_name , column . titl e, column . titl e, option . title , column . titl e, option . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = substring(replace(concat(',', ??, ','), concat(',', ?, ','), ','), 2, len(replace(concat(',', ??, ','), concat(',', ?, ','), ',')) - 2) ` , [ table . table_name , column . column_nam e, column . column_nam e, option . title , column . column_nam e, option . title ] ) ;
} else if ( driverType === 'sqlite3' ) {
} else if ( driverType === 'sqlite3' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(REPLACE(',' || ?? || ',', ',' || ? || ',', ','), ',') ` , [ table . table_name , column . titl e, column . titl e, option . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(REPLACE(',' || ?? || ',', ',' || ? || ',', ','), ',') ` , [ table . table_name , column . column_nam e, column . column_nam e, option . title ] ) ;
}
}
}
}
}
}
@ -909,19 +914,19 @@ export async function columnUpdate(req: Request, res: Response<TableType>) {
if ( column . uidt === UITypes . SingleSelect ) {
if ( column . uidt === UITypes . SingleSelect ) {
if ( driverType === 'mssql' ) {
if ( driverType === 'mssql' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = ? WHERE ?? LIKE ? ` , [ table . table_name , column . titl e, newOp . title , column . titl e, option . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = ? WHERE ?? LIKE ? ` , [ table . table_name , column . column_nam e, newOp . title , column . column_nam e, option . title ] ) ;
} else {
} else {
await baseModel . bulkUpdateAll ( { where : ` ( ${ column . titl e} ,eq, ${ option . title } ) ` } , { [ column . titl e] : newOp . title } ) ;
await baseModel . bulkUpdateAll ( { where : ` ( ${ column . column_nam e} ,eq, ${ option . title } ) ` } , { [ column . column_nam e] : newOp . title } ) ;
}
}
} else if ( column . uidt === UITypes . MultiSelect ) {
} else if ( column . uidt === UITypes . MultiSelect ) {
if ( driverType === 'mysql' || driverType === 'mysql2' ) {
if ( driverType === 'mysql' || driverType === 'mysql2' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', ??, ','), CONCAT(',', ?, ','), CONCAT(',', ?, ','))) WHERE FIND_IN_SET(?, ??) ` , [ table . table_name , column . titl e, column . titl e, option . title , newOp . title , option . title , column . titl e] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', ??, ','), CONCAT(',', ?, ','), CONCAT(',', ?, ','))) WHERE FIND_IN_SET(?, ??) ` , [ table . table_name , column . column_nam e, column . column_nam e, option . title , newOp . title , option . title , column . column_nam e] ) ;
} else if ( driverType === 'pg' ) {
} else if ( driverType === 'pg' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = array_to_string(array_replace(string_to_array(??, ','), ?, ?), ',') ` , [ table . table_name , column . titl e, column . titl e, option . title , newOp . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = array_to_string(array_replace(string_to_array(??, ','), ?, ?), ',') ` , [ table . table_name , column . column_nam e, column . column_nam e, option . title , newOp . title ] ) ;
} else if ( driverType === 'mssql' ) {
} else if ( driverType === 'mssql' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = substring(replace(concat(',', ??, ','), concat(',', ?, ','), concat(',', ?, ',')), 2, len(replace(concat(',', ??, ','), concat(',', ?, ','), concat(',', ?, ','))) - 2) ` , [ table . table_name , column . titl e, column . titl e, option . title , newOp . title , column . titl e, option . title , newOp . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = substring(replace(concat(',', ??, ','), concat(',', ?, ','), concat(',', ?, ',')), 2, len(replace(concat(',', ??, ','), concat(',', ?, ','), concat(',', ?, ','))) - 2) ` , [ table . table_name , column . column_nam e, column . column_nam e, option . title , newOp . title , column . column_nam e, option . title , newOp . title ] ) ;
} else if ( driverType === 'sqlite3' ) {
} else if ( driverType === 'sqlite3' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(REPLACE(',' || ?? || ',', ',' || ? || ',', ',' || ? || ','), ',') ` , [ table . table_name , column . titl e, column . titl e, option . title , newOp . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(REPLACE(',' || ?? || ',', ',' || ? || ',', ',' || ? || ','), ',') ` , [ table . table_name , column . column_nam e, column . column_nam e, option . title , newOp . title ] ) ;
}
}
}
}
}
}
@ -930,19 +935,19 @@ export async function columnUpdate(req: Request, res: Response<TableType>) {
let newOp = ch . def_option ;
let newOp = ch . def_option ;
if ( column . uidt === UITypes . SingleSelect ) {
if ( column . uidt === UITypes . SingleSelect ) {
if ( driverType === 'mssql' ) {
if ( driverType === 'mssql' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = ? WHERE ?? LIKE ? ` , [ table . table_name , column . titl e, newOp . title , column . titl e, ch . temp_title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = ? WHERE ?? LIKE ? ` , [ table . table_name , column . column_nam e, newOp . title , column . column_nam e, ch . temp_title ] ) ;
} else {
} else {
await baseModel . bulkUpdateAll ( { where : ` ( ${ column . titl e} ,eq, ${ ch . temp_title } ) ` } , { [ column . titl e] : newOp . title } ) ;
await baseModel . bulkUpdateAll ( { where : ` ( ${ column . column_nam e} ,eq, ${ ch . temp_title } ) ` } , { [ column . column_nam e] : newOp . title } ) ;
}
}
} else if ( column . uidt === UITypes . MultiSelect ) {
} else if ( column . uidt === UITypes . MultiSelect ) {
if ( driverType === 'mysql' || driverType === 'mysql2' ) {
if ( driverType === 'mysql' || driverType === 'mysql2' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', ??, ','), CONCAT(',', ?, ','), CONCAT(',', ?, ','))) WHERE FIND_IN_SET(?, ??) ` , [ table . table_name , column . titl e, column . titl e, ch . temp_title , newOp . title , ch . temp_title , column . titl e] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', ??, ','), CONCAT(',', ?, ','), CONCAT(',', ?, ','))) WHERE FIND_IN_SET(?, ??) ` , [ table . table_name , column . column_nam e, column . column_nam e, ch . temp_title , newOp . title , ch . temp_title , column . column_nam e] ) ;
} else if ( driverType === 'pg' ) {
} else if ( driverType === 'pg' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = array_to_string(array_replace(string_to_array(??, ','), ?, ?), ',') ` , [ table . table_name , column . titl e, column . titl e, ch . temp_title , newOp . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = array_to_string(array_replace(string_to_array(??, ','), ?, ?), ',') ` , [ table . table_name , column . column_nam e, column . column_nam e, ch . temp_title , newOp . title ] ) ;
} else if ( driverType === 'mssql' ) {
} else if ( driverType === 'mssql' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = substring(replace(concat(',', ??, ','), concat(',', ?, ','), concat(',', ?, ',')), 2, len(replace(concat(',', ??, ','), concat(',', ?, ','), concat(',', ?, ','))) - 2) ` , [ table . table_name , column . titl e, column . titl e, ch . temp_title , newOp . title , column . titl e, ch . temp_title , newOp . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = substring(replace(concat(',', ??, ','), concat(',', ?, ','), concat(',', ?, ',')), 2, len(replace(concat(',', ??, ','), concat(',', ?, ','), concat(',', ?, ','))) - 2) ` , [ table . table_name , column . column_nam e, column . column_nam e, ch . temp_title , newOp . title , column . column_nam e, ch . temp_title , newOp . title ] ) ;
} else if ( driverType === 'sqlite3' ) {
} else if ( driverType === 'sqlite3' ) {
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(REPLACE(',' || ?? || ',', ',' || ? || ',', ',' || ? || ','), ',') ` , [ table . table_name , column . titl e, column . titl e, ch . temp_title , newOp . title ] ) ;
await dbDriver . raw ( ` UPDATE ?? SET ?? = TRIM(REPLACE(',' || ?? || ',', ',' || ? || ',', ',' || ? || ','), ',') ` , [ table . table_name , column . column_nam e, column . column_nam e, ch . temp_title , newOp . title ] ) ;
}
}
}
}
}
}