@@ -188,7 +188,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
188188 * **Warning**: When you close a database (using db.close()),
189189 * all its statements are closed too and become unusable.
190190 *
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
192193 *
193194 * @see Database.html#prepare-dynamic
194195 * @see https://en.wikipedia.org/wiki/Prepared_statement
@@ -321,7 +322,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
321322
322323 /** Get one row of results of a statement.
323324 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
325326 to the statement before it is executed
326327 @return {Database.SqlValue[] } One row of result
327328
@@ -381,8 +382,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
381382 return results1 ;
382383 } ;
383384
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.
386387 @param {Statement.BindParams } [params] If set, the values will be bound
387388 to the statement, and it will be executed
388389 @return {Object<string, Database.SqlValue> } The row of result
@@ -519,7 +520,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
519520 ) ;
520521 } ;
521522
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
523525 @param {Object<string, Database.SqlValue> } valuesObj
524526 @private
525527 @nodoc
@@ -589,8 +591,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
589591 * Represents an SQLite database
590592 * @constructs Database
591593 * @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
594596 * @param {number[] } data An array of bytes representing
595597 * an SQLite database file
596598 */
@@ -611,10 +613,10 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
611613
612614 /** Execute an SQL query, ignoring the rows it returns.
613615 @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 `;`)
618620
619621 @example
620622 // Insert values in a table
@@ -644,48 +646,66 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
644646 * @typedef {{columns:string[], values:Database.SqlValue[][]} } Database.QueryExecResult
645647 * @property {string[] } columns the name of the columns of the result
646648 * (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
648651 */
649652
650653 /** Execute an SQL query, and returns the result.
651654 *
652655 * This is a wrapper against
653656 * {@link Database.prepare},
657+ * {@link Statement.bind},
654658 * {@link Statement.step},
655659 * {@link Statement.get},
656660 * and {@link Statement.free}.
657661 *
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)
661665 *
662666 * ## 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:
664669 *
665670 * | id | age | name |
666671 * |:--:|:---:|:------:|
667672 * | 1 | 1 | Ling |
668673 * | 2 | 18 | Paul |
669- * | 3 | 3 | Markus |
670674 *
671675 * We query it like that:
672676 * ```javascript
673677 * 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+ * );
675690 * ```
676691 *
677692 * `res` is now :
678693 * ```javascript
679694 * [
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" ]]}
682697 * ]
683698 * ```
684699 *
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.
686706 * @return {Database.QueryExecResult[] } The results of each statement
687707 */
688- Database . prototype [ "exec" ] = function exec ( sql ) {
708+ Database . prototype [ "exec" ] = function exec ( sql , params ) {
689709 var curresult ;
690710 var stmt ;
691711 if ( ! this . db ) {
@@ -713,6 +733,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
713733 if ( pStmt !== NULL ) {
714734 curresult = null ;
715735 stmt = new Statement ( pStmt , this ) ;
736+ if ( params != null ) {
737+ stmt . bind ( params ) ;
738+ }
716739 while ( stmt [ "step" ] ( ) ) {
717740 if ( curresult === null ) {
718741 curresult = {
@@ -739,14 +762,16 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
739762
740763 /** Execute an sql statement, and call a callback for each row of result.
741764
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.
744767
745768 @param {string } sql A string of SQL text. Can contain placeholders
746769 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
750775
751776 @return {Database } The database object. Useful for method chaining
752777
0 commit comments