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:



3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
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
# File 'ext/informixc.c', line 3065

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 2421 "informixc.ec"
#line 2422 "informixc.ec"
  char *c_query, *sid, *did;
/*
 * 	EXEC SQL end   declare section;
 */
#line 2423 "informixc.ec"


	Data_Get_Struct(db, database_t, dbt);
	did = dbt->database_id;
/*
 * 	EXEC SQL set connection :did;
 */
#line 2427 "informixc.ec"
  {
#line 2427 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 2427 "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 2439 "informixc.ec"
  {
#line 2439 "informixc.ec"
  sqli_prep(ESQLINTVERSION, sid, c_query,(ifx_literal_t *)0, (ifx_namelist_t *)0, -1, 0, 0 ); 
#line 2439 "informixc.ec"
  }
	if (SQLCODE < 0)
		raise_ifx_extended();

	alloc_input_slots(c, c_query);
/*
 * 	EXEC SQL describe :sid into output;
 */
#line 2444 "informixc.ec"
  {
#line 2444 "informixc.ec"
  sqli_describe_stmt(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, sid, 257), &output, 0);
#line 2444 "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 released 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.free
  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


3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
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
# File 'ext/informixc.c', line 3164

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


	Data_Get_Struct(self, cursor_t, c);

	did = c->database_id;
/*
 * 	EXEC SQL set connection :did;
 */
#line 2494 "informixc.ec"
  {
#line 2494 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 2494 "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 2509 "informixc.ec"
  {
#line 2510 "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 2510 "informixc.ec"
  }
			clean_input_slots(c);
		}
		else
/*
 * 			EXEC SQL execute :sid into descriptor output;
 */
#line 2514 "informixc.ec"
  {
#line 2514 "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 2514 "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 2526 "informixc.ec"
  {
#line 2526 "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 2526 "informixc.ec"
  }
			clean_input_slots(c);
		}
		else
/*
 * 			EXEC SQL execute :sid;
 */
#line 2530 "informixc.ec"
  {
#line 2530 "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 2530 "informixc.ec"
  }
	}
	if (SQLCODE < 0)
		raise_ifx_extended();

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

#freeObject

st.free

Frees the statement and the memory associated with it.



3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
# File 'ext/informixc.c', line 3274

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

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

	return Qnil;
}