| 1 | =head1 NAME | ||
| 2 | |||
| 3 | XAO::DO::FS::Glue::Connect_SQL - basic SQL connection | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | Only provides pure virtual methods that are overriden in actual drivers. | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | =head1 METHODS | ||
| 12 | |||
| 13 | =over | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | ############################################################################### | ||
| 18 | package XAO::DO::FS::Glue::Connect_SQL; | ||
| 19 | use strict; | ||
| 20 | use XAO::Objects; | ||
| 21 | use XAO::Utils; | ||
| 22 | |||
| 23 | use base XAO::Objects->load(objname => 'Atom'); | ||
| 24 | |||
| 25 | use vars qw($VERSION); | ||
| 26 | $VERSION=(0+sprintf('%u.%03u',(q$Id: Connect_SQL.pm,v 2.1 2007/05/09 21:03:09 am Exp $ =~ /\s(\d+)\.(\d+)\s/))) || die "Bad VERSION"; | ||
| 27 | |||
| 28 | ############################################################################### | ||
| 29 | |||
| 30 | =item sql_connect (%) | ||
| 31 | |||
| 32 | Establishes a connection to the database engine given. Arguments are: | ||
| 33 | |||
| 34 | dsn => data source name (DBI:dbname:options) | ||
| 35 | user => optional user name | ||
| 36 | password => optional password | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub sql_connect ($%) { | ||
| 41 | my $self=shift; | ||
| 42 | throw $self ((caller(0))[3])." - pure virtual method called"; | ||
| 43 | } | ||
| 44 | |||
| 45 | ############################################################################### | ||
| 46 | |||
| 47 | =item sql_connected () | ||
| 48 | |||
| 49 | Returns true if the database connection is currently established. | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub sql_connected ($) { | ||
| 54 | return shift->{'sql'} ? 1 : 0; | ||
| 55 | } | ||
| 56 | |||
| 57 | ############################################################################### | ||
| 58 | |||
| 59 | =item sql_disconnect () | ||
| 60 | |||
| 61 | Closes connection to the database. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub sql_disconnect ($) { | ||
| 66 | my $self=shift; | ||
| 67 | throw $self ((caller(0))[3])." - pure virtual method called"; | ||
| 68 | } | ||
| 69 | |||
| 70 | ############################################################################### | ||
| 71 | |||
| 72 | =item sql_do ($;@) | ||
| 73 | |||
| 74 | Sends a single query to the database with no anticipation of any | ||
| 75 | results. Can take arguments just the way prepare/execute do. | ||
| 76 | |||
| 77 | It is allowed to call sql_do() with an array reference in the second | ||
| 78 | argument. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub sql_do ($$;@) { | ||
| 83 | my ($self,$query,@values)=@_; | ||
| 84 | throw $self ((caller(0))[3])." - pure virtual method called"; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub sql_do_no_error ($$;@) { | ||
| 88 | my ($self,$query)=@_; | ||
| 89 | throw $self ((caller(0))[3])." - pure virtual method called"; | ||
| 90 | } | ||
| 91 | |||
| 92 | ############################################################################### | ||
| 93 | |||
| 94 | =item sql_execute ($;@) | ||
| 95 | |||
| 96 | Executes a previously prepared statement optionally substituting some | ||
| 97 | values, see sql_prepare(). Example: | ||
| 98 | |||
| 99 | my $pq=$self->sql_prepare("SELECT a,b FROM c WHERE d=?"); | ||
| 100 | foreach my $value (1..10) { | ||
| 101 | $self->sql_execute($pq,$i); | ||
| 102 | ... | ||
| 103 | } | ||
| 104 | |||
| 105 | As a shortcut it can also accept a text query in the first argument | ||
| 106 | instead of manually calling sql_prepare() first. It is suggested to do | ||
| 107 | so whenever you plan to call sql_execute() just once. | ||
| 108 | |||
| 109 | It is allowed to call sql_execute() with an array reference in the | ||
| 110 | second argument. | ||
| 111 | |||
| 112 | Returns a piece of data that should be passed into sql_fetch_row() | ||
| 113 | method. | ||
| 114 | |||
| 115 | When done sql_finish() should be called with the return value of | ||
| 116 | sql_execute() as a parameter. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub sql_execute ($$;@) { | ||
| 121 | my ($self,$pq,@values)=@_; | ||
| 122 | throw $self ((caller(0))[3])." - pure virtual method called"; | ||
| 123 | } | ||
| 124 | |||
| 125 | ############################################################################### | ||
| 126 | |||
| 127 | =item sql_fetch_row ($) | ||
| 128 | |||
| 129 | Returns a reference to an array containing next retrieved row. Example: | ||
| 130 | |||
| 131 | my $qr=$self->sql_execute("SELECT a,b FROM c"); | ||
| 132 | my $row=$self->sql_fetch_row($qr); | ||
| 133 | |||
| 134 | Don't forget to call sql_finish() when you're done. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub sql_fetch_row ($$) { | ||
| 139 | my ($self,$qr)=@_; | ||
| 140 | throw $self ((caller(0))[3])." - pure virtual method called"; | ||
| 141 | } | ||
| 142 | |||
| 143 | ############################################################################### | ||
| 144 | |||
| 145 | =item sql_finish ($) | ||
| 146 | |||
| 147 | Frees up whatever internal structures might be occupied as a result of | ||
| 148 | the previous call to sql_execute(). Example: | ||
| 149 | |||
| 150 | my $qr=$self->sql_execute("SHOW TABLES"); | ||
| 151 | ... | ||
| 152 | $self->sql_finish($qr); | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub sql_finish ($$) { | ||
| 157 | my ($self,$qr)=@_; | ||
| 158 | throw $self ((caller(0))[3])." - pure virtual method called"; | ||
| 159 | } | ||
| 160 | |||
| 161 | ############################################################################### | ||
| 162 | |||
| 163 | =item sql_first_column ($) | ||
| 164 | |||
| 165 | An optimisation method -- returns a reference to the array containing | ||
| 166 | all first elements of each row of the results set. Example: | ||
| 167 | |||
| 168 | my $qr=$self->sql_execute("SELECT unique_id FROM a"); | ||
| 169 | my $ids=$self->sql_first_column($qr); | ||
| 170 | |||
| 171 | There is no need to call sql_finish() after sql_first_column(). | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub sql_first_column ($$) { | ||
| 176 | my ($self,$qr)=@_; | ||
| 177 | throw $self ((caller(0))[3])." - pure virtual method called"; | ||
| 178 | } | ||
| 179 | |||
| 180 | ############################################################################### | ||
| 181 | |||
| 182 | =item sql_first_row ($) | ||
| 183 | |||
| 184 | An optimisation method -- returns a reference to the first row and | ||
| 185 | finished the query. | ||
| 186 | |||
| 187 | my $qr=$self->sql_execute("SELECT a,b,c FROM t WHERE d=?",$uid); | ||
| 188 | my $row=$self->sql_first_row($qr); | ||
| 189 | |||
| 190 | There is no need to call sql_finish() after sql_first_row(). | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub sql_first_row ($$) { | ||
| 195 | my ($self,$qr)=@_; | ||
| 196 | my $row=$self->sql_fetch_row($qr); | ||
| 197 | $self->sql_finish($qr); | ||
| 198 | return $row; | ||
| 199 | } | ||
| 200 | |||
| 201 | ############################################################################### | ||
| 202 | |||
| 203 | =item sql_prepare ($) | ||
| 204 | |||
| 205 | Prepares a query for subsequent execution using sql_execute() | ||
| 206 | method. Guaranteed to return a reference of some sort. Example: | ||
| 207 | |||
| 208 | my $pq=$self->sql_prepare("SELECT a,b FROM c WHERE d=?"); | ||
| 209 | $self->sql_execute($pq,123); | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | sub sql_prepare ($$) { | ||
| 214 | my ($self,$query)=@_; | ||
| 215 | throw $self ((caller(0))[3])." - pure virtual method called"; | ||
| 216 | } | ||
| 217 | |||
| 218 | ############################################################################### | ||
| 219 | 1; | ||
| 220 | __END__ |