使用sqlstring中的escape来处理字符串

This commit is contained in:
Xu Chang 2023-08-28 17:58:19 +08:00
parent 04ea9ec535
commit 262fca6780
4 changed files with 52 additions and 6 deletions

View File

@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.SqlTranslator = void 0;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importDefault(require("assert"));
var sqlstring_1 = tslib_1.__importDefault(require("sqlstring"));
var lodash_1 = require("lodash");
var types_1 = require("oak-domain/lib/types");
var relation_1 = require("oak-domain/lib/store/relation");
@ -963,7 +964,7 @@ var SqlTranslator = /** @class */ (function () {
return sql;
};
SqlTranslator.prototype.escapeStringValue = function (value) {
var result = "'".concat(value.replace(/'/g, '\\\'').replace(/"/g, '\\\"'), "'");
var result = sqlstring_1.default.escape(value);
return result;
};
return SqlTranslator;

View File

@ -27,6 +27,7 @@
"@types/luxon": "^2.3.2",
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.42",
"@types/sqlstring": "^2.3.0",
"@types/uuid": "^8.3.4",
"cross-env": "^7.0.3",
"mocha": "^10.0.0",

View File

@ -1,4 +1,5 @@
import assert from 'assert';
import SqlString from 'sqlstring';
import { assign, cloneDeep, identity, intersection, keys, set } from 'lodash';
import { Attribute, EntityDict, EXPRESSION_PREFIX, Index, OperateOption,
Q_FullTextValue, Ref, RefOrExpression, SelectOption, StorageSchema, SubQueryPredicateMetadata } from "oak-domain/lib/types";
@ -1177,7 +1178,7 @@ export abstract class SqlTranslator<ED extends EntityDict & BaseEntityDict> {
escapeStringValue(value: string): string {
const result = `'${value.replace(/'/g, '\\\'').replace(/"/g, '\\\"')}'`;
const result = SqlString.escape(value);
return result;
}
}

View File

@ -6,6 +6,7 @@ import { MysqlStore } from '../src/MySQL/store';
import { EntityDict, storageSchema } from './test-app-domain';
import { filter } from 'lodash';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
import { WebConfig } from './test-app-domain/Application/Schema';
describe('test mysqlstore', function () {
this.timeout(100000);
@ -1216,11 +1217,12 @@ describe('test mysqlstore', function () {
const context = new TestContext(store);
await context.begin();
let id = await generateNewIdAsync();
await context.operate('application', {
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
id,
name: 'xuchang',
description: 'tt',
type: 'web',
@ -1232,7 +1234,7 @@ describe('test mysqlstore', function () {
}
}, {});
const result = await context.select('application', {
let result = await context.select('application', {
data: {
id: 1,
name: 1,
@ -1242,9 +1244,50 @@ describe('test mysqlstore', function () {
appId: 1,
}
},
},
filter: {
id,
},
}, {});
// console.log(JSON.stringify(result));
id = await generateNewIdAsync();
await context.operate('application', {
id: await generateNewIdAsync(),
action: 'create',
data: {
id,
name: 'xuchang',
description: 'tt',
type: 'web',
systemId: 'system',
config: {
type: 'web',
wechat: {
appId: 'aaaa\\nddddd',
appSecret: '',
},
passport: ['email', 'mobile'],
},
}
}, {});
console.log(JSON.stringify(result));
result = await context.select('application', {
data: {
id: 1,
name: 1,
config: {
passport: [undefined, 1],
wechat: {
appId: 1,
}
},
},
filter: {
id,
},
}, {});
console.log((result[0].config as WebConfig)!.wechat!.appId);
});
it('[1.11]json as filter', async () => {
@ -1327,7 +1370,7 @@ describe('test mysqlstore', function () {
data: {
id,
destEntity: '1.12',
path: '',
paths: [''],
deActions: ['1.12'],
}
}, context, {});