是否可以在MYSQL查询中使用:bind(oracle样式)和Node?
我正在将node.js/typescript项目的数据库从Oracle迁移到MYSQL。 我在Oracle中的查询/dml都是以这种方式绑定的是否可以在MYSQL查询中使用:bind(oracle样式)和Node?,mysql,node.js,Mysql,Node.js,我正在将node.js/typescript项目的数据库从Oracle迁移到MYSQL。 我在Oracle中的查询/dml都是以这种方式绑定的 conn.execute('select date, name from table where id = :ID and field = :VAR', {ID: variable1, VAR: variable2}); 使用MYSQL时,我发现: connection.query('select
conn.execute('select date, name from table
where id = :ID and field = :VAR',
{ID: variable1, VAR: variable2});
使用MYSQL时,我发现:
connection.query('select date, name from table
where id = ? and field = ?',
[variable1, variable2]);
第二种方法对我来说更糟糕,原因如下:
我想在我的代码中重写很多sql调用
我认为第一种方法更可靠,因为您不担心由于SQL的变化而产生不可预测的结果
虽然我发现有人提到了第一种风格,但它并不能使它起作用
有什么建议吗?因为我没有找到任何可以解决问题的方法,所以我尝试解决这个问题。也许这会有帮助 首先,这段代码从emp中获取一个Oracle绑定接口类型,如
{ID:105,DEPT:'MKT'}
和一个查询,如'select*,其中ID=:ID和deptName=:DEPT'
,并将它们转换为[105,'MKT']
和'select*,从emp中ID=?和deptName=?'
这是密码
const endBindCharacters: string = ' )=';
function prepareSQL(sql: string, binders: Object = null, valueArray: TBindArray): string {
let ich: number = 0;
let bindVariable: string;
if (! binders) {
if (sql.indexOf(':') > 0) {
throw new CustomError(errorCodes.connection.sqlBoundWithNoBinders,
'No binders {} in a bound SQL ' + sql);
};
return sql;
};
while ((ich = sql.indexOf(':')) > 0) {
bindVariable = '';
while (!endBindCharacters.includes(sql[++ich]) && ich < sql.length) {
bindVariable += sql[ich];
};
if (binders[bindVariable]) {
valueArray.push(binders[bindVariable]);
} else {
throw new CustomError(errorCodes.connection.bindVariableNotInBinders, ' Bind variable ' + bindVariable +
' não encontradada no binders {} da expressão:\n' + sql)
};
sql = sql.replace(':' + bindVariable, ' ? ');
};
return sql;
};
const-endBindCharacters:string=')=';
函数prepareSQL(sql:string,binders:Object=null,valueArray:TBindArray):string{
设ich:number=0;
让bindVariable:string;
如果(!活页夹){
if(sql.indexOf(':')>0){
抛出新的CustomError(errorCodes.connection.SqlBoundWithNoBinder,
'绑定SQL'+SQL中没有绑定{});
};
返回sql;
};
而((ich=sql.indexOf(':'))>0){
bindVariable='';
while(!endBindCharacters.includes(sql[++ich])&&ich
这是包装纸。它将从回调中得到承诺
export async function executeSQL (conn: TConnection, sql: string,
binders: Object = {}): Promise<TReturn> {
let bindArray: TBindArray = [];
sql = prepareSQL(sql, binders, bindArray);
console.log(sql, binders, bindArray);
return new Promise<TReturn>(function(resolve, reject) {
conn.query(sql, bindArray , function(err: db.IError, results: TReturn) {
if(err) {reject(err)}
else {resolve(results)};
});
});
};
导出异步函数executeSQL(conn:TConnection,sql:string,
绑定器:对象={}):承诺{
让bindArray:TBindArray=[];
sql=prepareSQL(sql,binders,bindArray);
log(sql、binders、bindArray);
返回新承诺(功能(解决、拒绝){
conn.query(sql、bindArray、函数(err:db.IError、results:TReturn){
如果(错误){拒绝(错误)}
else{解析(结果)};
});
});
};
如果您无法让它工作,也许您应该发布为什么不工作。如果我尝试插入一个包含文本的字符串:
,会发生什么情况?或者,如果我运行一个SQL语句,其中的注释中有一个:
?或者,如果我用:
符号命名我的表或列(这在技术上是不常见的,但在技术上是允许的)?这一点都没有问题,因为此代码正是为了将占位符(绑定)更改为变量的内容。因此,代码“where name=:nameVariable”将用变量的内容替换绑定(nameVariable),无论其内部是否包含“:”或“)。@BobJarvis这是问题的解决方案