@@ -7,6 +7,15 @@ import {
77 SQLITE_BLOB ,
88 SQLITE_FLOAT ,
99 SQLITE_INTEGER ,
10+ SQLITE_STMTSTATUS_AUTOINDEX ,
11+ SQLITE_STMTSTATUS_FILTER_HIT ,
12+ SQLITE_STMTSTATUS_FILTER_MISS ,
13+ SQLITE_STMTSTATUS_FULLSCAN_STEP ,
14+ SQLITE_STMTSTATUS_MEMUSED ,
15+ SQLITE_STMTSTATUS_REPREPARE ,
16+ SQLITE_STMTSTATUS_RUN ,
17+ SQLITE_STMTSTATUS_SORT ,
18+ SQLITE_STMTSTATUS_VM_STEP ,
1019 SQLITE_TEXT ,
1120} from "./constants.ts" ;
1221
@@ -39,6 +48,7 @@ const {
3948 sqlite3_bind_parameter_name,
4049 sqlite3_changes,
4150 sqlite3_column_int,
51+ sqlite3_stmt_status,
4252} = ffi ;
4353
4454/** Types that can be possibly serialized as SQLite bind values */
@@ -55,6 +65,17 @@ export type BindValue =
5565 | BindValue [ ]
5666 | { [ key : string ] : BindValue } ;
5767
68+ type StmtStatusOp =
69+ | typeof SQLITE_STMTSTATUS_FULLSCAN_STEP
70+ | typeof SQLITE_STMTSTATUS_SORT
71+ | typeof SQLITE_STMTSTATUS_AUTOINDEX
72+ | typeof SQLITE_STMTSTATUS_VM_STEP
73+ | typeof SQLITE_STMTSTATUS_REPREPARE
74+ | typeof SQLITE_STMTSTATUS_RUN
75+ | typeof SQLITE_STMTSTATUS_FILTER_MISS
76+ | typeof SQLITE_STMTSTATUS_FILTER_HIT
77+ | typeof SQLITE_STMTSTATUS_MEMUSED ;
78+
5879export type BindParameters = BindValue [ ] | Record < string , BindValue > ;
5980export type RestBindParameters = BindValue [ ] | [ BindParameters ] ;
6081
@@ -738,6 +759,64 @@ export class Statement<TStatement extends object = Record<string, any>> {
738759 }
739760 }
740761
762+ #status( op : StmtStatusOp , reset ?: boolean ) : number {
763+ return sqlite3_stmt_status ( this . #handle, op , reset ? 1 : 0 ) ;
764+ }
765+
766+ /** This is the number of times that SQLite has stepped forward in a table as part of a full table scan.
767+ * Large numbers for this counter may indicate opportunities for performance improvement through careful use of indices. */
768+ statusFullscanStep ( reset ?: boolean ) : number {
769+ return this . #status( SQLITE_STMTSTATUS_FULLSCAN_STEP , reset ) ;
770+ }
771+
772+ /** This is the number of sort operations that have occurred.
773+ * A non-zero value in this counter may indicate an opportunity to improve performance through careful use of indices. */
774+ statusSort ( reset ?: boolean ) : number {
775+ return this . #status( SQLITE_STMTSTATUS_SORT , reset ) ;
776+ }
777+
778+ /** This is the number of rows inserted into transient indices that were created automatically in order to help joins run faster.
779+ * A non-zero value in this counter may indicate an opportunity to improve performance by adding permanent indices that do not need to be reinitialized each time the statement is run. */
780+ statusAutoindex ( reset ?: boolean ) : number {
781+ return this . #status( SQLITE_STMTSTATUS_AUTOINDEX , reset ) ;
782+ }
783+
784+ /** This is the number of virtual machine operations executed by the prepared statement if that number is less than or equal to 2147483647.
785+ * The number of virtual machine operations can be used as a proxy for the total work done by the prepared statement.
786+ * If the number of virtual machine operations exceeds 2147483647 then the value returned by this statement status code is undefined. */
787+ statusVmStep ( reset ?: boolean ) : number {
788+ return this . #status( SQLITE_STMTSTATUS_VM_STEP , reset ) ;
789+ }
790+
791+ /** This is the number of times that the prepare statement has been automatically regenerated due to schema changes or changes to bound parameters that might affect the query plan. */
792+ statusReprepare ( reset ?: boolean ) : number {
793+ return this . #status( SQLITE_STMTSTATUS_REPREPARE , reset ) ;
794+ }
795+
796+ /** This is the number of times that the prepared statement has been run.
797+ * A single "run" for the purposes of this counter is one or more calls to sqlite3_step() followed by a call to sqlite3_reset().
798+ * The counter is incremented on the first sqlite3_step() call of each cycle. */
799+ statusRun ( reset ?: boolean ) : number {
800+ return this . #status( SQLITE_STMTSTATUS_RUN , reset ) ;
801+ }
802+
803+ /** This is the number of times that a join step was bypassed because a Bloom filter returned not-found. */
804+ statusFilterMiss ( reset ?: boolean ) : number {
805+ return this . #status( SQLITE_STMTSTATUS_FILTER_MISS , reset ) ;
806+ }
807+
808+ /** The corresponding SQLITE_STMTSTATUS_FILTER_MISS value is the number of times that the Bloom filter returned a find,
809+ * and thus the join step had to be processed as normal. */
810+ statusFilterHit ( reset ?: boolean ) : number {
811+ return this . #status( SQLITE_STMTSTATUS_FILTER_HIT , reset ) ;
812+ }
813+
814+ /** This is the approximate number of bytes of heap memory used to store the prepared statement.
815+ * This value is not actually a counter. */
816+ statusMemused ( ) : number {
817+ return this . #status( SQLITE_STMTSTATUS_MEMUSED ) ;
818+ }
819+
741820 /** Free up the statement object. */
742821 finalize ( ) : void {
743822 if ( ! STATEMENTS_TO_DB . has ( this . #handle) ) return ;
0 commit comments