scope {
    /* Basic sanity checks for the Db and Stmt bindings. */
    var db = Fossil.Db.open('my.db','wc');
    assert db inherits Fossil.Db;
    assert 'Db' === typename db;
    //db.eachProperty(print);

    var st = db.prepare("CREATE TABLE IF NOT EXISTS t(a,b)");
    assert st inherits Fossil.Db.Stmt;
    assert 'Stmt' === typename st;
    assert undefined === st.step();
    st.finalize();
    assert 0 === catch{st.finalize()}.message.indexOf("'this'");
    st = 0; // will (in this case) clean up the wrapping part of the binding immediately

    assert db === db.execMulti(<<<EOSQL
                 DELETE FROM t;
                 INSERT INTO t VALUES(1,2);
                 INSERT INTO t VALUES(3,4);
                 EOSQL);
    var counter = 0;
    assert db === db.each({
        sql: 'select a a, b b from t', // string or buffer
        mode: 0, // 0===get each row as an object, else as an array
        callback:<<<EOF // string/buffer or function
          // Local vars defined in an each() callback:
          // integer columnCount, integer rowNumber, Array columnNames,
          // Object|Array this.
          counter = rowNumber;
          assert "object"===typename this;
          //'print("row", rowNumber, this)'
        EOF
        // bind: if sql prop contains bound params, then a value,
        // array of values, or an object with named params as keys.
        // See examples below.
    });
    assert 2 === counter;
    counter = 0;
    db.each({
        sql: 'select a a, b b from t',
        callback: proc(){
            counter = rowNumber;
            assert 'array' === typename this;
            //'print("row", rowNumber, this)'
        }
    });
    assert 2 === counter;

    var lastCol;
    counter = 0;
    db.each({
        sql: 'select a a, b b from t where a>:min AND a<$max',
        bind: {':min':1, $max:10} /* note that the ':' resp '$' is mandatory,
                                     but $ is an identifier char and need not
                                     be quoted. */,
        mode: 0, // 0 === fetch each row as an object
        callback: proc(){
            counter = rowNumber;
            assert 'object' === typename this;
            lastCol = this.a;
            //print("row", rowNumber, this)
        }
    });
    assert 1 === counter;
    assert 3 === lastCol;

    st = db.prepare('select a a, b b from t where a>?1 AND a<?2');
    st.bind([0, 10]);
    counter = 0;
    while(st.step()){++counter}
    assert 2 === counter;
    st.reset();
    st.bind(1, 1).bind(2,5);
    counter = 0;
    lastCol = 0;
    while(var row = st.stepArray()){
        ++counter;
        lastCol = row.1;
    }
    st.finalize();
    assert counter === 1;
    assert lastCol === 4;

    counter = 0;
    db.each({sql:'select * from t order by a',
             callback:'++counter>0 && return false'
            });
    assert 1 === counter;

    assert 42 === catch{
        db.each({sql:'select * from t order by a',
                 callback:'throw 42'})
    }.message;

    counter = 0;
    db.exec("create temp table ttmmpp(a)",
            "insert into ttmmpp values(1),(2),(3)"
           )
        .each({sql:"select a from ttmmpp",
               callback: 'counter += this.0'})
            ;
    assert 6 === counter;

    db.close();
    assert 0 === catch{db.close()}.message.indexOf("'this'");
}
