ndbc-mysql
Version:
The ndbc connector to connect mysql database.
411 lines (410 loc) • 14.3 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MySQLResultSet = exports.MySQLDatabaseMetaData = exports.MySQLStatement = exports.MySQLConnection = exports.MySQLDriver = void 0;
const ndbc_connector_1 = require("ndbc-connector");
const mysql = __importStar(require("mysql"));
const uitls_1 = require("./src/uitls");
class MySQLDriver {
connect(properties) {
let connection = new MySQLConnection(properties);
return connection.open();
}
}
exports.MySQLDriver = MySQLDriver;
class MySQLConnection {
constructor(properties) {
this.properties = properties;
this._properties = new Map();
this._properties = properties;
}
open() {
return new Promise((resolve, rejects) => {
try {
let user = this.getProperty("user", true);
let host = this.getProperty("host", true);
let password = this.getProperty("password", true);
let database = this.getProperty("database", true);
let port = this.getProperty("port", false, 3306);
var connection = mysql.createConnection({
host: host,
user: user,
password: password,
database: database,
port: port
});
connection.connect((error) => {
rejects(error);
});
this._connection = connection;
resolve(this);
}
catch (err) {
rejects(err);
}
});
}
close() {
this._connection.end();
}
createStatement(query) {
return new MySQLStatement(this, query);
}
getMetadata() {
return new MySQLDatabaseMetaData(this);
}
getProperty(propertyName, required, defaultValue) {
let value = this._properties.get(propertyName);
if (value == undefined || !(typeof value === "string")) {
if (required) {
throw new Error(`The ${propertyName} is required.`);
}
else {
value = defaultValue;
}
}
return value;
}
}
exports.MySQLConnection = MySQLConnection;
class MySQLStatement {
constructor(connection, queryString) {
this.connection = connection;
this.queryString = queryString;
}
execute(parameters) {
let values = [];
parameters.forEach((value, key) => {
values.push(value);
});
return new Promise((resolve, rejects) => this.connection._connection.query({ sql: this.queryString, values: values }, (err, results, fields) => {
if (err === undefined || err == null) {
resolve(new MySQLResultSet(results, fields));
}
else {
rejects(err);
}
}));
}
executeNonQuery(parameters) {
let values = [];
parameters.forEach((key, value) => {
values.push(value);
});
return new Promise((resolve, rejects) => this.connection._connection.query({ sql: this.queryString, values: values }, (err, results, fields) => {
if (err === undefined || err == null) {
resolve(1);
}
else {
rejects(err);
}
}));
}
}
exports.MySQLStatement = MySQLStatement;
class MySQLDatabaseMetaData {
constructor(connection) {
this.connection = connection;
this._properties = [];
}
getConnectionProperites() {
if (this._properties.length === 0) {
this._properties.push(new ndbc_connector_1.PropertyInfo("host", "string", "", ""));
this._properties.push(new ndbc_connector_1.PropertyInfo("port", "string", 3306, ""));
this._properties.push(new ndbc_connector_1.PropertyInfo("database", "string", "", ""));
this._properties.push(new ndbc_connector_1.PropertyInfo("user", "string", "", ""));
this._properties.push(new ndbc_connector_1.PropertyInfo("password", "string", "", ""));
}
return this._properties;
}
getClientInfoProperties() {
return new Promise((resolve, rejects) => {
resolve(new EmptyResultSet());
});
}
getServerInfoProperties() {
return new Promise((resolve, rejects) => {
resolve(new EmptyResultSet());
});
}
getCatalogs(catalog) {
return new Promise((resolve, rejects) => {
resolve(new EmptyResultSet());
});
}
getSchemas(catalog, schema) {
return __awaiter(this, void 0, void 0, function* () {
let statement = this.connection.createStatement("SHOW DATABASES");
let results;
let metadata = [];
let rows = [];
try {
results = yield statement.execute(new Map());
metadata.push(new ndbc_connector_1.ColumnInfo("TABLE_SCHEM", "string"));
while (results.next()) {
let row = [];
row.push(results.getValue(0));
rows.push(row);
}
}
finally {
if (results != undefined) {
results.close();
}
}
return new Promise((resolve, rejects) => {
resolve(new ValuesResultSet(metadata, rows));
});
});
}
getTables(catalog, schema, table, types) {
let query = "SELECT TABLE_CATALOG AS TABLE_CAT, TABLE_SCHEMA AS TABLE_SCHEM, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";
let whereClause = "";
let parameters = new Map();
if (!uitls_1.Utils.isNullOrEmpty(schema)) {
whereClause += " WHERE TABLE_SCHEMA = ? ";
parameters.set("TABLE_SCHEMA", schema);
}
if (!uitls_1.Utils.isNullOrEmpty(table)) {
if (whereClause === "") {
whereClause += " WHERE ";
}
else {
whereClause += " AND ";
}
whereClause += " TABLE_NAME = ? ";
parameters.set("TABLE_NAME", table);
}
let statement = this.connection.createStatement(query + whereClause);
return statement.execute(parameters);
}
getColumns(catalog, schema, table) {
let query = "SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, IS_NULLABLE, COLUMN_KEY, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, EXTRA, COLUMN_COMMENT "
+ "FROM INFORMATION_SCHEMA.COLUMNS";
let whereClause = "";
let parameters = new Map();
if (!uitls_1.Utils.isNullOrEmpty(schema)) {
whereClause += " WHERE TABLE_SCHEMA = ? ";
parameters.set("TABLE_SCHEMA", schema);
}
if (!uitls_1.Utils.isNullOrEmpty(table)) {
if (whereClause === "") {
whereClause += " WHERE ";
}
else {
whereClause += " AND ";
}
whereClause += " TABLE_NAME = ? ";
parameters.set("TABLE_NAME", table);
}
let statement = this.connection.createStatement(query + whereClause);
return statement.execute(parameters);
}
getProcedures(catalog, schema) {
throw new Error("Method not implemented.");
}
getProcedureParameters(catalog, schema, procedure) {
throw new Error("Method not implemented.");
}
}
exports.MySQLDatabaseMetaData = MySQLDatabaseMetaData;
class EmptyResultSet {
close() {
}
next() {
return false;
}
getValue(index) {
return null;
}
getMetadata() {
return [];
}
}
class ResultSetBase {
constructor() {
this._index = -1;
this._count = 0;
}
close() {
}
getValue(index) {
return null;
}
getMetadata() {
return [];
}
next() {
if (this._count <= 0) {
return false;
}
if (this._index < this._count - 1) {
this._index++;
return true;
}
else {
this._index = this._count;
return false;
}
}
ensureRange() {
if (this._index < 0) {
throw new Error("Please invoke next method first.");
}
if (this._index >= this._count) {
throw new Error("The index is out of range.");
}
}
}
class ValuesResultSet extends ResultSetBase {
constructor(columns, results) {
super();
this.columns = columns;
this.results = results;
this._count = results.length;
this._columnNames = [];
}
getValue(columnName) {
super.ensureRange();
let row = this.results[this._index];
let index = -1;
if (typeof columnName === "string") {
if (this._columnNames.length == 0) {
for (let index = 0; index < this.columns.length; index++) {
const element = this.columns[index];
this._columnNames.push(element.getColumnName());
}
}
index = this._columnNames.indexOf(columnName);
}
else {
index = columnName;
}
if (index == -1) {
throw Error(`Invalid of ${columnName} in getValue method`);
}
return row[index];
}
getMetadata() {
return this.columns;
}
}
class KeyValueResultSet extends ResultSetBase {
constructor(names, values) {
super();
this.names = names;
this.values = values;
this._metadata = [];
this._count = 1;
}
getValue(columnName) {
this.ensureRange();
let index = -1;
if (typeof columnName === "string") {
index = this.names.indexOf(columnName);
}
else {
index = columnName;
}
if (index == -1) {
throw Error(`Invalid of ${columnName} in getValue method`);
}
return this.values[index];
}
getMetadata() {
if (this._metadata.length == 0) {
for (let index = 0; index < this.names.length; index++) {
const name = this.names[index];
this._metadata.push(new ndbc_connector_1.ColumnInfo(name, "string"));
}
}
return this._metadata;
}
}
class MySQLResultSet extends ResultSetBase {
constructor(results, fields) {
super();
this.results = results;
this.fields = fields;
this._count = this.results === undefined ? 0 : this.results.length;
}
next() {
let value = super.next();
this._currentRow = undefined;
return value;
}
getValue(columnName) {
this.ensureRange();
if (this._currentRow === undefined) {
let rowObject = this.results[this._index];
this._currentRow = rowObject;
}
if (this._currentRow === undefined) {
return null;
}
let name;
if (typeof columnName === "number") {
if (this._indexes == undefined) {
this.getMetadata();
}
name = this._indexes[columnName];
}
else {
name = columnName;
}
if (name !== undefined) {
return this._currentRow[name];
}
else {
return null;
}
}
getMetadata() {
if (this._columns === undefined) {
this._columns = [];
this._indexes = [];
if (this.fields !== undefined) {
this.fields.forEach(element => {
if (this._columns != undefined) {
let column = new ndbc_connector_1.ColumnInfo(element.name, element.type.toString());
this._columns.push(column);
}
if (this._indexes != undefined) {
this._indexes.push(element.name);
}
});
}
}
return this._columns;
}
close() {
}
}
exports.MySQLResultSet = MySQLResultSet;