@@ -598,6 +598,35 @@ database_name(VALUE self, VALUE index)
598598
599599#endif
600600
601+ /* call-seq: stmt.sql
602+ *
603+ * Returns the SQL statement used to create this prepared statement
604+ */
605+ static VALUE
606+ get_sql (VALUE self )
607+ {
608+ sqlite3StmtRubyPtr ctx ;
609+ TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
610+ REQUIRE_OPEN_STMT (ctx );
611+
612+ return rb_obj_freeze (SQLITE3_UTF8_STR_NEW2 (sqlite3_sql (ctx -> st )));
613+ }
614+
615+ /* call-seq: stmt.expanded_sql
616+ *
617+ * Returns the SQL statement used to create this prepared statement, but
618+ * with bind parameters substituted in to the statement.
619+ */
620+ static VALUE
621+ get_expanded_sql (VALUE self )
622+ {
623+ sqlite3StmtRubyPtr ctx ;
624+ TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
625+ REQUIRE_OPEN_STMT (ctx );
626+
627+ return rb_obj_freeze (SQLITE3_UTF8_STR_NEW2 (sqlite3_expanded_sql (ctx -> st )));
628+ }
629+
601630void
602631init_sqlite3_statement (void )
603632{
@@ -615,6 +644,8 @@ init_sqlite3_statement(void)
615644 rb_define_method (cSqlite3Statement , "column_name" , column_name , 1 );
616645 rb_define_method (cSqlite3Statement , "column_decltype" , column_decltype , 1 );
617646 rb_define_method (cSqlite3Statement , "bind_parameter_count" , bind_parameter_count , 0 );
647+ rb_define_method (cSqlite3Statement , "sql" , get_sql , 0 );
648+ rb_define_method (cSqlite3Statement , "expanded_sql" , get_expanded_sql , 0 );
618649#ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
619650 rb_define_method (cSqlite3Statement , "database_name" , database_name , 1 );
620651#endif
0 commit comments