(options: Config)
| 45 | } |
| 46 | |
| 47 | const make = (options: Config) => |
| 48 | Effect.gen(function* () { |
| 49 | const native = (yield* Sqlite.Native) as DatabaseSync |
| 50 | |
| 51 | const compiler = Statement.makeCompilerSqlite(options.transformQueryNames) |
| 52 | const transformRows = options.transformResultNames |
| 53 | ? Statement.defaultTransforms(options.transformResultNames).array |
| 54 | : undefined |
| 55 | |
| 56 | const run = (query: string, params: ReadonlyArray<unknown> = []) => |
| 57 | Effect.withFiber<Array<Record<string, unknown>>, SqlError>((fiber) => { |
| 58 | const statement = native.prepare(query) |
| 59 | statement.setReadBigInts(Context.get(fiber.context, Client.SafeIntegers)) |
| 60 | try { |
| 61 | return Effect.succeed(statement.all(...(params as SQLInputValue[])) as Array<Record<string, unknown>>) |
| 62 | } catch (cause) { |
| 63 | return Effect.fail( |
| 64 | new SqlError({ |
| 65 | reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }), |
| 66 | }), |
| 67 | ) |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | const runValues = (query: string, params: ReadonlyArray<unknown> = []) => |
| 72 | Effect.withFiber<ReadonlyArray<ReadonlyArray<unknown>>, SqlError>((fiber) => { |
| 73 | const statement = native.prepare(query) |
| 74 | statement.setReadBigInts(Context.get(fiber.context, Client.SafeIntegers)) |
| 75 | statement.setReturnArrays(true) |
| 76 | try { |
| 77 | return Effect.succeed( |
| 78 | statement.all(...(params as SQLInputValue[])) as unknown as ReadonlyArray<ReadonlyArray<unknown>>, |
| 79 | ) |
| 80 | } catch (cause) { |
| 81 | return Effect.fail( |
| 82 | new SqlError({ |
| 83 | reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }), |
| 84 | }), |
| 85 | ) |
| 86 | } |
| 87 | }) |
| 88 | |
| 89 | const connection = identity<SqliteConnection>({ |
| 90 | execute(query, params, transformRows) { |
| 91 | return transformRows ? Effect.map(run(query, params), transformRows) : run(query, params) |
| 92 | }, |
| 93 | executeRaw(query, params) { |
| 94 | return run(query, params) |
| 95 | }, |
| 96 | executeValues(query, params) { |
| 97 | return runValues(query, params) |
| 98 | }, |
| 99 | executeUnprepared(query, params, transformRows) { |
| 100 | return this.execute(query, params, transformRows) |
| 101 | }, |
| 102 | executeStream() { |
| 103 | return Stream.die("executeStream not implemented") |
| 104 | }, |
no test coverage detected