Class: Fb::Database

Inherits:
Data
  • Object
show all
Defined in:
ext/fb/fb_ext.c

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(options) ⇒ Database

Initialize Database with Hash of values:

:database

Full Firebird connection string, e.g. ‘localhost:/var/fbdata/drivertest.fdb’ (required)

:username

database username (default: ‘sysdba’)

:password

database password (default: ‘masterkey’)

:charset

character set to be used with the connection (default: ‘NONE’)

:role

database role to connect using (default: nil)

:downcase_names

Column names are reported in lowercase, unless they were originally mixed case (default: nil).

:page_size

page size to use when creating a database (default: 4096)



2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
# File 'ext/fb/fb_ext.c', line 2774

static VALUE database_initialize(int argc, VALUE *argv, VALUE self)
{
	VALUE parms, database;

	if (argc >= 1) {
		parms = argv[0];
		if (TYPE(parms) == T_STRING) {
			parms = hash_from_connection_string(parms);
		} else {
			Check_Type(parms, T_HASH);
		}
		database = rb_hash_aref(parms, ID2SYM(rb_intern("database")));
		if (NIL_P(database)) rb_raise(rb_eFbError, "Database must be specified.");
		rb_iv_set(self, "@database", database);
		rb_iv_set(self, "@username", default_string(parms, "username", "sysdba"));
		rb_iv_set(self, "@password", default_string(parms, "password", "masterkey"));
		rb_iv_set(self, "@charset", default_string(parms, "charset", "NONE"));
		rb_iv_set(self, "@role", rb_hash_aref(parms, ID2SYM(rb_intern("role"))));
		rb_iv_set(self, "@downcase_names", rb_hash_aref(parms, ID2SYM(rb_intern("downcase_names"))));
		rb_iv_set(self, "@encoding", default_string(parms, "encoding", "ASCII-8BIT"));
		rb_iv_set(self, "@page_size", default_int(parms, "page_size", 4096));
	}
	return self;
}

Instance Attribute Details

#charsetObject

#databaseObject

#downcase_namesObject

#encodingObject

#page_sizeObject

#passwordObject

#roleObject

#usernameObject

Class Method Details

.connect(options) ⇒ Connection .connect(options) {|connection| ... } ⇒ nil

Connect to a database using the options given (see: Database.new for details of options Hash).

If a block is provided, the open connection is passed to it before being automatically closed.

Overloads:

  • .connect(options) ⇒ Connection

    Returns:

  • .connect(options) {|connection| ... } ⇒ nil

    Yields:

    • (connection)

    Returns:

    • (nil)


2902
2903
2904
2905
2906
2907
# File 'ext/fb/fb_ext.c', line 2902

static VALUE database_s_connect(int argc, VALUE *argv, VALUE klass)
{
	VALUE obj = database_allocate_instance(klass);
	database_initialize(argc, argv, obj);
	return database_connect(obj);
}

.create(options) ⇒ Database .create(options) {|connection| ... } ⇒ Database

Create a database using the specified options (see: Database.new for details of options Hash). If a block is provided, an open connection to the new database is passed to it before being automatically closed.

Overloads:



2853
2854
2855
2856
2857
2858
# File 'ext/fb/fb_ext.c', line 2853

static VALUE database_s_create(int argc, VALUE *argv, VALUE klass)
{
	VALUE obj = database_allocate_instance(klass);
	database_initialize(argc, argv, obj);
	return database_create(obj);
}

.drop(options) ⇒ nil

Drop the database specified by the options given (see: Database.new for details of options Hash).

Returns:

  • (nil)


2931
2932
2933
2934
2935
2936
# File 'ext/fb/fb_ext.c', line 2931

static VALUE database_s_drop(int argc, VALUE *argv, VALUE klass)
{
	VALUE obj = database_allocate_instance(klass);
	database_initialize(argc, argv, obj);
	return database_drop(obj);
}

Instance Method Details

#connectConnection #connect {|connection| ... } ⇒ nil

Connect to the database specified by the current database options.

If a block is provided, the open connection is passed to it before being automatically closed.

Overloads:

  • #connectConnection

    Returns:

  • #connect {|connection| ... } ⇒ nil

    Yields:

    • (connection)

    Returns:

    • (nil)


2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
# File 'ext/fb/fb_ext.c', line 2869

static VALUE database_connect(VALUE self)
{
	ISC_STATUS isc_status[20];
	char *dbp;
	long length;
	isc_db_handle handle = 0;
	VALUE database = rb_iv_get(self, "@database");

	Check_Type(database, T_STRING);
	dbp = connection_create_dbp(self, &length);
	isc_attach_database(isc_status, 0, StringValuePtr(database), &handle, length, dbp);
	xfree(dbp);
	fb_error_check(isc_status);
	{
		VALUE connection = connection_create(handle, self);
		if (rb_block_given_p()) {
			return rb_ensure(rb_yield, connection, connection_close, connection);
			return Qnil;
		} else {
			return connection;
		}
	}
}

#createDatabase #create {|connection| ... } ⇒ Database

Create a database using the current database options. If a block is provided, an open connection to the new database is passed to it before being automatically closed.

Overloads:



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
# File 'ext/fb/fb_ext.c', line 2807

static VALUE database_create(VALUE self)
{
	ISC_STATUS isc_status[20];
	isc_db_handle handle = 0;
	isc_tr_handle local_transact = 0;
	VALUE parms, fmt, stmt;
	char *sql;

	VALUE database = rb_iv_get(self, "@database");
	VALUE username = rb_iv_get(self, "@username");
	VALUE password = rb_iv_get(self, "@password");
	VALUE page_size = rb_iv_get(self, "@page_size");
	VALUE charset = rb_iv_get(self, "@charset");

	check_page_size(NUM2INT(page_size));

	parms = rb_ary_new3(5, database, username, password, page_size, charset);

	fmt = rb_str_new2("CREATE DATABASE '%s' USER '%s' PASSWORD '%s' PAGE_SIZE = %d DEFAULT CHARACTER SET %s;");
	stmt = rb_funcall(fmt, rb_intern("%"), 1, parms);
	sql = StringValuePtr(stmt);

	if (isc_dsql_execute_immediate(isc_status, &handle, &local_transact, 0, sql, 3, NULL) != 0) {
		fb_error_check(isc_status);
	}
	if (handle) {
		if (rb_block_given_p()) {
			VALUE connection = connection_create(handle, self);
			rb_ensure(rb_yield,connection,connection_close,connection);
		} else {
			isc_detach_database(isc_status, &handle);
			fb_error_check(isc_status);
		}
	}

	return self;
}

#dropnil

Drop the database specified by the current database options.

Returns:

  • (nil)


2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
# File 'ext/fb/fb_ext.c', line 2914

static VALUE database_drop(VALUE self)
{
	struct FbConnection *fb_connection;

	VALUE connection = database_connect(self);
	Data_Get_Struct(connection, struct FbConnection, fb_connection);
	isc_drop_database(fb_connection->isc_status, &fb_connection->db);
	fb_error_check(fb_connection->isc_status);
	/* fb_connection_remove(fb_connection); */
	return Qnil;
}