@@ -188,7 +188,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
188
188
* **Warning**: When you close a database (using db.close()),
189
189
* all its statements are closed too and become unusable.
190
190
*
191
- * Statements can't be created by the API user directly, only by Database::prepare
191
+ * Statements can't be created by the API user directly, only by
192
+ * Database::prepare
192
193
*
193
194
* @see Database.html#prepare-dynamic
194
195
* @see https://en.wikipedia.org/wiki/Prepared_statement
@@ -321,7 +322,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
321
322
322
323
/** Get one row of results of a statement.
323
324
If the first parameter is not provided, step must have been called before.
324
- @param {Statement.BindParams } [params=[] ] If set, the values will be bound
325
+ @param {Statement.BindParams } [params] If set, the values will be bound
325
326
to the statement before it is executed
326
327
@return {Database.SqlValue[] } One row of result
327
328
@@ -381,8 +382,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
381
382
return results1 ;
382
383
} ;
383
384
384
- /** Get one row of result as a javascript object, associating column names with
385
- their value in the current row.
385
+ /** Get one row of result as a javascript object, associating column names
386
+ with their value in the current row.
386
387
@param {Statement.BindParams } [params] If set, the values will be bound
387
388
to the statement, and it will be executed
388
389
@return {Object<string, Database.SqlValue> } The row of result
@@ -519,7 +520,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
519
520
) ;
520
521
} ;
521
522
522
- /** Bind names and values of an object to the named parameters of the statement
523
+ /** Bind names and values of an object to the named parameters of the
524
+ statement
523
525
@param {Object<string, Database.SqlValue> } valuesObj
524
526
@private
525
527
@nodoc
@@ -589,8 +591,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
589
591
* Represents an SQLite database
590
592
* @constructs Database
591
593
* @memberof module:SqlJs
592
- * Open a new database either by creating a new one or opening an existing one,
593
- * stored in the byte array passed in first argument
594
+ * Open a new database either by creating a new one or opening an existing
595
+ * one stored in the byte array passed in first argument
594
596
* @param {number[] } data An array of bytes representing
595
597
* an SQLite database file
596
598
*/
@@ -611,10 +613,10 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
611
613
612
614
/** Execute an SQL query, ignoring the rows it returns.
613
615
@param {string } sql a string containing some SQL text to execute
614
- @param {any[] } [params=[]] When the SQL statement contains placeholders,
615
- you can pass them in here. They will be bound to the statement
616
- before it is executed. If you use the params argument, you **cannot** provide an sql string
617
- that contains several queries (separated by `;`)
616
+ @param {Statement.BindParams } [params] When the SQL statement contains
617
+ placeholders, you can pass them in here. They will be bound to the statement
618
+ before it is executed. If you use the params argument, you **cannot**
619
+ provide an sql string that contains several statements (separated by `;`)
618
620
619
621
@example
620
622
// Insert values in a table
@@ -644,48 +646,66 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
644
646
* @typedef {{columns:string[], values:Database.SqlValue[][]} } Database.QueryExecResult
645
647
* @property {string[] } columns the name of the columns of the result
646
648
* (as returned by {@link Statement.getColumnNames})
647
- * @property {Database.SqlValue[][] } values one array per row, containing the column values
649
+ * @property {Database.SqlValue[][] } values one array per row, containing
650
+ * the column values
648
651
*/
649
652
650
653
/** Execute an SQL query, and returns the result.
651
654
*
652
655
* This is a wrapper against
653
656
* {@link Database.prepare},
657
+ * {@link Statement.bind},
654
658
* {@link Statement.step},
655
659
* {@link Statement.get},
656
660
* and {@link Statement.free}.
657
661
*
658
- * The result is an array of result elements. There are as many result elements
659
- * as the number of statements in your sql string (statements are separated
660
- * by a semicolon)
662
+ * The result is an array of result elements. There are as many result
663
+ * elements as the number of statements in your sql string (statements are
664
+ * separated by a semicolon)
661
665
*
662
666
* ## Example use
663
- * We have the following table, named *test* :
667
+ * We will create the following table, named *test* and query it with a
668
+ * multi-line statement using params:
664
669
*
665
670
* | id | age | name |
666
671
* |:--:|:---:|:------:|
667
672
* | 1 | 1 | Ling |
668
673
* | 2 | 18 | Paul |
669
- * | 3 | 3 | Markus |
670
674
*
671
675
* We query it like that:
672
676
* ```javascript
673
677
* var db = new SQL.Database();
674
- * var res = db.exec("SELECT id FROM test; SELECT age,name FROM test;");
678
+ * var res = db.exec(
679
+ * "DROP TABLE IF EXISTS test;\n"
680
+ * + "CREATE TABLE test (id INTEGER, age INTEGER, name TEXT);"
681
+ * + "INSERT INTO test VALUES ($id1, :age1, @name1);"
682
+ * + "INSERT INTO test VALUES ($id2, :age2, @name2);"
683
+ * + "SELECT id FROM test;"
684
+ * + "SELECT age,name FROM test WHERE id=$id1",
685
+ * {
686
+ * "$id1": 1, ":age1": 1, "@name 1": "Ling",
687
+ * "$id2": 2, ":age2": 18, "@name2": "Paul"
688
+ * }
689
+ * );
675
690
* ```
676
691
*
677
692
* `res` is now :
678
693
* ```javascript
679
694
* [
680
- * {columns: ['id'], values:[[1],[2],[3 ]]},
681
- * {columns: [' age',' name'], values:[[1,' Ling'],[18,'Paul'],[3,'Markus' ]]}
695
+ * {" columns":["id"]," values" :[[1],[2]]},
696
+ * {" columns":[" age"," name"]," values" :[[1," Ling" ]]}
682
697
* ]
683
698
* ```
684
699
*
685
- * @param {string } sql a string containing some SQL text to execute
700
+ @param {string } sql a string containing some SQL text to execute
701
+ @param {Statement.BindParams } [params] When the SQL statement contains
702
+ placeholders, you can pass them in here. They will be bound to the statement
703
+ before it is executed. If you use the params argument as an array,
704
+ you **cannot** provide an sql string that contains several statements
705
+ (separated by `;`). This limitation does not apply to params as an object.
686
706
* @return {Database.QueryExecResult[] } The results of each statement
687
707
*/
688
- Database . prototype [ "exec" ] = function exec ( sql ) {
708
+ Database . prototype [ "exec" ] = function exec ( sql , params ) {
689
709
var curresult ;
690
710
var stmt ;
691
711
if ( ! this . db ) {
@@ -713,6 +733,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
713
733
if ( pStmt !== NULL ) {
714
734
curresult = null ;
715
735
stmt = new Statement ( pStmt , this ) ;
736
+ if ( params != null ) {
737
+ stmt . bind ( params ) ;
738
+ }
716
739
while ( stmt [ "step" ] ( ) ) {
717
740
if ( curresult === null ) {
718
741
curresult = {
@@ -739,14 +762,16 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
739
762
740
763
/** Execute an sql statement, and call a callback for each row of result.
741
764
742
- **Currently** this method is synchronous, it will not return until the callback
743
- has been called on every row of the result. But this might change.
765
+ **Currently** this method is synchronous, it will not return until the
766
+ callback has been called on every row of the result. But this might change.
744
767
745
768
@param {string } sql A string of SQL text. Can contain placeholders
746
769
that will be bound to the parameters given as the second argument
747
- @param {Array<Database.SqlValue> } [params=[]] Parameters to bind to the query
748
- @param {function({Object}):void } callback A function that will be called on each row of result
749
- @param {function() } done A function that will be called when all rows have been retrieved
770
+ @param {Array<Database.SqlValue> } [params] Parameters to bind to the query
771
+ @param {function({Object}):void } callback A function that will be called on
772
+ each row of result
773
+ @param {function() } done A function that will be called when all rows have
774
+ been retrieved
750
775
751
776
@return {Database } The database object. Useful for method chaining
752
777
0 commit comments