Exemplo n.º 1
0
static VALUE typecast_bind_value(VALUE connection, VALUE r_value) {
  VALUE r_class = rb_obj_class(r_value);
  VALUE oci8_conn = rb_iv_get(connection, "@connection");
  // replace nil value with '' as otherwise OCI8 cannot get bind variable type
  // '' will be inserted as NULL by Oracle
  if (NIL_P(r_value))
    return RUBY_STRING("");
  else if (r_class == rb_cString)
    // if string is longer than 4000 characters then convert to CLOB
    return RSTRING_LEN(r_value) <= 4000 ? r_value : rb_funcall(cOCI8_CLOB, DO_ID_NEW, 2, oci8_conn, r_value);
  else if (r_class == rb_cBigDecimal)
    return rb_funcall(r_value, DO_ID_TO_S, 1, RUBY_STRING("F"));
  else if (r_class == rb_cTrueClass)
    return INT2NUM(1);
  else if (r_class == rb_cFalseClass)
    return INT2NUM(0);
  else if (r_class == rb_cByteArray)
    return rb_funcall(cOCI8_BLOB, DO_ID_NEW, 2, oci8_conn, r_value);
  else if (r_class == rb_cClass)
    return rb_funcall(r_value, DO_ID_TO_S, 0);
  else
    return r_value;
}
Exemplo n.º 2
0
static char * get_uri_option(VALUE query_hash, const char* key) {
  VALUE query_value;
  char * value = NULL;

  if(!rb_obj_is_kind_of(query_hash, rb_cHash)) { return NULL; }

  query_value = rb_hash_aref(query_hash, RUBY_STRING(key));

  if (Qnil != query_value) {
    value = StringValuePtr(query_value);
  }

  return value;
}
Exemplo n.º 3
0
static VALUE cConnection_character_set(VALUE self) {
  VALUE connection_container = rb_iv_get(self, "@connection");
  MYSQL *db;

  const char *encoding;

  if (Qnil == connection_container)
    return Qfalse;

  db = DATA_PTR(connection_container);

  encoding = mysql_character_set_name(db);

  return RUBY_STRING(encoding);
}
Exemplo n.º 4
0
static VALUE cDO_OracleConnection_initialize(VALUE self, VALUE uri) {
  VALUE r_host, r_port, r_path, r_user, r_password;
  VALUE r_query, r_time_zone;
  char *non_blocking = NULL;
  char *time_zone = NULL;
  char set_time_zone_command[80];

  char *host = "localhost", *port = "1521", *path = NULL;
  char *connect_string;
  long connect_string_length;
  VALUE oci8_conn;

  r_user = rb_funcall(uri, rb_intern("user"), 0);
  r_password = rb_funcall(uri, rb_intern("password"), 0);

  r_host = rb_funcall(uri, rb_intern("host"), 0);
  if ( Qnil != r_host && RSTRING_LEN(r_host) > 0) {
    host = StringValuePtr(r_host);
  }

  r_port = rb_funcall(uri, rb_intern("port"), 0);
  if ( Qnil != r_port ) {
    r_port = rb_funcall(r_port, DO_ID_TO_S, 0);
    port = StringValuePtr(r_port);
  }

  r_path = rb_funcall(uri, rb_intern("path"), 0);
  if ( Qnil != r_path ) {
    path = StringValuePtr(r_path);
  }

  // If just host name is specified then use it as TNS names alias
  if ((r_host != Qnil && RSTRING_LEN(r_host) > 0) &&
      (r_port == Qnil) &&
      (r_path == Qnil || RSTRING_LEN(r_path) == 0)) {
    connect_string = host;
  // If database name is specified in path (in format "/database")
  } else if (path != NULL && strlen(path) > 1) {
    connect_string_length = strlen(host) + strlen(port) + strlen(path) + 4;
    connect_string = (char *)calloc(connect_string_length, sizeof(char));
    snprintf(connect_string, connect_string_length, "//%s:%s%s", host, port, path);
  } else {
    rb_raise(eDO_ConnectionError, "Database must be specified");
  }

  // oci8_conn = rb_funcall(cOCI8, DO_ID_NEW, 3, r_user, r_password, RUBY_STRING(connect_string));
  oci8_conn = rb_funcall(cDO_OracleConnection, rb_intern("oci8_new"), 3, r_user, r_password, RUBY_STRING(connect_string));

  // Pull the querystring off the URI
  r_query = rb_funcall(uri, rb_intern("query"), 0);

  non_blocking = get_uri_option(r_query, "non_blocking");
  // Enable non-blocking mode
  if (non_blocking != NULL && strcmp(non_blocking, "true") == 0)
    rb_funcall(oci8_conn, rb_intern("non_blocking="), 1, Qtrue);
  // By default enable auto-commit mode
  rb_funcall(oci8_conn, rb_intern("autocommit="), 1, Qtrue);
  // Set prefetch rows to 100 to increase fetching performance SELECTs with many rows
  rb_funcall(oci8_conn, rb_intern("prefetch_rows="), 1, INT2NUM(100));

  // Set session time zone
  // at first look for option in connection string
  time_zone = get_uri_option(r_query, "time_zone");

  rb_iv_set(self, "@uri", uri);
  rb_iv_set(self, "@connection", oci8_conn);

  // if no option specified then look in ENV['TZ']
  if (time_zone == NULL) {
    r_time_zone = rb_funcall(cDO_OracleConnection, rb_intern("ruby_time_zone"), 0);
    if (!NIL_P(r_time_zone))
      time_zone = StringValuePtr(r_time_zone);
  }
  if (time_zone) {
    snprintf(set_time_zone_command, 80, "alter session set time_zone = '%s'", time_zone);
    execute_sql(self, RUBY_STRING(set_time_zone_command));
  }

  execute_sql(self, RUBY_STRING("alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'"));
  execute_sql(self, RUBY_STRING("alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS.FF'"));
  execute_sql(self, RUBY_STRING("alter session set nls_timestamp_tz_format = 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM'"));

  return Qtrue;
}
Exemplo n.º 5
0
VALUE cCommand_quote_date(VALUE self, VALUE value) {
  return rb_funcall(value, ID_STRFTIME, 1, RUBY_STRING("'%Y-%m-%d'"));
}
Exemplo n.º 6
0
VALUE cCommand_quote_date_time(VALUE self, VALUE value) {
  // TODO: Support non-local dates. we need to call #new_offset on the date to be
  // quoted and pass in the current locale's date offset (self.new_offset((hours * 3600).to_r / 86400)
  return rb_funcall(value, ID_STRFTIME, 1, RUBY_STRING("'%Y-%m-%d %H:%M:%S'"));
}