Permalink
Browse files

Make structure of C gems consistent over different adapters

  • Loading branch information...
1 parent ccbcc91 commit c3fb421745d8210ef847fca13e85ac57ce9ca0fb @dbussink dbussink committed May 4, 2010
@@ -10,9 +10,9 @@
#include "compat.h"
#include "error.h"
-#define RUBY_CLASS(name) rb_const_get(rb_cObject, rb_intern(name))
-#define DRIVER_CLASS(klass, parent) (rb_define_class_under(mDOMysql, klass, parent))
+
#define CONST_GET(scope, constant) (rb_funcall(scope, ID_CONST_GET, 1, rb_str_new2(constant)))
+#define DRIVER_CLASS(klass, parent) (rb_define_class_under(mMysql, klass, parent))
#define CHECK_AND_RAISE(mysql_result_value, query) if (0 != mysql_result_value) { raise_error(self, db, query); }
#ifdef _WIN32
@@ -55,20 +55,12 @@
// To store rb_intern values
-static ID ID_TO_I;
-static ID ID_TO_F;
-static ID ID_TO_S;
-static ID ID_TO_TIME;
static ID ID_NEW;
static ID ID_NEW_DATE;
static ID ID_CONST_GET;
static ID ID_RATIONAL;
-static ID ID_UTC;
-static ID ID_ESCAPE_SQL;
+static ID ID_ESCAPE;
static ID ID_STRFTIME;
-static ID ID_LOGGER;
-static ID ID_DEBUG;
-static ID ID_LEVEL;
static ID ID_LOG;
// Reference to Extlib module
@@ -92,12 +84,11 @@ static VALUE rb_cBigDecimal;
static VALUE rb_cByteArray;
// Classes that we'll build in Init
-static VALUE mDOMysql;
+static VALUE mMysql;
static VALUE cConnection;
static VALUE cCommand;
static VALUE cResult;
static VALUE cReader;
-static VALUE eArgumentError;
static VALUE eConnectionError;
static VALUE eDataError;
@@ -415,7 +406,7 @@ static char * get_uri_option(VALUE query_hash, const char * key) {
static void assert_file_exists(char * file, const char * message) {
if (file == NULL) { return; }
if (rb_funcall(rb_cFile, rb_intern("exist?"), 1, rb_str_new2(file)) == Qfalse) {
- rb_raise(eArgumentError, "%s", message);
+ rb_raise(rb_eArgError, "%s", message);
}
}
@@ -557,7 +548,7 @@ static void full_connect(VALUE self, MYSQL* db) {
mysql_ssl_set(db, ssl_client_key, ssl_client_cert, ssl_ca_cert, ssl_ca_path, ssl_cipher);
} else if(r_ssl != Qnil) {
- rb_raise(eArgumentError, "ssl must be passed a hash");
+ rb_raise(rb_eArgError, "ssl must be passed a hash");
}
}
#endif
@@ -734,11 +725,11 @@ static VALUE cCommand_set_types(int argc, VALUE *argv, VALUE self) {
if(TYPE(sub_entry) == T_CLASS) {
rb_ary_push(type_strings, sub_entry);
} else {
- rb_raise(eArgumentError, "Invalid type given");
+ rb_raise(rb_eArgError, "Invalid type given");
}
}
} else {
- rb_raise(eArgumentError, "Invalid type given");
+ rb_raise(rb_eArgError, "Invalid type given");
}
}
@@ -795,7 +786,7 @@ static VALUE build_query_from_args(VALUE klass, int count, VALUE *args) {
for ( i = 0; i < count; i++) {
rb_ary_push(array, (VALUE)args[i]);
}
- query = rb_funcall(klass, ID_ESCAPE_SQL, 1, array);
+ query = rb_funcall(klass, ID_ESCAPE, 1, array);
return query;
}
@@ -874,7 +865,7 @@ static VALUE cCommand_execute_reader(int argc, VALUE *argv, VALUE self) {
// Whoops... wrong number of types passed to set_types. Close the reader and raise
// and error
rb_funcall(reader, rb_intern("close"), 0);
- rb_raise(eArgumentError, "Field-count mismatch. Expected %ld fields, but the query yielded %d", RARRAY_LEN(field_types), field_count);
+ rb_raise(rb_eArgError, "Field-count mismatch. Expected %ld fields, but the query yielded %d", RARRAY_LEN(field_types), field_count);
}
for(i = 0; i < field_count; i++) {
@@ -988,13 +979,15 @@ static VALUE cReader_field_count(VALUE self) {
void Init_do_mysql() {
rb_require("bigdecimal");
rb_require("date");
+ rb_require("data_objects");
+
+ ID_CONST_GET = rb_intern("const_get");
- rb_funcall(rb_mKernel, rb_intern("require"), 1, rb_str_new2("data_objects"));
+ // Get references classes needed for Date/Time parsing
+ rb_cDate = CONST_GET(rb_mKernel, "Date");
+ rb_cDateTime = CONST_GET(rb_mKernel, "DateTime");
+ rb_cBigDecimal = CONST_GET(rb_mKernel, "BigDecimal");
- ID_TO_I = rb_intern("to_i");
- ID_TO_F = rb_intern("to_f");
- ID_TO_S = rb_intern("to_s");
- ID_TO_TIME = rb_intern("to_time");
ID_NEW = rb_intern("new");
#ifdef RUBY_LESS_THAN_186
ID_NEW_DATE = rb_intern("new0");
@@ -1003,19 +996,10 @@ void Init_do_mysql() {
#endif
ID_CONST_GET = rb_intern("const_get");
ID_RATIONAL = rb_intern("Rational");
- ID_UTC = rb_intern("utc");
- ID_ESCAPE_SQL = rb_intern("escape_sql");
+ ID_ESCAPE = rb_intern("escape_sql");
ID_STRFTIME = rb_intern("strftime");
- ID_LOGGER = rb_intern("logger");
- ID_DEBUG = rb_intern("debug");
- ID_LEVEL = rb_intern("level");
ID_LOG = rb_intern("log");
- // Store references to a few helpful clases that aren't in Ruby Core
- rb_cDate = RUBY_CLASS("Date");
- rb_cDateTime = RUBY_CLASS("DateTime");
- rb_cBigDecimal = RUBY_CLASS("BigDecimal");
-
// Get references to the Extlib module
mExtlib = CONST_GET(rb_mKernel, "Extlib");
rb_cByteArray = CONST_GET(mExtlib, "ByteArray");
@@ -1031,12 +1015,10 @@ void Init_do_mysql() {
cDO_Logger_Message = CONST_GET(cDO_Logger, "Message");
// Top Level Module that all the classes live under
- mDOMysql = rb_define_module_under(mDO, "Mysql");
-
- eArgumentError = CONST_GET(rb_mKernel, "ArgumentError");
+ mMysql = rb_define_module_under(mDO, "Mysql");
eConnectionError = CONST_GET(mDO, "ConnectionError");
eDataError = CONST_GET(mDO, "DataError");
- mEncoding = rb_define_module_under(mDOMysql, "Encoding");
+ mEncoding = rb_define_module_under(mMysql, "Encoding");
cConnection = DRIVER_CLASS("Connection", cDO_Connection);
rb_define_method(cConnection, "initialize", cConnection_initialize, 1);
@@ -1065,9 +1047,30 @@ void Init_do_mysql() {
rb_define_method(cReader, "fields", cReader_fields, 0);
rb_define_method(cReader, "field_count", cReader_field_count, 0);
+ rb_global_variable(&ID_NEW_DATE);
+ rb_global_variable(&ID_RATIONAL);
+ rb_global_variable(&ID_CONST_GET);
+ rb_global_variable(&ID_ESCAPE);
+ rb_global_variable(&ID_LOG);
+ rb_global_variable(&ID_NEW);
+
+ rb_global_variable(&rb_cDate);
+ rb_global_variable(&rb_cDateTime);
+ rb_global_variable(&rb_cBigDecimal);
+ rb_global_variable(&rb_cByteArray);
+
+ rb_global_variable(&mDO);
+ rb_global_variable(&cDO_Logger_Message);
+
+ rb_global_variable(&cResult);
+ rb_global_variable(&cReader);
+
+ rb_global_variable(&eConnectionError);
+ rb_global_variable(&eDataError);
+
struct errcodes *errs;
for (errs = errors; errs->error_name; errs++) {
- rb_const_set(mDOMysql, rb_intern(errs->error_name), INT2NUM(errs->error_no));
+ rb_const_set(mMysql, rb_intern(errs->error_name), INT2NUM(errs->error_no));
}
}
@@ -35,9 +35,9 @@ def default_prefix
if RUBY_PLATFORM =~ /mswin|mingw/
dir_config('mysql')
have_header 'my_global.h'
- have_header 'mysql.h' || exit(1)
- have_library 'libmysql' || exit(1)
- have_func('mysql_query', 'mysql.h') || exit(1)
+ have_header 'mysql.h'
+ have_library 'libmysql'
+ have_func('mysql_query', 'mysql.h')
have_func('mysql_ssl_set', 'mysql.h')
elsif mc = with_config('mysql-config', default_mysql_config_path)
includes = mysql_config('include').split(/\s+/).map do |dir|
@@ -46,7 +46,15 @@ def default_prefix
libs = mysql_config('libs').split(/\s+/).select {|lib| lib =~ /^-L/}.map do |dir|
dir.gsub(/^-L/, "")
end.uniq
+
+ linked = mysql_config('libs').split(/\s+/).select {|lib| lib =~ /^-l/}.map do |dir|
+ dir.gsub(/^-l/, "")
+ end.uniq
+
dir_config('mysql', includes, libs)
+ linked.each do |link|
+ have_library link
+ end
else
inc, lib = dir_config('mysql', default_prefix)
libs = ['m', 'z', 'socket', 'nsl']
@@ -60,8 +68,7 @@ def default_prefix
unless RUBY_PLATFORM =~ /mswin|mingw/
have_header 'mysql.h'
- have_library 'mysqlclient' || exit(1)
- have_func 'mysql_query' || exit(1)
+ have_func 'mysql_query'
have_func 'mysql_ssl_set'
end
@@ -38,14 +38,8 @@
#include "error.h"
#include "compat.h"
-#define ID_CONST_GET rb_intern("const_get")
-#define ID_PATH rb_intern("path")
-#define ID_NEW rb_intern("new")
-#define ID_ESCAPE rb_intern("escape_sql")
-#define ID_LOG rb_intern("log")
-
#define CONST_GET(scope, constant) (rb_funcall(scope, ID_CONST_GET, 1, rb_str_new2(constant)))
-#define POSTGRES_CLASS(klass, parent) (rb_define_class_under(mPostgres, klass, parent))
+#define DRIVER_CLASS(klass, parent) (rb_define_class_under(mPostgres, klass, parent))
#ifdef HAVE_RUBY_ENCODING_H
#include <ruby/encoding.h>
@@ -80,11 +74,11 @@
// To store rb_intern values
static ID ID_NEW_DATE;
-static ID ID_LOGGER;
-static ID ID_DEBUG;
-static ID ID_LEVEL;
-static ID ID_TO_S;
static ID ID_RATIONAL;
+static ID ID_CONST_GET;
+static ID ID_NEW;
+static ID ID_ESCAPE;
+static ID ID_LOG;
static VALUE mExtlib;
static VALUE mDO;
@@ -108,7 +102,6 @@ static VALUE cCommand;
static VALUE cResult;
static VALUE cReader;
-static VALUE eArgumentError;
static VALUE eConnectionError;
static VALUE eDataError;
@@ -451,11 +444,11 @@ static VALUE cCommand_set_types(int argc, VALUE *argv, VALUE self) {
if(TYPE(sub_entry) == T_CLASS) {
rb_ary_push(type_strings, sub_entry);
} else {
- rb_raise(eArgumentError, "Invalid type given");
+ rb_raise(rb_eArgError, "Invalid type given");
}
}
} else {
- rb_raise(eArgumentError, "Invalid type given");
+ rb_raise(rb_eArgError, "Invalid type given");
}
}
@@ -878,7 +871,7 @@ static VALUE cCommand_execute_reader(int argc, VALUE *argv[], VALUE self) {
// Whoops... wrong number of types passed to set_types. Close the reader and raise
// and error
rb_funcall(reader, rb_intern("close"), 0);
- rb_raise(eArgumentError, "Field-count mismatch. Expected %ld fields, but the query yielded %d", RARRAY_LEN(field_types), field_count);
+ rb_raise(rb_eArgError, "Field-count mismatch. Expected %ld fields, but the query yielded %d", RARRAY_LEN(field_types), field_count);
}
for ( i = 0; i < field_count; i++ ) {
@@ -984,24 +977,24 @@ static VALUE cReader_field_count(VALUE self) {
void Init_do_postgres() {
rb_require("date");
rb_require("bigdecimal");
+ rb_require("data_objects");
+
+ ID_CONST_GET = rb_intern("const_get");
// Get references classes needed for Date/Time parsing
rb_cDate = CONST_GET(rb_mKernel, "Date");
rb_cDateTime = CONST_GET(rb_mKernel, "DateTime");
rb_cBigDecimal = CONST_GET(rb_mKernel, "BigDecimal");
- rb_funcall(rb_mKernel, rb_intern("require"), 1, rb_str_new2("data_objects"));
-
#ifdef RUBY_LESS_THAN_186
ID_NEW_DATE = rb_intern("new0");
#else
ID_NEW_DATE = rb_intern("new!");
#endif
- ID_LOGGER = rb_intern("logger");
- ID_DEBUG = rb_intern("debug");
- ID_LEVEL = rb_intern("level");
- ID_TO_S = rb_intern("to_s");
ID_RATIONAL = rb_intern("Rational");
+ ID_NEW = rb_intern("new");
+ ID_ESCAPE = rb_intern("escape_sql");
+ ID_LOG = rb_intern("log");
// Get references to the Extlib module
mExtlib = CONST_GET(rb_mKernel, "Extlib");
@@ -1017,33 +1010,53 @@ void Init_do_postgres() {
cDO_Logger = CONST_GET(mDO, "Logger");
cDO_Logger_Message = CONST_GET(cDO_Logger, "Message");
- eArgumentError = CONST_GET(rb_mKernel, "ArgumentError");
mPostgres = rb_define_module_under(mDO, "Postgres");
eConnectionError = CONST_GET(mDO, "ConnectionError");
eDataError = CONST_GET(mDO, "DataError");
mEncoding = rb_define_module_under(mPostgres, "Encoding");
- cConnection = POSTGRES_CLASS("Connection", cDO_Connection);
+ cConnection = DRIVER_CLASS("Connection", cDO_Connection);
rb_define_method(cConnection, "initialize", cConnection_initialize, 1);
rb_define_method(cConnection, "dispose", cConnection_dispose, 0);
rb_define_method(cConnection, "character_set", cConnection_character_set , 0);
rb_define_method(cConnection, "quote_string", cConnection_quote_string, 1);
rb_define_method(cConnection, "quote_byte_array", cConnection_quote_byte_array, 1);
- cCommand = POSTGRES_CLASS("Command", cDO_Command);
+ cCommand = DRIVER_CLASS("Command", cDO_Command);
rb_define_method(cCommand, "set_types", cCommand_set_types, -1);
rb_define_method(cCommand, "execute_non_query", cCommand_execute_non_query, -1);
rb_define_method(cCommand, "execute_reader", cCommand_execute_reader, -1);
- cResult = POSTGRES_CLASS("Result", cDO_Result);
+ cResult = DRIVER_CLASS("Result", cDO_Result);
- cReader = POSTGRES_CLASS("Reader", cDO_Reader);
+ cReader = DRIVER_CLASS("Reader", cDO_Reader);
rb_define_method(cReader, "close", cReader_close, 0);
rb_define_method(cReader, "next!", cReader_next, 0);
rb_define_method(cReader, "values", cReader_values, 0);
rb_define_method(cReader, "fields", cReader_fields, 0);
rb_define_method(cReader, "field_count", cReader_field_count, 0);
+ rb_global_variable(&ID_NEW_DATE);
+ rb_global_variable(&ID_RATIONAL);
+ rb_global_variable(&ID_CONST_GET);
+ rb_global_variable(&ID_ESCAPE);
+ rb_global_variable(&ID_LOG);
+ rb_global_variable(&ID_NEW);
+
+ rb_global_variable(&rb_cDate);
+ rb_global_variable(&rb_cDateTime);
+ rb_global_variable(&rb_cBigDecimal);
+ rb_global_variable(&rb_cByteArray);
+
+ rb_global_variable(&mDO);
+ rb_global_variable(&cDO_Logger_Message);
+
+ rb_global_variable(&cResult);
+ rb_global_variable(&cReader);
+
+ rb_global_variable(&eConnectionError);
+ rb_global_variable(&eDataError);
+
struct errcodes *errs;
for (errs = errors; errs->error_name; errs++) {
Oops, something went wrong.

0 comments on commit c3fb421

Please sign in to comment.