|
1 | 1 | import assert from "node:assert/strict"; |
2 | 2 | import { test } from "node:test"; |
3 | | -import { buildOdbcConnectionString } from "./index.ts"; |
| 3 | +import type { ConnectionConfig } from "@omni-sql/ts-types"; |
| 4 | +import { OdbcAdapter, buildOdbcConnectionString } from "./index.ts"; |
| 5 | + |
| 6 | +const config: ConnectionConfig = { |
| 7 | + id: "odbc-test", label: "Mock ODBC", dialect: "odbc", endpoint: "Test DSN", user: "alice", |
| 8 | +}; |
4 | 9 |
|
5 | 10 | test("builds DSN and DSN-less connection strings without persisting credentials", () => { |
6 | 11 | assert.equal(buildOdbcConnectionString("Corporate DB", "alice", "s}ecret"), "DSN={Corporate DB};UID={alice};PWD={s}}ecret}"); |
7 | 12 | assert.equal(buildOdbcConnectionString("DRIVER={SQLite3};Database=C:\\db.sqlite;", "", undefined), "DRIVER={SQLite3};Database=C:\\db.sqlite"); |
8 | 13 | }); |
| 14 | + |
| 15 | +test("ODBC introspects tables, views, columns, and primary keys through the driver", async () => { |
| 16 | + let closeCount = 0; |
| 17 | + const connection = { |
| 18 | + close: async () => { closeCount++; }, |
| 19 | + tables: async () => [ |
| 20 | + { TABLE_CAT: "catalog", TABLE_SCHEM: "public", TABLE_NAME: "orders", TABLE_TYPE: "TABLE", REMARKS: " Orders " }, |
| 21 | + { TABLE_CAT: "catalog", TABLE_SCHEM: "public", TABLE_NAME: "orders", TABLE_TYPE: "TABLE" }, |
| 22 | + { TABLE_CAT: "catalog", TABLE_SCHEM: "public", TABLE_NAME: "order_view", TABLE_TYPE: "VIEW" }, |
| 23 | + { TABLE_NAME: "" }, |
| 24 | + ], |
| 25 | + columns: async (_catalog: unknown, _schema: unknown, table: string) => table === "orders" |
| 26 | + ? [ |
| 27 | + { COLUMN_NAME: "name", TYPE_NAME: "VARCHAR", NULLABLE: 1, ORDINAL_POSITION: 2, REMARKS: " Label " }, |
| 28 | + { COLUMN_NAME: "id", TYPE_NAME: "INTEGER", NULLABLE: 0, ORDINAL_POSITION: 1 }, |
| 29 | + ] |
| 30 | + : [], |
| 31 | + primaryKeys: async (_catalog: unknown, _schema: unknown, table: string) => table === "orders" |
| 32 | + ? [{ COLUMN_NAME: "id" }] : [], |
| 33 | + }; |
| 34 | + const driver = { connect: async () => connection } as unknown as ConstructorParameters<typeof OdbcAdapter>[2]; |
| 35 | + const adapter = new OdbcAdapter(config, "secret", driver); |
| 36 | + assert.deepEqual(await adapter.listAvailableSchemas(), ["public"]); |
| 37 | + await adapter.introspect(); |
| 38 | + assert.deepEqual(adapter.listTables("public").map((table) => [table.name, table.kind]), [ |
| 39 | + ["orders", "table"], ["order_view", "view"], |
| 40 | + ]); |
| 41 | + const columns = adapter.listColumns("public", "orders"); |
| 42 | + assert.deepEqual(columns.map((column) => column.name), ["id", "name"]); |
| 43 | + assert.equal(columns[0]?.isPrimaryKey, true); |
| 44 | + assert.equal(columns[1]?.description, "Label"); |
| 45 | + await adapter.close(); |
| 46 | + assert.equal(closeCount, 1); |
| 47 | +}); |
| 48 | + |
| 49 | +test("ODBC returns bounded query rows and closes its cursor", async () => { |
| 50 | + let closed = false; |
| 51 | + const result = Object.assign([ |
| 52 | + { id: 1, amount: 12n, created: new Date("2026-09-16T00:00:00.000Z") }, |
| 53 | + { id: 2, amount: 13n, created: new Date("2026-09-17T00:00:00.000Z") }, |
| 54 | + ], { |
| 55 | + columns: [ |
| 56 | + { name: "id", dataTypeName: "INTEGER", dataType: 4, nullable: false }, |
| 57 | + { name: "amount", dataTypeName: "BIGINT", dataType: -5, nullable: false }, |
| 58 | + { name: "created", dataTypeName: "TIMESTAMP", dataType: 93, nullable: false }, |
| 59 | + ], |
| 60 | + count: -1, |
| 61 | + }); |
| 62 | + const connection = { |
| 63 | + close: async () => undefined, |
| 64 | + query: async () => ({ fetch: async () => result, close: async () => { closed = true; }, noData: false }), |
| 65 | + }; |
| 66 | + const driver = { connect: async () => connection } as unknown as ConstructorParameters<typeof OdbcAdapter>[2]; |
| 67 | + const adapter = new OdbcAdapter(config, undefined, driver); |
| 68 | + const query = await adapter.runQuery("SELECT * FROM orders", 1); |
| 69 | + assert.deepEqual(query.rows, [[1, "12", "2026-09-16T00:00:00.000Z"]]); |
| 70 | + assert.equal(query.rowsMoreAvailable, true); |
| 71 | + assert.equal(query.rowsAffected, undefined); |
| 72 | + assert.equal(closed, true); |
| 73 | + await adapter.close(); |
| 74 | +}); |
| 75 | + |
| 76 | +test("ODBC classifies driver errors without exposing passwords", async () => { |
| 77 | + const driver = { connect: async () => { throw new Error("IM002 driver not found;PWD=secret"); } } as unknown as ConstructorParameters<typeof OdbcAdapter>[2]; |
| 78 | + const adapter = new OdbcAdapter(config, "secret", driver); |
| 79 | + const status = await adapter.test(); |
| 80 | + assert.equal(status.ok, false); |
| 81 | + assert.match(status.message ?? "", /PWD=\*\*\*/u); |
| 82 | + assert.doesNotMatch(status.message ?? "", /secret/u); |
| 83 | + await assert.rejects(adapter.connect(), (error: unknown) => |
| 84 | + error instanceof Error && "causeTag" in error && error.causeTag === "driver-missing"); |
| 85 | +}); |
0 commit comments