mysql icon indicating copy to clipboard operation
mysql copied to clipboard

the called parameter

Open ArkIv opened this issue 7 years ago • 5 comments

the called parameters do not work for me (..., { xxx: yyy }, and I use the multipleStatements parameter: true, it is necessary to me. without it the design does not work - (drop... create.) in one inquiry. interests me why I cannot use the called parameters - { param: yyy }: param? (excuse the translator of Yandex)

ArkIv avatar Jul 06 '19 22:07 ArkIv

Hi @ArkIv I'm sorry, I think the translator did not do a good job, as I'm not sure what the question is. If you want to re-post it in your native language, then I can play with the translator to likely get something more cohesive out :)

dougwilson avatar Jul 25 '19 15:07 dougwilson

Hi ! I will try so.. Я использую multipleStatements: true. мне нужно много взаимосвязанных запросов.. и это мне не подходит ' UPDATE users SET foo = ? '. я хочу использовать UPDATE posts SET title = :title", { title: "Hello MySQL" }). но приведенный пример:

connection.config.queryFormat = function (query, values) {
  if (!values) return query;
  return query.replace(/\:(\w+)/g, function (txt, key) {
    if (values.hasOwnProperty(key)) {
      return this.escape(values[key]);
    }
    return txt;
  }.bind(this));
};
connection.query("UPDATE posts SET title = :title", { title: "Hello MySQL" });

у меня не работает. . вот я и подумал возможно это связано с параметром multipleStatements: true, ? Мне он просто необходим. .====== но а пока я вышел из положения так: use ${dbName}; CREATE OR REPLACE procedure ${tableName}_nodeInsertRoot(IN autor VARCHAR(100), out idOut BIGINT) BEGIN INSERT INTO ${tableName} (root, lft, rgt, level, name, isMove, isPublished, autor) VALUES(NEXTVAL(${tableName}_key_root), 1, 2, 0, 'Корневой узел дерева', 0, 1,'${currentAutor}'); set idOut = LASTVAL(${tableName}_key_primary); END;

ArkIv avatar Jul 25 '19 18:07 ArkIv

Ah, I see. So the example you are referring to is really an example for how to use queryFormat, not really a fully functional way to do UPDATE posts SET title = :title", { title: "Hello MySQL" }. I believe the issue you are seeing is that the example does this.escape, which only works for values but you need identifiers as well, which is this.escapeId. MySQL has two different escape formats, which is ? and ?? in this module.

You may want to check out https://github.com/mysqljs/named-placeholders for a module dedicated to making the :name format work well.

dougwilson avatar Jul 25 '19 18:07 dougwilson

original:
var q = toUnnamed('select 1+:test', { test: 123});
mysql.createConnection().query(q[0], q[1]);

я правильно понял что это делает? уж не знаю что и представить..

query = 'select @x := :colname  from :tab where id=:id ;
            select @Y := :colname2  from :tab where id=:id2 ;
           select @Z := :colname3  from :tab where id=:id3 ;
              . . . '
q = toUnnamed(query, { id: 5, id2: 6, id3: 7, tab: 'tablename', colname2: 'name2',colname: 'name',colname3: 'name3' } );
mysql.createConnection().query(q[0], q[1]);

q[0] -> 'select ??  from ?? where id=?;
           select ??  from ?? where id=?;
           select ??  from ?? where id=?;'
q[1] -> [  'name' , 'tablename', 5, 'name2','tablename', 6, 'name3','tablename', 7 ]    !?

это в теории , я не могу сейчас подробно посмотреть... если это так, я не понимаю, почему не сделать сразу без q[1] ? q -> 'select name from 'tablename' where id=5; . . .

но главное я понял что {prop: value} из коробки не работает. а то я думал что , что-нибудь не дочитал. Спасибо!

ArkIv avatar Jul 25 '19 20:07 ArkIv

если это так, я не понимаю, почему не сделать сразу без q[1] ?

Yea, that would probably be a question to ask on https://github.com/mysqljs/named-placeholders because you're correct, it could all be just a single string.

dougwilson avatar Jul 25 '19 21:07 dougwilson