lite-ui-sql-parser
Version:
simple node sql parser
100 lines (88 loc) • 2.7 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports", "./util"], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require("./util"));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.util);
global.indexDefinition = mod.exports;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _util) {
"use strict";
Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports.indexDefinitionToSQL = indexDefinitionToSQL;
_exports.indexTypeToSQL = indexTypeToSQL;
_exports.indexOptionToSQL = indexOptionToSQL;
_exports.indexTypeAndOptionToSQL = indexTypeAndOptionToSQL;
function indexTypeToSQL(indexType) {
if (!indexType) return;
const {
keyword,
type
} = indexType;
return `${keyword.toUpperCase()} ${type.toUpperCase()}`;
}
function indexOptionToSQL(indexOpt) {
if (!indexOpt) return;
const {
type,
expr,
symbol
} = indexOpt;
const upperType = type.toUpperCase();
const indexOptArray = [];
indexOptArray.push(upperType);
switch (upperType) {
case 'KEY_BLOCK_SIZE':
if (symbol) indexOptArray.push(symbol);
indexOptArray.push((0, _util.literalToSQL)(expr));
break;
case 'BTREE':
case 'HASH':
indexOptArray.length = 0;
indexOptArray.push(indexTypeToSQL(indexOpt));
break;
case 'WITH PARSER':
indexOptArray.push(expr);
break;
case 'VISIBLE':
case 'INVISIBLE':
break;
case 'COMMENT':
indexOptArray.shift();
indexOptArray.push((0, _util.commentToSQL)(indexOpt));
break;
default:
break;
}
return indexOptArray.join(' ');
}
function indexTypeAndOptionToSQL(indexDefinition) {
const {
index_type: indexType,
index_options: indexOptions = [],
definition
} = indexDefinition;
const dataType = [];
dataType.push(indexTypeToSQL(indexType));
if (definition && definition.length) dataType.push(`(${definition.map(col => (0, _util.identifierToSql)(col)).join(', ')})`);
dataType.push(indexOptions && indexOptions.map(indexOptionToSQL).join(' '));
return dataType;
}
function indexDefinitionToSQL(indexDefinition) {
const indexSQL = [];
const {
keyword,
index
} = indexDefinition;
indexSQL.push((0, _util.toUpper)(keyword));
indexSQL.push(index);
indexSQL.push(...indexTypeAndOptionToSQL(indexDefinition));
return indexSQL.filter(_util.hasVal).join(' ');
}
});