UNPKG

sedk-mysql

Version:
97 lines 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BinderArray = exports.Binder = exports.BinderStore = void 0; const models_1 = require("./models"); class BinderStore { constructor(offset) { this.offset = offset; this.store = new Set(); if (!Number.isInteger(offset) || offset < 0) { throw new Error('Binder offset should be a positive integer'); } } add(binder) { if (this.store.has(binder)) { throw new Error('This binder already stored'); } binder.no = this.store.size + 1 + this.offset; this.store.add(binder); } getBinder(value) { const binder = new Binder(value, this.store.size + 1 + this.offset); this.store.add(binder); return binder; } getValues() { return Array.from(this.store).map(it => it.value); } } exports.BinderStore = BinderStore; class Binder { constructor(value, no) { this.value = value; this.mNo = undefined; this.mNo = no; this.type = Binder.getType(value); } set no(no) { if (this.mNo !== undefined) { throw new Error('This Binder already has a number'); } this.mNo = no; } get no() { return this.mNo; } toString() { return this.getStmt(); } getStmt() { if (this.mNo === undefined) { throw new Error(`You can't getStmt() from this binder, The binder is not stored and has undefined "No"`); } return '?'; } static getType(value) { if (value === null) { return models_1.ExpressionType.NULL; } else if (typeof value === 'boolean') { return models_1.ExpressionType.BOOLEAN; } else if ((0, models_1.isNumber)(value)) { return models_1.ExpressionType.NUMBER; } else if (typeof value === 'string') { return models_1.ExpressionType.TEXT; } else if (value instanceof Date) { return models_1.ExpressionType.DATE; } throw new Error(`Unknown type of value: ${value}`); } } exports.Binder = Binder; class BinderArray { constructor(binders) { this.binders = binders; BinderArray.throwIfBindersIsInvalid(binders); this.type = binders[0].type; } getStmt() { return `(${this.binders.map(it => it.getStmt()).join(', ')})`; } static throwIfBindersIsInvalid(binders) { if (binders.length === 0) { throw new Error('BinderArray must have at least one element'); } const type = binders[0].type; binders.forEach(it => { if (it.type !== type) { throw new Error('All binders in BinderArray must be same type'); } }); } } exports.BinderArray = BinderArray; //# sourceMappingURL=binder.js.map