Class: Informix::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/informix.rb,
ext/informixc.c

Overview

class Database

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, query) ⇒ Object

:nodoc:



3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
# File 'ext/informixc.c', line 3077

static VALUE
rb_statement_initialize(VALUE self, VALUE db, VALUE query)
{
	struct sqlda *output;
	cursor_t *c;
	database_t *dbt;
/*
 * 	EXEC SQL begin declare section;
 */
#line 2433 "informixc.ec"
#line 2434 "informixc.ec"
  char *c_query, *sid, *did;
/*
 * 	EXEC SQL end   declare section;
 */
#line 2435 "informixc.ec"


	Data_Get_Struct(db, database_t, dbt);
	did = dbt->database_id;
/*
 * 	EXEC SQL set connection :did;
 */
#line 2439 "informixc.ec"
  {
#line 2439 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 2439 "informixc.ec"
  }
	if (SQLCODE < 0)
		raise_ifx_extended();

	Data_Get_Struct(self, cursor_t, c);
	c->db = db;
	c->database_id = did;
	output = c->daOutput;
	snprintf(c->stmt_id, sizeof(c->stmt_id), "STMT%lX", dbt->idcount++);
	sid = c->stmt_id;
	c_query = StringValueCStr(query);

/*
 * 	EXEC SQL prepare :sid from :c_query;
 */
#line 2451 "informixc.ec"
  {
#line 2451 "informixc.ec"
  sqli_prep(ESQLINTVERSION, sid, c_query,(ifx_literal_t *)0, (ifx_namelist_t *)0, -1, 0, 0 ); 
#line 2451 "informixc.ec"
  }
	if (SQLCODE < 0)
		raise_ifx_extended();

	alloc_input_slots(c, c_query);
/*
 * 	EXEC SQL describe :sid into output;
 */
#line 2456 "informixc.ec"
  {
#line 2456 "informixc.ec"
  sqli_describe_stmt(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, sid, 257), &output, 0);
#line 2456 "informixc.ec"
  }
	c->daOutput = output;

	c->is_select = (SQLCODE == 0 || SQLCODE == SQ_EXECPROC);

	if (c->is_select)
		alloc_output_slots(c);
	else {
		xfree(c->daOutput);
		c->daOutput = NULL;
	}

	return self;
}

Class Method Details

._newObject



265
# File 'lib/informix.rb', line 265

alias _new new

.new(dbname, query) ⇒ Object

Creates a Statement object from query.

The Statement object is passed to the block if it’s given, and automatically dropped when the block terminates, returning the value of the block.

query may contain ‘?’ placeholders for input parameters; it must NOT be a query returning more than one row (use Cursor instead.)



277
278
279
280
281
282
283
284
285
# File 'lib/informix.rb', line 277

def self.new(dbname, query)
  stmt = _new(dbname, query)
  return stmt if !block_given?
  begin
    yield stmt
  ensure
    stmt.drop
  end
end

Instance Method Details

#[](*args) ⇒ Object Also known as: call, execute

st => fixnum or hash

Executes the previously prepared statement, binding params as input parameters.

Returns the record retrieved, in the case of a singleton select, or the number of rows affected, in the case of any other statement.

Examples:

Inserting records:

db.prepare('insert into state values(?, ?)') do |st|
  st.execute('CA', 'California')
  st.call('AZ', 'Arizona')
  st['TX', 'Texas')
end

Selecting one record (returns a hash):

cust = db.prepare('select * from customer where num = 101') do |st|
         st.execute
       end


3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
# File 'ext/informixc.c', line 3176

static VALUE
rb_statement_call(int argc, VALUE *argv, VALUE self)
{
	struct sqlda *input, *output;
	cursor_t *c;
/*
 * 	EXEC SQL begin declare section;
 */
#line 2499 "informixc.ec"
#line 2500 "informixc.ec"
  char *sid, *did;
/*
 * 	EXEC SQL end   declare section;
 */
#line 2501 "informixc.ec"


	Data_Get_Struct(self, cursor_t, c);

	did = c->database_id;
/*
 * 	EXEC SQL set connection :did;
 */
#line 2506 "informixc.ec"
  {
#line 2506 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 2506 "informixc.ec"
  }
	if (SQLCODE < 0)
		raise_ifx_extended();

	output = c->daOutput;
	input = &c->daInput;
	sid = c->stmt_id;

	if (argc != input->sqld)
		rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)",
			argc, input->sqld);

	if (c->is_select) {
		if (argc) {
			bind_input_params(c, argv);
/*
 * 			EXEC SQL execute :sid into descriptor output
 * 				using descriptor input;
 */
#line 2521 "informixc.ec"
  {
#line 2522 "informixc.ec"
  sqli_exec(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, sid, 257), input, (char *)0, (struct value *)0, output, (char *)0, (struct value *)0, 0);
#line 2522 "informixc.ec"
  }
			clean_input_slots(c);
		}
		else
/*
 * 			EXEC SQL execute :sid into descriptor output;
 */
#line 2526 "informixc.ec"
  {
#line 2526 "informixc.ec"
  sqli_exec(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, sid, 257), (ifx_sqlda_t *)0, (char *)0, (struct value *)0, output, (char *)0, (struct value *)0, 0);
#line 2526 "informixc.ec"
  }

		if (SQLCODE < 0)
			raise_ifx_extended();

		if (SQLCODE == SQLNOTFOUND)
			return Qnil;
		return make_result(c, rb_hash_new());
	}
	else {
		if (argc)  {
			bind_input_params(c, argv);
/*
 * 			EXEC SQL execute :sid using descriptor input;
 */
#line 2538 "informixc.ec"
  {
#line 2538 "informixc.ec"
  sqli_exec(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, sid, 257), input, (char *)0, (struct value *)0, (ifx_sqlda_t *)0, (char *)0, (struct value *)0, 0);
#line 2538 "informixc.ec"
  }
			clean_input_slots(c);
		}
		else
/*
 * 			EXEC SQL execute :sid;
 */
#line 2542 "informixc.ec"
  {
#line 2542 "informixc.ec"
  sqli_exec(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, sid, 257), (ifx_sqlda_t *)0, (char *)0, (struct value *)0, (ifx_sqlda_t *)0, (char *)0, (struct value *)0, 0);
#line 2542 "informixc.ec"
  }
	}
	if (SQLCODE < 0)
		raise_ifx_extended();

	return INT2FIX(sqlca.sqlerrd[2]);
}

#dropObject

st.drop

Frees the statement and the memory associated with it.



3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
# File 'ext/informixc.c', line 3286

static VALUE
rb_statement_drop(VALUE self)
{
	cursor_t *c;

	Data_Get_Struct(self, cursor_t, c);
	st_free(c);

	return Qnil;
}