@@ -6,6 +6,7 @@ static void deallocate(void *);
66static VALUE allocate (VALUE klass );
77static size_t memsize (const void * p );
88static VALUE appender_initialize (VALUE klass , VALUE con , VALUE schema , VALUE table );
9+ static VALUE appender_error_message (VALUE self );
910static VALUE appender_begin_row (VALUE self );
1011static VALUE appender_end_row (VALUE self );
1112static VALUE appender_append_bool (VALUE self , VALUE val );
@@ -34,7 +35,7 @@ static VALUE appender__append_time(VALUE self, VALUE hour, VALUE min, VALUE sec,
3435static VALUE appender__append_timestamp (VALUE self , VALUE year , VALUE month , VALUE day , VALUE hour , VALUE min , VALUE sec , VALUE micros );
3536static VALUE appender__append_hugeint (VALUE self , VALUE lower , VALUE upper );
3637static VALUE appender__append_uhugeint (VALUE self , VALUE lower , VALUE upper );
37- static VALUE appender_flush (VALUE self );
38+ static VALUE appender__flush (VALUE self );
3839static VALUE appender_close (VALUE self );
3940
4041static const rb_data_type_t appender_data_type = {
@@ -82,6 +83,30 @@ static VALUE appender_initialize(VALUE self, VALUE con, VALUE schema, VALUE tabl
8283 return self ;
8384}
8485
86+ /* call-seq:
87+ * appender.error_message -> String
88+ *
89+ * Returns the error message of the appender. If there is no error, then it returns nil.
90+ *
91+ * require 'duckdb'
92+ * db = DuckDB::Database.open
93+ * con = db.connect
94+ * con.query('CREATE TABLE users (id INTEGER, name VARCHAR)')
95+ * appender = con.appender('users')
96+ * appender.error_message # => nil
97+ */
98+ static VALUE appender_error_message (VALUE self ) {
99+ rubyDuckDBAppender * ctx ;
100+ const char * msg ;
101+ TypedData_Get_Struct (self , rubyDuckDBAppender , & appender_data_type , ctx );
102+
103+ msg = duckdb_appender_error (ctx -> appender );
104+ if (msg == NULL ) {
105+ return Qnil ;
106+ }
107+ return rb_str_new2 (msg );
108+ }
109+
85110/* call-seq:
86111 * appender.begin_row -> self
87112 *
@@ -507,32 +532,15 @@ static VALUE appender__append_uhugeint(VALUE self, VALUE lower, VALUE upper) {
507532 return self ;
508533}
509534
510- /* call-seq:
511- * appender.flush -> self
512- *
513- * Flushes the appender to the table, forcing the cache of the appender to be cleared. If flushing the data triggers a
514- * constraint violation or any other error, then all data is invalidated, and this method raises DuckDB::Error.
515- *
516- * require 'duckdb'
517- * db = DuckDB::Database.open
518- * con = db.connect
519- * con.query('CREATE TABLE users (id INTEGER, name VARCHAR)')
520- * appender = con.appender('users')
521- * appender
522- * .begin_row
523- * .append_int32(1)
524- * .append_varchar('Alice')
525- * .end_row
526- * .flush
527- */
528- static VALUE appender_flush (VALUE self ) {
535+ /* :nodoc: */
536+ static VALUE appender__flush (VALUE self ) {
529537 rubyDuckDBAppender * ctx ;
530538 TypedData_Get_Struct (self , rubyDuckDBAppender , & appender_data_type , ctx );
531539
532540 if (duckdb_appender_flush (ctx -> appender ) == DuckDBError ) {
533- rb_raise ( eDuckDBError , "failed to flush" ) ;
541+ return Qfalse ;
534542 }
535- return self ;
543+ return Qtrue ;
536544}
537545
538546static VALUE appender_close (VALUE self ) {
@@ -552,6 +560,7 @@ void rbduckdb_init_duckdb_appender(void) {
552560 cDuckDBAppender = rb_define_class_under (mDuckDB , "Appender" , rb_cObject );
553561 rb_define_alloc_func (cDuckDBAppender , allocate );
554562 rb_define_method (cDuckDBAppender , "initialize" , appender_initialize , 3 );
563+ rb_define_method (cDuckDBAppender , "error_message" , appender_error_message , 0 );
555564 rb_define_method (cDuckDBAppender , "begin_row" , appender_begin_row , 0 );
556565 rb_define_method (cDuckDBAppender , "end_row" , appender_end_row , 0 );
557566 rb_define_method (cDuckDBAppender , "append_bool" , appender_append_bool , 1 );
@@ -569,13 +578,13 @@ void rbduckdb_init_duckdb_appender(void) {
569578 rb_define_method (cDuckDBAppender , "append_varchar_length" , appender_append_varchar_length , 2 );
570579 rb_define_method (cDuckDBAppender , "append_blob" , appender_append_blob , 1 );
571580 rb_define_method (cDuckDBAppender , "append_null" , appender_append_null , 0 );
572- rb_define_method (cDuckDBAppender , "flush" , appender_flush , 0 );
573581 rb_define_method (cDuckDBAppender , "close" , appender_close , 0 );
574582
575583#ifdef HAVE_DUCKDB_H_GE_V1_1_0
576584 rb_define_method (cDuckDBAppender , "append_default" , appender_append_default , 0 );
577585#endif
578586
587+ rb_define_private_method (cDuckDBAppender , "_flush" , appender__flush , 0 );
579588 rb_define_private_method (cDuckDBAppender , "_append_date" , appender__append_date , 3 );
580589 rb_define_private_method (cDuckDBAppender , "_append_interval" , appender__append_interval , 3 );
581590 rb_define_private_method (cDuckDBAppender , "_append_time" , appender__append_time , 4 );
0 commit comments