File Coverage

blib/lib/DBD/SQLite2.pm
Criterion Covered Total %
statement 71 83 85.5
branch 23 40 57.5
condition 11 53 20.7
subroutine 9 11 81.8
pod 0 1 0.0
total 114 188 60.6


line stmt bran cond sub pod time code
1             # $Id: SQLite2.pm,v 1.2 2004/09/10 15:43:39 matt Exp $
2              
3             package DBD::SQLite2;
4 22     22   435378 use strict;
  22         55  
  22         965  
5              
6 22     22   2352 use DBI;
  22         24866  
  22         1347  
7 22     22   125 use vars qw($err $errstr $state $drh $VERSION @ISA);
  22         44  
  22         3125  
8             $VERSION = '0.36';
9              
10 22     22   128 use DynaLoader();
  22         43  
  22         37312  
11             @ISA = ('DynaLoader');
12              
13             __PACKAGE__->bootstrap($VERSION);
14              
15             $drh = undef;
16              
17             sub driver {
18 21 50   21 0 3295 return $drh if $drh;
19 21         59 my ($class, $attr) = @_;
20              
21 21         90 $class .= "::dr";
22              
23 21         169 $drh = DBI::_new_drh($class, {
24             Name => 'SQLite2',
25             Version => $VERSION,
26             Attribution => 'DBD::SQLite2 by Matt Sergeant',
27             });
28              
29 21         1268 return $drh;
30             }
31              
32             sub CLONE {
33 0     0   0 undef $drh;
34             }
35              
36             package DBD::SQLite2::dr;
37              
38             sub connect {
39 25     25   490080 my ($drh, $dbname, $user, $auth, $attr) = @_;
40              
41 25         303 my $dbh = DBI::_new_dbh($drh, {
42             Name => $dbname,
43             });
44              
45 25         897 my $real_dbname = $dbname;
46 25 100       161 if ($dbname =~ /=/) {
47 24         100 foreach my $attrib (split(/;/, $dbname)) {
48 24         79 my ($k, $v) = split(/=/, $attrib, 2);
49 24 50       106 if ($k eq 'dbname') {
50 24         81 $real_dbname = $v;
51             }
52             else {
53             # TODO: add to attribs
54             }
55             }
56             }
57 25 50       180035 DBD::SQLite2::db::_login($dbh, $real_dbname, $user, $auth)
58             or return undef;
59              
60 25         326 return $dbh;
61             }
62              
63             package DBD::SQLite2::db;
64              
65             sub prepare {
66 148     148   14221835 my ($dbh, $statement, @attribs) = @_;
67              
68 148         1063 my $sth = DBI::_new_sth($dbh, {
69             Statement => $statement,
70             });
71              
72 148 50       8917 DBD::SQLite2::st::_prepare($sth, $statement, @attribs)
73             or return undef;
74              
75 148         3901 return $sth;
76             }
77              
78              
79             sub table_info {
80 3     3   9 my ($dbh, $CatVal, $SchVal, $TblVal, $TypVal) = @_;
81             # SQL/CLI (ISO/IEC JTC 1/SC 32 N 0595), 6.63 Tables
82             # Based on DBD::Oracle's
83             # See also http://www.ch-werner.de/sqliteodbc/html/sqliteodbc_8c.html#a117
84              
85 3         7 my @Where = ();
86 3         6 my $Sql;
87 3 50 33     81 if ( defined($CatVal) && $CatVal eq '%'
    50 33        
    50 33        
      0        
      0        
      33        
      33        
      33        
      0        
      0        
      33        
      33        
      33        
      0        
      0        
      0        
      0        
88             && defined($SchVal) && $SchVal eq ''
89             && defined($TblVal) && $TblVal eq '') { # Rule 19a
90 0         0 $Sql = <<'SQL';
91             SELECT NULL TABLE_CAT
92             , NULL TABLE_SCHEM
93             , NULL TABLE_NAME
94             , NULL TABLE_TYPE
95             , NULL REMARKS
96             SQL
97             }
98             elsif ( defined($SchVal) && $SchVal eq '%'
99             && defined($CatVal) && $CatVal eq ''
100             && defined($TblVal) && $TblVal eq '') { # Rule 19b
101 0         0 $Sql = <<'SQL';
102             SELECT NULL TABLE_CAT
103             , NULL TABLE_SCHEM
104             , NULL TABLE_NAME
105             , NULL TABLE_TYPE
106             , NULL REMARKS
107             SQL
108             }
109             elsif ( defined($TypVal) && $TypVal eq '%'
110             && defined($CatVal) && $CatVal eq ''
111             && defined($SchVal) && $SchVal eq ''
112             && defined($TblVal) && $TblVal eq '') { # Rule 19c
113 0         0 $Sql = <<'SQL';
114             SELECT NULL TABLE_CAT
115             , NULL TABLE_SCHEM
116             , NULL TABLE_NAME
117             , t.tt TABLE_TYPE
118             , NULL REMARKS
119             FROM (
120             SELECT 'TABLE' tt UNION
121             SELECT 'VIEW' tt UNION
122             SELECT 'LOCAL TEMPORARY' tt
123             ) t
124             ORDER BY TABLE_TYPE
125             SQL
126             }
127             else {
128 3         9 $Sql = <<'SQL';
129             SELECT *
130             FROM
131             (
132             SELECT NULL TABLE_CAT
133             , NULL TABLE_SCHEM
134             , tbl_name TABLE_NAME
135             , TABLE_TYPE
136             , NULL REMARKS
137             , sql sqlite_sql
138             FROM (
139             SELECT tbl_name, upper(type) TABLE_TYPE, sql
140             FROM sqlite_master
141             WHERE type IN ( 'table','view')
142             UNION ALL
143             SELECT tbl_name, 'LOCAL TEMPORARY' TABLE_TYPE, sql
144             FROM sqlite_temp_master
145             WHERE type IN ( 'table','view')
146             UNION ALL
147             SELECT 'sqlite_master' tbl_name, 'SYSTEM TABLE' TABLE_TYPE, NULL sql
148             UNION ALL
149             SELECT 'sqlite_temp_master' tbl_name, 'SYSTEM TABLE' TABLE_TYPE, NULL sql
150             )
151             )
152             SQL
153 3 50       10 if ( defined $TblVal ) {
154 3         10 push @Where, "TABLE_NAME LIKE '$TblVal'";
155             }
156 3 50       10 if ( defined $TypVal ) {
157 3         5 my $table_type_list;
158 3         142 $TypVal =~ s/^\s+//;
159 3         6 $TypVal =~ s/\s+$//;
160 3         11 my @ttype_list = split (/\s*,\s*/, $TypVal);
161 3         79 foreach my $table_type (@ttype_list) {
162 0 0       0 if ($table_type !~ /^'.*'$/) {
163 0         0 $table_type = "'" . $table_type . "'";
164             }
165 0         0 $table_type_list = join(", ", @ttype_list);
166             }
167 3 50       9 push @Where, "TABLE_TYPE IN (\U$table_type_list)"
168             if $table_type_list;
169             }
170 3 50       18 $Sql .= ' WHERE ' . join("\n AND ", @Where ) . "\n" if @Where;
171 3         6 $Sql .= " ORDER BY TABLE_TYPE, TABLE_SCHEM, TABLE_NAME\n";
172             }
173 3 50       16 my $sth = $dbh->prepare($Sql) or return undef;
174 3 50       3024 $sth->execute or return undef;
175 3         18 $sth;
176             }
177              
178              
179             sub primary_key_info {
180 3     3   36999 my($dbh, $catalog, $schema, $table) = @_;
181              
182 3         7 my @pk_info;
183              
184 3         18 my $sth_tables = $dbh->table_info($catalog, $schema, $table, '');
185              
186             # this is a hack but much simpler than using pragma index_list etc
187             # also the pragma doesn't list 'INTEGER PRIMARK KEY' autoinc PKs!
188 3         150 while ( my $row = $sth_tables->fetchrow_hashref ) {
189 10 100       71 my $sql = $row->{sqlite_sql} or next;
190 8 100       419 next unless $sql =~ /(.*?)\s*PRIMARY\s+KEY\s*(?:\(\s*(.*?)\s*\))?/si;
191 5   100     49 my @pk = split /\s*,\s*/, $2 || '';
192 5 100       16 unless (@pk) {
193 2         6 my $prefix = $1;
194 2         16 $prefix =~ s/.*create\s+table\s+.*?\(//i;
195 2         11 $prefix = (split /\s*,\s*/, $prefix)[-1];
196 2         8 @pk = (split /\s+/, $prefix)[0]; # take first word as name
197             }
198             #warn "GOT PK $row->{TABLE_NAME} (@pk)\n";
199 5         9 my $key_seq = 0;
200 5         9 for my $pk_field (@pk) {
201 7         396 push @pk_info, {
202             TABLE_SCHEM => $row->{TABLE_SCHEM},
203             TABLE_NAME => $row->{TABLE_NAME},
204             COLUMN_NAME => $pk_field,
205             KEY_SEQ => ++$key_seq,
206             PK_NAME => 'PRIMARY KEY',
207             };
208             }
209             }
210              
211 3 50       27 my $sponge = DBI->connect("DBI:Sponge:", '','')
212             or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr");
213 3         3978 my @names = qw(TABLE_CAT TABLE_SCHEM TABLE_NAME COLUMN_NAME KEY_SEQ PK_NAME);
214 7         70 my $sth = $sponge->prepare("column_info $table", {
215 3 50       16 rows => [ map { [ @{$_}{@names} ] } @pk_info ],
  7         9  
216             NUM_OF_FIELDS => scalar @names,
217             NAME => \@names,
218             }) or return $dbh->DBI::set_err($sponge->err(), $sponge->errstr());
219 3         392 return $sth;
220             }
221              
222             sub type_info_all {
223 0     0     my ($dbh) = @_;
224 0           return; # XXX code just copied from DBD::Oracle, not yet thought about
225 0           my $names = {
226             TYPE_NAME => 0,
227             DATA_TYPE => 1,
228             COLUMN_SIZE => 2,
229             LITERAL_PREFIX => 3,
230             LITERAL_SUFFIX => 4,
231             CREATE_PARAMS => 5,
232             NULLABLE => 6,
233             CASE_SENSITIVE => 7,
234             SEARCHABLE => 8,
235             UNSIGNED_ATTRIBUTE => 9,
236             FIXED_PREC_SCALE =>10,
237             AUTO_UNIQUE_VALUE =>11,
238             LOCAL_TYPE_NAME =>12,
239             MINIMUM_SCALE =>13,
240             MAXIMUM_SCALE =>14,
241             SQL_DATA_TYPE =>15,
242             SQL_DATETIME_SUB=>16,
243             NUM_PREC_RADIX =>17,
244             };
245 0           my $ti = [
246             $names,
247             [ 'CHAR', 1, 255, '\'', '\'', 'max length', 1, 1, 3,
248             undef, '0', '0', undef, undef, undef, 1, undef, undef
249             ],
250             [ 'NUMBER', 3, 38, undef, undef, 'precision,scale', 1, '0', 3,
251             '0', '0', '0', undef, '0', 38, 3, undef, 10
252             ],
253             [ 'DOUBLE', 8, 15, undef, undef, undef, 1, '0', 3,
254             '0', '0', '0', undef, undef, undef, 8, undef, 10
255             ],
256             [ 'DATE', 9, 19, '\'', '\'', undef, 1, '0', 3,
257             undef, '0', '0', undef, '0', '0', 11, undef, undef
258             ],
259             [ 'VARCHAR', 12, 1024*1024, '\'', '\'', 'max length', 1, 1, 3,
260             undef, '0', '0', undef, undef, undef, 12, undef, undef
261             ]
262             ];
263 0           return $ti;
264             }
265              
266              
267             1;
268             __END__