@@ -31,20 +31,20 @@ typedef struct rb_sqlite3_protected_funcall_args {
3131static VALUE
3232rb_sqlite3_protected_funcall_body (VALUE protected_funcall_args_ptr )
3333{
34- protected_funcall_args_t * args =
35- (protected_funcall_args_t * )protected_funcall_args_ptr ;
34+ protected_funcall_args_t * args =
35+ (protected_funcall_args_t * )protected_funcall_args_ptr ;
3636
37- return rb_funcall2 (args -> self , args -> method , args -> argc , args -> params );
37+ return rb_funcall2 (args -> self , args -> method , args -> argc , args -> params );
3838}
3939
4040static VALUE
4141rb_sqlite3_protected_funcall (VALUE self , ID method , int argc , VALUE * params ,
42- int * exc_status )
42+ int * exc_status )
4343{
44- protected_funcall_args_t args = {
45- .self = self , .method = method , .argc = argc , .params = params
46- };
47- return rb_protect (rb_sqlite3_protected_funcall_body , (VALUE )(& args ), exc_status );
44+ protected_funcall_args_t args = {
45+ .self = self , .method = method , .argc = argc , .params = params
46+ };
47+ return rb_protect (rb_sqlite3_protected_funcall_body , (VALUE )(& args ), exc_status );
4848}
4949
5050/* called in rb_sqlite3_aggregator_step and rb_sqlite3_aggregator_final. It
@@ -54,36 +54,36 @@ rb_sqlite3_protected_funcall(VALUE self, ID method, int argc, VALUE *params,
5454static VALUE
5555rb_sqlite3_aggregate_instance (sqlite3_context * ctx )
5656{
57- VALUE aw = (VALUE ) sqlite3_user_data (ctx );
58- VALUE handler_klass = rb_iv_get (aw , "-handler_klass" );
59- VALUE inst ;
60- VALUE * inst_ptr = sqlite3_aggregate_context (ctx , (int )sizeof (VALUE ));
57+ VALUE aw = (VALUE ) sqlite3_user_data (ctx );
58+ VALUE handler_klass = rb_iv_get (aw , "-handler_klass" );
59+ VALUE inst ;
60+ VALUE * inst_ptr = sqlite3_aggregate_context (ctx , (int )sizeof (VALUE ));
6161
62- if (!inst_ptr ) {
63- rb_fatal ("SQLite is out-of-merory" );
64- }
62+ if (!inst_ptr ) {
63+ rb_fatal ("SQLite is out-of-merory" );
64+ }
6565
66- inst = * inst_ptr ;
66+ inst = * inst_ptr ;
6767
68- if (inst == Qfalse ) { /* Qfalse == 0 */
69- VALUE instances = rb_iv_get (aw , "-instances" );
70- int exc_status ;
68+ if (inst == Qfalse ) { /* Qfalse == 0 */
69+ VALUE instances = rb_iv_get (aw , "-instances" );
70+ int exc_status ;
7171
72- inst = rb_class_new_instance (0 , NULL , cAggregatorInstance );
73- rb_iv_set (inst , "-handler_instance" , rb_sqlite3_protected_funcall (
74- handler_klass , rb_intern ("new" ), 0 , NULL , & exc_status ));
75- rb_iv_set (inst , "-exc_status" , INT2NUM (exc_status ));
72+ inst = rb_class_new_instance (0 , NULL , cAggregatorInstance );
73+ rb_iv_set (inst , "-handler_instance" , rb_sqlite3_protected_funcall (
74+ handler_klass , rb_intern ("new" ), 0 , NULL , & exc_status ));
75+ rb_iv_set (inst , "-exc_status" , INT2NUM (exc_status ));
7676
77- rb_ary_push (instances , inst );
77+ rb_ary_push (instances , inst );
7878
79- * inst_ptr = inst ;
80- }
79+ * inst_ptr = inst ;
80+ }
8181
82- if (inst == Qnil ) {
83- rb_fatal ("SQLite called us back on an already destroyed aggregate instance" );
84- }
82+ if (inst == Qnil ) {
83+ rb_fatal ("SQLite called us back on an already destroyed aggregate instance" );
84+ }
8585
86- return inst ;
86+ return inst ;
8787}
8888
8989/* called by rb_sqlite3_aggregator_final. Unlinks and frees the
@@ -92,84 +92,84 @@ rb_sqlite3_aggregate_instance(sqlite3_context *ctx)
9292static void
9393rb_sqlite3_aggregate_instance_destroy (sqlite3_context * ctx )
9494{
95- VALUE aw = (VALUE ) sqlite3_user_data (ctx );
96- VALUE instances = rb_iv_get (aw , "-instances" );
97- VALUE * inst_ptr = sqlite3_aggregate_context (ctx , 0 );
98- VALUE inst ;
95+ VALUE aw = (VALUE ) sqlite3_user_data (ctx );
96+ VALUE instances = rb_iv_get (aw , "-instances" );
97+ VALUE * inst_ptr = sqlite3_aggregate_context (ctx , 0 );
98+ VALUE inst ;
9999
100- if (!inst_ptr || (inst = * inst_ptr )) {
101- return ;
102- }
100+ if (!inst_ptr || (inst = * inst_ptr )) {
101+ return ;
102+ }
103103
104- if (inst == Qnil ) {
105- rb_fatal ("attempt to destroy aggregate instance twice" );
106- }
104+ if (inst == Qnil ) {
105+ rb_fatal ("attempt to destroy aggregate instance twice" );
106+ }
107107
108- rb_iv_set (inst , "-handler_instance" , Qnil ); // may catch use-after-free
109- if (rb_ary_delete (instances , inst ) == Qnil ) {
110- rb_fatal ("must be in instances at that point" );
111- }
108+ rb_iv_set (inst , "-handler_instance" , Qnil ); // may catch use-after-free
109+ if (rb_ary_delete (instances , inst ) == Qnil ) {
110+ rb_fatal ("must be in instances at that point" );
111+ }
112112
113- * inst_ptr = Qnil ;
113+ * inst_ptr = Qnil ;
114114}
115115
116116static void
117- rb_sqlite3_aggregator_step (sqlite3_context * ctx , int argc , sqlite3_value * * argv )
117+ rb_sqlite3_aggregator_step (sqlite3_context * ctx , int argc , sqlite3_value * * argv )
118118{
119- VALUE inst = rb_sqlite3_aggregate_instance (ctx );
120- VALUE handler_instance = rb_iv_get (inst , "-handler_instance" );
121- VALUE * params = NULL ;
122- VALUE one_param ;
123- int exc_status = NUM2INT (rb_iv_get (inst , "-exc_status" ));
124- int i ;
125-
126- if (exc_status ) {
127- return ;
128- }
129-
130- if (argc == 1 ) {
131- one_param = sqlite3val2rb (argv [0 ]);
132- params = & one_param ;
133- }
134- if (argc > 1 ) {
135- params = xcalloc ((size_t )argc , sizeof (VALUE ));
136- for (i = 0 ; i < argc ; i ++ ) {
137- params [i ] = sqlite3val2rb (argv [i ]);
119+ VALUE inst = rb_sqlite3_aggregate_instance (ctx );
120+ VALUE handler_instance = rb_iv_get (inst , "-handler_instance" );
121+ VALUE * params = NULL ;
122+ VALUE one_param ;
123+ int exc_status = NUM2INT (rb_iv_get (inst , "-exc_status" ));
124+ int i ;
125+
126+ if (exc_status ) {
127+ return ;
128+ }
129+
130+ if (argc == 1 ) {
131+ one_param = sqlite3val2rb (argv [0 ]);
132+ params = & one_param ;
133+ }
134+ if (argc > 1 ) {
135+ params = xcalloc ((size_t )argc , sizeof (VALUE ));
136+ for (i = 0 ; i < argc ; i ++ ) {
137+ params [i ] = sqlite3val2rb (argv [i ]);
138+ }
139+ }
140+ rb_sqlite3_protected_funcall (
141+ handler_instance , rb_intern ("step" ), argc , params , & exc_status );
142+ if (argc > 1 ) {
143+ xfree (params );
138144 }
139- }
140- rb_sqlite3_protected_funcall (
141- handler_instance , rb_intern ("step" ), argc , params , & exc_status );
142- if (argc > 1 ) {
143- xfree (params );
144- }
145-
146- rb_iv_set (inst , "-exc_status" , INT2NUM (exc_status ));
145+
146+ rb_iv_set (inst , "-exc_status" , INT2NUM (exc_status ));
147147}
148148
149149/* we assume that this function is only called once per execution context */
150150static void
151- rb_sqlite3_aggregator_final (sqlite3_context * ctx )
151+ rb_sqlite3_aggregator_final (sqlite3_context * ctx )
152152{
153- VALUE inst = rb_sqlite3_aggregate_instance (ctx );
154- VALUE handler_instance = rb_iv_get (inst , "-handler_instance" );
155- int exc_status = NUM2INT (rb_iv_get (inst , "-exc_status" ));
153+ VALUE inst = rb_sqlite3_aggregate_instance (ctx );
154+ VALUE handler_instance = rb_iv_get (inst , "-handler_instance" );
155+ int exc_status = NUM2INT (rb_iv_get (inst , "-exc_status" ));
156156
157- if (!exc_status ) {
158- VALUE result = rb_sqlite3_protected_funcall (
159- handler_instance , rb_intern ("finalize" ), 0 , NULL , & exc_status );
160157 if (!exc_status ) {
161- set_sqlite3_func_result (ctx , result );
158+ VALUE result = rb_sqlite3_protected_funcall (
159+ handler_instance , rb_intern ("finalize" ), 0 , NULL , & exc_status );
160+ if (!exc_status ) {
161+ set_sqlite3_func_result (ctx , result );
162+ }
162163 }
163- }
164164
165- if (exc_status ) {
166- /* the user should never see this, as Statement.step() will pick up the
167- * outstanding exception and raise it instead of generating a new one
168- * for SQLITE_ERROR with message "Ruby Exception occurred" */
169- sqlite3_result_error (ctx , "Ruby Exception occurred" , -1 );
170- }
165+ if (exc_status ) {
166+ /* the user should never see this, as Statement.step() will pick up the
167+ * outstanding exception and raise it instead of generating a new one
168+ * for SQLITE_ERROR with message "Ruby Exception occurred" */
169+ sqlite3_result_error (ctx , "Ruby Exception occurred" , -1 );
170+ }
171171
172- rb_sqlite3_aggregate_instance_destroy (ctx );
172+ rb_sqlite3_aggregate_instance_destroy (ctx );
173173}
174174
175175/* call-seq: define_aggregator2(aggregator)
@@ -205,69 +205,69 @@ rb_sqlite3_aggregator_final(sqlite3_context * ctx)
205205VALUE
206206rb_sqlite3_define_aggregator2 (VALUE self , VALUE aggregator , VALUE ruby_name )
207207{
208- /* define_aggregator is added as a method to SQLite3::Database in database.c */
209- sqlite3RubyPtr ctx = sqlite3_database_unwrap (self );
210- int arity , status ;
211- VALUE aw ;
212- VALUE aggregators ;
213-
214- if (!ctx -> db ) {
215- rb_raise (rb_path2class ("SQLite3::Exception" ), "cannot use a closed database" );
216- }
217-
218- if (rb_respond_to (aggregator , rb_intern ("arity" ))) {
219- VALUE ruby_arity = rb_funcall (aggregator , rb_intern ("arity" ), 0 );
220- arity = NUM2INT (ruby_arity );
221- } else {
222- arity = -1 ;
223- }
224-
225- if (arity < -1 || arity > 127 ) {
208+ /* define_aggregator is added as a method to SQLite3::Database in database.c */
209+ sqlite3RubyPtr ctx = sqlite3_database_unwrap (self );
210+ int arity , status ;
211+ VALUE aw ;
212+ VALUE aggregators ;
213+
214+ if (!ctx -> db ) {
215+ rb_raise (rb_path2class ("SQLite3::Exception" ), "cannot use a closed database" );
216+ }
217+
218+ if (rb_respond_to (aggregator , rb_intern ("arity" ))) {
219+ VALUE ruby_arity = rb_funcall (aggregator , rb_intern ("arity" ), 0 );
220+ arity = NUM2INT (ruby_arity );
221+ } else {
222+ arity = -1 ;
223+ }
224+
225+ if (arity < -1 || arity > 127 ) {
226226#ifdef PRIsVALUE
227- rb_raise (rb_eArgError , "%" PRIsVALUE " arity=%d out of range -1..127" ,
228- self , arity );
227+ rb_raise (rb_eArgError , "%" PRIsVALUE " arity=%d out of range -1..127" ,
228+ self , arity );
229229#else
230- rb_raise (rb_eArgError , "Aggregator arity=%d out of range -1..127" , arity );
230+ rb_raise (rb_eArgError , "Aggregator arity=%d out of range -1..127" , arity );
231231#endif
232- }
233-
234- if (!rb_ivar_defined (self , rb_intern ("-aggregators" ))) {
235- rb_iv_set (self , "-aggregators" , rb_ary_new ());
236- }
237- aggregators = rb_iv_get (self , "-aggregators" );
238-
239- aw = rb_class_new_instance (0 , NULL , cAggregatorWrapper );
240- rb_iv_set (aw , "-handler_klass" , aggregator );
241- rb_iv_set (aw , "-instances" , rb_ary_new ());
242-
243- status = sqlite3_create_function (
244- ctx -> db ,
245- StringValueCStr (ruby_name ),
246- arity ,
247- SQLITE_UTF8 ,
248- (void * )aw ,
249- NULL ,
250- rb_sqlite3_aggregator_step ,
251- rb_sqlite3_aggregator_final
252- );
253-
254- if (status != SQLITE_OK ) {
255- rb_sqlite3_raise (ctx -> db , status );
256- return self ; // just in case rb_sqlite3_raise returns.
257- }
258-
259- rb_ary_push (aggregators , aw );
260-
261- return self ;
232+ }
233+
234+ if (!rb_ivar_defined (self , rb_intern ("-aggregators" ))) {
235+ rb_iv_set (self , "-aggregators" , rb_ary_new ());
236+ }
237+ aggregators = rb_iv_get (self , "-aggregators" );
238+
239+ aw = rb_class_new_instance (0 , NULL , cAggregatorWrapper );
240+ rb_iv_set (aw , "-handler_klass" , aggregator );
241+ rb_iv_set (aw , "-instances" , rb_ary_new ());
242+
243+ status = sqlite3_create_function (
244+ ctx -> db ,
245+ StringValueCStr (ruby_name ),
246+ arity ,
247+ SQLITE_UTF8 ,
248+ (void * )aw ,
249+ NULL ,
250+ rb_sqlite3_aggregator_step ,
251+ rb_sqlite3_aggregator_final
252+ );
253+
254+ if (status != SQLITE_OK ) {
255+ rb_sqlite3_raise (ctx -> db , status );
256+ return self ; // just in case rb_sqlite3_raise returns.
257+ }
258+
259+ rb_ary_push (aggregators , aw );
260+
261+ return self ;
262262}
263263
264264void
265265rb_sqlite3_aggregator_init (void )
266266{
267- /* rb_class_new generatos class with undefined allocator in ruby 1.9 */
268- cAggregatorWrapper = rb_funcall (rb_cClass , rb_intern ("new" ), 0 );
269- rb_gc_register_mark_object (cAggregatorWrapper );
267+ /* rb_class_new generatos class with undefined allocator in ruby 1.9 */
268+ cAggregatorWrapper = rb_funcall (rb_cClass , rb_intern ("new" ), 0 );
269+ rb_gc_register_mark_object (cAggregatorWrapper );
270270
271- cAggregatorInstance = rb_funcall (rb_cClass , rb_intern ("new" ), 0 );
272- rb_gc_register_mark_object (cAggregatorInstance );
271+ cAggregatorInstance = rb_funcall (rb_cClass , rb_intern ("new" ), 0 );
272+ rb_gc_register_mark_object (cAggregatorInstance );
273273}
0 commit comments