0

I'm using tagged templates to build a query with its params.

function query (strings, ...args) {
    return {
        sql: strings.join('?'),
        params: args
    }
}

const storeId = '417-123';
const id = 10;
const res = query`select * from trad.customers where store_id = ${ storeId } and customer_id = ${ id }`;
// res.sql: select * from trad.customers where store_id = ? and customer_id = ?;
// res.params: [ '417-123', 10 ];

Sometimes I need to pass a variable which cannot be passed as a SQL parameter (for instance, a table name) to the query. I'm stuck with this...

const nim = '0850204';
const id = 143;
const res = query`select * from _${ nim }_ tickets where tick_id = ${ id }`;
// what I get:
// res.sql: select * from _?_ tickets where tick_id = ?;
// res.params: [ '0850204', 143 ];

// what I'd like
// res.sql: select * from _0850204_ tickets where tick_id = ?;
// res.params: [ 143 ];

How can I get around this ? Thank you for your help :)

EDIT : I used a special char as a flag to tell when to replace the variable directly... This way, when the char # is found I replace directly the following value. I'm pretty sure we can do better but I don't know how...

function query (strings, ...args) {

    const del = [];
    const replace = [];
    let sql = strings
        .map((data, index) => {
            if (data.endsWith('#') === false) return data;
            replace.push(args[index]);
            del.push(index);
            return data;
        })
        .join('?');

    args = args.filter((d, index) => del.includes(index) === false);

    let n = 0;
    while (sql.includes('#?')) {
        sql = sql.replace('#?', replace[n]);
    }

    return { sql, params: args };
}

const nim = '0850204';
const id = 143;
// Notice the '#'
const res = query`select * from _#${ nim }_ tickets where tick_id = ${ id }`;
// res.sql: select * from _0850204_ tickets where tick_id = ?;
// res.params: [ 143 ];
Auré Km
  • 19
  • 1
  • 1
    Many of us know perfectly how template strings work and can answer in a minute... but we can't guess the goal when it's not described. You seem to want to handle numbers (`1` and `2`) in a different way than strings (`000`). Or is the logic different ? Can you be specific here ? – Denys Séguret Sep 17 '18 at 07:42
  • @DenysSéguret I think the question is if it's possible to pass directly the value of `nim` in strings rather than having it inside args – LS_ Sep 17 '18 at 07:47
  • @AuréKm It might be useful if you could provide some detail on why you're looking to do that, it seems like what you want to do defeats the object of using template strings in the first place – OliverRadini Sep 17 '18 at 08:03
  • @OliverRadini, I edited my post to give a real life exemple of what I need. I hope it helps ;) – Auré Km Sep 17 '18 at 08:49
  • So you're trying to _sometimes_ replace variables in the strings with a variable, _sometimes_ with a `?` – OliverRadini Sep 17 '18 at 09:01
  • I'm trying to always replace variables in the strings with a ?, but sometimes I'd like to replace a part with a variable in the input string before passing it to the tag function. Is this possible ? – Auré Km Sep 17 '18 at 09:05

0 Answers0