Class: Informix::Database

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

Constant Summary collapse

IfxVersion =
rb_struct_define("IfxVersion", "server_type", "major",
"minor", "level", "os", "full", NULL)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

:nodoc:



2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
# File 'ext/informixc.c', line 2257

static VALUE
rb_database_initialize(int argc, VALUE *argv, VALUE self)
{
	VALUE arg[3], version;
	VALUE server_type, major, minor, os, level, full;
	database_t *dbt;

/*
 * 	EXEC SQL begin declare section;
 */
#line 1954 "informixc.ec"
#line 1955 "informixc.ec"
  char *dbname, *user = NULL, *pass = NULL;
  char *did, *cid, *sid;
struct version_t
  {
      char server_type[41], major[3], minor[3], os[3], level[3];
      char full[61];
  }  c_version;
/*
 * 	EXEC SQL end   declare section;
 */
#line 1961 "informixc.ec"


	rb_scan_args(argc, argv, "12", &arg[0], &arg[1], &arg[2]);

	if (NIL_P(arg[0]))
		rb_raise(rb_eProgrammingError, "A database name must be specified");

	Data_Get_Struct(self, database_t, dbt);

	dbname  = StringValueCStr(arg[0]);
	snprintf(dbt->database_id, IDSIZE, "DB%lX", self);
	snprintf(dbt->cursor_id, IDSIZE, "CUR%lX", dbt->idcount);
	snprintf(dbt->stmt_id, IDSIZE, "STMT%lX", dbt->idcount);
	++dbt->idcount;
	did = dbt->database_id;

	if (!NIL_P(arg[1]))
		user = StringValueCStr(arg[1]);

	if (!NIL_P(arg[2]))
		pass = StringValueCStr(arg[2]);

	if (user && pass)
/*
 * 		EXEC SQL connect to :dbname as :did user :user
 * 			using :pass with concurrent transaction;
 */
#line 1984 "informixc.ec"
  {
#line 1985 "informixc.ec"
  ifx_conn_t      *_sqiconn;
  _sqiconn = (ifx_conn_t *)ifx_alloc_conn_user(user, pass);
  sqli_connect_open(ESQLINTVERSION, 0, dbname, did, _sqiconn, 1);
  ifx_free_conn_user(&_sqiconn);
#line 1985 "informixc.ec"
  }
	else
/*
 * 		EXEC SQL connect to :dbname as :did with concurrent transaction;
 */
#line 1987 "informixc.ec"
  {
#line 1987 "informixc.ec"
  sqli_connect_open(ESQLINTVERSION, 0, dbname, did, (ifx_conn_t *)0, 1);
#line 1987 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

	cid = dbt->cursor_id;
	sid = dbt->stmt_id;

/*
 * 	EXEC SQL prepare :sid from
 * 		'select colname, coltype, collength, extended_id,
 * 			type, default, c.colno
 * 		from syscolumns c, outer sysdefaults d
 * 		where c.tabid = ? and c.tabid = d.tabid and c.colno = d.colno
 * 		order by c.colno';
 */
#line 1995 "informixc.ec"
  {
#line 2000 "informixc.ec"
  sqli_prep(ESQLINTVERSION, sid, "select colname, coltype, collength, extended_id,			type, default, c.colno		from syscolumns c, outer sysdefaults d		where c.tabid = ? and c.tabid = d.tabid and c.colno = d.colno		order by c.colno",(ifx_literal_t *)0, (ifx_namelist_t *)0, 2, 0, 0 ); 
#line 2000 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

/*
 * 	EXEC SQL declare :cid cursor for :sid;
 */
#line 2005 "informixc.ec"
  {
#line 2005 "informixc.ec"
  sqli_curs_decl_dynm(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, cid, 0), cid, sqli_curs_locate(ESQLINTVERSION, sid, 1), 0, 0);
#line 2005 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

/*
 * 	EXEC SQL select dbinfo('version', 'server-type'),
 * 					dbinfo('version', 'major'),
 * 					dbinfo('version', 'minor'),
 * 					dbinfo('version', 'os'),
 * 					dbinfo('version', 'level'),
 * 					dbinfo('version', 'full')
 * 			  into :c_version from systables where tabid = 1;
 */
#line 2010 "informixc.ec"
  {
#line 2016 "informixc.ec"
  static const char *sqlcmdtxt[] =
#line 2016 "informixc.ec"
    {
#line 2016 "informixc.ec"
    "select dbinfo ( 'version' , 'server-type' ) , dbinfo ( 'version' , 'major' ) , dbinfo ( 'version' , 'minor' ) , dbinfo ( 'version' , 'os' ) , dbinfo ( 'version' , 'level' ) , dbinfo ( 'version' , 'full' ) from systables where tabid = 1",
    0
    };
#line 2016 "informixc.ec"
static ifx_cursor_t _SQ0 = {0};
  static ifx_sqlvar_t _sqobind[] = 
    {
      { 114, 41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 114, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 114, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 114, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 114, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 114, 61, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
#line 2016 "informixc.ec"
    };
#line 2016 "informixc.ec"
  _sqobind[0].sqldata = (c_version).server_type;
#line 2016 "informixc.ec"
  _sqobind[1].sqldata = (c_version).major;
#line 2016 "informixc.ec"
  _sqobind[2].sqldata = (c_version).minor;
#line 2016 "informixc.ec"
  _sqobind[3].sqldata = (c_version).os;
#line 2016 "informixc.ec"
  _sqobind[4].sqldata = (c_version).level;
#line 2016 "informixc.ec"
  _sqobind[5].sqldata = (c_version).full;
#line 2016 "informixc.ec"
  sqli_slct(ESQLINTVERSION, &_SQ0,sqlcmdtxt,0,(ifx_sqlvar_t *)0,6,_sqobind,0,(ifx_literal_t *)0,(ifx_namelist_t *)0,0);
#line 2016 "informixc.ec"
  }


	OBJ_FREEZE(server_type = rb_str_new2(c_version.server_type));
	OBJ_FREEZE(major = rb_str_new2(c_version.major));
	OBJ_FREEZE(minor = rb_str_new2(c_version.minor));
	OBJ_FREEZE(os = rb_str_new2(c_version.os));
	OBJ_FREEZE(level = rb_str_new2(c_version.level));
	OBJ_FREEZE(full = rb_str_new2(c_version.full));

	version = rb_struct_new(rb_cIfxVersion, server_type, major, minor, os,
							level, full, NULL);
	OBJ_FREEZE(version);
	rb_iv_set(self, "@version", version);

	return self;
}

Instance Attribute Details

#versionObject (readonly)

Exact version of the database server to which a Database object is connected



76
77
78
# File 'lib/informix.rb', line 76

def version
  @version
end

Class Method Details

.open(dbname, user = nil, password = nil) ⇒ Object

Creates a Database object connected to dbname as user with password. If these are not given, connects to dbname as the current user.

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



100
101
102
103
104
105
106
107
108
# File 'lib/informix.rb', line 100

def self.open(dbname, user = nil, password = nil)
  db = new(dbname, user, password)
  return db unless block_given?
  begin
    yield db
  ensure
    db.close
  end
end

Instance Method Details

#closeObject Also known as: disconnect

db.close => db

Disconnects db and returns nil.



2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
# File 'ext/informixc.c', line 2434

static VALUE
rb_database_close(VALUE self)
{
	database_t *dbt;
/*
 * 	EXEC SQL begin declare section;
 */
#line 2044 "informixc.ec"
#line 2045 "informixc.ec"
  char *id;
/*
 * 	EXEC SQL end   declare section;
 */
#line 2046 "informixc.ec"


	Data_Get_Struct(self, database_t, dbt);
	id = dbt->database_id;
/*
 * 	EXEC SQL disconnect :id;
 */
#line 2050 "informixc.ec"
  {
#line 2050 "informixc.ec"
  sqli_connect_close(0, id, 0, 0);
#line 2050 "informixc.ec"
  }

	return Qnil;
}

#columns(tablename) ⇒ Object

db.columns(tablename) => array

Returns an array with information for every column of the given table.



2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
# File 'ext/informixc.c', line 2736

static VALUE
rb_database_columns(VALUE self, VALUE tablename)
{
	VALUE v, column, result;
	char *stype;
	static char *stypes[] = {
		"CHAR", "SMALLINT", "INTEGER", "FLOAT", "SMALLFLOAT", "DECIMAL",
		"SERIAL", "DATE", "MONEY", "NULL", "DATETIME", "BYTE",
		"TEXT", "VARCHAR", "INTERVAL", "NCHAR", "NVARCHAR", "INT8",
		"SERIAL8", "SET", "MULTISET", "LIST", "UNNAMED ROW", "NAMED ROW",
		"VARIABLE-LENGTH OPAQUE TYPE"
	};

	static char *qualifiers[] = {
		"YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND"
	};

	database_t *dbt;
/*
 * 	EXEC SQL begin declare section;
 */
#line 2232 "informixc.ec"
#line 2233 "informixc.ec"
  char *did, *cid;
  char *tabname;
int tabid, xid;
  char colname[129];
short coltype, collength;
  char deftype[2];
  char defvalue[257];
/*
 * 	EXEC SQL end   declare section;
 */
#line 2240 "informixc.ec"


	Data_Get_Struct(self, database_t, dbt);
	did = dbt->database_id;
/*
 * 	EXEC SQL set connection :did;
 */
#line 2244 "informixc.ec"
  {
#line 2244 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 2244 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

	tabname = StringValueCStr(tablename);

/*
 * 	EXEC SQL select tabid into :tabid from systables where tabname = :tabname;
 */
#line 2251 "informixc.ec"
  {
#line 2251 "informixc.ec"
  static const char *sqlcmdtxt[] =
#line 2251 "informixc.ec"
    {
#line 2251 "informixc.ec"
    "select tabid from systables where tabname = ?",
    0
    };
#line 2251 "informixc.ec"
static ifx_cursor_t _SQ0 = {0};
  static ifx_sqlvar_t _sqibind[] = 
    {
      { 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
#line 2251 "informixc.ec"
    };
  static ifx_sqlvar_t _sqobind[] = 
    {
      { 102, sizeof(tabid), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
#line 2251 "informixc.ec"
    };
#line 2251 "informixc.ec"
  _sqibind[0].sqldata = tabname;
#line 2251 "informixc.ec"
  _sqobind[0].sqldata = (char *) &tabid;
#line 2251 "informixc.ec"
  sqli_slct(ESQLINTVERSION, &_SQ0,sqlcmdtxt,1,_sqibind,1,_sqobind,0,(ifx_literal_t *)0,(ifx_namelist_t *)0,0);
#line 2251 "informixc.ec"
  }

	if (SQLCODE == SQLNOTFOUND)
		rb_raise(rb_eProgrammingError, "Table '%s' doesn't exist", tabname);

	result = rb_ary_new();
	cid = dbt->cursor_id;
/*
 * 	EXEC SQL open :cid using :tabid;
 */
#line 2258 "informixc.ec"
  {
#line 2258 "informixc.ec"
  static ifx_sqlvar_t _sqibind[] = 
    {
      { 102, sizeof(tabid), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
#line 2258 "informixc.ec"
    };
  static ifx_sqlda_t _SD0 = { 1, _sqibind, {0}, 1, 0 };
#line 2258 "informixc.ec"
  _sqibind[0].sqldata = (char *) &tabid;
  sqli_curs_open(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, cid, 256), &_SD0, (char *)0, (struct value *)0, 1, 0);
#line 2258 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

	for(;;) {
/*
 * 		EXEC SQL fetch :cid into :colname, :coltype, :collength, :xid,
 * 			:deftype, :defvalue;
 */
#line 2264 "informixc.ec"
  {
#line 2265 "informixc.ec"
  static ifx_sqlvar_t _sqobind[] = 
    {
      { 114, 129, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 101, sizeof(coltype), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 101, sizeof(collength), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 102, sizeof(xid), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 100, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 114, 257, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
#line 2265 "informixc.ec"
    };
  static ifx_sqlda_t _SD0 = { 6, _sqobind, {0}, 6, 0 };
  static _FetchSpec _FS1 = { 0, 1, 0 };
#line 2265 "informixc.ec"
  _sqobind[0].sqldata = colname;
#line 2265 "informixc.ec"
  _sqobind[1].sqldata = (char *) &coltype;
#line 2265 "informixc.ec"
  _sqobind[2].sqldata = (char *) &collength;
#line 2265 "informixc.ec"
  _sqobind[3].sqldata = (char *) &xid;
#line 2265 "informixc.ec"
  _sqobind[4].sqldata = deftype;
#line 2265 "informixc.ec"
  _sqobind[5].sqldata = defvalue;
  sqli_curs_fetch(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, cid, 256), (ifx_sqlda_t *)0, &_SD0, (char *)0, &_FS1);
#line 2265 "informixc.ec"
  }
		if (SQLCODE < 0)
			raise_ifx_extended();

		if (SQLCODE == SQLNOTFOUND)
			break;

		column = rb_hash_new();
		rb_hash_aset(column, sym_name, rb_str_new2(colname));
		rb_hash_aset(column, sym_type, INT2FIX(coltype));
		rb_hash_aset(column, sym_nullable, coltype&0x100? Qfalse: Qtrue);
		rb_hash_aset(column, sym_xid, INT2FIX(xid));

		if ((coltype&0xFF) < 23) {
			stype = coltype == 4118? stypes[23]: stypes[coltype&0xFF];
		}
		else {
			stype = stypes[24];
		}
		rb_hash_aset(column, sym_stype, rb_str_new2(stype));
		rb_hash_aset(column, sym_length, INT2FIX(collength));

		switch(coltype&0xFF) {
		case SQLVCHAR:
		case SQLNVCHAR:
		case SQLMONEY:
		case SQLDECIMAL:
			rb_hash_aset(column, sym_precision, INT2FIX(collength >> 8));
			rb_hash_aset(column, sym_scale, INT2FIX(collength&0xFF));
			break;
		case SQLDATE:
		case SQLDTIME:
		case SQLINTERVAL:
			rb_hash_aset(column, sym_length, INT2FIX(collength >> 8));
			rb_hash_aset(column, sym_precision, INT2FIX((collength&0xF0) >> 4));
			rb_hash_aset(column, sym_scale, INT2FIX(collength&0xF));
			break;
		default:
			rb_hash_aset(column, sym_precision, INT2FIX(0));
			rb_hash_aset(column, sym_scale, INT2FIX(0));
		}

		if (!deftype[0]) {
			v = Qnil;
		}
		else {
			switch(deftype[0]) {
			case 'C': {
				char current[28];
				snprintf(current, sizeof(current), "CURRENT %s TO %s",
					qualifiers[(collength&0xF0) >> 5],
					qualifiers[(collength&0xF)>>1]);
				v = rb_str_new2(current);
				break;
			}
			case 'L':
				switch (coltype & 0xFF) {
				case SQLCHAR:
				case SQLNCHAR:
				case SQLVCHAR:
				case SQLNVCHAR:
					v = rb_str_new2(defvalue);
					break;
				default: {
					char *s = defvalue;
					while(*s++ != ' ');
					if ((coltype&0xFF) == SQLFLOAT ||
						(coltype&0xFF) == SQLSMFLOAT ||
						(coltype&0xFF) == SQLMONEY ||
						(coltype&0xFF) == SQLDECIMAL)
						v = rb_float_new(atof(s));
					else
						v = LONG2FIX(atol(s));
				}
				}
				break;
			case 'N':
				v = rb_str_new2("NULL");
				break;
			case 'T':
				v = rb_str_new2("today");
				break;
			case 'U':
				v = rb_str_new2("user");
				break;
			case 'S':
			default: /* XXX */
				v = Qnil;
			}
		}
		rb_hash_aset(column, sym_default, v);
		rb_ary_push(result, column);
	}

/*
 * 	EXEC SQL close :cid;
 */
#line 2359 "informixc.ec"
  {
#line 2359 "informixc.ec"
  sqli_curs_close(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, cid, 256));
#line 2359 "informixc.ec"
  }

	return result;
}

#commitObject

db.commit => db

Commits a transaction and returns __self__.



2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
# File 'ext/informixc.c', line 2587

static VALUE
rb_database_commit(VALUE self)
{
	database_t *dbt;
/*
 * 	EXEC SQL begin declare section;
 */
#line 2133 "informixc.ec"
#line 2134 "informixc.ec"
  char *did;
/*
 * 	EXEC SQL end   declare section;
 */
#line 2135 "informixc.ec"


	Data_Get_Struct(self, database_t, dbt);
	did = dbt->database_id;
/*
 * 	EXEC SQL set connection :did;
 */
#line 2139 "informixc.ec"
  {
#line 2139 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 2139 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

/*
 * 	EXEC SQL commit;
 */
#line 2144 "informixc.ec"
  {
#line 2144 "informixc.ec"
  sqli_trans_commit();
#line 2144 "informixc.ec"
  }

	return self;
}

#cursor(query, options = nil, &block) ⇒ 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.

query may contain ‘?’ placeholders for input parameters.

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

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

Examples:

This creates a SequentialCursor

cur = db.cursor('select * from orders where order_date > ?')

This creates a ScrollCursor

cur = db.cursor('select * from customer', :scroll => true)

This creates an InsertCursor

cur = db.cursor('insert into stock values(?, ?, ?, ?, ?, ?)')


177
178
179
# File 'lib/informix.rb', line 177

def cursor(query, options = nil, &block)
  Cursor.new(self, query, options, &block)
end

#execute(query, *args) ⇒ Object

Shortcut to create, execute and release a Statement object from query.

query may contain ‘?’ placeholders for input parameters; it cannot be a query returning more than one row (use Database#cursor instead.)

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:

Deleting records:

db.execute('delete from orders where order_date = ?', Date.today)

Updating a record:

db.execute('update items set quantity = ? where item_num = ?', 10, 101)


152
153
154
# File 'lib/informix.rb', line 152

def execute(query, *args)
  Statement.new(self, query) {|stmt| stmt.execute(*args) }
end

#foreach(query, options = nil, &block) ⇒ Object

Shortcut to create, open and iterate a cursor object based on query using options. The records are retrieved as arrays.

The cursor object is passed to the block and automatically released when the block terminates. Returns __self__.

query may contain ‘?’ placeholders for input parameters.

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

Examples:

Iterating over a table:

db.foreach('select * from customer') do |cust|
  # do something with cust
  puts "#{cust[0] cust[1]}"
end

Same thing, using input parameters:

query = 'select * from orders where order_date = ?'
db.foreach(query, :params => [Date.today]) do |order|
  # do something with order
end


207
208
209
210
# File 'lib/informix.rb', line 207

def foreach(query, options = nil, &block)
  Cursor.open(self, query, options) {|cur| cur.each(&block)}
  self
end

#foreach_hash(query, options = nil, &block) ⇒ Object

Similar to Database#foreach, except that retrieves records as hashes instead of arrays.

Examples:

Iterating over a table:

db.foreach_hash('select * from customer') do |cust|
  # do something with cust
  puts "#{cust['fname'] cust['lname']}"
end


222
223
224
225
# File 'lib/informix.rb', line 222

def foreach_hash(query, options = nil, &block)
  Cursor.open(self, query, options) {|cur| cur.each_hash(&block)}
  self
end

#immediate(arg) ⇒ Object

db.immediate(query) => fixnum

Executes query and returns the number of rows affected. query must not return rows. Executes efficiently any DDL (CREATE, DROP, ALTER), DCL (GRANT, REVOKE) and non-parameterized DML (INSERT, UPDATE, DELETE) statements, except SELECT.

Examples:

Granting CONNECT to user:

db.immediate "grant connect to #{user}"

Creating a table:

db.immediate 'create table test(id serial, code char(2), desc varchar(30))'

Deleting records:

db.immediate 'delete from test where id = 7'


2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
# File 'ext/informixc.c', line 2484

static VALUE
rb_database_immediate(VALUE self, VALUE arg)
{
	database_t *dbt;
/*
 * 	EXEC SQL begin declare section;
 */
#line 2078 "informixc.ec"
#line 2079 "informixc.ec"
  char *query, *did;
/*
 * 	EXEC SQL end   declare section;
 */
#line 2080 "informixc.ec"


	Data_Get_Struct(self, database_t, dbt);
	did = dbt->database_id;
/*
 * 	EXEC SQL set connection :did;
 */
#line 2084 "informixc.ec"
  {
#line 2084 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 2084 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

	query = StringValueCStr(arg);
/*
 * 	EXEC SQL execute immediate :query;
 */
#line 2090 "informixc.ec"
  {
#line 2090 "informixc.ec"
  sqli_exec_immed(query);
#line 2090 "informixc.ec"
  }
	if (SQLCODE < 0)
		raise_ifx_extended();

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

#prepare(query, &block) ⇒ Object

Shortcut to create 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 Database#cursor instead.)

Examples:

Preparing a statement:

st = db.prepare('delete from orders where order_date = ?')

Using a block:

query 'update items set quantity = ? where item_num = ?'
db.prepare(query) do |st|
  # do something with st
  # the last expression evaluated will be returned
end


131
132
133
# File 'lib/informix.rb', line 131

def prepare(query, &block)
  Statement.new(self, query, &block)
end

#rollbackObject

db.rollback => db

Rolls back a transaction and returns __self__.



2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
# File 'ext/informixc.c', line 2537

static VALUE
rb_database_rollback(VALUE self)
{
	database_t *dbt;
/*
 * 	EXEC SQL begin declare section;
 */
#line 2107 "informixc.ec"
#line 2108 "informixc.ec"
  char *did;
/*
 * 	EXEC SQL end   declare section;
 */
#line 2109 "informixc.ec"


	Data_Get_Struct(self, database_t, dbt);
	did = dbt->database_id;
/*
 * 	EXEC SQL set connection :did;
 */
#line 2113 "informixc.ec"
  {
#line 2113 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 2113 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

/*
 * 	EXEC SQL rollback;
 */
#line 2118 "informixc.ec"
  {
#line 2118 "informixc.ec"
  sqli_trans_rollback();
#line 2118 "informixc.ec"
  }

	return self;
}

#slob(type = Slob::CLOB, options = nil, &block) ⇒ Object

Shortcut to create a Slob object.

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

type can be Slob::BLOB or Slob::CLOB

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

:sbspace     => Sbspace name
:estbytes    => Estimated size, in bytes
:extsz       => Allocation extent size
:createflags => Create-time flags
:openflags   => Access mode
:maxbytes    => Maximum size
:col_info    => Get the previous values from the column-level storage
                characteristics for the specified database column

Examples:

Creating a CLOB:

slob = db.slob

Creating a BLOB without log and passing it to a block:

slob = db.slob(Slob::BLOB, :createflags=>Slob:NOLOG) do |slob|
  # do something with slob
end


255
256
257
# File 'lib/informix.rb', line 255

def slob(type = Slob::CLOB, options = nil, &block)
  Slob.new(self, type, options, &block)
end

#transactionObject

db.transaction {|db| block } => db

Opens a transaction and executes block, passing __self__ as parameter. If an exception is raised, the transaction is rolled back. It is commited otherwise.

Returns __self__.

Examples:

A bulk insert using an insert cursor. Requires a transaction:

db.transaction do |db|
  db.cursor('insert into stock values(?, ?, ?, ?, ?, ?)') |cur|
    cur.open
    # Loading a file separated by '|'
    File.open(filename).each do |line|
      fields = line.split('|')
      cur.put(*fields)
    end
  end
end


2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
# File 'ext/informixc.c', line 2662

static VALUE
rb_database_transaction(VALUE self)
{
	VALUE ret;
	database_t *dbt;
/*
 * 	EXEC SQL begin declare section;
 */
#line 2185 "informixc.ec"
#line 2186 "informixc.ec"
  char *did;
/*
 * 	EXEC SQL end   declare section;
 */
#line 2187 "informixc.ec"


	Data_Get_Struct(self, database_t, dbt);
	did = dbt->database_id;
/*
 * 	EXEC SQL set connection :did;
 */
#line 2191 "informixc.ec"
  {
#line 2191 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 2191 "informixc.ec"
  }

	if (SQLCODE < 0)
		raise_ifx_extended();

/*
 * 	EXEC SQL commit;
 */
#line 2196 "informixc.ec"
  {
#line 2196 "informixc.ec"
  sqli_trans_commit();
#line 2196 "informixc.ec"
  }
/*
 * 	EXEC SQL begin work;
 */
#line 2197 "informixc.ec"
  {
#line 2197 "informixc.ec"
  sqli_trans_begin2((mint)1);
#line 2197 "informixc.ec"
  }
	ret = rb_rescue(rb_yield, self, database_transfail, self);

	if (ret == Qundef)
		rb_raise(rb_eOperationalError, "Transaction rolled back");

/*
 * 	EXEC SQL commit;
 */
#line 2203 "informixc.ec"
  {
#line 2203 "informixc.ec"
  sqli_trans_commit();
#line 2203 "informixc.ec"
  }

	return self;
}