cubrid 0.6-x86-mswin32-60
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/conn.c +405 -0
- data/ext/cubrid.c +235 -0
- data/ext/cubrid.h +98 -0
- data/ext/error.c +144 -0
- data/ext/extconf.rb +53 -0
- data/ext/oid.c +727 -0
- data/ext/stmt.c +1074 -0
- data/lib/cubrid.so +0 -0
- metadata +62 -0
data/ext/conn.c
ADDED
@@ -0,0 +1,405 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2008 Search Solution Corporation. All rights reserved by Search Solution.
|
3
|
+
*
|
4
|
+
* Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
* are permitted provided that the following conditions are met:
|
6
|
+
*
|
7
|
+
* - Redistributions of source code must retain the above copyright notice,
|
8
|
+
* this list of conditions and the following disclaimer.
|
9
|
+
*
|
10
|
+
* - Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
* this list of conditions and the following disclaimer in the documentation
|
12
|
+
* and/or other materials provided with the distribution.
|
13
|
+
*
|
14
|
+
* - Neither the name of the <ORGANIZATION> nor the names of its contributors
|
15
|
+
* may be used to endorse or promote products derived from this software without
|
16
|
+
* specific prior written permission.
|
17
|
+
*
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
19
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
21
|
+
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
22
|
+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
23
|
+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
24
|
+
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
26
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
27
|
+
* OF SUCH DAMAGE.
|
28
|
+
*
|
29
|
+
*/
|
30
|
+
|
31
|
+
#include "cubrid.h"
|
32
|
+
|
33
|
+
extern VALUE cConnection;
|
34
|
+
|
35
|
+
extern VALUE cubrid_stmt_new(Connection *con, char *stmt, int option);
|
36
|
+
extern VALUE cubrid_stmt_execute(int argc, VALUE* argv, VALUE self);
|
37
|
+
extern VALUE cubrid_stmt_fetch(VALUE self);
|
38
|
+
extern VALUE cubrid_stmt_close(VALUE self);
|
39
|
+
extern VALUE cubrid_oid_new(Connection *con, char *oid_str);
|
40
|
+
|
41
|
+
static void
|
42
|
+
cubrid_conn_free(void *p)
|
43
|
+
{
|
44
|
+
Connection *c = (Connection *)p;
|
45
|
+
T_CCI_ERROR error;
|
46
|
+
|
47
|
+
if (c->handle) {
|
48
|
+
cci_disconnect(c->handle, &error);
|
49
|
+
c->handle = 0;
|
50
|
+
}
|
51
|
+
|
52
|
+
free(p);
|
53
|
+
}
|
54
|
+
|
55
|
+
VALUE
|
56
|
+
cubrid_conn_new(char *host, int port, char *db, char *user, char *passwd)
|
57
|
+
{
|
58
|
+
VALUE conn;
|
59
|
+
Connection *c;
|
60
|
+
int handle;
|
61
|
+
|
62
|
+
handle = cci_connect(host, port, db, user, passwd);
|
63
|
+
if (handle < 0) {
|
64
|
+
cubrid_handle_error(handle, NULL);
|
65
|
+
return Qnil;
|
66
|
+
}
|
67
|
+
|
68
|
+
conn = Data_Make_Struct(cConnection, Connection, 0, cubrid_conn_free, c);
|
69
|
+
|
70
|
+
c->handle = handle;
|
71
|
+
strcpy(c->host, host);
|
72
|
+
c->port = port;
|
73
|
+
strcpy(c->db, db);
|
74
|
+
strcpy(c->user, user);
|
75
|
+
c->auto_commit = Qfalse;
|
76
|
+
|
77
|
+
return conn;
|
78
|
+
}
|
79
|
+
|
80
|
+
/* call-seq:
|
81
|
+
* close() -> nil
|
82
|
+
*
|
83
|
+
* 데이터베이스와의 연결을 헤제합니다. 이 연결로부터 생성된 Statement도 모두 close됩니다.
|
84
|
+
*/
|
85
|
+
VALUE
|
86
|
+
cubrid_conn_close(VALUE self)
|
87
|
+
{
|
88
|
+
Connection *c;
|
89
|
+
T_CCI_ERROR error;
|
90
|
+
|
91
|
+
GET_CONN_STRUCT(self, c);
|
92
|
+
|
93
|
+
if (c->handle) {
|
94
|
+
cci_disconnect(c->handle, &error);
|
95
|
+
c->handle = 0;
|
96
|
+
}
|
97
|
+
|
98
|
+
return Qnil;
|
99
|
+
}
|
100
|
+
|
101
|
+
static VALUE
|
102
|
+
cubrid_conn_prepare_internal(int argc, VALUE* argv, VALUE self)
|
103
|
+
{
|
104
|
+
Connection *con;
|
105
|
+
VALUE sql, option;
|
106
|
+
|
107
|
+
GET_CONN_STRUCT(self, con);
|
108
|
+
CHECK_CONNECTION(con, Qnil);
|
109
|
+
|
110
|
+
rb_scan_args(argc, argv, "11", &sql, &option);
|
111
|
+
|
112
|
+
if (NIL_P(sql)) {
|
113
|
+
rb_raise(rb_eStandardError, "SQL is required.");
|
114
|
+
}
|
115
|
+
|
116
|
+
if (NIL_P(option)) {
|
117
|
+
option = INT2NUM(0);
|
118
|
+
}
|
119
|
+
|
120
|
+
return cubrid_stmt_new(con, StringValueCStr(sql), NUM2INT(option));
|
121
|
+
}
|
122
|
+
|
123
|
+
/* call-seq:
|
124
|
+
* prepare(sql <, option>) -> Statement
|
125
|
+
* prepare(sql <, option>) { |stmt| block } -> nil
|
126
|
+
*
|
127
|
+
* 주어진 SQL을 실행할 준비를 하고 Statement 객체를 반환합니다.
|
128
|
+
* SQL은 데이터베이스 서버로 보내져 파싱되어 실행할 수 있도록 준비됩니다.
|
129
|
+
*
|
130
|
+
* option으로 Cubrid::INCLUDE_OID를 줄 수 있는데, 이것은 SQL 실행 결과에 OID를 포함하도록 합니다.
|
131
|
+
* 실행 결과에 포함된 OID는 Statement.get_oid 메쏘드로 얻을 수 있습니다.
|
132
|
+
*
|
133
|
+
* block 주어지면 생성된 Statement 객체를 인수로 전달하여 block을 실행시킵니다.
|
134
|
+
* block의 수행이 끝나면 Statement 객체는 더이상 유효하지 않습니다.
|
135
|
+
*
|
136
|
+
* con = Cubrid.connect('demodb')
|
137
|
+
* stmt = con.prepare('SELECT * FROM db_user')
|
138
|
+
* stmt.execute
|
139
|
+
* r = stmt.fetch
|
140
|
+
* stmt.close
|
141
|
+
* con.close
|
142
|
+
*
|
143
|
+
* con.prepare('SELECT * FROM db_user') { |stmt|
|
144
|
+
* stmt.execute
|
145
|
+
* r = stmt.fetch
|
146
|
+
* }
|
147
|
+
* con.close
|
148
|
+
*
|
149
|
+
*/
|
150
|
+
VALUE
|
151
|
+
cubrid_conn_prepare(int argc, VALUE* argv, VALUE self)
|
152
|
+
{
|
153
|
+
VALUE stmt;
|
154
|
+
Connection *con;
|
155
|
+
|
156
|
+
GET_CONN_STRUCT(self, con);
|
157
|
+
|
158
|
+
stmt = cubrid_conn_prepare_internal(argc, argv, self);
|
159
|
+
|
160
|
+
if (rb_block_given_p()) {
|
161
|
+
rb_yield(stmt);
|
162
|
+
cubrid_stmt_close(stmt);
|
163
|
+
return Qnil;
|
164
|
+
}
|
165
|
+
|
166
|
+
return stmt;
|
167
|
+
}
|
168
|
+
|
169
|
+
/* call-seq:
|
170
|
+
* query(sql <, option>) -> Statement
|
171
|
+
* query(sql <, option>) { |row| block } -> nil
|
172
|
+
*
|
173
|
+
* 주어진 SQL을 실행할 준비를 하고 실행까지 시킨 후 Statement 객체를 반환합니다.
|
174
|
+
* 따라서 Statement.execute를 수행할 필요없이 바로 결과를 받아올 수 있습니다.
|
175
|
+
*
|
176
|
+
* option으로 Cubrid::INCLUDE_OID를 줄 수 있는데, 이것은 prepare 메쏘드의 그것과 동일합니다.
|
177
|
+
*
|
178
|
+
* block 주어지면 Statement.fetch를 호출하여 얻어온 결과를 인수로 전달하여 block을 실행시킵니다.
|
179
|
+
* block은 SQL 실행 결과로 넘어온 모든 row 대해서 한번씩 호출됩니다.
|
180
|
+
* block이 끝나면 Statement 객체는 더 이상 유효하지 않습니다.
|
181
|
+
*
|
182
|
+
* con = Cubrid.connect('demodb')
|
183
|
+
* stmt = con.query('SELECT * FROM db_user')
|
184
|
+
* while row = stmt.fetch
|
185
|
+
* print row
|
186
|
+
* end
|
187
|
+
* stmt.close
|
188
|
+
* con.close
|
189
|
+
*
|
190
|
+
* stmt = con.query('SELECT * FROM db_user') { |row|
|
191
|
+
* print row
|
192
|
+
* }
|
193
|
+
* con.close
|
194
|
+
*/
|
195
|
+
VALUE
|
196
|
+
cubrid_conn_query(int argc, VALUE* argv, VALUE self)
|
197
|
+
{
|
198
|
+
VALUE stmt;
|
199
|
+
Connection *con;
|
200
|
+
|
201
|
+
GET_CONN_STRUCT(self, con);
|
202
|
+
|
203
|
+
stmt = cubrid_conn_prepare_internal(argc, argv, self);
|
204
|
+
cubrid_stmt_execute(0, NULL, stmt);
|
205
|
+
|
206
|
+
if (rb_block_given_p()) {
|
207
|
+
VALUE row;
|
208
|
+
|
209
|
+
while(1) {
|
210
|
+
row = cubrid_stmt_fetch(stmt);
|
211
|
+
if (NIL_P(row)) {
|
212
|
+
break;
|
213
|
+
}
|
214
|
+
rb_yield(row);
|
215
|
+
}
|
216
|
+
|
217
|
+
cubrid_stmt_close(stmt);
|
218
|
+
return Qnil;
|
219
|
+
}
|
220
|
+
|
221
|
+
return stmt;
|
222
|
+
}
|
223
|
+
|
224
|
+
VALUE
|
225
|
+
cubrid_conn_end_tran(Connection *con, int type)
|
226
|
+
{
|
227
|
+
int res;
|
228
|
+
T_CCI_ERROR error;
|
229
|
+
|
230
|
+
CHECK_CONNECTION(con, Qnil);
|
231
|
+
|
232
|
+
res = cci_end_tran(con->handle, type, &error);
|
233
|
+
if (res < 0){
|
234
|
+
cubrid_handle_error(res, &error);
|
235
|
+
}
|
236
|
+
|
237
|
+
return Qnil;
|
238
|
+
}
|
239
|
+
|
240
|
+
/* call-seq:
|
241
|
+
* commit() -> nil
|
242
|
+
*
|
243
|
+
* 트랜잭션을 commit으로 종료합니다.
|
244
|
+
* 트랜잭션이 종료되면 이 연결로 부터 생성된 모든 Statement 객체도 모두 close 됩니다.
|
245
|
+
*
|
246
|
+
*/
|
247
|
+
VALUE
|
248
|
+
cubrid_conn_commit(VALUE self)
|
249
|
+
{
|
250
|
+
Connection *con;
|
251
|
+
|
252
|
+
GET_CONN_STRUCT(self, con);
|
253
|
+
cubrid_conn_end_tran(con, CCI_TRAN_COMMIT);
|
254
|
+
|
255
|
+
return Qnil;
|
256
|
+
}
|
257
|
+
|
258
|
+
/* call-seq:
|
259
|
+
* rollback() -> nil
|
260
|
+
*
|
261
|
+
* 트랜잭션을 rollback으로 종료합니다.
|
262
|
+
* 트랜잭션이 종료되면 이 연결로 부터 생성된 모든 Statement 객체도 모두 close 됩니다.
|
263
|
+
*
|
264
|
+
*/
|
265
|
+
VALUE
|
266
|
+
cubrid_conn_rollback(VALUE self)
|
267
|
+
{
|
268
|
+
Connection *con;
|
269
|
+
|
270
|
+
GET_CONN_STRUCT(self, con);
|
271
|
+
cubrid_conn_end_tran(con, CCI_TRAN_ROLLBACK);
|
272
|
+
|
273
|
+
return Qnil;
|
274
|
+
}
|
275
|
+
|
276
|
+
/* call-seq:
|
277
|
+
* auto_commit? -> true or false
|
278
|
+
*
|
279
|
+
* Connection이 auto commit 모드인지 아닌지를 반환합니다.
|
280
|
+
* Connection은 기본적으로 auto commit 모드가 아니며, auto_commit= 메쏘드로 auto commit 여부를 설정할 수 있습니다.
|
281
|
+
*
|
282
|
+
*/
|
283
|
+
VALUE
|
284
|
+
cubrid_conn_get_auto_commit(VALUE self)
|
285
|
+
{
|
286
|
+
Connection *con;
|
287
|
+
|
288
|
+
GET_CONN_STRUCT(self, con);
|
289
|
+
CHECK_CONNECTION(con, Qnil);
|
290
|
+
|
291
|
+
return con->auto_commit;
|
292
|
+
}
|
293
|
+
|
294
|
+
/* call-seq:
|
295
|
+
* auto_commit= true or false -> nil
|
296
|
+
*
|
297
|
+
* Connection의 auto commit 모드를 설정합니다.
|
298
|
+
* auto commit이 true로 설정되면 Statement.execute의 실행이 끝난 후 바로 commit이 실행됩니다.
|
299
|
+
*
|
300
|
+
*/
|
301
|
+
VALUE
|
302
|
+
cubrid_conn_set_auto_commit(VALUE self, VALUE auto_commit)
|
303
|
+
{
|
304
|
+
Connection *con;
|
305
|
+
|
306
|
+
GET_CONN_STRUCT(self, con);
|
307
|
+
CHECK_CONNECTION(con, self);
|
308
|
+
|
309
|
+
con->auto_commit = auto_commit;
|
310
|
+
return Qnil;
|
311
|
+
}
|
312
|
+
|
313
|
+
/* call-seq:
|
314
|
+
* to_s() -> string
|
315
|
+
*
|
316
|
+
* Connection의 현재 연결 정보를 문자열로 반환합니다.
|
317
|
+
*/
|
318
|
+
VALUE
|
319
|
+
cubrid_conn_to_s(VALUE self)
|
320
|
+
{
|
321
|
+
char buf[MAX_STR_LEN];
|
322
|
+
Connection *con;
|
323
|
+
|
324
|
+
GET_CONN_STRUCT(self, con);
|
325
|
+
sprintf(buf, "host: %s, port: %d, db: %s, user: %s", con->host, con->port, con->db, con->user);
|
326
|
+
|
327
|
+
return rb_str_new2(buf);
|
328
|
+
}
|
329
|
+
|
330
|
+
/* call-seq:
|
331
|
+
* glo_new(classname <, filename>) -> Oid
|
332
|
+
*
|
333
|
+
* 새로운 GLO 객체를 생성하고 Oid로 반환합니다.
|
334
|
+
* CUBRID는 바이너리 데이터를 저장할 수 있도록 GLO를 제공합니다. GLO 객체는 OID로 직접 접근할 수 있습니다.
|
335
|
+
*
|
336
|
+
* filename이 주어지면 해당 파일의 데이터를 데이터베이스에 저장합니다. 주어지지 않으면 빈 GLO 객체를 생성합니다.
|
337
|
+
*
|
338
|
+
* con = Cubrid.connect('subway')
|
339
|
+
* con.query('create table attachfile under glo (name string)')
|
340
|
+
* con.commit
|
341
|
+
*
|
342
|
+
* glo = con.glo_new('attachfile', 'pic.jpg')
|
343
|
+
* glo.glo_size #=> 1234
|
344
|
+
*
|
345
|
+
* glo = con.glo_new('attachfile')
|
346
|
+
* glo.glo_size #=> 0
|
347
|
+
* glo.glo_save('pic.jpg')
|
348
|
+
* glo.glo_size #=> 1234
|
349
|
+
*/
|
350
|
+
VALUE
|
351
|
+
cubrid_conn_glo_new(VALUE self, VALUE table, VALUE file)
|
352
|
+
{
|
353
|
+
char oid_str[MAX_STR_LEN], *table_name, *file_name;
|
354
|
+
int res;
|
355
|
+
T_CCI_ERROR error;
|
356
|
+
Connection *con;
|
357
|
+
|
358
|
+
GET_CONN_STRUCT(self, con);
|
359
|
+
CHECK_CONNECTION(con, Qnil);
|
360
|
+
|
361
|
+
if (NIL_P(table)) {
|
362
|
+
rb_raise(rb_eArgError, "class name is required.");
|
363
|
+
return Qnil;
|
364
|
+
}
|
365
|
+
table_name = StringValueCStr(table);
|
366
|
+
|
367
|
+
if (NIL_P(file)) {
|
368
|
+
file_name = NULL;
|
369
|
+
} else {
|
370
|
+
file_name = StringValueCStr(file);
|
371
|
+
}
|
372
|
+
|
373
|
+
res = cci_glo_new(con->handle, table_name, file_name, oid_str, &error);
|
374
|
+
if (res < 0) {
|
375
|
+
cubrid_handle_error(res, &error);
|
376
|
+
return Qnil;
|
377
|
+
}
|
378
|
+
|
379
|
+
return cubrid_oid_new(con, oid_str);
|
380
|
+
}
|
381
|
+
|
382
|
+
/* call-seq:
|
383
|
+
* server_version() -> string
|
384
|
+
*
|
385
|
+
* 연결된 서버의 버전을 문자열로 반환합니다.
|
386
|
+
*/
|
387
|
+
VALUE
|
388
|
+
cubrid_conn_server_version(VALUE self)
|
389
|
+
{
|
390
|
+
char ver_str[MAX_STR_LEN];
|
391
|
+
int res;
|
392
|
+
Connection *con;
|
393
|
+
|
394
|
+
GET_CONN_STRUCT(self, con);
|
395
|
+
CHECK_CONNECTION(con, Qnil);
|
396
|
+
|
397
|
+
res = cci_get_db_version(con->handle, ver_str, MAX_STR_LEN);
|
398
|
+
if (res < 0) {
|
399
|
+
cubrid_handle_error(res, NULL);
|
400
|
+
return Qnil;
|
401
|
+
}
|
402
|
+
|
403
|
+
return rb_str_new2(ver_str);
|
404
|
+
}
|
405
|
+
|
data/ext/cubrid.c
ADDED
@@ -0,0 +1,235 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2008 Search Solution Corporation. All rights reserved by Search Solution.
|
3
|
+
*
|
4
|
+
* Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
* are permitted provided that the following conditions are met:
|
6
|
+
*
|
7
|
+
* - Redistributions of source code must retain the above copyright notice,
|
8
|
+
* this list of conditions and the following disclaimer.
|
9
|
+
*
|
10
|
+
* - Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
* this list of conditions and the following disclaimer in the documentation
|
12
|
+
* and/or other materials provided with the distribution.
|
13
|
+
*
|
14
|
+
* - Neither the name of the <ORGANIZATION> nor the names of its contributors
|
15
|
+
* may be used to endorse or promote products derived from this software without
|
16
|
+
* specific prior written permission.
|
17
|
+
*
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
19
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
21
|
+
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
22
|
+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
23
|
+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
24
|
+
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
26
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
27
|
+
* OF SUCH DAMAGE.
|
28
|
+
*
|
29
|
+
*/
|
30
|
+
|
31
|
+
#include "cubrid.h"
|
32
|
+
|
33
|
+
char *cci_client_name = "CCI"; /* for lower than 7.0 */
|
34
|
+
VALUE cCubrid, cConnection, cStatement, cOid;
|
35
|
+
extern VALUE cubrid_conn_new(char *host, int port, char *db, char *user, char *passwd);
|
36
|
+
|
37
|
+
/* call-seq:
|
38
|
+
* connect(db, host, port, user, password) -> Connection
|
39
|
+
*
|
40
|
+
* 데이터베이스 서버에 연결하고 Connection 객체를 반환합니다.
|
41
|
+
* db는 반드시 주어져야 하고, host, port, user, password는 옵션입니다.
|
42
|
+
* 이들의 디폴트 값은 각각 'localhost', 30000, 'PUBLIC', '' 입니다.
|
43
|
+
*
|
44
|
+
* con = Cubrid.connect('demodb', '192.168.1.1', '33000', 'foo','bar')
|
45
|
+
* con.to_s #=> host: 192.168.1.1, port: 33000, db: demodb, user: foo
|
46
|
+
*
|
47
|
+
* con = Cubrid.connect('subway')
|
48
|
+
* con.to_s #=> host: localhost, port: 30000, db: subway, user: PUBLIC
|
49
|
+
*
|
50
|
+
* con = Cubrid.connect('subway', '192.168.1.2')
|
51
|
+
* con.to_s #=> host: 192.168.1.2, port: 33000, db: subway, user: PUBLIC
|
52
|
+
*/
|
53
|
+
VALUE cubrid_connect(int argc, VALUE* argv, VALUE self)
|
54
|
+
{
|
55
|
+
VALUE host, port, db, user, passwd;
|
56
|
+
|
57
|
+
rb_scan_args(argc, argv, "14", &db, &host, &port, &user, &passwd);
|
58
|
+
|
59
|
+
if (NIL_P(db))
|
60
|
+
rb_raise(rb_eStandardError, "DB name is required.");
|
61
|
+
|
62
|
+
if (NIL_P(host))
|
63
|
+
host = rb_str_new2("localhost");
|
64
|
+
|
65
|
+
if (NIL_P(port))
|
66
|
+
port = INT2NUM(30000);
|
67
|
+
|
68
|
+
if (NIL_P(user))
|
69
|
+
user = rb_str_new2("PUBLIC");
|
70
|
+
|
71
|
+
if (NIL_P(passwd))
|
72
|
+
passwd = rb_str_new2("");
|
73
|
+
|
74
|
+
return cubrid_conn_new(StringValueCStr(host), NUM2INT(port),
|
75
|
+
StringValueCStr(db), StringValueCStr(user), StringValueCStr(passwd));
|
76
|
+
}
|
77
|
+
|
78
|
+
/* from conn.c */
|
79
|
+
extern VALUE cubrid_conn_close(VALUE self);
|
80
|
+
extern VALUE cubrid_conn_prepare(int argc, VALUE* argv, VALUE self);
|
81
|
+
extern VALUE cubrid_conn_query(int argc, VALUE* argv, VALUE self);
|
82
|
+
extern VALUE cubrid_conn_commit(VALUE self);
|
83
|
+
extern VALUE cubrid_conn_rollback(VALUE self);
|
84
|
+
extern VALUE cubrid_conn_get_auto_commit(VALUE self);
|
85
|
+
extern VALUE cubrid_conn_set_auto_commit(VALUE self, VALUE auto_commit);
|
86
|
+
extern VALUE cubrid_conn_to_s(VALUE self);
|
87
|
+
extern VALUE cubrid_conn_glo_new(VALUE self, VALUE table, VALUE file);
|
88
|
+
extern VALUE cubrid_conn_server_version(VALUE self);
|
89
|
+
|
90
|
+
/* from stmt.c */
|
91
|
+
extern VALUE cubrid_stmt_close(VALUE self);
|
92
|
+
extern VALUE cubrid_stmt_bind(int argc, VALUE* argv, VALUE self);
|
93
|
+
extern VALUE cubrid_stmt_execute(int argc, VALUE* argv, VALUE self);
|
94
|
+
extern VALUE cubrid_stmt_affected_rows(VALUE self);
|
95
|
+
extern VALUE cubrid_stmt_fetch(VALUE self);
|
96
|
+
extern VALUE cubrid_stmt_fetch_hash(VALUE self);
|
97
|
+
extern VALUE cubrid_stmt_each(VALUE self);
|
98
|
+
extern VALUE cubrid_stmt_each_hash(VALUE self);
|
99
|
+
extern VALUE cubrid_stmt_column_info(VALUE self);
|
100
|
+
extern VALUE cubrid_stmt_get_oid(VALUE self);
|
101
|
+
|
102
|
+
/* from oid.c */
|
103
|
+
extern VALUE cubrid_oid_to_s(VALUE self);
|
104
|
+
extern VALUE cubrid_oid_table(VALUE self);
|
105
|
+
extern VALUE cubrid_oid_refresh(VALUE self);
|
106
|
+
extern VALUE cubrid_oid_get_value(VALUE self, VALUE attr_name);
|
107
|
+
extern VALUE cubrid_oid_set_value(VALUE self, VALUE attr_name, VALUE val);
|
108
|
+
extern VALUE cubrid_oid_save(VALUE self);
|
109
|
+
extern VALUE cubrid_oid_drop(VALUE self);
|
110
|
+
extern VALUE cubrid_oid_lock(VALUE self);
|
111
|
+
extern VALUE cubrid_oid_glo_load(VALUE self, VALUE file_name);
|
112
|
+
extern VALUE cubrid_oid_glo_save(VALUE self, VALUE file_name);
|
113
|
+
extern VALUE cubrid_oid_glo_drop(VALUE self);
|
114
|
+
extern VALUE cubrid_oid_glo_size(VALUE self);
|
115
|
+
extern VALUE cubrid_oid_each(VALUE self);
|
116
|
+
extern VALUE cubrid_oid_method_missing(int argc, VALUE* argv, VALUE self);
|
117
|
+
extern VALUE cubrid_oid_to_hash(VALUE self);
|
118
|
+
|
119
|
+
/* CUBRID[http://www.cubrid.com] ruby driver
|
120
|
+
*
|
121
|
+
* CUBRID ruby driver는 ruby에서 CUBRID 데이터베이스 서버에 접속하여 질의를 할 수 있도록 해주는 모듈입니다.
|
122
|
+
*
|
123
|
+
* * Connection
|
124
|
+
* * Statement
|
125
|
+
* * Oid
|
126
|
+
*
|
127
|
+
* con = Cubrid.connect('demodb', '192.168.1.1', '33000', 'foo','bar')
|
128
|
+
* stmt = con.prepare('SELECT * FROM db_user')
|
129
|
+
* stmt.execute
|
130
|
+
* stmt.each { |row|
|
131
|
+
* print row
|
132
|
+
* }
|
133
|
+
* stmt.close
|
134
|
+
* con.close
|
135
|
+
*
|
136
|
+
*/
|
137
|
+
void Init_cubrid()
|
138
|
+
{
|
139
|
+
cCubrid = rb_define_module("Cubrid");
|
140
|
+
rb_define_module_function(cCubrid, "connect", cubrid_connect, -1);
|
141
|
+
|
142
|
+
rb_define_const(cCubrid, "CHAR", INT2NUM(CCI_U_TYPE_CHAR));
|
143
|
+
rb_define_const(cCubrid, "VARCHAR", INT2NUM(CCI_U_TYPE_STRING));
|
144
|
+
rb_define_const(cCubrid, "STRING", INT2NUM(CCI_U_TYPE_STRING));
|
145
|
+
rb_define_const(cCubrid, "NCHAR", INT2NUM(CCI_U_TYPE_NCHAR));
|
146
|
+
rb_define_const(cCubrid, "VARNCHAR", INT2NUM(CCI_U_TYPE_VARNCHAR));
|
147
|
+
rb_define_const(cCubrid, "BIT", INT2NUM(CCI_U_TYPE_BIT));
|
148
|
+
rb_define_const(cCubrid, "VARBIT", INT2NUM(CCI_U_TYPE_VARBIT));
|
149
|
+
rb_define_const(cCubrid, "NUMERIC", INT2NUM(CCI_U_TYPE_NUMERIC));
|
150
|
+
rb_define_const(cCubrid, "INT", INT2NUM(CCI_U_TYPE_INT));
|
151
|
+
rb_define_const(cCubrid, "SHORT", INT2NUM(CCI_U_TYPE_SHORT));
|
152
|
+
rb_define_const(cCubrid, "MONETARY", INT2NUM(CCI_U_TYPE_MONETARY));
|
153
|
+
rb_define_const(cCubrid, "FLOAT", INT2NUM(CCI_U_TYPE_FLOAT));
|
154
|
+
rb_define_const(cCubrid, "DOUBLE", INT2NUM(CCI_U_TYPE_DOUBLE));
|
155
|
+
rb_define_const(cCubrid, "DATE", INT2NUM(CCI_U_TYPE_DATE));
|
156
|
+
rb_define_const(cCubrid, "TIME", INT2NUM(CCI_U_TYPE_TIME));
|
157
|
+
rb_define_const(cCubrid, "TIMESTAMP", INT2NUM(CCI_U_TYPE_TIMESTAMP));
|
158
|
+
rb_define_const(cCubrid, "SET", INT2NUM(CCI_U_TYPE_SET));
|
159
|
+
rb_define_const(cCubrid, "MULTISET", INT2NUM(CCI_U_TYPE_MULTISET));
|
160
|
+
rb_define_const(cCubrid, "SEQUENCE", INT2NUM(CCI_U_TYPE_SEQUENCE));
|
161
|
+
rb_define_const(cCubrid, "OBJECT", INT2NUM(CCI_U_TYPE_OBJECT));
|
162
|
+
|
163
|
+
rb_define_const(cCubrid, "INCLUDE_OID", INT2NUM(CCI_PREPARE_INCLUDE_OID));
|
164
|
+
rb_define_const(cCubrid, "READ_LOCK", INT2NUM(CCI_OID_LOCK_READ));
|
165
|
+
rb_define_const(cCubrid, "WRITE_LOCK", INT2NUM(CCI_OID_LOCK_WRITE));
|
166
|
+
|
167
|
+
/* connection */
|
168
|
+
cConnection = rb_define_class_under(cCubrid, "Connection", rb_cObject);
|
169
|
+
rb_define_method(cConnection, "close", cubrid_conn_close, 0); /* in conn.c */
|
170
|
+
rb_define_method(cConnection, "commit", cubrid_conn_commit, 0); /* in conn.c */
|
171
|
+
rb_define_method(cConnection, "rollback", cubrid_conn_rollback, 0); /* in conn.c */
|
172
|
+
rb_define_method(cConnection, "prepare", cubrid_conn_prepare, -1); /* in conn.c */
|
173
|
+
rb_define_method(cConnection, "query", cubrid_conn_query, -1); /* in conn.c */
|
174
|
+
rb_define_method(cConnection, "auto_commit?", cubrid_conn_get_auto_commit, 0); /* in conn.c */
|
175
|
+
rb_define_method(cConnection, "auto_commit=", cubrid_conn_set_auto_commit, 1); /* in conn.c */
|
176
|
+
rb_define_method(cConnection, "to_s", cubrid_conn_to_s, 0); /* in conn.c */
|
177
|
+
rb_define_method(cConnection, "glo_new", cubrid_conn_glo_new, 2); /* in conn.c */
|
178
|
+
rb_define_method(cConnection, "server_version", cubrid_conn_server_version, 0); /* in conn.c */
|
179
|
+
|
180
|
+
/* statement */
|
181
|
+
cStatement = rb_define_class_under(cCubrid, "Statement", rb_cObject);
|
182
|
+
rb_define_method(cStatement, "bind", cubrid_stmt_bind, -1); /* in stmt.c */
|
183
|
+
rb_define_method(cStatement, "execute", cubrid_stmt_execute, -1); /* in stmt.c */
|
184
|
+
rb_define_method(cStatement, "affected_rows", cubrid_stmt_affected_rows, 0); /* in stmt.c */
|
185
|
+
rb_define_method(cStatement, "column_info", cubrid_stmt_column_info, 0); /* in stmt.c */
|
186
|
+
rb_define_method(cStatement, "fetch", cubrid_stmt_fetch, 0); /* in stmt.c */
|
187
|
+
rb_define_method(cStatement, "fetch_hash", cubrid_stmt_fetch_hash, 0); /* in stmt.c */
|
188
|
+
rb_define_method(cStatement, "each", cubrid_stmt_each, 0); /* in stmt.c */
|
189
|
+
rb_define_method(cStatement, "each_hash", cubrid_stmt_each_hash, 0); /* in stmt.c */
|
190
|
+
rb_define_method(cStatement, "close", cubrid_stmt_close, 0); /* in stmt.c */
|
191
|
+
rb_define_method(cStatement, "get_oid", cubrid_stmt_get_oid, 0); /* in stmt.c */
|
192
|
+
/* stmt.to_s */
|
193
|
+
|
194
|
+
/* oid */
|
195
|
+
cOid = rb_define_class_under(cCubrid, "Oid", rb_cObject);
|
196
|
+
rb_define_method(cOid, "to_s", cubrid_oid_to_s, 0); /* in oid.c */
|
197
|
+
rb_define_method(cOid, "table", cubrid_oid_table, 0); /* in oid.c */
|
198
|
+
rb_define_method(cOid, "[]", cubrid_oid_get_value, 1); /* in oid.c */
|
199
|
+
rb_define_method(cOid, "[]=", cubrid_oid_set_value, 2); /* in oid.c */
|
200
|
+
rb_define_method(cOid, "each", cubrid_oid_each, 0); /* in oid.c */
|
201
|
+
rb_define_method(cOid, "refresh", cubrid_oid_refresh, 0); /* in oid.c */
|
202
|
+
rb_define_method(cOid, "save", cubrid_oid_save, 0); /* in oid.c */
|
203
|
+
rb_define_method(cOid, "drop", cubrid_oid_drop, 0); /* in oid.c */
|
204
|
+
rb_define_method(cOid, "lock", cubrid_oid_lock, 1); /* in oid.c */
|
205
|
+
rb_define_method(cOid, "to_hash", cubrid_oid_to_hash, 0); /* in oid.c */
|
206
|
+
rb_define_method(cOid, "glo_load", cubrid_oid_glo_load, 1); /* in oid.c */
|
207
|
+
rb_define_method(cOid, "glo_save", cubrid_oid_glo_save, 1); /* in oid.c */
|
208
|
+
rb_define_method(cOid, "glo_drop", cubrid_oid_glo_drop, 0); /* in oid.c */
|
209
|
+
rb_define_method(cOid, "glo_size", cubrid_oid_glo_size, 0); /* in oid.c */
|
210
|
+
rb_define_method(cOid, "method_missing", cubrid_oid_method_missing, -1); /* in oid.c */
|
211
|
+
}
|
212
|
+
|
213
|
+
/* Document-class: Cubrid::Connection
|
214
|
+
* Connection 클래스는 데이터베이스 서버에 대한 연결을 유지하고 트랜잭션을 연산을 수행합니다.
|
215
|
+
*/
|
216
|
+
|
217
|
+
/* Document-class: Cubrid::Statement
|
218
|
+
* Statement 클래스는 질의문을 수행하고 그 결과를 반환합니다.
|
219
|
+
*/
|
220
|
+
|
221
|
+
/* Document-class: Cubrid::Oid
|
222
|
+
* Oid 클래스는 CUBRID instance에 대하여 직접 연산을 수행할 수 있도록 합니다.
|
223
|
+
* CUBRID는 저장되어 있는 instance들의 고유한 식별자를 OID라는 이름으로 제공합니다.
|
224
|
+
* OID를 통해서 직접 해당하는 instance에 접근하여 read/write/update/delete 연산을 할 수 있습니다.
|
225
|
+
*
|
226
|
+
*/
|
227
|
+
|
228
|
+
/* TODO:
|
229
|
+
bind method & stored procedure
|
230
|
+
save point
|
231
|
+
db parameter : isolation level, max result count, lock time out, ..
|
232
|
+
glo : load to buffer, save from buffer, performance task
|
233
|
+
schema infomation
|
234
|
+
*/
|
235
|
+
|