| 1 | # $Id: MappedQuery.pm,v 1.23 2009/06/03 06:47:32 severin Exp $ | ||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | MQdb::MappedQuery - DESCRIPTION of Object | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | An Object_relational_mapping (ORM) design pattern based on mapping | ||
| 9 | named_column results from any query into attributes of an object. | ||
| 10 | As long as the column_names are parsable into attributes, any query is ok. | ||
| 11 | This is an evolution of several ideas I have either used or created over | ||
| 12 | the last 15 years of coding. This is a variation on the ActiveRecord design | ||
| 13 | pattern but it trades more flexibility, power and control for slightly | ||
| 14 | less automation. It still provides a development speed/ease advange | ||
| 15 | over many ORM patterns. | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | MappedQuery is an abstract superclass that is a variation on the | ||
| 20 | ActiveRecord design pattern. Instead of actively mapping | ||
| 21 | a table into an object, this will actively map the result of | ||
| 22 | a query into an object. The query is standardized for a subclass | ||
| 23 | of this object, and the columns returned by the query define | ||
| 24 | the attributes of the object. This gives much more flexibility | ||
| 25 | than the standard implementation of ActiveRecord. Since this | ||
| 26 | design pattern is based around mapping a query (from potentially a | ||
| 27 | multiple table join) to a single class object, this pattern is | ||
| 28 | called MappedQuery. | ||
| 29 | |||
| 30 | In this particular implementation of this design pattern | ||
| 31 | (mainly due to some limitations in perl) several aspects | ||
| 32 | must be hand coded as part of the implementation of a | ||
| 33 | subclass. Subclasses must handcode | ||
| 34 | - all accessor methods | ||
| 35 | - override the mapRow method | ||
| 36 | - APIs for all explicit fetch methods | ||
| 37 | (by using the superclass fetch_single and fetch_multiple) | ||
| 38 | - the store methods are coded by general DBI code (no framework assistance) | ||
| 39 | |||
| 40 | Individual MQdb::Database handle objects are assigned at an instance level | ||
| 41 | for each object. This is different from some ActiveRecord implementations | ||
| 42 | which place database handles into a global context or at the Class level. | ||
| 43 | By placing it with each instance, this allows creation of instances of the | ||
| 44 | same class pulled from two different databases, but with similar schemas. | ||
| 45 | This is very useful when building certain types of data analysis systems. | ||
| 46 | |||
| 47 | The only restriction is that the database handle must be able run the | ||
| 48 | queries that the object requires for it to work. | ||
| 49 | |||
| 50 | Future implementations could do more automatic code generation | ||
| 51 | but this version already speeds development time by 2x-3x | ||
| 52 | without imposing any limitations and retains all the flexibility | ||
| 53 | of handcoding with DBI. | ||
| 54 | |||
| 55 | =head1 CONTACT | ||
| 56 | |||
| 57 | Jessica Severin <jessica.severin@gmail.com> | ||
| 58 | |||
| 59 | =head1 LICENSE | ||
| 60 | |||
| 61 | * Software License Agreement (BSD License) | ||
| 62 | * MappedQueryDB [MQdb] toolkit | ||
| 63 | * copyright (c) 2006-2009 Jessica Severin | ||
| 64 | * All rights reserved. | ||
| 65 | * Redistribution and use in source and binary forms, with or without | ||
| 66 | * modification, are permitted provided that the following conditions are met: | ||
| 67 | * * Redistributions of source code must retain the above copyright | ||
| 68 | * notice, this list of conditions and the following disclaimer. | ||
| 69 | * * Redistributions in binary form must reproduce the above copyright | ||
| 70 | * notice, this list of conditions and the following disclaimer in the | ||
| 71 | * documentation and/or other materials provided with the distribution. | ||
| 72 | * * Neither the name of Jessica Severin nor the | ||
| 73 | * names of its contributors may be used to endorse or promote products | ||
| 74 | * derived from this software without specific prior written permission. | ||
| 75 | * | ||
| 76 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY | ||
| 77 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 78 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 79 | * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
| 80 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| 81 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 82 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
| 83 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 84 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| 85 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 86 | |||
| 87 | =head1 APPENDIX | ||
| 88 | |||
| 89 | The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _ | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | $VERSION=0.954; | ||
| 94 | |||
| 95 | package MQdb::MappedQuery; | ||
| 96 | |||
| 97 | use strict; | ||
| 98 | use MQdb::Database; | ||
| 99 | use MQdb::DBStream; | ||
| 100 | |||
| 101 | use MQdb::DBObject; | ||
| 102 | our @ISA = qw(MQdb::DBObject); | ||
| 103 | |||
| 104 | |||
| 105 | ################################################# | ||
| 106 | # Subclass must override these methods | ||
| 107 | ################################################# | ||
| 108 | |||
| 109 | =head2 mapRow | ||
| 110 | |||
| 111 | Description: This method must be overridden by subclasses to do the mapping of columns | ||
| 112 | from the query response into attributes of the object. This is part of the | ||
| 113 | internal factory machinery. The instance of the class is created before this method is | ||
| 114 | called and the default init() method has already been called. The purpose of this | ||
| 115 | method is to initialize the rest of the state of the instance based on the $row_hash | ||
| 116 | Arg (1) : $row_hash perl hash | ||
| 117 | Arg (2) : optional $dbc DBI connection (not generally used by most sublcasses) | ||
| 118 | Returntype : $self | ||
| 119 | Exceptions : none | ||
| 120 | Caller : only called by internal factory methods | ||
| 121 | Example : | ||
| 122 | sub mapRow { | ||
| 123 | my $self = shift; | ||
| 124 | my $rowHash = shift; | ||
| 125 | |||
| 126 | $self->primary_id($rowHash->{'symbol_id'}); | ||
| 127 | $self->type($rowHash->{'sym_type'}); | ||
| 128 | $self->symbol($rowHash->{'sym_value'}); | ||
| 129 | return $self; | ||
| 130 | } | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | sub mapRow { | ||
| 135 | my $self = shift; | ||
| 136 | my $row_hash = shift; | ||
| 137 | my $dbc = shift; #optional | ||
| 138 | |||
| 139 | die("mapRow must be implemented by subclasses"); | ||
| 140 | #should by implemented by subclass to map columns into instance variables | ||
| 141 | |||
| 142 | return $self; | ||
| 143 | } | ||
| 144 | |||
| 145 | =head2 store | ||
| 146 | |||
| 147 | Description: This method is just an empty template as part of the API definition. | ||
| 148 | How it is defined, and how parameters are handled are completely up to each | ||
| 149 | subclass. Each subclass should override and implement. | ||
| 150 | Returntype : $self | ||
| 151 | Exceptions : none | ||
| 152 | Caller : general loader scripts | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub store { | ||
| 157 | my $self = shift; | ||
| 158 | die("store must be implemented by subclass"); | ||
| 159 | } | ||
| 160 | |||
| 161 | ################################################# | ||
| 162 | # Factory methods | ||
| 163 | ################################################# | ||
| 164 | |||
| 165 | =head2 fetch_single | ||
| 166 | |||
| 167 | Description: General purpose template method for fetching a single instance | ||
| 168 | of this class(subclass) using the mapRow method to convert | ||
| 169 | a row of data into an object. | ||
| 170 | Arg (1) : $database (MQdb::Database) | ||
| 171 | Arg (2) : $sql (string of SQL statement with place holders) | ||
| 172 | Arg (3...) : optional parameters to map to the placehodlers within the SQL | ||
| 173 | Returntype : instance of this Class (subclass) | ||
| 174 | Exceptions : none | ||
| 175 | Caller : subclasses (not public methods) | ||
| 176 | Example : | ||
| 177 | sub fetch_by_id { | ||
| 178 | my $class = shift; | ||
| 179 | my $db = shift; | ||
| 180 | my $id = shift; | ||
| 181 | my $sql = "SELECT * FROM symbol WHERE symbol_id=?"; | ||
| 182 | return $class->fetch_single($db, $sql, $id); | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | sub fetch_single { | ||
| 189 | my $class = shift; | ||
| 190 | my $db = shift; | ||
| 191 | my $sql = shift; | ||
| 192 | my @params = @_; | ||
| 193 | |||
| 194 | die("no database defined\n") unless($db); | ||
| 195 | my $dbc = $db->get_connection; | ||
| 196 | my $sth = $dbc->prepare($sql, { ora_auto_lob => 0 }); | ||
| 197 | $sth->execute(@params); | ||
| 198 | |||
| 199 | my $obj = undef; | ||
| 200 | my $row_hash = $sth->fetchrow_hashref; | ||
| 201 | if($row_hash) { | ||
| 202 | $obj = $class->new(); | ||
| 203 | $obj->database($db); | ||
| 204 | $obj = $obj->mapRow($row_hash, $dbc); #required by subclass | ||
| 205 | } | ||
| 206 | |||
| 207 | $sth->finish; | ||
| 208 | return $obj; | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | =head2 fetch_multiple | ||
| 213 | |||
| 214 | Description: General purpose template method for fetching an array of instance | ||
| 215 | of this class(subclass) using the mapRow method to convert | ||
| 216 | a row of data into an object. | ||
| 217 | Arg (1) : $database (MQdb::Database) | ||
| 218 | Arg (2) : $sql (string of SQL statement with place holders) | ||
| 219 | Arg (3...) : optional parameters to map to the placehodlers within the SQL | ||
| 220 | Returntype : array of all instances of this Class (subclass) which match the query | ||
| 221 | Exceptions : none | ||
| 222 | Caller : subclasses (not public methods) | ||
| 223 | Example : | ||
| 224 | sub fetch_all_by_value { | ||
| 225 | my $class = shift; | ||
| 226 | my $db = shift; | ||
| 227 | my $name = shift; | ||
| 228 | my $sql = "SELECT * FROM symbol WHERE sym_value=?"; | ||
| 229 | return $class->fetch_multiple($db, $sql, $name); | ||
| 230 | } | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | sub fetch_multiple { | ||
| 235 | my $class = shift; | ||
| 236 | my $db = shift; | ||
| 237 | my $sql = shift; | ||
| 238 | my @params = @_; | ||
| 239 | |||
| 240 | die("no database defined\n") unless($db); | ||
| 241 | my $obj_list = []; | ||
| 242 | |||
| 243 | my $dbc = $db->get_connection; | ||
| 244 | my $sth = $dbc->prepare($sql, { ora_auto_lob => 0 }); | ||
| 245 | eval { $sth->execute(@params); }; | ||
| 246 | if($@) { | ||
| 247 | printf("ERROR with query: %s\n", $sql); | ||
| 248 | printf(" params: "); | ||
| 249 | foreach my $param (@params) { | ||
| 250 | print("'%s' ", $param); | ||
| 251 | } | ||
| 252 | print("\n"); | ||
| 253 | die; | ||
| 254 | } | ||
| 255 | |||
| 256 | while(my $row_hash = $sth->fetchrow_hashref) { | ||
| 257 | |||
| 258 | my $obj = $class->new(); | ||
| 259 | $obj->database($db); | ||
| 260 | $obj = $obj->mapRow($row_hash, $dbc); #required by subclass | ||
| 261 | |||
| 262 | push @$obj_list, $obj; | ||
| 263 | } | ||
| 264 | $sth->finish; | ||
| 265 | return $obj_list; | ||
| 266 | } | ||
| 267 | |||
| 268 | |||
| 269 | =head2 stream_multiple | ||
| 270 | |||
| 271 | Description: General purpose template method for fetching multiple instance | ||
| 272 | of this class(subclass) using the mapRow method to convert | ||
| 273 | a row of data into an object. Instead of instantiating all | ||
| 274 | instance at once and returning as array, this method returns | ||
| 275 | a DBStream instance which then creates each instance from an | ||
| 276 | open handle on each $stream->next_in_stream() call. | ||
| 277 | Arg (1) : $database (MQdb::Database) | ||
| 278 | Arg (2) : $sql (string of SQL statement with place holders) | ||
| 279 | Arg (3...) : optional parameters to map to the placehodlers within the SQL | ||
| 280 | Returntype : DBStream object | ||
| 281 | Exceptions : none | ||
| 282 | Caller : subclasses use this internally when creating new API stream_by....() methods | ||
| 283 | Example : | ||
| 284 | sub stream_by_value { | ||
| 285 | my $class = shift; | ||
| 286 | my $db = shift; | ||
| 287 | my $name = shift; | ||
| 288 | my $sql = "SELECT * FROM symbol WHERE sym_value=?"; | ||
| 289 | return $class->stream_multiple($db, $sql, $name); | ||
| 290 | } | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub stream_multiple { | ||
| 295 | my $class = shift; | ||
| 296 | my $db = shift; | ||
| 297 | my $sql = shift; | ||
| 298 | my @params = @_; | ||
| 299 | |||
| 300 | die("no database defined\n") unless($db); | ||
| 301 | |||
| 302 | my $stream = new MQdb::DBStream(db=>$db, class=>$class); | ||
| 303 | $stream->prepare($sql, @params); | ||
| 304 | return $stream; | ||
| 305 | } | ||
| 306 | |||
| 307 | |||
| 308 | =head2 fetch_col_value | ||
| 309 | |||
| 310 | Description: General purpose function to allow fetching of a single column from a single row. | ||
| 311 | Arg (1) : $sql (string of SQL statement with place holders) | ||
| 312 | Arg (2...) : optional parameters to map to the placehodlers within the SQL | ||
| 313 | Example : $value = $self->fetch_col_value($db, | ||
| 314 | "select symbol_id from symbol where sym_type=? and sym_value=?", | ||
| 315 | $type,$value); | ||
| 316 | Returntype : scalar value | ||
| 317 | Exceptions : none | ||
| 318 | Caller : within subclasses to easy development | ||
| 319 | |||
| 320 | =cut | ||
| 321 | |||
| 322 | sub fetch_col_value { | ||
| 323 | my $class = shift; | ||
| 324 | my $db = shift; | ||
| 325 | my $sql = shift; | ||
| 326 | my @params = @_; | ||
| 327 | |||
| 328 | die("no database defined\n") unless($db); | ||
| 329 | my $dbc = $db->get_connection; | ||
| 330 | my $sth = $dbc->prepare($sql); | ||
| 331 | $sth->execute(@params); | ||
| 332 | my ($value) = $sth->fetchrow_array(); | ||
| 333 | $sth->finish; | ||
| 334 | return $value; | ||
| 335 | } | ||
| 336 | |||
| 337 | |||
| 338 | =head2 fetch_col_array | ||
| 339 | |||
| 340 | Description: General purpose function to allow fetching of a single column from many rows into a simple array. | ||
| 341 | Arg (1) : $sql (string of SQL statement with place holders) | ||
| 342 | Arg (2...) : optional parameters to map to the placehodlers within the SQL | ||
| 343 | Example : $array_ref = $self->fetch_col_array($db, "select some_column from my_table where source_id=?", $id); | ||
| 344 | Returntype : array reference of scalar values | ||
| 345 | Exceptions : none | ||
| 346 | Caller : within subclasses to easy development | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | sub fetch_col_array { | ||
| 351 | my $class = shift; | ||
| 352 | my $db = shift; | ||
| 353 | my $sql = shift; | ||
| 354 | my @params = @_; | ||
| 355 | |||
| 356 | my @col_array=(); | ||
| 357 | |||
| 358 | die("no database defined\n") unless($db); | ||
| 359 | my $dbc = $db->get_connection; | ||
| 360 | my $sth = $dbc->prepare($sql); | ||
| 361 | $sth->execute(@params); | ||
| 362 | |||
| 363 | while(my ($value) = $sth->fetchrow_array()) { | ||
| 364 | push @col_array, $value; | ||
| 365 | } | ||
| 366 | $sth->finish; | ||
| 367 | return \@col_array; | ||
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | =head2 next_sequence_id | ||
| 372 | |||
| 373 | Description: Convenience method for working with SEQUENCES in ORACLE databases. | ||
| 374 | Arg (1) : $sequenceName | ||
| 375 | Returntype : scalar of the nextval in the sequence | ||
| 376 | Exceptions : none | ||
| 377 | |||
| 378 | =cut | ||
| 379 | |||
| 380 | sub next_sequence_id { | ||
| 381 | my $self = shift; | ||
| 382 | my $sequenceName = shift; | ||
| 383 | |||
| 384 | my $dbh = $self->database->get_connection; | ||
| 385 | |||
| 386 | my $sql = 'select '. $sequenceName . '.nextval from sys.dual'; | ||
| 387 | my $sth = $dbh->prepare($sql); | ||
| 388 | $sth->execute(); | ||
| 389 | my ($dbID) = $sth->fetchrow_array(); | ||
| 390 | $sth->finish; | ||
| 391 | #printf("incremented sequence $sequenceName id:%d\n", $dbID); | ||
| 392 | |||
| 393 | return $dbID; | ||
| 394 | } | ||
| 395 | |||
| 396 | |||
| 397 | |||
| 398 | |||
| 399 | 1; | ||
| 400 | |||
| 401 | |||
| 402 | |||
| 403 | |||
| 404 |