Class: OracleDB::Stmt

Inherits:
Object
  • Object
show all
Defined in:
lib/oracledb.rb,
ext/oracledb/rboradb_stmt.c

Instance Method Summary collapse

Instance Method Details

#batch_errorsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'ext/oracledb/rboradb_stmt.c', line 106

static VALUE stmt_batch_errors(VALUE self)
{
    Stmt_t *stmt = To_Stmt(self);
    uint32_t i, count;
    dpiErrorInfo *errors;
    VALUE ary, tmp;

    if (dpiStmt_getBatchErrorCount(stmt->handle, &count) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    ary = rb_ary_new_capa(count);
    if (count == 0) {
        return ary;
    }
    errors = RB_ALLOCV_N(dpiErrorInfo, tmp, count);
    if (dpiStmt_getBatchErrors(stmt->handle, count, errors) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    for (i = 0; i < count; i++) {
        rb_ary_push(ary, rboradb_from_dpiErrorInfo(&errors[i]));
    }
    RB_ALLOCV_END(tmp);
    return ary;
}

#bind(key, var = nil, **kw) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/oracledb.rb', line 108

def bind(key, var = nil, **kw)
  if !var.is_a?(Var)
    var = Var.new(self, var, array_size: @array_size, **kw)
  end
  if key.is_a? Numeric
    __bind_by_pos(key, var)
  else
    __bind_by_name(key, var)
  end
  @bind_vars ||= {}
  @bind_vars[key] = var
end

#bind_countObject



131
132
133
134
# File 'ext/oracledb/rboradb_stmt.c', line 131

static VALUE stmt_bind_count(VALUE self)
{
    GET_UINT32(Stmt, BindCount);
}

#bind_namesObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'ext/oracledb/rboradb_stmt.c', line 136

static VALUE stmt_bind_names(VALUE self)
{
    Stmt_t *stmt = To_Stmt(self);
    const char **names;
    uint32_t *name_lengths;
    uint32_t idx, count;
    VALUE bind_names;

    if (dpiStmt_getBindCount(stmt->handle, &count) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    names = ALLOCA_N(const char *, count);
    name_lengths = ALLOCA_N(uint32_t, count);
    if (dpiStmt_getBindNames(stmt->handle, &count, names, name_lengths) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    bind_names = rb_ary_new_capa(count);
    for (idx = 0; idx < count; idx++) {
        rb_ary_push(bind_names, rb_enc_str_new(names[idx], name_lengths[idx], rb_utf8_encoding()));
    }
    return bind_names;
}

#close(*args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ext/oracledb/rboradb_stmt.c', line 92

static VALUE stmt_close(int argc, VALUE *argv, VALUE self)
{
    Stmt_t *stmt = To_Stmt(self);
    VALUE tag;

    rb_scan_args(argc, argv, "01", &tag);
    OptExportString(tag);
    if (dpiStmt_close(stmt->handle, OPT_RSTRING_PTR(tag), OPT_RSTRING_LEN(tag)) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    RB_GC_GUARD(tag);
    return Qnil;
}

#define(pos, var = nil, **kw) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/oracledb.rb', line 121

def define(pos, var = nil, **kw)
  if !var.is_a?(Var)
    var = Var.new(self, var, array_size: @array_size, **kw)
  end
  __define(pos, var)
  @define_vars[pos - 1] = var
end

#execute(mode: nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/oracledb.rb', line 92

def execute(mode: nil)
  @num_query_columns = __execute(mode)
  if @num_query_columns != 0
    @define_vars = Array.new(@num_query_columns)
    if block_given?
      num_fetched = 0
      while row = fetch
        yield(row)
        num_fetched += 1
      end
      return num_fetched
    end
  end
  nil
end

#fetchObject



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/oracledb.rb', line 129

def fetch
  if !@defined && @num_query_columns != 0
    @define_vars.each_index do |idx|
      if @define_vars[idx].nil?
        define(idx + 1, query_info(idx + 1))
      end
    end
  end
  buffer_row_index = __fetch
  buffer_row_index && @define_vars.map do |var|
    var.get(buffer_row_index)
  end
end

#fetch_array_sizeObject



159
160
161
162
# File 'ext/oracledb/rboradb_stmt.c', line 159

static VALUE stmt_fetch_array_size(VALUE self)
{
    return rb_ivar_get(self, id_at_array_size);
}

#fetch_array_size=(array_size) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
# File 'ext/oracledb/rboradb_stmt.c', line 323

static VALUE stmt_set_fetch_array_size(VALUE self, VALUE array_size)
{
    Stmt_t *stmt = To_Stmt(self);
    uint32_t size = NUM2UINT(array_size);

    if (dpiStmt_setFetchArraySize(stmt->handle, size) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    rb_ivar_set(self, id_at_array_size, UINT2NUM(size));
    return Qnil;
}

#implicit_resultObject



164
165
166
167
168
169
170
171
172
173
# File 'ext/oracledb/rboradb_stmt.c', line 164

static VALUE stmt_implicit_result(VALUE self)
{
    Stmt_t *stmt = To_Stmt(self);
    dpiStmt *implicit_result;

    if (dpiStmt_getImplicitResult(stmt->handle, &implicit_result) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    return implicit_result ? rboradb_from_dpiStmt(implicit_result, stmt->dconn, 0, 0) : Qnil;
}

#infoObject



143
144
145
146
# File 'lib/oracledb.rb', line 143

def info
  @info ||= __info
  @info
end

#last_rowidObject



180
181
182
183
184
185
186
187
188
189
# File 'ext/oracledb/rboradb_stmt.c', line 180

static VALUE stmt_last_rowid(VALUE self)
{
    Stmt_t *stmt = To_Stmt(self);
    dpiRowid *rowid;

    if (dpiStmt_getLastRowid(stmt->handle, &rowid) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    return rowid ? rboradb_from_dpiRowid(rowid, stmt->dconn, 1) : Qnil;
}

#num_query_columnsObject



223
224
225
226
227
# File 'ext/oracledb/rboradb_stmt.c', line 223

static VALUE stmt_num_query_columns(VALUE self)
{
    Stmt_t *stmt = To_Stmt(self);
    return UINT2NUM(stmt->num_query_columns);
}

#oci_attr(attr, attr_data_type) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'ext/oracledb/rboradb_stmt.c', line 229

static VALUE stmt_oci_attr(VALUE self, VALUE attr, VALUE attr_data_type)
{
    Stmt_t *stmt = To_Stmt(self);
    OciAttrDataType type = rboradb_to_OciAttrDataType(attr_data_type);
    dpiDataBuffer val;
    uint32_t len;

    if (dpiStmt_getOciAttr(stmt->handle, NUM2UINT(attr), &val, &len) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    switch (type) {
    case RBORADB_OCI_ATTR_DATA_TYPE_BOOLEAN:
        return val.asBoolean ? Qtrue : Qfalse;
    case RBORADB_OCI_ATTR_DATA_TYPE_TEXT:
        return rb_enc_str_new(val.asString, len, rb_utf8_encoding());
    case RBORADB_OCI_ATTR_DATA_TYPE_UB1:
        return INT2FIX(val.asUint8);
    case RBORADB_OCI_ATTR_DATA_TYPE_UB2:
        return INT2FIX(val.asUint16);
    case RBORADB_OCI_ATTR_DATA_TYPE_UB4:
        return UINT2NUM(val.asUint32);
    case RBORADB_OCI_ATTR_DATA_TYPE_UB8:
        return ULL2NUM(val.asUint64);
    }
    return Qnil;
}

#prefetch_rowsObject



256
257
258
259
# File 'ext/oracledb/rboradb_stmt.c', line 256

static VALUE stmt_prefetch_rows(VALUE self)
{
    GET_UINT32(Stmt, PrefetchRows);
}

#prefetch_rows=(obj) ⇒ Object



335
336
337
338
# File 'ext/oracledb/rboradb_stmt.c', line 335

static VALUE stmt_set_prefetch_rows(VALUE self, VALUE obj)
{
    SET_UINT32(Stmt, PrefetchRows);
}

#query_info(pos) ⇒ Object



261
262
263
264
265
266
267
268
269
270
# File 'ext/oracledb/rboradb_stmt.c', line 261

static VALUE stmt_query_info(VALUE self, VALUE pos)
{
    Stmt_t *stmt = To_Stmt(self);
    dpiQueryInfo info;

    if (dpiStmt_getQueryInfo(stmt->handle, NUM2UINT(pos), &info) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    return rboradb_from_dpiQueryInfo(&info, stmt->dconn);
}

#row_countObject



283
284
285
286
# File 'ext/oracledb/rboradb_stmt.c', line 283

static VALUE stmt_row_count(VALUE self)
{
    GET_UINT64(Stmt, RowCount);
}

#row_countsObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'ext/oracledb/rboradb_stmt.c', line 288

static VALUE stmt_row_counts(VALUE self)
{
    Stmt_t *stmt = To_Stmt(self);
    uint32_t i, num_counts;
    uint64_t *counts;
    VALUE ary;

    if (dpiStmt_getRowCounts(stmt->handle, &num_counts, &counts) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    ary = rb_ary_new_capa(num_counts);
    for (i = 0; i < num_counts; i++) {
        rb_ary_push(ary, ULL2NUM(counts[i]));
    }
    return ary;
}

#scroll(*args) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/oracledb/rboradb_stmt.c', line 310

static VALUE stmt_scroll(int argc, VALUE *argv, VALUE self)
{
    Stmt_t *stmt = To_Stmt(self);
    VALUE mode;
    VALUE offset;

    rb_scan_args(argc, argv, "11", &mode, &offset);
    if (rbOraDBStmt_scroll(stmt->dconn->handle, stmt->handle, rboradb_to_dpiFetchMode(mode), NIL_P(offset) ? 0 : NUM2INT(offset), stmt->buffer_row_index) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    return Qnil;
}

#set_oci_attr(attr, attr_data_type, value) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'ext/oracledb/rboradb_stmt.c', line 340

static VALUE stmt_set_oci_attr(VALUE self, VALUE attr, VALUE attr_data_type, VALUE value)
{
    Stmt_t *stmt = To_Stmt(self);
    OciAttrDataType type = rboradb_to_OciAttrDataType(attr_data_type);
    union {
        int boolval;
        char *textval;
        uint8_t ub1val;
        uint16_t ub2val;
        uint32_t ub4val;
        uint64_t ub8val;
    } val;
    uint32_t len = 0;

    switch (type) {
    case RBORADB_OCI_ATTR_DATA_TYPE_BOOLEAN:
        val.boolval = RTEST(value) ? 1 : 0;
        break;
    case RBORADB_OCI_ATTR_DATA_TYPE_TEXT:
        ExportString(value);
        val.textval = RSTRING_PTR(value);
        len = RSTRING_LEN(value);
        break;
    case RBORADB_OCI_ATTR_DATA_TYPE_UB1:
        val.ub1val = NUM2UINT(value);
        break;
    case RBORADB_OCI_ATTR_DATA_TYPE_UB2:
        val.ub2val = NUM2UINT(value);
        break;
    case RBORADB_OCI_ATTR_DATA_TYPE_UB4:
        val.ub4val = NUM2UINT(value);
        break;
    case RBORADB_OCI_ATTR_DATA_TYPE_UB8:
        val.ub8val = NUM2ULL(value);
        break;
    }
    if (dpiStmt_setOciAttr(stmt->handle, NUM2UINT(attr), &val, len) != DPI_SUCCESS) {
        RBORADB_RAISE_ERROR(stmt);
    }
    return Qnil;
}

#subscr_query_idObject



305
306
307
308
# File 'ext/oracledb/rboradb_stmt.c', line 305

static VALUE stmt_subscr_query_id(VALUE self)
{
    GET_UINT64(Stmt, SubscrQueryId);
}