@maiyunnet/kebab 6.2.1 → 6.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/sql.js +42 -83
- package/package.json +1 -1
- package/www/example/ctr/test.js +5 -2
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
7
7
|
*/
|
|
8
8
|
/** --- 当前系统版本号 --- */
|
|
9
|
-
export const VER = '6.2.
|
|
9
|
+
export const VER = '6.2.2';
|
|
10
10
|
// --- 服务端用的路径 ---
|
|
11
11
|
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
12
12
|
/** --- /xxx/xxx --- */
|
package/lib/sql.js
CHANGED
|
@@ -703,8 +703,6 @@ export class Sql {
|
|
|
703
703
|
* @param suf 表后缀,仅请在 field 表名时倒入后缀,前面加 # 代表要强制 AS,可能是分表查询时用
|
|
704
704
|
*/
|
|
705
705
|
field(str, pre = '', suf = '') {
|
|
706
|
-
let left = '';
|
|
707
|
-
let right = '';
|
|
708
706
|
const q = this._service === ESERVICE.MYSQL ? '`' : '"';
|
|
709
707
|
if (Array.isArray(str)) {
|
|
710
708
|
this._data.push(...str[1]);
|
|
@@ -713,21 +711,21 @@ export class Sql {
|
|
|
713
711
|
if (typeof str === 'number') {
|
|
714
712
|
str = str.toString();
|
|
715
713
|
}
|
|
716
|
-
str = str.trim()
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
714
|
+
str = str.trim() // --- 去除前导尾随 ---
|
|
715
|
+
.replace(/ {2,}/g, ' ') // --- 去除多余的空格 ---
|
|
716
|
+
.replace(/ +([),])/g, ' $1')
|
|
717
|
+
.replace(/([(,]) +/g, '$1 ')
|
|
718
|
+
.replace(/(\W)(JOIN|WHERE|UNION)(\W)/ig, '$1$3');
|
|
721
719
|
// --- 先判断 suf 强制性 AS ---
|
|
722
|
-
|
|
723
|
-
if (
|
|
720
|
+
const sufAs = suf.startsWith('#');
|
|
721
|
+
if (sufAs) {
|
|
724
722
|
// --- 强制 AS ---
|
|
725
723
|
suf = suf.slice(1);
|
|
726
|
-
sufAs = true;
|
|
727
724
|
}
|
|
728
725
|
// --- 先判断有没有别名(也就是 as) ---
|
|
729
|
-
const
|
|
730
|
-
|
|
726
|
+
const asPos = str.toLowerCase().indexOf(' as ');
|
|
727
|
+
let left = '';
|
|
728
|
+
let right = '';
|
|
731
729
|
if (asPos === -1) {
|
|
732
730
|
// --- 没有 as ---
|
|
733
731
|
let spacePos = str.lastIndexOf(' ');
|
|
@@ -736,21 +734,12 @@ export class Sql {
|
|
|
736
734
|
// --- 连接符 ---
|
|
737
735
|
spacePos = -1;
|
|
738
736
|
}
|
|
739
|
-
if (spacePos !== -1) {
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
// --- OK ---
|
|
743
|
-
left = str.slice(0, spacePos);
|
|
744
|
-
right = spaceRight;
|
|
745
|
-
}
|
|
746
|
-
else {
|
|
747
|
-
left = str;
|
|
748
|
-
right = '';
|
|
749
|
-
}
|
|
737
|
+
if (spacePos !== -1 && /^[a-zA-Z_`"][\w`"]*$/.test(str.slice(spacePos + 1))) {
|
|
738
|
+
left = str.slice(0, spacePos);
|
|
739
|
+
right = str.slice(spacePos + 1);
|
|
750
740
|
}
|
|
751
741
|
else {
|
|
752
742
|
left = str;
|
|
753
|
-
right = '';
|
|
754
743
|
}
|
|
755
744
|
}
|
|
756
745
|
else {
|
|
@@ -760,90 +749,60 @@ export class Sql {
|
|
|
760
749
|
}
|
|
761
750
|
if (right) {
|
|
762
751
|
// --- 处理右侧 ---
|
|
752
|
+
const hasQuote = right.startsWith(q);
|
|
753
|
+
if (!left.includes('.')) {
|
|
754
|
+
// --- 存储表别名 ---
|
|
755
|
+
this._alias.push(hasQuote ? right.slice(1, -1) : right);
|
|
756
|
+
}
|
|
763
757
|
if (this._service === ESERVICE.MYSQL) {
|
|
764
|
-
|
|
765
|
-
if (!left.includes('.')) {
|
|
766
|
-
// --- 存储表别名 ---
|
|
767
|
-
this._alias.push(right.slice(1, -1));
|
|
768
|
-
}
|
|
769
|
-
right = '`' + pre + right.slice(1);
|
|
770
|
-
}
|
|
771
|
-
else {
|
|
772
|
-
if (!left.includes('.')) {
|
|
773
|
-
// --- 存储表别名 ---
|
|
774
|
-
this._alias.push(right);
|
|
775
|
-
}
|
|
776
|
-
right = '`' + pre + right + '`';
|
|
777
|
-
}
|
|
778
|
-
right = ' AS ' + right;
|
|
758
|
+
right = ' AS `' + pre + (hasQuote ? right.slice(1) : right + '`');
|
|
779
759
|
}
|
|
780
760
|
else {
|
|
781
|
-
|
|
782
|
-
if (!left.includes('.')) {
|
|
783
|
-
// --- 存储表别名 ---
|
|
784
|
-
this._alias.push(right.slice(1, -1));
|
|
785
|
-
}
|
|
786
|
-
if (pre) {
|
|
787
|
-
right = `"${pre}".${right}`;
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
else {
|
|
791
|
-
if (!left.includes('.')) {
|
|
792
|
-
// --- 存储表别名 ---
|
|
793
|
-
this._alias.push(right);
|
|
794
|
-
}
|
|
795
|
-
right = (pre ? `"${pre}".` : '') + `"${right}"`;
|
|
796
|
-
}
|
|
797
|
-
right = ' AS ' + right;
|
|
761
|
+
right = ' AS ' + (pre ? `"${pre}".` : '') + (hasQuote ? right : `"${right}"`);
|
|
798
762
|
}
|
|
799
763
|
}
|
|
800
|
-
else {
|
|
764
|
+
else if (sufAs) {
|
|
801
765
|
// --- 没有右侧 ---
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
this._alias.push(left.startsWith('`') || left.startsWith('"') ? left.slice(1, -1) : left);
|
|
807
|
-
}
|
|
808
|
-
right = ' AS ' + this.field(left, pre);
|
|
766
|
+
// --- 强制 AS ---
|
|
767
|
+
if (!left.includes('.')) {
|
|
768
|
+
// --- 存储表别名 ---
|
|
769
|
+
this._alias.push(left.startsWith(q) ? left.slice(1, -1) : left);
|
|
809
770
|
}
|
|
771
|
+
right = ' AS ' + this.field(left, pre);
|
|
810
772
|
}
|
|
811
773
|
// --- 处理 left ---
|
|
812
774
|
if (/^[\w`"_.*]+$/.test(left)) {
|
|
775
|
+
// --- 简单字段或表名 ---
|
|
776
|
+
/** --- 分左右 --- */
|
|
813
777
|
const l = left.split('.');
|
|
814
778
|
if (l[0] === '*') {
|
|
815
779
|
return '*' + right;
|
|
816
780
|
}
|
|
817
|
-
|
|
818
|
-
l[0] = l[0].replace(/[`"]/g, '');
|
|
819
|
-
}
|
|
781
|
+
l[0] = l[0].replace(this._service === ESERVICE.MYSQL ? /`/g : /"/g, '');
|
|
820
782
|
if (l[1] === undefined) {
|
|
821
|
-
// --- xxx ---
|
|
783
|
+
// --- xxx,代表无 . 右侧 ---
|
|
784
|
+
if ((this._service === ESERVICE.MYSQL && l[0].startsWith('"')) ||
|
|
785
|
+
(this._service === ESERVICE.PGSQL && l[0].startsWith(`'`))) {
|
|
786
|
+
// --- 字符串,无需包裹 ---
|
|
787
|
+
return l[0] + right;
|
|
788
|
+
}
|
|
822
789
|
if (/^[A-Z0-9_]+$/.test(l[0])) {
|
|
823
790
|
// --- 纯大写是内置函数,不能加 `" ---
|
|
824
791
|
return l[0] + right;
|
|
825
792
|
}
|
|
826
793
|
return this._service === ESERVICE.MYSQL ?
|
|
827
|
-
|
|
828
|
-
(pre ? `"${pre}".` : '') +
|
|
794
|
+
`\`${pre}${l[0]}${suf}\`${right}` :
|
|
795
|
+
(pre ? `"${pre}".` : '') + `"${l[0]}${suf}"${right}`;
|
|
829
796
|
}
|
|
830
797
|
// --- x.xxx ---
|
|
831
798
|
// --- 只有在此模式才知道 . 前面的一定是表名,因此自动加 sql 级的 _pre ---
|
|
832
|
-
const w = l[1] === '*'
|
|
833
|
-
? '*' :
|
|
834
|
-
(q + ((l[1].startsWith('`') || l[1].startsWith('"')) ?
|
|
835
|
-
l[1].slice(1, -1) :
|
|
836
|
-
l[1]) + q);
|
|
799
|
+
const w = l[1] === '*' ? '*' : q + (l[1].startsWith(q) ? l[1].slice(1, -1) : l[1]) + q;
|
|
837
800
|
return this._service === ESERVICE.MYSQL ?
|
|
838
|
-
|
|
839
|
-
(this._pre ? `"${this._pre}".` : '') +
|
|
840
|
-
}
|
|
841
|
-
else {
|
|
842
|
-
// return left.replace(/([(, ])([a-zA-Z`"_][\w`"_.]*)(?=[), ])/g, (
|
|
843
|
-
return left.replace(/(^|[(, ])([a-zA-Z`"_][\w`"_.]*)(?=[), ]|$)/g, (t, t1, t2) => {
|
|
844
|
-
return t1 + this.field(t2, pre, suf);
|
|
845
|
-
}) + right;
|
|
801
|
+
`\`${this._pre}${l[0]}${suf}\`.${w}${right}` :
|
|
802
|
+
(this._pre ? `"${this._pre}".` : '') + `"${l[0]}${suf}".${w}${right}`;
|
|
846
803
|
}
|
|
804
|
+
// --- 复杂字段或表达式 ---
|
|
805
|
+
return left.replace(/(^|[(, ])([a-zA-Z`"_][\w`"_.]*)(?=[), ]|$)/g, (t, t1, t2) => t1 + this.field(t2, pre, suf)) + right;
|
|
847
806
|
}
|
|
848
807
|
/**
|
|
849
808
|
* --- 判断传入值是否是 field,还是别的对象 ---
|
package/package.json
CHANGED
package/www/example/ctr/test.js
CHANGED
|
@@ -2422,9 +2422,12 @@ Result:<pre id="result">Nothing.</pre>`);
|
|
|
2422
2422
|
<b>getSql() :</b> ${s}<br>
|
|
2423
2423
|
<b>getData():</b> <pre>${JSON.stringify(sd, undefined, 4)}</pre>
|
|
2424
2424
|
<b>format() :</b> ${sql.format(s, sd)}<hr>`);
|
|
2425
|
-
|
|
2425
|
+
const data = this._get['s'] === 'pgsql' ?
|
|
2426
|
+
[`IFNULL(user.nick, '') nick`, `IFNULL(user.nick, 'a2') nick2`] :
|
|
2427
|
+
[`IFNULL(user.nick, "") nick`, `IFNULL(user.nick, "a2") nick2`];
|
|
2428
|
+
s = sql.select(['SUM(user.age) age', 'UTC_TIMESTAMP', 'FROM_UNIXTIME(user.time, \'%Y-%m\') as time', ...data], 'order').leftJoin('user', { 'order.user_id': lSql.column('user.id') }).getSql();
|
|
2426
2429
|
sd = sql.getData();
|
|
2427
|
-
echo.push(`<pre>sql.select(['SUM(user.age) age', 'UTC_TIMESTAMP', 'FROM_UNIXTIME(user.time, \\'%Y-%m\\') as time'], 'order').leftJoin('user', { 'order.user_id': lSql.column('user.id') });</pre>
|
|
2430
|
+
echo.push(`<pre>sql.select(['SUM(user.age) age', 'UTC_TIMESTAMP', 'FROM_UNIXTIME(user.time, \\'%Y-%m\\') as time', \`${data[0]}\`, \`${data[1]}\`], 'order').leftJoin('user', { 'order.user_id': lSql.column('user.id') });</pre>
|
|
2428
2431
|
<b>getSql() :</b> ${s}<br>
|
|
2429
2432
|
<b>getData():</b> <pre>${JSON.stringify(sd, undefined, 4)}</pre>
|
|
2430
2433
|
<b>format() :</b> ${sql.format(s, sd)}<hr>`);
|