1 Star 0 Fork 0

speedphp/speedsql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
speed.decorator.ts 4.67 KB
一键复制 编辑 原始数据 按行查看 历史
speedphp 提交于 2021-10-07 00:51 +08:00 . fixed a bug for sql rewrite
import { speedPromisePool, resultTypeMap } from './speed.service';
import { ResultSetHeader } from 'mysql2';
const paramMetadataKey = Symbol('param');
function log(message?: any, ...optionalParams: any[]) {
console.log(message, ...optionalParams);
}
function ResultType(constructorFunction) {
const newConstructorFunction: any = function (...args) {
const func: any = function () {
return new constructorFunction(...args);
};
func.prototype = constructorFunction.prototype;
return new func();
};
newConstructorFunction.prototype = constructorFunction.prototype;
return function (target, propertyKey: string) {
resultTypeMap.set(
[target.constructor.name, propertyKey].toString(),
newConstructorFunction(),
);
//never return
};
}
function Param(name: string) {
return function (
target: any,
propertyKey: string | symbol,
parameterIndex: number,
) {
const existingParameters: [string, number][] =
Reflect.getOwnMetadata(paramMetadataKey, target, propertyKey) || [];
existingParameters.push([name, parameterIndex]);
Reflect.defineMetadata(
paramMetadataKey,
existingParameters,
target,
propertyKey,
);
};
}
function convertSQLParams(
args: any[],
target: any,
propertyKey: string,
decoratorSQL: string,
): [string, any[]] {
const queryValues = [];
let argsVal;
if (typeof args[0] === 'object') {
argsVal = new Map(
Object.getOwnPropertyNames(args[0]).map((valName) => [
valName,
args[0][valName],
]),
);
} else {
const existingParameters: [string, number][] = Reflect.getOwnMetadata(
paramMetadataKey,
target,
propertyKey,
);
argsVal = new Map(
existingParameters.map(([argName, argIdx]) => [argName, args[argIdx]]),
);
}
const regExp = /#{(\w+)}/;
let match;
while(match = regExp.exec(decoratorSQL)){
const [replaceTag, matchName] = match;
decoratorSQL = decoratorSQL.replace(new RegExp(replaceTag, 'g'), '?');
queryValues.push(argsVal.get(matchName));
}
// [...decoratorSQL.matchAll(regExp)].forEach((match) => {
// const [replaceTag, matchName] = match;
// decoratorSQL = decoratorSQL.replace(new RegExp(replaceTag, 'g'), '?');
// queryValues.push(argsVal.get(matchName));
// });
return [decoratorSQL, queryValues];
}
async function queryForExecute(
sql: string,
args: any[],
target,
propertyKey: string,
): Promise<ResultSetHeader> {
let sqlValues = [];
let newSql = sql;
if (args.length > 0) {
[newSql, sqlValues] = convertSQLParams(args, target, propertyKey, newSql);
}
const [result] = await speedPromisePool.query(newSql, sqlValues);
return <ResultSetHeader>result;
}
function Insert(sql: string) {
return function (
target,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
descriptor.value = async (...args: any[]) => {
const result: ResultSetHeader = await queryForExecute(
sql,
args,
target,
propertyKey,
);
return result.insertId;
};
};
}
function Update(sql: string) {
return function (
target,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
descriptor.value = async (...args: any[]) => {
const result: ResultSetHeader = await queryForExecute(
sql,
args,
target,
propertyKey,
);
return result.affectedRows;
};
};
}
function Select(sql: string) {
return function (
target,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
descriptor.value = async (...args: any[]) => {
let newSql = sql;
let sqlValues = [];
if (args.length > 0) {
[newSql, sqlValues] = convertSQLParams(args, target, propertyKey, newSql);
}
const [rows] = await speedPromisePool.query(newSql, sqlValues);
if (Object.keys(rows).length === 0) {
return;
}
const records = [];
const resultType = resultTypeMap.get(
[target.constructor.name, propertyKey].toString(),
);
for (const rowIndex in rows) {
const entity = Object.create(resultType);
Object.getOwnPropertyNames(resultType).forEach(function (propertyRow) {
if (rows[rowIndex].hasOwnProperty(propertyRow)) {
Object.defineProperty(
entity,
propertyRow,
Object.getOwnPropertyDescriptor(rows[rowIndex], propertyRow),
);
}
});
records.push(entity);
}
return records;
};
};
}
export { log, Param, ResultType, Select, Insert, Update, Update as Delete };
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/SpeedPHP/speedsql.git
git@gitee.com:SpeedPHP/speedsql.git
SpeedPHP
speedsql
speedsql
main

搜索帮助