Beispiel #1
0
/**
 * This function provides the execute method for the Transaction class. This
 * method only works when a transaction applies to a single connection.
 *
 * @param  self  A reference to the Transaction object that the execute has
 *               been called for.
 * @param  sql   A reference to the SQL statement to be executed.
 *
 * @return  A reference to a result set of the SQL statement represents a
 *          query, nil otherwise.
 *
 */
static VALUE executeOnTransaction(VALUE self, VALUE sql) {
  VALUE results      = Qnil,
        list         = rb_iv_get(self, "@connections"),
        value        = 0,
        connection   = Qnil,
        statement    = Qnil;
  TransactionHandle *transaction = NULL;
  int size         = 0;

  /* Check that the transaction is active. */
  Data_Get_Struct(self, TransactionHandle, transaction);
  if(transaction->handle == 0) {
    rb_fireruby_raise(NULL, "Executed called on inactive transaction.");
  }

  /* Check that we have only one connection for the transaction. */
  value = rb_funcall(list, rb_intern("size"), 0);
  size  = (TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value));
  if(size > 1) {
    rb_fireruby_raise(NULL,
                      "Execute called on a transaction that spans multiple " \
                      "connections. Unable to determine which connection to " \
                      "execute the SQL statement through.");
  }

  connection = rb_ary_entry(list, 0);
  return rb_execute_sql(connection, sql, rb_ary_new(), self);
}
Beispiel #2
0
/**
 * This function provides the drop method for the Generator class.
 *
 * @param  self  A reference to the Generator object to be dropped.
 *
 * @return  A reference to the Generator object dropped.
 *
 */
static VALUE dropGenerator(int argc, VALUE *argv, VALUE self) {
  VALUE name, connection, transaction = Qnil;
  char sql[100];

  name = rb_iv_get(self, "@name");
  connection = rb_iv_get(self, "@connection");
  rb_scan_args(argc, argv, "01", &transaction);

  sprintf(sql, "DROP GENERATOR %s", StringValuePtr(name));
  rb_execute_sql(connection, rb_str_new2(sql), Qnil, transaction);

  return(self);
}
Beispiel #3
0
VALUE selectGeneratorValue(VALUE self, int step, VALUE transaction) {
  VALUE  result_set = Qnil,
         row        = Qnil,
         connection = rb_iv_get(self, "@connection"),
         name       = rb_iv_get(self, "@name");
  char sql[100];

  sprintf(sql, "SELECT GEN_ID(%s, %d) FROM RDB$DATABASE", StringValuePtr(name), step);
  result_set = rb_execute_sql(connection, rb_str_new2(sql), Qnil, transaction);
  row = rb_funcall(result_set, rb_intern("fetch"), 0);
  rb_funcall(result_set, rb_intern("close"), 0);
  
  row = rb_funcall(row, rb_intern("values"), 0);
  return rb_funcall(row, rb_intern("first"), 0);
}
Beispiel #4
0
/**
 * This function attempts to create a new Generator given a name and database
 * connection. The function provides the create class method for the Generator
 * class.
 *
 * @param  klass       This parameter is ignored.
 * @param  name        A reference to a String containing the name of the new
 *                     generator.
 * @param  connection  A reference to the Connection object to create the new
 *                     generator through.
 *
 * @return  A reference to a Generator object.
 *
 */
static VALUE createGenerator(int argc, VALUE *argv, VALUE klass) {
  VALUE name, connection, transaction = Qnil;
  char sql[100];

  rb_scan_args(argc, argv, "21", &name, &connection, &transaction);
  if(TYPE(name) != T_STRING) {
    rb_fireruby_raise(NULL, "Invalid generator name specified.");
  }

  if(TYPE(connection) != T_DATA &&
     RDATA(connection)->dfree != (RUBY_DATA_FUNC)connectionFree) {
    rb_fireruby_raise(NULL,
                      "Invalid connection specified for generator creation.");
  }

  sprintf(sql, "CREATE GENERATOR %s", StringValuePtr(name));
  rb_execute_sql(connection, rb_str_new2(sql), Qnil, transaction);

  return rb_generator_new(name, connection);
}
Beispiel #5
0
/**
 * This method provides the exists? class method for the Generator class.
 *
 * @param  klass       This parameter is ignored.
 * @param  name        A reference to a String containing the generator name to
 *                     check for.
 * @param  connection  A reference to the Connection object that the check will
 *                     be made through.
 *
 * @return  True if the generator already exists in the database, false
 *          otherwise.
 *
 */
static VALUE doesGeneratorExist(int argc, VALUE *argv, VALUE klass) {
  VALUE name, connection, transaction = Qnil,
        exists = Qfalse,
        result_set = Qnil;
  char sql[200]; // 93(statement) + 2*(31)max_generator_name = 155

  rb_scan_args(argc, argv, "21", &name, &connection, &transaction);
  if(TYPE(connection) != T_DATA ||
     RDATA(connection)->dfree != (RUBY_DATA_FUNC)connectionFree) {
    rb_fireruby_raise(NULL, "Invalid connection specified.");
  }

  sprintf(sql, "SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE RDB$GENERATOR_NAME in ('%s', UPPER('%s'))", StringValuePtr(name), StringValuePtr(name));
  result_set = rb_execute_sql(connection, rb_str_new2(sql), Qnil, transaction);
  if(Qnil != rb_funcall(result_set, rb_intern("fetch"), 0)) {
    exists = Qtrue;
  }
  rb_funcall(result_set, rb_intern("close"), 0);

  return(exists);
}
Beispiel #6
0
/**
 * This function provides the execute_for method for the Connection class.
 *
 * @param  self         A reference to the connection object to perform the
 *                      execution through.
 * @param  sql          A reference to the SQL statement to be executed.
 * @param  params       Array containing parameter values for the statement.
 * @param  transaction  A reference to the transction that the statement will
 *                      be executed under.
 *
 * @return  Either a ResultSet object for a query statement or nil for a
 *          non-query statement.
 *
 */
VALUE executeOnConnectionWithParams(VALUE self, VALUE sql, VALUE params, VALUE transaction) {
  return rb_execute_sql(self, sql, params, transaction);
}