Repository: tediousjs/node-mssql (js)

Issue:
Template literals with callbacks
I had to monkey patch Request to use template literals with callbacks

```
mssql.Request.prototype.template=function(){
    const values = Array.prototype.slice.call(arguments);
    const strings = values.shift();
    return this._template(strings, values);
}
```

This allows me to do this:

```
let hw='hello world';
let request=new mssql.Request(pool);
request.query(request.template`SELECT ${hw}`,(err,result)=>{
    if(err){done(err); return;}
    console.log(result.recordset);
});
```

It would be nice if something like this would be included so that those of us trying to avoid promises can use template literals.

Pull request:
Template literals function for use with callbacks
fixes #957 

This allows doing this:

```js
let hw='hello world';
let request=new mssql.Request(pool);
request.query(request.template`SELECT ${hw}`,(err,result)=>{
    if(err){done(err); return;}
    console.log(result.recordset);
});
```

Patch:
diff --git a/README.md b/README.md
index 45e3dbb..52aee2e 100644
--- a/README.md
+++ b/README.md
@@ -250,6 +250,14 @@ sql.connect(config, err => {
 
         console.dir(result)
     })
+
+    // Using template literal
+
+    const request = new sql.Request()
+    request.query(request.template`select * from mytable where id = ${value}`, (err, result) => {
+        // ... error checks
+        console.dir(result)
+    })
 })
 
 sql.on('error', err => {
diff --git a/lib/base/request.js b/lib/base/request.js
index ff52422..05d7582 100644
--- a/lib/base/request.js
+++ b/lib/base/request.js
@@ -40,6 +40,18 @@ class Request extends EventEmitter {
     this.parameters = {}
   }
 
+  /**
+   * Generate sql string and set imput parameters from tagged template string.
+   *
+   * @param {Template literal} template
+   * @return {String}
+   */
+  template () {
+    const values = Array.prototype.slice.call(arguments)
+    const strings = values.shift()
+    return this._template(strings, values)
+  }
+
   /**
    * Fetch request from tagged template string.
    *


Test patch:
diff --git a/test/common/unit.js b/test/common/unit.js
index 8df698a..c4dd0e1 100644
--- a/test/common/unit.js
+++ b/test/common/unit.js
@@ -273,4 +273,11 @@ describe('Unit', () => {
       command: 'select * from myTable where id in (@param1_0, @param1_1, @param1_2)'
     })
   })
+
+  it('tagged template literal request', () => {
+    const req = new sql.Request()
+    const sqlstr = req.template`select * from myTable where id = ${123}`
+    assert.strictEqual(sqlstr, 'select * from myTable where id = @param1')
+    assert.strictEqual(req.parameters.param1.value, 123)
+  })
 })