HDB/MOD/oracle.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 164 CC 18 SCAR 28.3
CC
18
Cov
0%
names
42.0
Type_TEXT
30.0
Type_FLOAT
20.0
CRAP 16.9
1 #############################################################################
2 ## Name: oracle.pm
3 ## Purpose: HDB::MOD::oracle -> for DBD::Oracle
4 ## Author: Graciliano M. P.
5 ## Modified by:
6 ## Created: 14/01/2003
7 ## RCS-ID:
8 ## Copyright: (c) 2004 Graciliano M. P.
9 ## Licence: This program is free software; you can redistribute it and/or
10 ## modify it under the same terms as Perl itself
11 #############################################################################
12
13 package HDB::MOD::oracle ;
14 use DBD::Oracle ;
15
16 use strict qw(vars) ;
17 no warnings ;
18
19 our $VERSION = '1.0' ;
20 our @ISA = qw(HDB::MOD) ;
21
22 my (%OPENED_DBH) ;
23
24 my %SQL = (
25 LIKE => 1 ,
26 REGEXP => 0 ,
27 LOCK_TABLE => 0 ,
28 SHOW => 0 ,
29 LIMIT => 0 ,
30 TYPES => [qw(NUMBER VARCHAR VARCHAR2 BOOLEAN)] ,
31 TYPES_MASK => {
32 'BOOLEAN' => 'NUMBER(1)' ,
33 } ,
34 ) ;
35
36 #######
37 # NEW #
38 #######
39
40 sub new {
41 my $this = shift ;
42
43 $this->{SQL} = \%SQL ;
44 $this->{name} = 'HDB::Oracle' ;
45
46 bless($this , __PACKAGE__) ;
47 return( $this ) ;
48 }
49
50 ###########
51 # CONNECT #
52 ###########
53
54 sub MOD_connect {
55 my $this = shift ;
56 my ( $pass ) = @_ ;
57
58 my $db = $this->{db} ;
59 my $host = $this->{host} ;
60
61 $this->{dbh} = DBI->connect("DBI:Oracle:host=$host;sid=$db", $this->{user} , $pass , { RaiseError => 0 , PrintError => 1 , AutoCommit => 1 }) ;
62
63 if (! $this->{dbh} ) { return $this->Error("Can't connect to db $db\@$host!") ;}
64
65 return( $this->{dbh} ) ;
66 }
67
68 ##############
69 # PRIMARYKEY #
70 ##############
71
72 sub PRIMARYKEY { return "PRIMARY KEY" ;}
73
74 #################
75 # AUTOINCREMENT #
76 #################
77
78 sub AUTOINCREMENT { return "NUMBER PRIMARY KEY" ;}
79
80 #########
81 # LIMIT #
82 #########
83
84 sub LIMIT {
85 my $this = shift ;
86 my ( $sz , $offset ) = @_ ;
87
88 my $into_where = $offset ? "rownum >= $offset && $offset <= " . ($offset+$sz) : "rownum <= $sz" ;
89
90 return( undef , $into_where) ;
91 }
92
93 #############
94 # TYPE_TEXT #
95 #############
96
97 sub Type_TEXT {
98 my $this = shift ;
99 my ( $sz ) = @_ ;
100
101 if ( $sz eq '0' ) { return $this->Type_INTEGER() ;}
102
103 $sz = 65535 if $sz <= 0 ;
104
105 my $ret = $sz <= 250 ? "VARCHAR($sz)" :
106 ($sz <= 4000 ? "VARCHAR2($sz)" : "LONG") ;
107
108 return $ret ;
109 }
110
111 ################
112 # TYPE_INTEGER #
113 ################
114
115 sub Type_INTEGER {
116 my $this = shift ;
117 my ( $sz ) = @_ ;
118
119 $sz =~ s/\D+//g ;
120 $sz = 10 if $sz <= 0 ;
121 $sz = 38 if $sz > 38 ;
122
123 return "NUMBER($sz)" ;
124 }
125
126 ##############
127 # TYPE_FLOAT #
128 ##############
129
130 sub Type_FLOAT {
131 my $this = shift ;
132 my ( $type , $args ) = @_ ;
133
134 if ( $args !~ /\d/ ) { return "NUMBER(38)" ;}
135
136 my $tp_arg ;
137 if ( $args =~ /(\d+)\D+(\d+)/ ) { $tp_arg = "($1,$2)" ;}
138 elsif ( $args =~ /(\d+)/ ) { $tp_arg = "($1)" ;}
139
140 return "NUMBER$tp_arg" ;
141 }
142
143 ##########
144 # TABLES #
145 ##########
146
147 sub tables {
148 my $this = shift ;
149
150 $this->{sql} = "SELECT TABLE_NAME FROM user_tables ORDER BY TABLE_NAME" ;
151
152 eval{
153 $this->_undef_sth ;
154 $this->{sth} = $this->dbh->prepare( $this->{sql} ) ;
155 $this->{sth}->execute ;
156 };
157 if ( $@ ) { $this->Error("SQL error: $this->{sql}") ;}
158
159 my @tables = $this->Return('$') ;
160
161 $this->_undef_sth ;
162
163 return( sort @tables ) ;
164 }
165
166 #########
167 # NAMES #
168 #########
169
170 sub names {
171 my $this = shift ;
172 my ( $table ) = @_ ;
173
174 $table = HDB::CMDS::_format_table_name($table) ;
175
176 if (! $table) { return $this->Error('Invalid table!') ;}
177 elsif ( $this->{CACHE}{names}{$table} ) { return @{ $this->{CACHE}{names}{$table} } ;}
178
179 $this->{sql} = "SELECT * FROM $table WHERE(rownum <= 1)" ;
180
181 $this->_undef_sth ;
182 eval{
183 $this->{sth} = $this->dbh->prepare( $this->{sql} ) ;
184 $this->{sth}->execute ;
185 };
186
187 return $this->Error("SQL error: $this->{sql}") if $@ ;
188
189 my @names ;
190 eval { @names = map { substr($_ , 0) } @{ $this->{sth}->{'NAME'} } };
191
192 $this->_undef_sth ;
193
194 return () if !@names ;
195
196 if ( $this->{cache} ) { $this->{CACHE}{names}{$table} = \@names ;}
197
198 return @names ;
199 }
200
201 #############
202 # ON_CREATE #
203 #############
204
205 sub ON_CREATE {
206 my $this = shift ;
207 my ( $table , $cols , $order ) = @_ ;
208
209 $this->dbh->do(qq`
210 create sequence HDBseq_$table
211 start with 1
212 increment by 1
213 nomaxvalue
214 `);
215
216 $this->dbh->do(qq`
217 create trigger HDBtrigger_$table
218 before insert on $table
219 for each row begin
220 select HDBseq_$table.nextval into :new.ID from dual;
221 end;
222 `);
223
224 }
225
226 ###########
227 # ON_DROP #
228 ###########
229
230 sub ON_DROP {
231 my $this = shift ;
232 my ( $table ) = @_ ;
233
234 $this->dbh->do(qq` drop sequence HDBseq_$table `);
235 }
236
237 ##############
238 # LOCK_TABLE #
239 ##############
240
241 sub lock_table { $_[0]->dbh->do("LOCK TABLE $_[1] IN EXCLUSIVE MODE") ;}
242
243 ################
244 # UNLOCK_TABLE #
245 ################
246
247 sub unlock_table { $_[0]->dbh->do("COMMIT") ;}
248
249 #######
250 # END #
251 #######
252
253 1;
254
255