mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.31k stars 2.53k forks source link

Named parameters support #920

Open sidorares opened 10 years ago

sidorares commented 10 years ago

I'm thinking to add named parameters support to mysql2 ( https://github.com/sidorares/node-mysql2/pull/125 ), by default hidden behind config flag. Would you like to see similar functionality here? What do you think of api?

  var db = mysql.createConnection({ namedParameters: true });
  db.query('select :a + :b as sum', {a: 1, b: 3}, callback);
dougwilson commented 10 years ago

I saw it and I think people would like it. I personally like namedPlaceholders and the flag better. I think a must-have feature for it would be the ability to turn to on on a per-query basis (and a per-format-call basis), right? It would make it much easier to start using on a very large code base, since you would have to check all the queries for :blah contents.

I'm not firm on any arguments, but if like to hear your comments on it :) I'm in general +1, as it would, at the basic level, make using the same value multiple times in the query much simpler.

Also, is there an equivalent for ?? in the named placeholders?

sidorares commented 10 years ago

I'll rename it to namedPlaceholders

I need to think about ?? in query and differences in parameters semantics between query & execute

It's already possible to change any flag per query by accessing connection.config, but yes, maybe we need more explicit api

adrianblynch commented 10 years ago

+1 for named param support.

@sidorares - Does the "possible to change any flag per query" apply to pools as well?

We're exporting a pool of connections with:

var pool = mysql.createPool(config.database.main);
exports.con = pool;

Would I have to export another pool where flags can be changed?

Sorry for the change of direction :P

dougwilson commented 10 years ago

@adrianblynch each connection from a pool will simply inherit options from the pool itself. So if namedParameters is off in the pool, it'll be off for each connection, though when this is added, you can simply enable named parameters on a query-by-query basis anyway

adrianblynch commented 10 years ago

Cheers @dougwilson :+1:

Are named params coming soon?

dougwilson commented 10 years ago

Yep!

dougwilson commented 9 years ago

Right now you still have to use the named-placeholders libraries in your own code. Example usage is at https://www.npmjs.com/package/named-placeholders#usage

pihvi commented 8 years ago

Here's a library for this also supporting ?? syntax:

var sql = require('yesql').mysql

var selectById = sql('SELECT * from ::table_name WHERE id = :id;')
connection.query(selectById({id: 5, table_name: 'pokemon'}), callback)