Class: OCI8
- Defined in:
- lib/oci8/oci8.rb,
lib/oci8/compat.rb,
lib/oci8/object.rb,
lib/oci8/object.rb,
lib/oci8/bindtype.rb,
lib/oci8/datetime.rb,
lib/oci8/metadata.rb,
lib/oci8/oracle_version.rb,
ext/oci8/oci8.c,
ext/oci8/stmt.c
Overview
The class to access Oracle database server.
example:
# output the emp table's content as CSV format.
conn = OCI8.new(username, password)
conn.exec('select * from emp') do |row|
puts row.join(',')
end
# execute PL/SQL block with bind variables.
conn = OCI8.new(username, password)
conn.exec('BEGIN procedure_name(:1, :2); END;',
value_for_the_first_parameter,
value_for_the_second_parameter)
Defined Under Namespace
Modules: BindType, Math, Metadata, Object, Win32Util Classes: BFILE, BLOB, BindArgumentHelper, CLOB, Cursor, NCLOB, NamedCollection, NamedType, OracleVersion, TDO
Constant Summary collapse
- STMT_SELECT =
:select_stmt
- STMT_UPDATE =
:update_stmt
- STMT_DELETE =
:delete_stmt
- STMT_INSERT =
:insert_stmt
- STMT_CREATE =
:create_stmt
- STMT_DROP =
:drop_stmt
- STMT_ALTER =
:alter_stmt
- STMT_BEGIN =
:begin_stmt
- STMT_DECLARE =
:declare_stmt
- RAW =
:raw
- SQLT_CHR =
varchar, varchar2
:varchar2
- SQLT_NUM =
number, double precision, float, real, numeric, int, integer, smallint
:number
- SQLT_LNG =
long
:long
- SQLT_DAT =
date
:date
- SQLT_BIN =
raw
:raw
- SQLT_LBI =
long raw
:long_raw
- SQLT_AFC =
char
:char
- SQLT_IBFLOAT =
binary_float
:binary_float
- SQLT_IBDOUBLE =
binary_double
:binary_double
- SQLT_RDD =
rowid
:rowid
- SQLT_CLOB =
clob
:clob
- SQLT_BLOB =
blob
:blob
- SQLT_BFILE =
bfile
:bfile
- SQLT_RSET =
ref cursor
116
- SQLT_TIMESTAMP =
timestamp
:timestamp
- SQLT_TIMESTAMP_TZ =
timestamp with time zone
:timestamp_tz
- SQLT_INTERVAL_YM =
interval year to month
:interval_ym
- SQLT_INTERVAL_DS =
interval day to second
:interval_ds
- SQLT_TIMESTAMP_LTZ =
timestamp with local time zone
:timestamp_ltz
- SQLT_NAMES =
mapping of sql type number to sql type name.
{}
- OCI_ATTR_DATA_SIZE =
Attribute Types in oci.h
1
- OCI_ATTR_DATA_TYPE =
maximum size of the data
2
- OCI_ATTR_NAME =
the SQL type of the column/argument
4
- OCI_ATTR_PRECISION =
the name of the column/argument
5
- OCI_ATTR_SCALE =
precision if number type
6
- OCI_ATTR_IS_NULL =
scale if number type
7
- OCI_ATTR_TYPE_NAME =
is it null ?
8
- OCI_ATTR_SCHEMA_NAME =
name of the named data type or a package name
9
- OCI_ATTR_SUB_NAME =
the schema name
10
- OCI_ATTR_POSITION =
type name if package private type
11
- OCI_ATTR_PDSCL =
relative position
16
- OCI_ATTR_FSPRECISION =
packed decimal scale
OCI_ATTR_PDSCL
- OCI_ATTR_PDPRC =
fs prec for datetime data types
17
- OCI_ATTR_LFPRECISION =
packed decimal format
OCI_ATTR_PDPRC
- OCI_ATTR_CHARSET_ID =
fs prec for datetime data types
31
- OCI_ATTR_CHARSET_FORM =
Character Set ID
32
- OCI_ATTR_NUM_COLS =
Character Set Form
102
- OCI_ATTR_LIST_COLUMNS =
number of columns
103
- OCI_ATTR_RDBA =
parameter of the column list
104
- OCI_ATTR_CLUSTERED =
DBA of the segment header
105
- OCI_ATTR_PARTITIONED =
whether the table is clustered
106
- OCI_ATTR_INDEX_ONLY =
whether the table is partitioned
107
- OCI_ATTR_LIST_ARGUMENTS =
whether the table is index only
108
- OCI_ATTR_LIST_SUBPROGRAMS =
parameter of the argument list
109
- OCI_ATTR_REF_TDO =
parameter of the subprogram list
110
- OCI_ATTR_LINK =
REF to the type descriptor
111
- OCI_ATTR_MIN =
the database link name
112
- OCI_ATTR_MAX =
minimum value
113
- OCI_ATTR_INCR =
maximum value
114
- OCI_ATTR_CACHE =
increment value
115
- OCI_ATTR_ORDER =
number of sequence numbers cached
116
- OCI_ATTR_HW_MARK =
whether the sequence is ordered
117
- OCI_ATTR_TYPE_SCHEMA =
high-water mark
118
- OCI_ATTR_TIMESTAMP =
type’s schema name
119
- OCI_ATTR_NUM_PARAMS =
timestamp of the object
121
- OCI_ATTR_OBJID =
number of parameters
122
- OCI_ATTR_OVERLOAD_ID =
object id for a table or view
125
- OCI_ATTR_TABLESPACE =
overload ID for funcs and procs
126
- OCI_ATTR_LTYPE =
table name space
128
- OCI_ATTR_IS_TEMPORARY =
list type
130
- OCI_ATTR_IS_TYPED =
whether table is temporary
131
- OCI_ATTR_DURATION =
whether table is typed
132
- OCI_ATTR_IS_INVOKER_RIGHTS =
duration of temporary table
133
- OCI_ATTR_OBJ_NAME =
is invoker rights
134
- OCI_ATTR_OBJ_SCHEMA =
top level schema obj name
135
- OCI_ATTR_OBJ_ID =
schema name
136
- OCI_ATTR_LEVEL =
OCI_ATTR_OVERLOAD = 210 # is this position overloaded
211
- OCI_ATTR_HAS_DEFAULT =
level for structured types
212
- OCI_ATTR_IOMODE =
has a default value
213
- OCI_ATTR_RADIX =
in, out inout
214
- OCI_ATTR_TYPECODE =
OCI_ATTR_NUM_ARGS = 215 # total number of arguments
216
- OCI_ATTR_COLLECTION_TYPECODE =
object or collection
217
- OCI_ATTR_VERSION =
varray or nested table
218
- OCI_ATTR_IS_INCOMPLETE_TYPE =
user assigned version
219
- OCI_ATTR_IS_SYSTEM_TYPE =
is this an incomplete type
220
- OCI_ATTR_IS_PREDEFINED_TYPE =
a system type
221
- OCI_ATTR_IS_TRANSIENT_TYPE =
a predefined type
222
- OCI_ATTR_IS_SYSTEM_GENERATED_TYPE =
a transient type
223
- OCI_ATTR_HAS_NESTED_TABLE =
system generated type
224
- OCI_ATTR_HAS_LOB =
contains nested table attr
225
- OCI_ATTR_HAS_FILE =
has a lob attribute
226
- OCI_ATTR_COLLECTION_ELEMENT =
has a file attribute
227
- OCI_ATTR_NUM_TYPE_ATTRS =
has a collection attribute
228
- OCI_ATTR_LIST_TYPE_ATTRS =
number of attribute types
229
- OCI_ATTR_NUM_TYPE_METHODS =
list of type attributes
230
- OCI_ATTR_LIST_TYPE_METHODS =
number of type methods
231
- OCI_ATTR_MAP_METHOD =
list of type methods
232
- OCI_ATTR_ORDER_METHOD =
map method of type
233
- OCI_ATTR_NUM_ELEMS =
order method of type
234
- OCI_ATTR_ENCAPSULATION =
number of elements
235
- OCI_ATTR_IS_SELFISH =
encapsulation level
236
- OCI_ATTR_HAS_RESULT =
OCI_ATTR_IS_VIRTUAL = 237 # virtual OCI_ATTR_IS_INLINE = 238 # inline OCI_ATTR_IS_CONSTANT = 239 # constant
240
- OCI_ATTR_IS_CONSTRUCTOR =
has result
241
- OCI_ATTR_IS_DESTRUCTOR =
constructor
242
- OCI_ATTR_IS_MAP =
OCI_ATTR_IS_OPERATOR = 243 # operator
244
- OCI_ATTR_IS_ORDER =
a map method
245
- OCI_ATTR_IS_RNDS =
order method
246
- OCI_ATTR_IS_RNPS =
read no data state method
247
- OCI_ATTR_IS_WNDS =
read no process state
248
- OCI_ATTR_IS_WNPS =
write no data state method
249
- OCI_ATTR_IS_SUBTYPE =
write no process state
258
- OCI_ATTR_SUPERTYPE_SCHEMA_NAME =
259
- OCI_ATTR_SUPERTYPE_NAME =
260
- OCI_ATTR_LIST_OBJECTS =
list of objects in schema
261
- OCI_ATTR_NCHARSET_ID =
char set id
262
- OCI_ATTR_LIST_SCHEMAS =
list of schemas
263
- OCI_ATTR_MAX_PROC_LEN =
max procedure length
264
- OCI_ATTR_MAX_COLUMN_LEN =
max column name length
265
- OCI_ATTR_CURSOR_COMMIT_BEHAVIOR =
cursor commit behavior
266
- OCI_ATTR_MAX_CATALOG_NAMELEN =
catalog namelength
267
- OCI_ATTR_CATALOG_LOCATION =
catalog location
268
- OCI_ATTR_SAVEPOINT_SUPPORT =
savepoint support
269
- OCI_ATTR_NOWAIT_SUPPORT =
nowait support
270
- OCI_ATTR_AUTOCOMMIT_DDL =
autocommit DDL
271
- OCI_ATTR_LOCKING_MODE =
locking mode
272
- OCI_ATTR_IS_FINAL_TYPE =
OCI_ATTR_CLIENT_IDENTIFIER = 278 # value of client id to set
279
- OCI_ATTR_IS_INSTANTIABLE_TYPE =
is final type ?
280
- OCI_ATTR_IS_FINAL_METHOD =
is instantiable type ?
281
- OCI_ATTR_IS_INSTANTIABLE_METHOD =
is final method ?
282
- OCI_ATTR_IS_OVERRIDING_METHOD =
is instantiable method ?
283
- OCI_ATTR_CHAR_USED =
OCI_ATTR_DESC_SYNBASE = 284 # Describe the base object
285
- OCI_ATTR_CHAR_SIZE =
char length semantics
286
- OCI_ATTR_CONDITION =
char length
342
- OCI_ATTR_COMMENT =
rule condition
343
- OCI_ATTR_VALUE =
comment
344
- OCI_ATTR_EVAL_CONTEXT_OWNER =
Anydata value
345
- OCI_ATTR_EVAL_CONTEXT_NAME =
eval context owner
346
- OCI_ATTR_EVALUATION_FUNCTION =
eval context name
347
- OCI_ATTR_VAR_TYPE =
eval function name
348
- OCI_ATTR_VAR_VALUE_FUNCTION =
variable type
349
- OCI_ATTR_VAR_METHOD_FUNCTION =
variable value function
350
- OCI_ATTR_LIST_TABLE_ALIASES =
OCI_ATTR_ACTION_CONTEXT = 351 # action context
352
- OCI_ATTR_LIST_VARIABLE_TYPES =
list of table aliases
353
- OCI_ATTR_TABLE_NAME =
list of variable types
356
- OCI_PTYPE_UNK =
OCI Parameter Types
0
- OCI_PTYPE_TABLE =
unknown
1
- OCI_PTYPE_VIEW =
table
2
- OCI_PTYPE_PROC =
view
3
- OCI_PTYPE_FUNC =
procedure
4
- OCI_PTYPE_PKG =
function
5
- OCI_PTYPE_TYPE =
package
6
- OCI_PTYPE_SYN =
user-defined type
7
- OCI_PTYPE_SEQ =
synonym
8
- OCI_PTYPE_COL =
sequence
9
- OCI_PTYPE_ARG =
column
10
- OCI_PTYPE_LIST =
argument
11
- OCI_PTYPE_TYPE_ATTR =
list
12
- OCI_PTYPE_TYPE_COLL =
user-defined type’s attribute
13
- OCI_PTYPE_TYPE_METHOD =
collection type’s element
14
- OCI_PTYPE_TYPE_ARG =
user-defined type’s method
15
- OCI_PTYPE_TYPE_RESULT =
user-defined type method’s arg
16
- OCI_PTYPE_SCHEMA =
user-defined type method’s result
17
- OCI_PTYPE_DATABASE =
schema
18
- OCI_PTYPE_RULE =
database
19
- OCI_PTYPE_RULE_SET =
rule
20
- OCI_PTYPE_EVALUATION_CONTEXT =
rule set
21
- OCI_PTYPE_TABLE_ALIAS =
evaluation context
22
- OCI_PTYPE_VARIABLE_TYPE =
table alias
23
- OCI_PTYPE_NAME_VALUE =
variable type
24
- OCI_LTYPE_UNK =
OCI List Types
0
- OCI_LTYPE_COLUMN =
unknown
1
- OCI_LTYPE_ARG_PROC =
column list
2
- OCI_LTYPE_ARG_FUNC =
procedure argument list
3
- OCI_LTYPE_SUBPRG =
function argument list
4
- OCI_LTYPE_TYPE_ATTR =
subprogram list
5
- OCI_LTYPE_TYPE_METHOD =
type attribute
6
- OCI_LTYPE_TYPE_ARG_PROC =
type method
7
- OCI_LTYPE_TYPE_ARG_FUNC =
type method w/o result argument list
8
- OCI_LTYPE_SCH_OBJ =
type method w/result argument list
9
- OCI_LTYPE_DB_SCH =
schema object list
10
- OCI_LTYPE_TYPE_SUBTYPE =
database schema list
11
- OCI_LTYPE_TABLE_ALIAS =
subtype list
12
- OCI_LTYPE_VARIABLE_TYPE =
table alias list
13
- OCI_LTYPE_NAME_VALUE =
variable type list
14
- OCI_DURATION_INVALID =
OBJECT Duration in oro.h
0xFFFF
- OCI_DURATION_BEGIN =
10
- OCI_DURATION_NULL =
OCI_DURATION_BEGIN - 1
- OCI_DURATION_DEFAULT =
OCI_DURATION_BEGIN - 2
- OCI_DURATION_USER_CALLBACK =
OCI_DURATION_BEGIN - 3
- OCI_DURATION_NEXT =
OCI_DURATION_BEGIN - 4
- OCI_DURATION_SESSION =
OCI_DURATION_BEGIN
- OCI_DURATION_TRANS =
OCI_DURATION_BEGIN + 1
- OCI_DURATION_CALL =
OCI_DURATION_BEGIN + 2
- OCI_DURATION_STATEMENT =
OCI_DURATION_BEGIN + 3
- OCI_DURATION_CALLOUT =
OCI_DURATION_BEGIN + 4
Class Method Summary collapse
-
.encoding ⇒ Object
(new in ruby 1.9).
-
.encoding=(enc) ⇒ Object
(new in ruby 1.9).
Instance Method Summary collapse
- #charset_id2name(csid) ⇒ Object
- #charset_name2id(name) ⇒ Object
-
#describe_any(object_name) ⇒ Object
return a subclass of OCI8::Metadata::Base which has information about object_name.
-
#describe_database(database_name) ⇒ Object
returns a OCI8::Metadata::Database.
-
#describe_function(function_name) ⇒ Object
returns a OCI8::Metadata::Function in the current schema.
-
#describe_package(package_name) ⇒ Object
returns a OCI8::Metadata::Package in the current schema.
-
#describe_procedure(procedure_name) ⇒ Object
returns a OCI8::Metadata::Procedure in the current schema.
-
#describe_schema(schema_name) ⇒ Object
returns a OCI8::Metadata::Schema in the database.
-
#describe_sequence(sequence_name) ⇒ Object
returns a OCI8::Metadata::Sequence in the current schema.
-
#describe_synonym(synonym_name, check_public_also = true) ⇒ Object
returns a OCI8::Metadata::Synonym in the current schema.
-
#describe_table(table_name, table_only = false) ⇒ Object
returns a OCI8::Metadata::Table or a OCI8::Metadata::View.
-
#describe_type(type_name) ⇒ Object
returns a OCI8::Metadata::Type in the current schema.
-
#describe_view(view_name) ⇒ Object
returns a OCI8::Metadata::View in the current schema.
-
#exec(sql, *bindvars) ⇒ Object
Executes the sql statement.
- #get_tdo_by_class(klass) ⇒ Object
- #get_tdo_by_metadata(metadata) ⇒ Object
- #inspect ⇒ Object
-
#oracle_server_version ⇒ Object
:call-seq: oracle_server_version -> oraver.
-
#select_one(sql, *bindvars) ⇒ Object
:call-seq: select_one(sql, *bindvars) -> first_one_row.
- #username ⇒ Object
Methods inherited from OCIHandle
Constructor Details
This class inherits a constructor from OCIHandle
Class Method Details
.encoding ⇒ Object
(new in ruby 1.9)
Returns Oracle client encoding.
String values passed to Oracle, such as SQL statements, bind values etc., are converted from their encoding to the Oracle client encoding.
If Encoding.default_internal
is nil, string values got from Oracle are tagged by OCI8.encoding
. If not nil, they are converted from OCI8.encoding
to Encoding.default_internal
by default.
If it is ‘ASCII-8BIT’, no encoding conversions are done.
157 158 159 160 |
# File 'ext/oci8/encoding.c', line 157
static VALUE oci8_get_encoding(VALUE klass)
{
return rb_enc_from_encoding(oci8_encoding);
}
|
.encoding=(enc) ⇒ Object
(new in ruby 1.9)
Sets Oracle client encoding.
170 171 172 173 174 175 176 177 178 |
# File 'ext/oci8/encoding.c', line 170
static VALUE oci8_set_encoding(VALUE klass, VALUE encoding)
{
if (NIL_P(encoding)) {
oci8_encoding = NULL;
} else {
oci8_encoding = rb_to_encoding(encoding);
}
return encoding;
}
|
Instance Method Details
#charset_id2name(csid) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'ext/oci8/encoding.c', line 35
VALUE oci8_charset_id2name(VALUE svc, VALUE csid)
{
VALUE name = rb_hash_aref(csid2name, csid);
if (!NIL_P(name)) {
return name;
}
Check_Type(csid, T_FIXNUM);
if (have_OCINlsCharSetIdToName) {
/* Oracle 9iR2 or upper */
char buf[OCI_NLS_MAXBUFSZ];
sword rv;
rv = OCINlsCharSetIdToName(oci8_envhp, TO_ORATEXT(buf), sizeof(buf), (ub2)FIX2INT(csid));
if (rv != OCI_SUCCESS) {
return Qnil;
}
name = rb_usascii_str_new_cstr(buf);
} else {
/* Oracle 9iR1 or lower */
oci8_exec_sql_var_t bind_vars[2];
char buf[OCI_NLS_MAXBUFSZ];
ub2 buflen = 0;
int ival = FIX2INT(csid);
/* :name */
bind_vars[0].valuep = buf;
bind_vars[0].value_sz = OCI_NLS_MAXBUFSZ;
bind_vars[0].dty = SQLT_CHR;
bind_vars[0].indp = NULL;
bind_vars[0].alenp = &buflen;
/* :csid */
bind_vars[1].valuep = &ival;
bind_vars[1].value_sz = sizeof(int);
bind_vars[1].dty = SQLT_INT;
bind_vars[1].indp = NULL;
bind_vars[1].alenp = NULL;
/* convert chaset id to charset name by querying Oracle server. */
oci8_exec_sql(oci8_get_svcctx(svc), "BEGIN :name := nls_charset_name(:csid); END;", 0, NULL, 2, bind_vars, 1);
if (buflen == 0) {
return Qnil;
}
name = rb_usascii_str_new(buf, buflen);
}
OBJ_FREEZE(name);
rb_hash_aset(csid2name, csid, name);
rb_hash_aset(csname2id, name, csid);
return name;
}
|
#charset_name2id(name) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'ext/oci8/encoding.c', line 86
static VALUE oci8_charset_name2id(VALUE svc, VALUE name)
{
VALUE csid;
name = rb_funcall(name, id_upcase, 0);
csid = rb_hash_aref(csname2id, StringValue(name));
if (!NIL_P(csid)) {
return csid;
}
if (have_OCINlsCharSetNameToId) {
/* Oracle 9iR2 or upper */
ub2 rv;
rv = OCINlsCharSetNameToId(oci8_envhp, RSTRING_ORATEXT(name));
if (rv == 0) {
return Qnil;
}
csid = INT2FIX(rv);
} else {
/* Oracle 9iR1 or lower */
oci8_exec_sql_var_t bind_vars[2];
int ival;
sb2 ind = 0; /* null indicator */
/* :csid */
bind_vars[0].valuep = &ival;
bind_vars[0].value_sz = sizeof(int);
bind_vars[0].dty = SQLT_INT;
bind_vars[0].indp = &ind;
bind_vars[0].alenp = NULL;
/* :name */
bind_vars[1].valuep = RSTRING_PTR(name);
bind_vars[1].value_sz = RSTRING_LEN(name);
bind_vars[1].dty = SQLT_CHR;
bind_vars[1].indp = NULL;
bind_vars[1].alenp = NULL;
/* convert chaset name to charset id by querying Oracle server. */
oci8_exec_sql(oci8_get_svcctx(svc), "BEGIN :csid := nls_charset_id(:name); END;", 0, NULL, 2, bind_vars, 1);
if (ind) {
return Qnil;
}
csid = INT2FIX(ival);
}
rb_hash_aset(csid2name, csid, name);
rb_hash_aset(csname2id, name, csid);
return csid;
}
|
#describe_any(object_name) ⇒ Object
return a subclass of OCI8::Metadata::Base which has information about object_name. OCI8::Metadata::Table, OCI8::Metadata::View, OCI8::Metadata::Procedure, OCI8::Metadata::Function, OCI8::Metadata::Package, OCI8::Metadata::Type, OCI8::Metadata::Synonym or OCI8::Metadata::Sequence
2014 2015 2016 |
# File 'lib/oci8/metadata.rb', line 2014 def describe_any(object_name) __describe(object_name, OCI8::Metadata::Unknown, true) end |
#describe_database(database_name) ⇒ Object
returns a OCI8::Metadata::Database.
2074 2075 2076 |
# File 'lib/oci8/metadata.rb', line 2074 def describe_database(database_name) __describe(database_name, OCI8::Metadata::Database, false) end |
#describe_function(function_name) ⇒ Object
returns a OCI8::Metadata::Function in the current schema.
2050 2051 2052 |
# File 'lib/oci8/metadata.rb', line 2050 def describe_function(function_name) __describe(function_name, OCI8::Metadata::Function, false) end |
#describe_package(package_name) ⇒ Object
returns a OCI8::Metadata::Package in the current schema.
2054 2055 2056 |
# File 'lib/oci8/metadata.rb', line 2054 def describe_package(package_name) __describe(package_name, OCI8::Metadata::Package, false) end |
#describe_procedure(procedure_name) ⇒ Object
returns a OCI8::Metadata::Procedure in the current schema.
2046 2047 2048 |
# File 'lib/oci8/metadata.rb', line 2046 def describe_procedure(procedure_name) __describe(procedure_name, OCI8::Metadata::Procedure, false) end |
#describe_schema(schema_name) ⇒ Object
returns a OCI8::Metadata::Schema in the database.
2070 2071 2072 |
# File 'lib/oci8/metadata.rb', line 2070 def describe_schema(schema_name) __describe(schema_name, OCI8::Metadata::Schema, false) end |
#describe_sequence(sequence_name) ⇒ Object
returns a OCI8::Metadata::Sequence in the current schema.
2066 2067 2068 |
# File 'lib/oci8/metadata.rb', line 2066 def describe_sequence(sequence_name) __describe(sequence_name, OCI8::Metadata::Sequence, false) end |
#describe_synonym(synonym_name, check_public_also = true) ⇒ Object
returns a OCI8::Metadata::Synonym in the current schema.
2062 2063 2064 |
# File 'lib/oci8/metadata.rb', line 2062 def describe_synonym(synonym_name, check_public_also = true) __describe(synonym_name, OCI8::Metadata::Synonym, check_public_also) end |
#describe_table(table_name, table_only = false) ⇒ Object
returns a OCI8::Metadata::Table or a OCI8::Metadata::View. If the name is a current schema’s synonym name or a public synonym name, it returns a OCI8::Metadata::Table or a OCI8::Metadata::View which the synonym refers.
If the second argument is true, this returns a OCI8::Metadata::Table in the current schema.
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 |
# File 'lib/oci8/metadata.rb', line 2024 def describe_table(table_name, table_only = false) if table_only # check my own tables only. __describe(table_name, OCI8::Metadata::Table, false) else # check tables, views, synonyms and public synonyms. = __describe(table_name, OCI8::Metadata::Unknown, true) case when OCI8::Metadata::Table, OCI8::Metadata::View when OCI8::Metadata::Synonym describe_table(.translated_name) else raise OCIError.new("ORA-04043: object #{table_name} does not exist", 4043) end end end |
#describe_type(type_name) ⇒ Object
returns a OCI8::Metadata::Type in the current schema.
2058 2059 2060 |
# File 'lib/oci8/metadata.rb', line 2058 def describe_type(type_name) __describe(type_name, OCI8::Metadata::Type, false) end |
#describe_view(view_name) ⇒ Object
returns a OCI8::Metadata::View in the current schema.
2042 2043 2044 |
# File 'lib/oci8/metadata.rb', line 2042 def describe_view(view_name) __describe(view_name, OCI8::Metadata::View, false) end |
#exec(sql, *bindvars) ⇒ Object
Executes the sql statement. The type of return value depends on the type of sql statement: select; insert, update and delete; create, alter and drop; and PL/SQL.
When bindvars are specified, they are bound as bind variables before execution.
select statements without block
It returns the instance of OCI8::Cursor.
example:
conn = OCI8.new('scott', 'tiger')
cursor = conn.exec('SELECT * FROM emp')
while r = cursor.fetch()
puts r.join(',')
end
cursor.close
conn.logoff
select statements with a block
It acts as iterator and returns the processed row counts. Fetched data is passed to the block as array. NULL value becomes nil in ruby.
example:
conn = OCI8.new('scott', 'tiger')
num_rows = conn.exec('SELECT * FROM emp') do |r|
puts r.join(',')
end
puts num_rows.to_s + ' rows were processed.'
conn.logoff
PL/SQL block (ruby-oci8 1.0)
It returns the array of bind variables’ values.
example:
conn = OCI8.new('scott', 'tiger')
conn.exec("BEGIN :str := TO_CHAR(:num, 'FM0999'); END;", 'ABCD', 123)
# => ["0123", 123]
conn.logoff
Above example uses two bind variables which names are :str and :num. These initial values are “the string whose width is 4 and whose value is ‘ABCD’” and “the number whose value is 123”. This method returns the array of these bind variables, which may modified by PL/SQL statement. The order of array is same with that of bind variables.
If a block is given, it is ignored.
PL/SQL block (ruby-oci8 2.0)
It returns the number of processed rows.
example:
conn = OCI8.new('scott', 'tiger')
conn.exec("BEGIN :str := TO_CHAR(:num, 'FM0999'); END;", 'ABCD', 123)
# => 1
conn.logoff
If a block is given, the bind variables’ values are passed to the block after executed.
conn = OCI8.new('scott', 'tiger')
conn.exec("BEGIN :str := TO_CHAR(:num, 'FM0999'); END;", 'ABCD', 123) do |str, num|
puts str # => '0123'
puts num # => 123
end
conn.logoff
FYI, the following code do same on ruby-oci8 1.0 and ruby-oci8 2.0.
conn.exec(sql, *bindvars) { |*outvars| outvars }
Other SQL statements
It returns the number of processed rows.
example:
conn = OCI8.new('scott', 'tiger')
num_rows = conn.exec('UPDATE emp SET sal = sal * 1.1')
puts num_rows.to_s + ' rows were updated.'
conn.logoff
example:
conn = OCI8.new('scott', 'tiger')
conn.exec('CREATE TABLE test (col1 CHAR(6))') # => 0
conn.logoff
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/oci8/oci8.rb', line 100 def exec(sql, *bindvars) begin cursor = parse(sql) ret = cursor.exec(*bindvars) case cursor.type when :select_stmt if block_given? cursor.fetch { |row| yield(row) } # for each row ret = cursor.row_count() else ret = cursor cursor = nil # unset cursor to skip cursor.close in ensure block ret end when :begin_stmt, :declare_stmt # PL/SQL block if block_given? ary = [] cursor.keys.sort.each do |key| ary << cursor[key] end yield(*ary) else ret end else ret # number of rows processed end ensure cursor.nil? || cursor.close end end |
#get_tdo_by_class(klass) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/oci8/object.rb', line 8 def get_tdo_by_class(klass) @id_to_tdo ||= {} @name_to_tdo ||= {} tdo = @name_to_tdo[klass.typename] return tdo if tdo = describe_any(klass.typename) if .is_a? OCI8::Metadata::Synonym = describe_any(.translated_name) end unless .is_a? OCI8::Metadata::Type raise "unknown typename #{klass.typename}" end OCI8::TDO.new(self, , klass) end |
#get_tdo_by_metadata(metadata) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/oci8/object.rb', line 24 def () @id_to_tdo ||= {} @name_to_tdo ||= {} tdo = @id_to_tdo[.tdo_id] return tdo if tdo schema_name = .schema_name name = .name full_name = schema_name + '.' + name klass = OCI8::Object::Base.get_class_by_typename(full_name) klass = OCI8::Object::Base.get_class_by_typename(name) if klass.nil? if klass.nil? if schema_name == username eval <<EOS module Object class #{name.downcase.gsub(/(^|_)(.)/) { $2.upcase }} < OCI8::Object::Base set_typename('#{name}') end end EOS klass = OCI8::Object::Base.get_class_by_typename(name) else eval <<EOS module Object module #{schema_name.downcase.gsub(/(^|_)(.)/) { $2.upcase }} class #{name.downcase.gsub(/(^|_)(.)/) { $2.upcase }} < OCI8::Object::Base set_typename('#{full_name}') end end end EOS klass = OCI8::Object::Base.get_class_by_typename(full_name) end end OCI8::TDO.new(self, , klass) end |
#inspect ⇒ Object
155 156 157 |
# File 'lib/oci8/oci8.rb', line 155 def inspect "#<OCI8:#{username}>" end |
#oracle_server_version ⇒ Object
:call-seq:
oracle_server_version -> oraver
Returns an OCI8::OracleVersion of the Oracle server version.
See also: OCI8.oracle_client_version
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/oci8/oci8.rb', line 165 def oracle_server_version unless defined? @oracle_server_version if vernum = oracle_server_vernum # If the Oracle client is Oracle 9i or upper, # get the server version from the OCI function OCIServerRelease. @oracle_server_version = OCI8::OracleVersion.new(vernum) else # Otherwise, get it from v$version. self.exec('select banner from v$version') do |row| if /^Oracle.*?(\d+\.\d+\.\d+\.\d+\.\d+)/ =~ row[0] @oracle_server_version = OCI8::OracleVersion.new($1) break end end end end @oracle_server_version end |
#select_one(sql, *bindvars) ⇒ Object
:call-seq:
select_one(sql, *bindvars) -> first_one_row
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/oci8/oci8.rb', line 135 def select_one(sql, *bindvars) cursor = self.parse(sql) begin cursor.exec(*bindvars) row = cursor.fetch ensure cursor.close end return row end |
#username ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/oci8/oci8.rb', line 146 def username @username || begin exec('select user from dual') do |row| @username = row[0] end @username end end |