|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +import { test, before, after } from 'node:test' |
| 19 | +import assert from 'node:assert/strict' |
| 20 | +import { createSqliteDatabase, createTestTable } from './test_utils' |
| 21 | +import { AdbcDatabase, AdbcConnection, AdbcStatement } from '../lib/index.js' |
| 22 | +import { tableFromArrays, Table } from 'apache-arrow' |
| 23 | + |
| 24 | +let db: AdbcDatabase |
| 25 | +let conn: AdbcConnection |
| 26 | +let stmt: AdbcStatement |
| 27 | + |
| 28 | +before(async () => { |
| 29 | + db = await createSqliteDatabase() |
| 30 | + conn = await db.connect() |
| 31 | + stmt = await conn.createStatement() |
| 32 | + await createTestTable(stmt, 'bind_test') |
| 33 | +}) |
| 34 | + |
| 35 | +after(async () => { |
| 36 | + try { |
| 37 | + await stmt?.close() |
| 38 | + await conn?.close() |
| 39 | + await db?.close() |
| 40 | + } catch { |
| 41 | + // ignore |
| 42 | + } |
| 43 | +}) |
| 44 | + |
| 45 | +test('statement: bind and query data', async () => { |
| 46 | + const recordBatchToBind = tableFromArrays({ |
| 47 | + id: [null], |
| 48 | + name: ['test_name'], |
| 49 | + }) |
| 50 | + |
| 51 | + assert.strictEqual(recordBatchToBind.numRows, 1) |
| 52 | + await stmt.bind(recordBatchToBind) |
| 53 | + |
| 54 | + await stmt.setSqlQuery('INSERT INTO bind_test (id, name) VALUES (?, ?)') |
| 55 | + const insertResult = await stmt.executeUpdate() |
| 56 | + assert.strictEqual(insertResult, 1) |
| 57 | + |
| 58 | + await stmt.setSqlQuery('SELECT id, name FROM bind_test') |
| 59 | + const reader = await stmt.executeQuery() |
| 60 | + |
| 61 | + let rowCount = 0 |
| 62 | + for await (const batch of reader) { |
| 63 | + rowCount += batch.numRows |
| 64 | + const idVector = batch.getChild('id') |
| 65 | + const nameVector = batch.getChild('name') |
| 66 | + assert.strictEqual(idVector?.get(0), null) |
| 67 | + assert.strictEqual(nameVector?.get(0), 'test_name') |
| 68 | + } |
| 69 | + |
| 70 | + assert.strictEqual(rowCount, 1) |
| 71 | +}) |
| 72 | + |
| 73 | +test('statement: bind multi-batch table throws descriptive error', async () => { |
| 74 | + // Both batches must share the same schema instance for Table to accept them |
| 75 | + const base = tableFromArrays({ id: [10], name: ['first'] }) |
| 76 | + const batch1 = base.batches[0] |
| 77 | + const batch2 = base.batches[0] // same schema, reused to construct a multi-batch Table |
| 78 | + const multiTable = new Table([batch1, batch2]) |
| 79 | + assert.strictEqual(multiTable.batches.length, 2) |
| 80 | + |
| 81 | + const stmt2 = await conn.createStatement() |
| 82 | + const error = await stmt2.bind(multiTable).catch((e) => e) |
| 83 | + assert.ok(error instanceof Error) |
| 84 | + assert.match(error.message, /bind\(\).*batches|batches.*bind\(\)/i) |
| 85 | + await stmt2.close() |
| 86 | +}) |
0 commit comments