mysqljs / sqlstring

Simple SQL escape and format for MySQL
MIT License
403 stars 78 forks source link

Named Placeholder Support #18

Closed craighardcastle closed 7 years ago

craighardcastle commented 7 years ago

Hi,

I would like to add named placeholder support so that the order of values don't matter and so that I can reuse the same value multiple times but only specify it once.

The basic idea is this:

const sql = SqlString.format('SELECT ::columns FROM ::table WHERE id = :id', {
    id: 1,
    columns: ['username', 'email'], 
    table: 'users'
});
console.log(sql); // SELECT `username`, `email` FROM `users` WHERE id = 1

Please see the attached diff.

Thanks,

Craig

dougwilson commented 7 years ago

In our org we have the module https://github.com/mysqljs/named-placeholders that does the named placeholder translation like what you added here. Before I look over the PR too much, have you looked at that repo yet?

craighardcastle commented 7 years ago

Hi,

To be honest I didn't realise that existed, however I still think it would be good if named placeholders were supported without the need for another module.

My diff is pretty small and I think should be performant, but might be possible to improve slightly if it was too slow.

Il leave it up to you :) The diff is really small so I hope you have time to look at it.

Thanks,

Craig

dougwilson commented 7 years ago

Generally, it seems to do what the other module is already doing. Why not just use that module in this PR instead of a second implementation we have to maintain?

craighardcastle commented 7 years ago

I guess if there is already a supported module then fair enough :)