-
Notifications
You must be signed in to change notification settings - Fork 5
/
cubrid.pm
1457 lines (1101 loc) · 51.2 KB
/
cubrid.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# cubrid.pm
#
# Copyright (C) 2008 Search Solution Corporation. All rights reserved by Search Solution.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# - Neither the name of the <ORGANIZATION> nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
#
use 5.008;
use warnings;
use strict;
{ package DBD::cubrid;
use DBI ();
use DynaLoader ();
use vars qw(@ISA $VERSION $err $errstr $sqlstate $drh $dbh);
@ISA = qw(DynaLoader);
require_version DBI 1.61;
$VERSION = '11.2.0.0010';
bootstrap DBD::cubrid $VERSION;
$drh = undef; # holds driver handle once initialized
$err = 0; # holds error code for DBI::err
$errstr = ''; # holds error string for DBI::errstr
$sqlstate = ''; # holds five character SQLSTATE code
sub driver {
return $drh if $drh;
my($class, $attr) = @_;
$class .= "::dr";
DBD::cubrid::st->install_method ('cubrid_lob_get');
DBD::cubrid::st->install_method ('cubrid_lob_export');
DBD::cubrid::st->install_method ('cubrid_lob_import');
DBD::cubrid::st->install_method ('cubrid_lob_close');
$drh = DBI::_new_drh ($class, {
'Name' => 'cubrid',
'Version' => $VERSION,
'Err' => \$DBD::cubrid::err,
'Errstr' => \$DBD::cubrid::errstr,
'Attribution' => 'DBD::cubrid by Zhang Hui'
})
or return undef;
return $drh;
}
sub CLONE {
undef $drh;
}
}
{ package DBD::cubrid::dr; # ====== DRIVER ======
use strict;
sub connect {
my ($drh, $dsn, $user, $passwd, $attrhash) = @_;
my %connect_attr;
if ($dsn =~ /=/) {
my ($n,$v);
$dsn =~ s/^\s+//;
$dsn =~ s/\s+$//;
$dsn =~ s/^DBI:cubrid://;
$dsn =~ s/^dbi:cubrid://;
my @dsn = map {
($n,$v) = split /\s*=\s*/, $_, -1;
Carp::carp("DSN component '$_' is not in 'name=value' format")
unless defined $v && defined $n;
(uc($n), $v)
} split /\s*;\s*/, $dsn;
my %dsn = @dsn;
foreach (%dsn) {
$connect_attr{$_} = $dsn{$_};
}
}
else {
Carp::carp("DSN $dsn is not in 'name=value' format");
}
$user = 'public' if not defined $user;
my ($host, $port, $dbname);
if ($connect_attr{HOST}) {
$host = $connect_attr{HOST};
} else {
$host = 'localhost';
}
if ($connect_attr{PORT}) {
$port = $connect_attr{PORT};
} else {
$port = 33000;
}
if ($connect_attr{DATABASE}) {
$dbname = $connect_attr{DATABASE};
} else {
$dbname = '';
}
my $connect_dsn = "cci:cubrid:$host:$port:$dbname" . ':::';
my $is_connect_attr = 0;
if ($connect_attr{ALTHOSTS}) {
$connect_dsn .= "?alhosts=$connect_attr{ALTHOSTS}";
$is_connect_attr = 1;
}
if ($connect_attr{RCTIME}) {
if ($is_connect_attr) {
$connect_dsn .= "&rctime=$connect_attr{RCTIME}";
} else {
$connect_dsn .= "?rctime=$connect_attr{RCTIME}";
$is_connect_attr = 1;
}
}
if ($connect_attr{LOGIN_TIMEOUT}) {
if ($is_connect_attr) {
$connect_dsn .= "&login_timeout=$connect_attr{LOGIN_TIMEOUT}";
} else {
$connect_dsn .= "?login_timeout=$connect_attr{LOGIN_TIMEOUT}";
$is_connect_attr = 1;
}
}
if ($connect_attr{QUERY_TIMEOUT}) {
if ($is_connect_attr) {
$connect_dsn .= "&query_timeout=$connect_attr{QUERY_TIMEOUT}";
} else {
$connect_dsn .= "?query_timeout=$connect_attr{QUERY_TIMEOUT}";
$is_connect_attr = 1;
}
}
if ($connect_attr{DISCONNECT_ON_QUERY_TIMEOUT}) {
if ($is_connect_attr) {
$connect_dsn .= "&disconnect_on_query_timeout=$connect_attr{DISCONNECT_ON_QUERY_TIMEOUT}";
} else {
$connect_dsn .= "?disconnect_on_query_timeout=$connect_attr{DISCONNECT_ON_QUERY_TIMEOUT}";
$is_connect_attr = 1;
}
}
my ($dbh) = DBI::_new_dbh ($drh, {
'Name' => $dbname,
'User' => $user,
});
DBD::cubrid::db::_login($dbh, $connect_dsn, $user, $passwd, $attrhash) or return undef;
$dbh
}
} # end the package of DBD::cubrid::dr
{ package DBD::cubrid::db; # ====== DATABASE ======
use strict;
use DBI qw(:sql_types);
sub prepare {
my ($dbh, $statement, @attribs) = @_;
return undef if ! defined $statement;
my $sth = DBI::_new_sth ($dbh, {
'Statement' => $statement,
});
DBD::cubrid::st::_prepare($sth, $statement, @attribs) or return undef;
$sth
}
sub ping {
my $dbh = shift;
local $SIG{__WARN__} = sub { } if $dbh->FETCH('PrintError');
my $ret = DBD::cubrid::db::_ping($dbh);
return $ret ? 1 : 0;
}
sub get_info {
my ($dbh, $info_type) = @_;
require DBD::cubrid::GetInfo;
my $v = $DBD::cubrid::GetInfo::info{int($info_type)};
$v = $v->($dbh) if ref $v eq 'CODE';
return $v;
}
sub table_info {
my ($dbh, $catalog, $schema, $table, $type, $attr) = @_;
my @names = qw(TABLE_CAT TABLE_SCHEM TABLE_NAME TABLE_TYPE REMARKS);
my @rows;
my $sponge = DBI->connect("DBI:Sponge:", '','')
or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr");
if ((defined $catalog && $catalog eq "%") &&
(!defined($schema) || $schema eq "") &&
(!defined($table) || $table eq ""))
{
@rows = (); # Empty, because CUBRID doesn't support catalogs (yet)
}
elsif ((defined $schema && $schema eq "%") &&
(!defined($catalog) || $catalog eq "") &&
(!defined($table) || $table eq ""))
{
@rows = (); # Empty, because CUBRID doesn't support schemas (yet)
}
elsif ((defined $type && $type eq "%") &&
(!defined($catalog) || $catalog eq "") &&
(!defined($schema) || $schema eq "") &&
(!defined($table) || $table eq ""))
{
@rows = (
[ undef, undef, undef, "TABLE", undef ],
[ undef, undef, undef, "VIEW", undef ],
);
}
else
{
$table = '%' unless defined $table;
my ($want_tables, $want_views);
if (defined $type && $type ne "") {
if (($type =~ m/^table$/i) || ($type =~ m/^view$/i) || ($type =~ /^'table','view'$/i)) {
$want_tables = ($type =~ m/table/i);
$want_views = ($type =~ m/view/i);
}
else {
Carp::carp ("\$type must be TABLE, VIEW or 'TABELE','VIEW'");
}
}
else {
$want_tables = $want_views = 1;
}
my $sql = "SELECT class_name, class_type FROM db_class where class_name like " . $dbh->quote($table);
my $sth = $dbh->prepare ($sql) or return undef;
$sth->execute or return DBI::set_err($dbh, $sth->err(), $sth->errstr());
while (my $ref = $sth->fetchrow_arrayref()) {
my $type = (defined $ref->[1] &&
$ref->[1] =~ /VCLASS/i) ? 'VIEW' : 'TABLE';
next if $type eq 'TABLE' && not $want_tables;
next if $type eq 'VIEW' && not $want_views;
push @rows, [ undef, undef, $ref->[0], $type, undef ];
}
}
my $sth = $sponge->prepare("table_info",
{
rows => \@rows,
NUM_OF_FIELDS => scalar @names,
NAME => \@names,
}
) or return $dbh->DBI::set_err($sponge->err(), $sponge->errstr());
return $sth;
}
sub column_info {
my $dbh = shift;
my ($catalog, $schema, $table, $column) = @_;
# ODBC allows a NULL to mean all columns, so we'll accept undef
$column = '%' unless defined $column;
my $table_id = $dbh->quote_identifier($table);
my @names = qw(
TABLE_CAT TABLE_SCHEM TABLE_NAME COLUMN_NAME DATA_TYPE TYPE_NAME COLUMN_SIZE
BUFFER_LENGTH DECIMAL_DIGITS NUM_PREC_RADIX NULLABLE REMARKS COLUMN_DEF
SQL_DATA_TYPE SQL_DATETIME_SUB CHAR_OCTET_LENGTH ORDINAL_POSITION IS_NULLABLE
CHAR_SET_CAT CHAR_SET_SCHEM CHAR_SET_NAME COLLATION_CAT COLLATION_SCHEM
COLLATION_NAME UDT_CAT UDT_SCHEM UDT_NAME DOMAIN_CAT DOMAIN_SCHEM DOMAIN_NAME
SCOPE_CAT SCOPE_SCHEM SCOPE_NAME MAX_CARDINALITY DTD_IDENTIFIER IS_SELF_REF
);
my @col_info;
local $dbh->{FetchHashKeyName} = 'NAME_lc';
my $desc_sth = $dbh->prepare("SHOW COLUMNS FROM $table_id LIKE " . $dbh->quote($column));
my $desc = $dbh->selectall_arrayref($desc_sth, { Columns=>{} });
my $ordinal_pos = 0;
for my $row (@$desc) {
my $type = $row->{type};
if (!defined($type)) {
$type = "NULL";
}
my $info = {
TABLE_CAT => $catalog,
TABLE_SCHEM => $schema,
TABLE_NAME => $table,
COLUMN_NAME => $row->{field},
NULLABLE => ($row->{null} eq 'YES') ? 1 : 0,
IS_NULLABLE => ($row->{null} eq 'YES') ? "YES" : "NO",
TYPE_NAME => uc($type),
COLUMN_DEF => $row->{default},
ORDINAL_POSITION => ++$ordinal_pos,
};
if ($type =~ /CHAR/) {
$info->{DATA_TYPE} = SQL_VARCHAR;
$info->{DATA_TYPE} = SQL_CHAR if $type =~ /^CHAR/;
if ($type =~ /\(/) {
my @tmp = split /\(/, $type;
my @tmp1 = split /\)/, $tmp[1];
$info->{COLUMN_SIZE} = $tmp1[0];
}
else {
$info->{COLUMN_SIZE} = 1073741823;
}
}
elsif ($type =~ /STRING/) {
$info->{DATA_TYPE} = SQL_VARCHAR;
if ($type =~ /\(/) {
my @tmp = split /\(/, $type;
my @tmp1 = split /\)/, $tmp[1];
$info->{COLUMN_SIZE} = $tmp1[0];
}
else {
$info->{COLUMN_SIZE} = 1073741823;
}
}
elsif ($type =~ /INT/) {
$info->{NUM_PREC_RADIX} = 10;
if ($type =~ /BIGINT/) {
$info->{DATA_TYPE} = SQL_BIGINT;
$info->{COLUMN_SIZE} = 19;
}
elsif ($type =~ /SMALLINT/) {
$info->{DATA_TYPE} = SQL_SMALLINT;
$info->{COLUMN_SIZE} = 5;
}
else {
$info->{DATA_TYPE} = SQL_INTEGER;
$info->{COLUMN_SIZE} = 10;
}
}
elsif ($type =~ /SHORT/) {
$info->{DATA_TYPE} = SQL_SMALLINT;
$info->{COLUMN_SIZE} = 5;
$info->{NUM_PREC_RADIX} = 10;
}
elsif ($type =~ /NUMERIC/) {
$info->{DATA_TYPE} = SQL_NUMERIC;
$info->{COLUMN_SIZE} = 38;
if ($type =~ /\(/) {
my @tmp = split /\(/, $type;
my @tmp1 = split /\)/, $tmp[1];
my @tmp2 = split /,/, $tmp1[0];
$info->{DECIMAL_DIGITS} = $tmp2[1];
$info->{NUM_PREC_RADIX} = $tmp2[0];
}
else {
$info->{DECIMAL_DIGITS} = 15;
$info->{NUM_PREC_RADIX} = 10;
}
}
elsif ($type =~ /MONETARY/) {
$info->{DATA_TYPE} = SQL_FLOAT;
$info->{COLUMN_SIZE} = 14;
$info->{DECIMAL_DIGITS} = 2;
}
elsif ($type =~ /BLOB/) {
$info->{DATA_TYPE} = SQL_BLOB;
$info->{COLUMN_SIZE} = 0;
}
elsif ($type =~ /CLOB/) {
$info->{DATA_TYPE} = SQL_CLOB;
$info->{COLUMN_SIZE} = 0;
}
elsif ($type =~ /FLOAT/) {
$info->{DATA_TYPE} = SQL_FLOAT;
$info->{COLUMN_SIZE} = 14;
$info->{NUM_PREC_RADIX} = 10;
}
elsif ($type=~ /DOUBLE/) {
$info->{DATA_TYPE} = SQL_DOUBLE;
$info->{COLUMN_SIZE} = 28;
$info->{NUM_PREC_RADIX} = 10;
}
elsif ($type =~ /TIME/) {
if ($type =~ /TIMESTAMP|DATETIME/) {
$info->{DATA_TYPE} = SQL_TYPE_TIMESTAMP;
$info->{COLUMN_SIZE} = 19 if $type =~ /^TIMESTAMP/;
$info->{COLUMN_SIZE} = 23 if $type =~ /^DATETIME/;
}
else {
$info->{DATA_TYPE} = SQL_TYPE_TIME;
$info->{COLUMN_SIZE} = 8;
}
}
elsif ($type =~ /DATE/) {
if ($type =~ /DATETIME/) {
$info->{DATA_TYPE} = SQL_TYPE_TIMESTAMP;
$info->{COLUMN_SIZE} = 23;
}
else {
$info->{DATA_TYPE} = SQL_TYPE_DATE;
$info->{COLUMN_SIZE} = 10;
}
}
elsif ($type =~ /BIT/) {
if ($type =~ /VARYING/) {
$info->{DATA_TYPE} = SQL_VARBINARY;
}
else {
$info->{DATA_TYPE} = SQL_BINARY;
}
if ($type =~ /\(/) {
my @tmp = split /\(/, $type;
my @tmp1 = split /\)/, $tmp[1];
$info->{COLUMN_SIZE} = $tmp1[0];
}
else {
$info->{COLUMN_SIZE} = 1073741823;
}
}
elsif ($type =~ /^NULL$/) {
$info->{DATA_TYPE} = SQL_UNKNOWN_TYPE;
}
else {
$info->{DATA_TYPE} = SQL_VARCHAR;
}
$info->{SQL_DATA_TYPE} ||= $info->{DATA_TYPE};
push @col_info, [
$info->{TABLE_CAT},
$info->{TABLE_SCHEM},
$info->{TABLE_NAME},
$info->{COLUMN_NAME},
$info->{DATA_TYPE},
$info->{TYPE_NAME},
$info->{COLUMN_SIZE},
$info->{BUFFER_LENGTH},
$info->{DECIMAL_DIGITS},
$info->{NUM_PREC_RADIX},
$info->{NULLABLE},
$info->{REMARKS},
$info->{COLUMN_DEF},
$info->{SQL_DATA_TYPE},
$info->{SQL_DATETIME_SUB},
$info->{CHAR_OCTET_LENGTH},
$info->{ORDINAL_POSITION},
$info->{IS_NULLABLE},
$info->{CHAR_SET_CAT},
$info->{CHAR_SET_SCHEM},
$info->{CHAR_SET_NAME},
$info->{COLLATION_CAT},
$info->{COLLATION_SCHEM},
$info->{COLLATION_NAME},
$info->{UDT_CAT},
$info->{UDT_SCHEM},
$info->{UDT_NAME},
$info->{DOMAIN_CAT},
$info->{DOMAIN_SCHEM},
$info->{DOMAIN_NAME},
$info->{SCOPE_CAT},
$info->{SCOPE_SCHEM},
$info->{SCOPE_NAME},
$info->{MAX_CARDINALITY},
$info->{DTD_IDENTIFIER},
$info->{IS_SELF_REF}
];
}
my $sponge = DBI->connect("DBI:Sponge:", '','')
or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr");
my $sth = $sponge->prepare("column_info $table_id", {
rows => \@col_info,
NUM_OF_FIELDS => scalar @names,
NAME => \@names,
}) or return $dbh->DBI::set_err($sponge->err(), $sponge->errstr());
return $sth;
}
sub primary_key_info {
my ($dbh, $catalog, $schema, $table) = @_;
my @names = qw(
TABLE_CAT TABLE_SCHEM TABLE_NAME COLUMN_NAME KEY_SEQ PK_NAME
);
my @col_info;
my $desc = DBD::cubrid::db::_primary_key_info ($dbh, $table);
for my $row (@$desc) {
push @col_info, [
$catalog,
$schema,
$row->[0],
$row->[1],
$row->[2],
$row->[3],
];
}
my $sponge = DBI->connect ("DBI:Sponge:", '','')
or return $dbh->DBI::set_err ($DBI::err, "DBI::Sponge: $DBI::errstr");
my $sth= $sponge->prepare ("primary_key_info $table", {
rows => \@col_info,
NUM_OF_FIELDS => scalar @names,
NAME => \@names,
}) or return $dbh->DBI::set_err($sponge->err(), $sponge->errstr());
return $sth;
}
sub foreign_key_info {
my ($dbh,
$pk_catalog, $pk_schema, $pk_table,
$fk_catalog, $fk_schema, $fk_table,
) = @_;
my @names = qw(
PKTABLE_CAT PKTABLE_SCHEM PKTABLE_NAME PKCOLUMN_NAME
FKTABLE_CAT FKTABLE_SCHEM FKTABLE_NAME FKCOLUMN_NAME
KEY_SEQ UPDATE_RULE DELETE_RULE FK_NAME PK_NAME DEFERRABILITY
);
my @col_info;
my $desc = DBD::cubrid::db::_foreign_key_info ($dbh, $pk_table, $fk_table);
for my $row (@$desc) {
push @col_info, [
$pk_catalog,
$pk_schema,
$row->[0],
$row->[1],
$fk_catalog,
$fk_schema,
$row->[2],
$row->[3],
$row->[4],
$row->[5],
$row->[6],
$row->[7],
$row->[8],
undef
];
}
my $sponge = DBI->connect ("DBI:Sponge:", '','')
or return $dbh->DBI::set_err ($DBI::err, "DBI::Sponge: $DBI::errstr");
my $sth= $sponge->prepare ("foreign_key_info", {
rows => \@col_info,
NUM_OF_FIELDS => scalar @names,
NAME => \@names,
}) or return $dbh->DBI::set_err($sponge->err(), $sponge->errstr());
return $sth;
}
sub type_info_all {
my ($dbh) = @_;
my $type_info_all = [
{
TYPE_NAME => 0,
DATA_TYPE => 1,
COLUMN_SIZE => 2,
LITERAL_PREFIX => 3,
LITERAL_SUFFIX => 4,
CREATE_PARAMS => 5,
NULLABLE => 6,
CASE_SENSITIVE => 7,
SEARCHABLE => 8,
UNSIGNED_ATTRIBUTE => 9,
FIXED_PREC_SCALE => 10,
AUTO_UNIQUE_VALUE => 11,
LOCAL_TYPE_NAME => 12,
MINIMUM_SCALE => 13,
MAXIMUM_SCALE => 14,
SQL_DATA_TYPE => 15,
SQL_DATETIME_SUB => 16,
NUM_PREC_RADIX => 17,
INTERVAL_PRECISION => 18,
},
["CHAR", SQL_CHAR, 1073741823, q{'}, q{'}, "length",
1, 0, 3, -1, 0, 0, "CHAR", -1, -1, SQL_CHAR, -1, -1, -1],
["VARCHAR", SQL_VARCHAR, 1073741823, q{'}, q{'}, "length",
1, 0, 3, -1, 0, 0, "CHAR VARYING", -1, -1, SQL_VARCHAR, -1, -1, -1],
["BIT", SQL_BINARY, 1073741823 / 8, q{X'}, q{'}, "length",
1, 0, 3, -1, 0, 0, "BIT", -1, -1, SQL_BINARY, -1, -1, -1],
["BIT VARYING", SQL_VARBINARY, 1073741823 / 8, q{X'}, q{'}, "length",
1, 0, 3, -1, 0, 0, "BIT VARYING", -1, -1, SQL_VARBINARY, -1, -1, -1],
["NUMERIC", SQL_NUMERIC, 38, undef, undef, "precision, scale",
1, 0, 2, 0, 0, 0, "NUMERIC", 0, 38, SQL_NUMERIC, -1, 10, -1],
["DECIMAL", SQL_DECIMAL, 38, undef, undef, "precision, scale",
1, 0, 2, 0, 0, 0, "DECIMAL", 0, 38, SQL_DECIMAL, -1, 10, -1],
["INTEGER", SQL_INTEGER, 10, undef, undef, undef,
1, 0, 2, 0, 0, 0, "INTEGER", -1, -1, SQL_INTEGER, -1, 10, -1],
["SMALLINT", SQL_SMALLINT, 5, undef, undef, undef,
1, 0, 2, 0, 0, 0, "SMALLINT", -1, -1, SQL_SMALLINT, -1, 10, -1],
["REAL", SQL_REAL, 14, undef, undef, "precision",
1, 0, 2, 0, 0, 0, "REAL", -1, -1, SQL_REAL, -1, 10, -1],
["FLOAT", SQL_FLOAT, 14, undef, undef, "precision",
1, 0, 2, 0, 0, 0, "FLOAT", -1, -1, SQL_FLOAT, -1, 10, -1],
["DOUBLE", SQL_DOUBLE, 28, undef, undef, "precision",
1, 0, 2, 0, 0, 0, "DOUBLE", -1, -1, SQL_DOUBLE, -1, 10, -1],
["DATE", SQL_TYPE_DATE, 10, q{DATE '}, q{'}, undef,
1, 0, 2, 0, 0, 0, "DATE", -1, -1, SQL_DATETIME, 1, -1, -1],
["TIME", SQL_TYPE_TIME, 8, q{TIME '}, q{'}, undef,
1, 0, 2, 0, 0, 0, "TIME", -1, -1, SQL_DATETIME, 2, -1, -1],
["TIMESTAMP", SQL_TYPE_TIMESTAMP, 19, q{TIMESTAMP '}, q{'}, undef,
1, 0, 2, 0, 0, 0, "TIMESTAMP", -1, -1, SQL_DATETIME, 3, -1, -1],
["BIGINT", SQL_BIGINT, 19, undef, undef, undef,
1, 0, 2, 0, 0, 0, "BIGINT", -1, -1, SQL_INTEGER, -1, 10, -1],
["DATETIME", SQL_TYPE_TIMESTAMP, 23, q{DATETIME '}, q{'}, undef,
1, 0, 2, 0, 0, 0, "DATETIME", -1, -1, SQL_DATETIME, 3, -1, -1],
["ENUM", SQL_VARCHAR, 0, undef, undef, undef,
1, 0, 3, 0, 0, 0, "ENUM", -1, -1, SQL_VARCHAR, -1, -1, -1],
["BLOB", SQL_BLOB, 0, undef, undef, undef,
1, 0, 3, 0, 0, 0, "BLOB", -1, -1, SQL_BLOB, -1, -1, -1],
["CLOB", SQL_CLOB, 0, undef, undef, undef,
1, 0, 3, 0, 0, 0, "CLOB", -1, -1, SQL_CLOB, -1, -1, -1],
];
return $type_info_all;
}
} # end of package DBD::cubrid::db
{ package DBD::cubrid::st; # ====== STATEMENT ======
}
1;
__END__
=head1
DBD::cubrid - CUBRID driver for the Perl5 Database Interface (DBI)
=head1 SYNOPSIS
use DBI;
$dsn = "DBI:cubrid:database=$database;host=$hostname;port=$port";
$dbh = DBI->connect ($dsn, $user, $password);
$sth = $dbh->prepare("SELECT * FROM TABLE");
$sth->execute;
$numFields = $sth->{'NUM_OF_FIELDS'};
$sth->finish;
$dbh->disconnect;
=head1 EXAMPLE
#!/usr/bin/perl
use strict;
use DBI;
# Connect to the database.
my $dbh = DBI->connect (
"DBI:cubrid:database=testdb;host=localhost;port=33000", "public", "",
{RaiseError => 1, AutoCommit => 0});
# Drop table 'foo'. This may fail, if 'foo' doesn't exist.
# Thus we put an eval around it.
eval { $dbh->do("DROP TABLE foo") };
print "Dropping foo failed: $@\n" if $@;
# Create a new table 'foo'. This must not fail, thus we don't
# catch errors.
$dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))");
# INSERT some data into 'foo'.
$dbh->do("INSERT INTO foo VALUES (1, "Tim");
# Same thing, but using placeholders
$dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen");
# Now retrieve data from the table.
my $sth = $dbh->prepare("SELECT * FROM foo");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
}
$sth->finish();
# Disconnect from the database.
$dbh->disconnect();
=head1 DESCRIPTION
DBD::cubrid is a Perl module that works with the DBI module to provide access to
CUBRID databases.
=head1 Module Documentation
This documentation describes driver specific behavior and restrictions. It is
not supposed to be used as the only reference for the user. In any case
consult the B<DBI> documentation first!
=for html <a href="http://search.cpan.org/~timb/DBI/DBI.pm">Latest DBI documentation.</a>
=head1 THE DBI CLASS
=head2 DBI Class Methods
=head3 B<connect>
use DBI;
$dsn = "DBI:cubrid:database=$database";
$dsn = "DBI:cubrid:database=$database;host=$hostname";
$dsn = "DBI:cubrid:database=$database;host=$hostname;port=$port";
$dbh = DBI->connect ($dsn, $user, $password);
This method creates a database handle by connecting to a database, and is the DBI
equivalent of the "new" method. You can also use "dbi:cubrid..." in $dsn. There
are some properties you can configure when create the conntion.
If the HA feature is enabled, you munst specify the connection information of the
standby server, which is used for failover when failure occurs, in the url string
argument of this function.
B<althosts>=standby_broker1_host,standby_broker2_host, . . . : String. Specifies the
broker information of the standby server, which is used for failover when it is
impossible to connect to the active server. You can specify multiple brokers for failover,
and the connection to the brokers is attempted in the order listed in alhosts.
B<rctime> : INT. An interval between the attempts to connect to the active broker in
which failure occurred. After a failure occurs, the system connects to the broker
specified by althosts (failover), terminates the transaction, and then attempts to
connect to he active broker of the master database at every rctime. The default value
is 600 seconds.
B<login_timeout> : INT. Configures the login timeout of CUBRID.
B<query_timeout>: INT. Configures the query timeout of CUBRID.
B<disconnect_on_query_timeout> : String. Make the query_timeout effective.
The value maybe true, on, yes, false, off and no.
The following are some examples about different $dsn:
$db = "testdb";
$host= "192.168.10.29";
$port = 33088;
$dsn = "dbi:cubrid:database=$db;host=$host;port=$port";
# connection $dsn string when property(alhosts) specified for HA
$alhosts = "192.168.10.28:31000,192.168.10.27:33055";
$dsn = "dbi:cubrid:database=$db;host=$host;port=$port;alhosts=$alhosts";
# connection $dsn string when property(alhosts, rctime) specified for HA
$rctime = 1000;
$dsn = "dbi:cubrid:database=$db;host=$host;port=$port;alhosts=$alhosts;rctime=$rctime";
$query = 600;
$dsn = "dbi:cubrid:database=$db;host=$host;port=$port;query_timeout=$query;disconnect_on_query_timeout=yes";
B<Tips: the autocommit has been removed from the dsn since RB-8.4.4. If you wanna set up the autocommit of the connection, you have to do:>
$dbh= DBI->connect ($dsn, $user, $password,
{ RaiseError => 1, PrintError => 1, AutoCommit => 0 });
=head1 DBI Database Handle Object
=head2 Database Handle Methods
=head3 B<do>
$rows = $dbh->do ($statement) or die $dbh->errstr;
$rows = $dbh->do ($statement, \%attr) or die $dbh->errstr;
$rows = $dbh->do ($statement, \%attr, @bind_values);
Prepare and execute a single statement. Returns the number of rows affected or undef
on error. A return value of -1 means the number of rows is not known, not applicable,
or not available. For example:
$dbh->do ("CREATE TABLE test_cubrid (id INT, name varchar(60))");
$dbh->do ("INSERT INTO test_cubrid VALUES (1, 'Jobs')")
$dbh->do ("INSERT INTO test_cubrid VALUES (?, ?)", undef, (2, "Gate"));
=head3 B<prepare>
$sth = $dbh->prepare ($statement, \%attr);
Prepares a statement for later execution by the database engine and returns a reference
to a statement handle object. You can get C<$sth-E<gt>{NUM_OF_PARAMS}> after the statement
prepared.
=head3 B<commit>
$dbh->commit or die $dbh->errstr;
Issues a COMMIT to the server, indicating that the current transaction is finished and
that all changes made will be visible to other processes. If AutoCommit is enabled,
then a warning is given and no COMMIT is issued. Returns true on success, false on error.
=head3 B<rollback>
$dbh->rollback or dir $dbh->errstr;
Issues a ROLLBACK to the server, which discards any changes made in the current transaction.
If AutoCommit is enabled, then a warning is given and no ROLLBACK is issued. Returns true
on success, and false on error.
=head3 B<disconnect>
$dbh->disconnect or warn $dbh->errstr;
Disconnects from the CUBRID database. If the C<AutoCommit> is on, any uncommitted changes
will be committed before disconnect is called. Otherwise, it will roll back.
=head3 B<selectall_arrayref>
$ary_ref = $dbh->selectall_arrayref ($statement);
$ary_ref = $dbh->selectall_arrayref ($statement, \%attr);
$ary_ref = $dbh->selectall_arrayref ($statement, \%attr, @bind_values);
Returns a reference to an array containing the rows returned by preparing and executing
the SQL string. See the DBI documentation for full details.
=head3 B<selectall_hashref>
$hash_ref = $dbh->selectall_hashref ($statement, $key_field);
Returns a reference to a hash containing the rows returned by preparing and executing
the SQL string. See the DBI documentation for full details.
=head3 B<selectcol_arrayref>
$ary_ref = $dbh->selectcol_arrayref ($statement, \%attr, @bind_values);
Returns a reference to an array containing the first column from each rows returned by
preparing and executing the SQL string. It is possible to specify exactly which columns
to return. See the DBI documentation for full details.
=head3 B<selectrow_array>
@row_ary = $dbh->selectrow_array ($statement);
@row_ary = $dbh->selectrow_array ($statement, \%attr);
@row_ary = $dbh->selectrow_array ($statement, \%attr, @bind_values);
Returns an array of row information after preparing and executing the provided SQL string.
The rows are returned by calling L</fetchrow_array>. The string can also be a statement handle
generated by a previous prepare. Note that only the first row of data is returned. If called
in a scalar context, only the first column of the first row is returned. Because this is not
portable, it is not recommended that you use this method in that way.
=head3 B<selectrow_arrayref>
@row_ary = $dbh->selectrow_arrayref ($statement);
@row_ary = $dbh->selectrow_arrayref ($statement, \%attr);
@row_ary = $dbh->selectrow_arrayref ($statement, \%attr, @bind_values);
Exactly the same as L</selectrow_array>, except that it returns a reference to an array, by
internal use of the L</fetchrow_arrayref> method.
=head3 B<selectrow_hashref>
$hash_ref = $dbh->selectrow_hashref ($statement);
$hash_ref = $dbh->selectrow_hashref ($statement, \%attr);
$hash_ref = $dbh->selectrow_hashref ($statement, \%attr, @bind_values);
Exactly the same as L</selectrow_array>, except that it returns a reference to an hash, by
internal use of the L</fetchrow_hashref> method.
=head3 B<last_insert_id>
$insert_id = $dbh->last_insert_id ($catalog, $schema, $table, $field);
$insert_id = $dbh->last_insert_id ($catalog, $schema, $table, $field, \%attr);
Attempts to retrieve the ID generated for the AUTO_INCREMENT column which is updated by the
previous INSERT query. This method only be available immediately after the insert statement
has executed for CUBRID. And CUBRID will also ignore the $catalog, $schema, $table and $field
parameters. CUBRID will return NULL if no last insert id. For example:
$insert_id = $dbh->last_insert_id (undef, undef, undef, undef);
=head3 B<ping>
$rv = $dbh->ping;
This method is used to check the validity of a database handle. The value returned is either 0,
indicating that the connection is no longer valid, or 1, indicating the connection is valid.
=head3 B<get_info>
$value = $dbh->get_info ($info_type);
DBD::cubrid supports C<get_info()>, bug (currently) only a few info types.
=head3 B<table_info>
$sth = $dbh->table_info ($catalog, $schema, $table, $type);
#then $sth->fetchall_arrayref or $sth->fetchall_hashref etc
DBD::cubrid supports attributes for C<table_info()>.
In CUBRID, this method will return all tables and views visible to the current user.
CUBRID doesn't support catalog and schema now. The table argument will do a LIKE search
if a percent sign (%) or an underscore (_) is detected in the argment. The type argument
accepts a value of either "TABLE" or "VIEW" (using both is the defualt action). Note
that a statement handle is returned, not a direct list of table. See the examples below
for ways to handle this.
The following fields are returned:
B<TABLE_CAT>: Always NULL, as CUBRID doesn't have the concept of catalogs.
B<TABLE_SCHEM>: Always NULL, as CUBRID doesn't have the concept of schemas.
B<TABLE_NAME>: Name of the table (or view, synonym, etc).
B<TABLE_TYPE>: The type of object returned. It will be "TABLE" or "VIEW".
B<REMARKS>: A description of the table. Always NULL (undef).
Examples of use:
# Display all tables and views to the current user
$sth = $dbh->table_info (undef, undef, undef, undef);
for my $info (@{$sth->fetchall_arrayref({})}) {
print "\$info->{TABLE_NAME} = $info->{TABLE_NAME} \t \$info->{TABLE_TYPE} = $info->{TABLE_TYPE}\n";
}
# Display the specified table
$sth = $dbh->table (undef, undef, 'test_%', undef);
for my $info (@{$sth->fetchall_arrayref({})}) {
print "\$info->{TABLE_NAME} = $info->{TABLE_NAME} \t \$info->{TABLE_TYPE} = $info->{TABLE_TYPE}\n";
}
=head3 B<tables>
@names = $dbh->tables ($catalog, $schema, $table, $type);
Simple interface to L</table_info>. Returns a list of matching table names.
See more information in DBI document;
=head3 B<primary_key_info>
$sth = $dbh->primary_key_info ($catalog, $schema, $table);
# then $sth->fetchall_arrayref or $sth->fetchall_hashref etc
CUBRID does not support catalogues and schemas so TABLE_CAT and TABLE_SCHEM are ignored
as selection criterion. The TABLE_CAT and TABLE_SCHEM fields of a fetched row is always
NULL (undef).
The result set is ordered by TABLE_NAME, COLUMN_NAME, KEY_SEQ, and PK_NAME.
=head3 B<primary_key>