@@ -460,6 +460,40 @@ bind_parameter_count(VALUE self)
460460 return INT2NUM (sqlite3_bind_parameter_count (ctx -> st ));
461461}
462462
463+ /** call-seq: stmt.named_params
464+ *
465+ * Return the list of named parameters in the statement.
466+ * This returns a frozen array of strings (without the leading prefix character).
467+ * The values of this list can be used to bind parameters
468+ * to the statement using bind_param. Positional (?NNN) and anonymous (?)
469+ * parameters are excluded.
470+ *
471+ */
472+ static VALUE
473+ named_params (VALUE self )
474+ {
475+ sqlite3StmtRubyPtr ctx ;
476+ TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
477+
478+ REQUIRE_LIVE_DB (ctx );
479+ REQUIRE_OPEN_STMT (ctx );
480+
481+ int param_count = sqlite3_bind_parameter_count (ctx -> st );
482+ VALUE params = rb_ary_new2 (param_count );
483+
484+ // The first host parameter has an index of 1, not 0.
485+ for (int i = 1 ; i <= param_count ; i ++ ) {
486+ const char * name = sqlite3_bind_parameter_name (ctx -> st , i );
487+ // We ignore positional and anonymous parameters, and also null values, since there can be
488+ // gaps in the list.
489+ if (name && * name != '?' ) {
490+ VALUE param = interned_utf8_cstr (name + 1 );
491+ rb_ary_push (params , param );
492+ }
493+ }
494+ return rb_obj_freeze (params );
495+ }
496+
463497enum stmt_stat_sym {
464498 stmt_stat_sym_fullscan_steps ,
465499 stmt_stat_sym_sorts ,
@@ -689,6 +723,7 @@ init_sqlite3_statement(void)
689723 rb_define_method (cSqlite3Statement , "column_name" , column_name , 1 );
690724 rb_define_method (cSqlite3Statement , "column_decltype" , column_decltype , 1 );
691725 rb_define_method (cSqlite3Statement , "bind_parameter_count" , bind_parameter_count , 0 );
726+ rb_define_method (cSqlite3Statement , "named_params" , named_params , 0 );
692727 rb_define_method (cSqlite3Statement , "sql" , get_sql , 0 );
693728 rb_define_method (cSqlite3Statement , "expanded_sql" , get_expanded_sql , 0 );
694729#ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
0 commit comments