|
| 1 | +import { NITRO_SQLITE_NULL, isSimpleNullHandlingEnabled } from '../nullHandling' |
| 2 | +import { HybridNitroSQLite } from '../nitro' |
| 3 | +import type { |
| 4 | + NativeSQLiteQueryParams, |
| 5 | + BatchQueryResult, |
| 6 | + BatchQueryCommand, |
| 7 | + NativeBatchQueryCommand, |
| 8 | + SQLiteValue, |
| 9 | + SQLiteQueryParamItem, |
| 10 | +} from '../types' |
| 11 | + |
| 12 | +export function executeBatch( |
| 13 | + dbName: string, |
| 14 | + commands: BatchQueryCommand[] |
| 15 | +): BatchQueryResult { |
| 16 | + const transformedCommands = isSimpleNullHandlingEnabled() |
| 17 | + ? toNativeBatchQueryCommands(commands) |
| 18 | + : (commands as NativeBatchQueryCommand[]) |
| 19 | + |
| 20 | + const result = HybridNitroSQLite.executeBatch(dbName, transformedCommands) |
| 21 | + return result |
| 22 | +} |
| 23 | + |
| 24 | +export async function executeBatchAsync( |
| 25 | + dbName: string, |
| 26 | + commands: BatchQueryCommand[] |
| 27 | +): Promise<BatchQueryResult> { |
| 28 | + const transformedCommands = isSimpleNullHandlingEnabled() |
| 29 | + ? toNativeBatchQueryCommands(commands) |
| 30 | + : (commands as NativeBatchQueryCommand[]) |
| 31 | + |
| 32 | + const result = await HybridNitroSQLite.executeBatchAsync( |
| 33 | + dbName, |
| 34 | + transformedCommands |
| 35 | + ) |
| 36 | + return result |
| 37 | +} |
| 38 | + |
| 39 | +function replaceWithNativeNull(value: SQLiteQueryParamItem): SQLiteValue { |
| 40 | + if (value === undefined || value === null) { |
| 41 | + return NITRO_SQLITE_NULL |
| 42 | + } |
| 43 | + return value |
| 44 | +} |
| 45 | + |
| 46 | +function toNativeBatchQueryCommands( |
| 47 | + commands: BatchQueryCommand[] |
| 48 | +): NativeBatchQueryCommand[] { |
| 49 | + return commands.map((command) => { |
| 50 | + const transformedParams = command.params?.map((param) => { |
| 51 | + if (Array.isArray(param)) { |
| 52 | + return param.map((p) => replaceWithNativeNull(p)) |
| 53 | + } |
| 54 | + return replaceWithNativeNull(param) |
| 55 | + }) as NativeSQLiteQueryParams | NativeSQLiteQueryParams[] |
| 56 | + |
| 57 | + return { |
| 58 | + query: command.query, |
| 59 | + params: transformedParams, |
| 60 | + } |
| 61 | + }) |
| 62 | +} |
0 commit comments