 M: sqlite-db dispose ( db -- ) dispose-db ;
 
 TUPLE: sqlite-statement < throwable-statement ;
-! INSTANCE: sqlite-statement throwable-statement
 
 TUPLE: sqlite-result-set < result-set has-more? ;
 


M: sqlite-statement bind-tuple ( tuple statement -- )
    [
        statement-in-params
        [
            [ sql-spec-column-name ":" prepend ]
            [ sql-spec-slot-name rot get-slot-named ]
            [ sql-spec-type ] tri 3array
        ] with map
    ] keep
    bind-statement ;

    dup zero? [ "last-id failed" throw ] when ;

M: sqlite-db insert-tuple* ( tuple statement -- )
    execute-statement last-insert-id >>primary-key drop ;

M: sqlite-result-set #columns ( result-set -- n )
    handle>> sqlite-#columns ;

M: sqlite-result-set row-column ( result-set n -- obj )
    [ handle>> ] [ sqlite-column ] bi* ;

M: sqlite-result-set row-column-typed ( result-set n -- obj )
    dup pick out-params>> nth type>>
    >r >r handle>> r> r> sqlite-column-typed ;

M: sqlite-result-set advance-row ( result-set -- )
    dup handle>> sqlite-next >>has-more? drop ;

M: sqlite-result-set more-rows? ( result-set -- ? )
    has-more?>> ;

M: sqlite-statement query-results ( query -- result-set )
    sqlite-maybe-prepare
    dup handle>> sqlite-result-set construct-result-set
    dup advance-row ;

M: sqlite-db begin-transaction ( -- ) "BEGIN" sql-command ;
