| 1 | ############################################################################# | ||
| 2 | ## Name: sqlite.pm | ||
| 3 | ## Purpose: HDB::MOD::sqlite -> for DBD::SQLite | ||
| 4 | ## Author: Graciliano M. P. | ||
| 5 | ## Modified by: | ||
| 6 | ## Created: 14/01/2003 | ||
| 7 | ## RCS-ID: | ||
| 8 | ## Copyright: (c) 2002 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 | # TESTED WITH DBD::SQLite 0.21 on Win32|Linux | ||
| 14 | |||
| 15 | package HDB::MOD::sqlite ; | ||
| 16 | use DBD::SQLite ; | ||
| 17 | |||
| 18 | use strict qw(vars) ; | ||
| 19 | no warnings ; | ||
| 20 | |||
| 21 | our $VERSION = '1.0' ; | ||
| 22 | our @ISA = qw(HDB::MOD) ; | ||
| 23 | |||
| 24 | my (%OPENED_DBH) ; | ||
| 25 | |||
| 26 | my %SQL = ( | ||
| 27 | LIKE => 1 , | ||
| 28 | REGEXP => 0 , | ||
| 29 | LOCK_TABLE => 0 , | ||
| 30 | SHOW => 0 , | ||
| 31 | LIMIT => 1 , | ||
| 32 | TYPES => [qw(VARCHAR TEXT INTEGER FLOAT BOOLEAN CLOB BLOB TIMESTAMP NUMERIC)] , | ||
| 33 | ) ; | ||
| 34 | |||
| 35 | my %HPL = ( | ||
| 36 | UNLINK_DISCONNECT => 1 , | ||
| 37 | ) ; | ||
| 38 | |||
| 39 | ####### | ||
| 40 | # NEW # | ||
| 41 | ####### | ||
| 42 | |||
| 43 | sub new { | ||
| 44 | my $this = shift ; | ||
| 45 | |||
| 46 | $this->{SQL} = \%SQL ; | ||
| 47 | $this->{HPL} = \%HPL ; | ||
| 48 | $this->{name} = 'HDB::SQLite' ; | ||
| 49 | |||
| 50 | bless($this , __PACKAGE__) ; | ||
| 51 | return( $this ) ; | ||
| 52 | } | ||
| 53 | |||
| 54 | ########### | ||
| 55 | # CONNECT # | ||
| 56 | ########### | ||
| 57 | |||
| 58 | sub MOD_connect { | ||
| 59 | my $this = shift ; | ||
| 60 | my ( $pass ) = @_ ; | ||
| 61 | |||
| 62 | my $file = $this->{file} ; | ||
| 63 | |||
| 64 | if ( $OPENED_DBH{$file}{db} ) { | ||
| 65 | $this->{dbh} = $OPENED_DBH{$file}{db} ; | ||
| 66 | $OPENED_DBH{$file}{x}++ ; | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | $this->{dbh} = DBI->connect("dbi:SQLite:dbname=$file", $this->{user} , $pass , { RaiseError => 0 , PrintError => 1 , AutoCommit => 1 }) ; | ||
| 70 | |||
| 71 | if (! $this->{dbh} ) { return $this->Error("Can't connect to db $file!") ;} | ||
| 72 | else { | ||
| 73 | $OPENED_DBH{$file}{db} = $this->{dbh} ; | ||
| 74 | $OPENED_DBH{$file}{x}++ ; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | return $this->{dbh} ; | ||
| 79 | } | ||
| 80 | |||
| 81 | ################## | ||
| 82 | # MOD_DISCONNECT # | ||
| 83 | ################## | ||
| 84 | |||
| 85 | sub MOD_disconnect { | ||
| 86 | my $this = shift ; | ||
| 87 | my $file = $this->{file} ; | ||
| 88 | $this->{dbh}->disconnect if ( $OPENED_DBH{$file} && !$OPENED_DBH{$file}{x} ) ; | ||
| 89 | } | ||
| 90 | |||
| 91 | ############## | ||
| 92 | # DISCONNECT # | ||
| 93 | ############## | ||
| 94 | |||
| 95 | sub disconnect { | ||
| 96 | my $this = shift ; | ||
| 97 | |||
| 98 | my $file = $this->{file} ; | ||
| 99 | $OPENED_DBH{$file}{x}-- ; | ||
| 100 | |||
| 101 | if ( $OPENED_DBH{$file}{x} <= 0 ) { | ||
| 102 | $OPENED_DBH{$file}{db} = undef ; | ||
| 103 | delete $OPENED_DBH{$file} ; | ||
| 104 | } | ||
| 105 | |||
| 106 | $this->SUPER::disconnect(@_) ; | ||
| 107 | } | ||
| 108 | |||
| 109 | ########## | ||
| 110 | # TABLES # | ||
| 111 | ########## | ||
| 112 | |||
| 113 | sub tables { | ||
| 114 | my $this = shift ; | ||
| 115 | |||
| 116 | $this->{sql} = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" ; | ||
| 117 | |||
| 118 | eval{ | ||
| 119 | $this->_undef_sth ; | ||
| 120 | $this->{sth} = $this->dbh->prepare( $this->{sql} ) ; | ||
| 121 | $this->{sth}->execute ; | ||
| 122 | }; | ||
| 123 | if ( $@ ) { $this->Error("SQL error: $this->{sql}") ;} | ||
| 124 | |||
| 125 | my @tables = $this->Return('$') ; | ||
| 126 | |||
| 127 | $this->_undef_sth ; | ||
| 128 | |||
| 129 | return( sort @tables ) ; | ||
| 130 | } | ||
| 131 | |||
| 132 | ################# | ||
| 133 | # TABLE_COLUMNS # | ||
| 134 | ################# | ||
| 135 | |||
| 136 | sub table_columns { | ||
| 137 | my $this = shift ; | ||
| 138 | my ( $table ) = @_ ; | ||
| 139 | |||
| 140 | $table = HDB::CMDS::_format_table_name($table) ; | ||
| 141 | |||
| 142 | if (! $table) { $this->Error('Invalid table!') ; return ;} | ||
| 143 | |||
| 144 | my $sql = $this->select( "sqlite_master" , "name = $table" , cols => 'sql' , '$' ) ; | ||
| 145 | |||
| 146 | my ($cols) = ( $sql =~ /^\s*CREATE\s+TABLE\s+\S+\s*.*?\(\s*(.*?)\s*\)/gs ); | ||
| 147 | $cols .= ' ,' ; | ||
| 148 | my (%cols) = ( $cols =~ /(\w+)\s*(.*?)\s*,/gs ); | ||
| 149 | |||
| 150 | foreach my $Key (sort keys %cols ) { | ||
| 151 | $cols{$Key} =~ s/\s+/ /gs ; | ||
| 152 | $cols{$Key} = "\U$cols{$Key}\E" ; | ||
| 153 | } | ||
| 154 | |||
| 155 | return( %cols ) ; | ||
| 156 | } | ||
| 157 | |||
| 158 | ################# | ||
| 159 | # AUTOINCREMENT # | ||
| 160 | ################# | ||
| 161 | |||
| 162 | sub AUTOINCREMENT { return "INTEGER PRIMARY KEY" ;} | ||
| 163 | |||
| 164 | ######### | ||
| 165 | # LIMIT # | ||
| 166 | ######### | ||
| 167 | |||
| 168 | sub LIMIT { | ||
| 169 | my $this = shift ; | ||
| 170 | my ( $sz , $offset ) = @_ ; | ||
| 171 | my $limit = $offset > 0 ? "$offset,$sz" : $sz ; | ||
| 172 | return( "LIMIT $limit" ) ; | ||
| 173 | } | ||
| 174 | |||
| 175 | ####### | ||
| 176 | # END # | ||
| 177 | ####### | ||
| 178 | |||
| 179 | 1; | ||
| 180 | |||
| 181 |