@monkdb/monkdb
Version:
🚀 Official TypeScript SDK for MonkDB — a unified, AI-native database for diverse data workloads
64 lines • 1.91 kB
JavaScript
// --- src/connection/MonkCursor.ts ---
import { MonkProgrammingError } from '../errors/MonkErrors.js';
export class MonkCursor {
constructor(connection, converter) {
this.connection = connection;
this.converter = converter;
this.result = null;
this.index = 0;
this.rows = [];
}
async execute(sql, params) {
if (this.connection.isClosed()) {
throw new MonkProgrammingError('Connection has been closed');
}
const result = await this.connection.client.sql(sql, params);
this.result = result;
this.rows = this.convertRows(result);
this.index = 0;
}
fetchone() {
if (!this.result || this.index >= this.rows.length)
return null;
return this.rows[this.index++];
}
fetchmany(count = 1) {
const fetched = this.rows.slice(this.index, this.index + count);
this.index += fetched.length;
return fetched;
}
fetchall() {
const remaining = this.rows.slice(this.index);
this.index = this.rows.length;
return remaining;
}
convertRows(result) {
if (!result.col_types) {
throw new MonkProgrammingError('Missing col_types in result');
}
const converters = result.col_types.map((type) => this.converter.get(type));
return result.rows.map((row) => row.map((val, i) => converters[i](val)));
}
get rowcount() {
return this.result?.rowcount ?? -1;
}
get description() {
if (!this.result)
return null;
return this.result.cols.map((col) => [
col,
null,
null,
null,
null,
null,
null,
]);
}
close() {
this.result = null;
this.index = 0;
this.rows = [];
}
}
//# sourceMappingURL=MonkCursor.js.map