Module: Informix::Cursor

Defined in:
lib/informix.rb,
ext/informixc.c

Overview

class Slob

Class Method Summary collapse

Class Method Details

.new(db, query, options = nil) ⇒ Object

Shortcut to create a cursor object based on query using options.

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

options can be a Hash object with the following possible keys:

:scroll => true or false
:hold   => true or false


338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/informix.rb', line 338

def self.new(db, query, options = nil)
  if options
    Hash === options||raise(TypeError,"options must be supplied as a Hash")
  end
  cur = new0(db, query, options)
  return cur unless block_given?
  begin
    yield cur
  ensure
    cur.free
  end
end

.new0(*args) ⇒ Object

The underlying class method that prepares a cursor and creates the respective cursor object.



4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
# File 'ext/informixc.c', line 4107

static VALUE
rb_cursor_s_new0(int argc, VALUE *argv, VALUE self)
{
	VALUE db, query, options, ret;
	VALUE scroll, hold;
	struct sqlda *output;
	cursor_t c, *cur;
	database_t *dbt;
	long id;
/*
 * 	EXEC SQL begin declare section;
 */
#line 3090 "informixc.ec"
#line 3091 "informixc.ec"
  char *c_query;
  char *cid, *sid, *did;
/*
 * 	EXEC SQL end   declare section;
 */
#line 3093 "informixc.ec"


	memset(&c, 0, sizeof(c));
	rb_scan_args(argc, argv, "21", &db, &query, &options);
	Data_Get_Struct(db, database_t, dbt);
	did = dbt->database_id;
/*
 * 	EXEC SQL set connection :did;
 */
#line 3099 "informixc.ec"
  {
#line 3099 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 3099 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

	c.db = db;
	c.database_id = did;
	scroll = hold = Qfalse;
	id = dbt->idcount++;
	snprintf(c.cursor_id, sizeof(c.cursor_id), "CUR%lX", id);
	snprintf(c.stmt_id, sizeof(c.stmt_id), "STMT%lX", id);
	cid = c.cursor_id; sid = c.stmt_id;
	c_query = StringValueCStr(query);

	if (!NIL_P(options)) {
		Check_Type(options, T_HASH);
		scroll = rb_hash_aref(options, sym_scroll);
		hold = rb_hash_aref(options, sym_hold);
	}

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

	if (RTEST(scroll) && RTEST(hold))
/*
 * 		EXEC SQL declare :cid scroll cursor with hold for :sid;
 */
#line 3124 "informixc.ec"
  {
#line 3124 "informixc.ec"
  sqli_curs_decl_dynm(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, cid, 0), cid, sqli_curs_locate(ESQLINTVERSION, sid, 1), 4128, 0);
#line 3124 "informixc.ec"
  }
	else if (RTEST(hold))
/*
 * 		EXEC SQL declare :cid cursor with hold for :sid;
 */
#line 3126 "informixc.ec"
  {
#line 3126 "informixc.ec"
  sqli_curs_decl_dynm(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, cid, 0), cid, sqli_curs_locate(ESQLINTVERSION, sid, 1), 4096, 0);
#line 3126 "informixc.ec"
  }
	else if (RTEST(scroll))
/*
 * 		EXEC SQL declare :cid scroll cursor for :sid;
 */
#line 3128 "informixc.ec"
  {
#line 3128 "informixc.ec"
  sqli_curs_decl_dynm(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, cid, 0), cid, sqli_curs_locate(ESQLINTVERSION, sid, 1), 32, 0);
#line 3128 "informixc.ec"
  }
	else
/*
 * 		EXEC SQL declare :cid cursor for :sid;
 */
#line 3130 "informixc.ec"
  {
#line 3130 "informixc.ec"
  sqli_curs_decl_dynm(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, cid, 0), cid, sqli_curs_locate(ESQLINTVERSION, sid, 1), 0, 0);
#line 3130 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

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

	c.is_select = (SQLCODE == 0 || SQLCODE == SQ_EXECPROC);

	if (c.is_select) {
		alloc_output_slots(&c);
		if (scroll)
			ret = cursorbase_alloc(rb_cScrollCursor);
		else
			ret = cursorbase_alloc(rb_cSequentialCursor);
	}
	else {
		xfree(c.daOutput);
		c.daOutput = NULL;
		ret = cursorbase_alloc(rb_cInsertCursor);
	}
	Data_Get_Struct(ret, cursor_t, cur);
	memcpy(cur, &c, sizeof(cursor_t));

	return ret;
}

.open(db, query, options = nil) ⇒ Object

Shortcut to create and open a cursor object based on query using options in a single step.

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

options can be a Hash object with the following possible keys:

:scroll => true or false
:hold   => true or false
:params => input parameters as an Array or nil


363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/informix.rb', line 363

def self.open(db, query, options = nil)
  params = nil
  if options
    Hash === options||raise(TypeError,"options must be supplied as a Hash")
    (params = options[:params]) && (Array === params ||
               raise(TypeError,"params must be supplied as an Array"))
  end
  cur = new(db, query, options)
  params ? cur.open(*params) : cur.open
  return cur unless block_given?
  begin
    yield cur
  ensure
    cur.free
  end
end