Sha256: 8592f1a75e8fcd99cddbacb3e5a329767af17067c6920ac45fd10a3f852df339

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

#include "query.h"

VALUE query_execute(Query *query) {
  try {
    return UINT2NUM(
      query->bind.size() == 0
        ? query->handle->conn()->execute(query->sql)
        : query->handle->conn()->execute(query->sql, query->bind)
    );
  }
  catch (dbi::Error &e) {
    query->error = e.what();
    return Qfalse;
  }
}

VALUE query_execute_statement(Query *query) {
  try {
    return UINT2NUM(
      query->bind.size() == 0
        ? query->statement->execute()
        : query->statement->execute(query->bind)
    );
  }
  catch (dbi::Error &e) {
    query->error = e.what();
    return Qfalse;
  }
}

void query_bind_values(Query *query, VALUE bind_values) {
  for (int i = 0; i < RARRAY_LEN(bind_values); i++) {
    VALUE bind_value = rb_ary_entry(bind_values, i);

    if (bind_value == Qnil) {
      query->bind.push_back(dbi::PARAM(dbi::null()));
    }
    else if (bind_value == Qtrue) {
      query->bind.push_back(dbi::PARAM("1"));
    }
    else if (bind_value == Qfalse) {
      query->bind.push_back(dbi::PARAM("0"));
    }
    else if (rb_obj_is_kind_of(bind_value, rb_cIO) ==  Qtrue || rb_obj_is_kind_of(bind_value, cStringIO) ==  Qtrue) {
      bind_value = rb_funcall(bind_value, rb_intern("read"), 0);
      query->bind.push_back(dbi::PARAM_BINARY((unsigned char*)RSTRING_PTR(bind_value), RSTRING_LEN(bind_value)));
    }
    else {
      bind_value = TO_S(bind_value);
      if (strcmp(rb_enc_get(bind_value)->name, "UTF-8") != 0)
        bind_value = rb_str_encode(bind_value, rb_str_new2("UTF-8"), 0, Qnil);
      query->bind.push_back(dbi::PARAM((unsigned char*)RSTRING_PTR(bind_value), RSTRING_LEN(bind_value)));
    }
  }
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
swift-0.6.1 ext/query.cc
swift-0.6.0 ext/query.cc