| 1 | package DBR::Manual; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 DRAFT | ||
| 6 | |||
| 7 | B<This documentation is in early draft form!> | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | DBR - Database Repository ORM (object-relational mapper). | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | # common packages | ||
| 16 | use DBR; | ||
| 17 | use DBR::Util::Logger; | ||
| 18 | use DBR::Util::Operator; | ||
| 19 | |||
| 20 | # typical connection | ||
| 21 | my $logger = DBR::Util::Logger->new( -logpath => './dbr.log', -logLevel => 'debug2' ); | ||
| 22 | my $dbr = DBR->new( -logger => $logger, -conf => './dbr.conf' ); | ||
| 23 | my $dbh = $dbr->connect( 'car_dealer' ); # instance handle | ||
| 24 | |||
| 25 | # metadata access (dbr tools) | ||
| 26 | my $meta = $dbr->get_instance( 'dbrconf' ); | ||
| 27 | |||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | DBR stands for Database Repository, and functions as an ORM | ||
| 32 | (Object Relational Mapper) to your database. | ||
| 33 | |||
| 34 | DBR tries to make your use of a database safe, concise, efficient | ||
| 35 | and clear. The objective is to treat your database records like | ||
| 36 | objects, and deliver the most common functionality with a minimum | ||
| 37 | of fuss. You shouldn't have wo worry about efficiency, readability | ||
| 38 | or value translation. | ||
| 39 | |||
| 40 | Admittedly, DBR isn't going to be an instant solution for someone | ||
| 41 | who wants to hit the ground running with a lightweight application. | ||
| 42 | It is primarily designed for large applications with large schemas. | ||
| 43 | (though it's just as capable of handling a two table SQLite database | ||
| 44 | as it is a 1,000 table monster) | ||
| 45 | |||
| 46 | In order to get much of any functionality, you'll have to spend a | ||
| 47 | little time teaching it about your schema(s). The tools included | ||
| 48 | will allow you to scan your schemas, enter relationships, and so on. | ||
| 49 | After you've done this, you can begin to reap the benefits. | ||
| 50 | |||
| 51 | A bit of a disclaimer: DBR isn't going to fit all people's tastes. | ||
| 52 | It's not attempting to be a flexible foundation class or toolkit for | ||
| 53 | you to build your schema specific modules on top of (as many other | ||
| 54 | ORM packages attempt to be.) The intent is to try to solve the ORM | ||
| 55 | problem with a design where metadata is king, not code. | ||
| 56 | |||
| 57 | |||
| 58 | =head2 FEATURES | ||
| 59 | |||
| 60 | =over | ||
| 61 | |||
| 62 | =item concise | ||
| 63 | |||
| 64 | Even when your task requires touching several tables in the underlying database, it takes surpsisingly little code to fetch the data you need. Then you just use the data and the DBR objects will attempt to do the "right thing"(TM). | ||
| 65 | |||
| 66 | For example: | ||
| 67 | |||
| 68 | my $orders = $dbrh->orders->where( 'customer.name' => LIKE '%Jones' ); | ||
| 69 | |||
| 70 | while ( $orders->next ) { | ||
| 71 | print "Order " . $_->order_id . " shipped " . $_->shipment->method->name . "\n"; | ||
| 72 | } | ||
| 73 | |||
| 74 | would be the equivalent of writing out some SQL like: | ||
| 75 | |||
| 76 | select | ||
| 77 | o.order_id, | ||
| 78 | m.name | ||
| 79 | from | ||
| 80 | orders o, | ||
| 81 | customers c, | ||
| 82 | shipments s, | ||
| 83 | ship_methods m | ||
| 84 | where | ||
| 85 | c.name like '%Jones' and | ||
| 86 | o.customer_id = c.cust_id and | ||
| 87 | o.shipment_id = s.shipment_id and | ||
| 88 | s.method_id = m.method_id; | ||
| 89 | |||
| 90 | =item smart | ||
| 91 | |||
| 92 | DBR automatically profiles your code. It remembers what fields you need, | ||
| 93 | and fetches them for you next time. It also reads ahead in the resultset whenever | ||
| 94 | fetching related records. Both of these features prevent it from issuing bazillions of | ||
| 95 | tiny queries to the database. | ||
| 96 | |||
| 97 | =item efficient | ||
| 98 | |||
| 99 | Most DBR objects are blessed arrayrefs instead of hashrefs. Using DBR to fetch | ||
| 100 | your data is almost as fast as using fetchrow_arrayref, but far more powerful. | ||
| 101 | |||
| 102 | Access to large quantities of data are automatically chunked behind the scenes. | ||
| 103 | Stop worrying about blowing up the memory on your server just because you need | ||
| 104 | to retrieve a few million records in one shot. | ||
| 105 | |||
| 106 | =item convenient | ||
| 107 | |||
| 108 | Database fields can hold a representation of data that you can access | ||
| 109 | in raw or formatted form via automatic translators that wrap the field. | ||
| 110 | |||
| 111 | Currently available for Dollars, Unixtime, Percent and Enumeration. | ||
| 112 | |||
| 113 | =item no SQL | ||
| 114 | |||
| 115 | Table joins are replaced with the names of relationships associated with | ||
| 116 | foreign keys. All you end up doing is: | ||
| 117 | |||
| 118 | $car->model->make->name # Don't worry about the underlying DB queries. It's efficient. | ||
| 119 | |||
| 120 | or | ||
| 121 | |||
| 122 | where( 'car.model.make.name' => 'Ford' ) | ||
| 123 | |||
| 124 | =item post-fetch organization | ||
| 125 | |||
| 126 | Create a lookup map (hash): | ||
| 127 | |||
| 128 | # what preference did customers have for shipping method by gender last year? | ||
| 129 | |||
| 130 | $dbh->orders-> | ||
| 131 | where( order.date => BETWEEN( '1/1/2008', '1/1/2009' ) )-> | ||
| 132 | hashmap_multi( 'order.date.month', 'shipment.method.name', 'customer.gender' ); | ||
| 133 | |||
| 134 | or | ||
| 135 | |||
| 136 | # get purchase items grouped by shipment method and order | ||
| 137 | |||
| 138 | $dbh->items-> | ||
| 139 | where( 'order.customer' => $customer_id )-> | ||
| 140 | hashmap_multi( 'order.shipment.method.name', 'order.order_id' ); | ||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | =back | ||
| 145 | |||
| 146 | =head1 CONFIGURATION | ||
| 147 | |||
| 148 | =head2 Configuration File | ||
| 149 | |||
| 150 | Contains the information needed to get to the configuration/metadata database. | ||
| 151 | |||
| 152 | This database contains all the information about schemas and instances you | ||
| 153 | will be connecting to. | ||
| 154 | |||
| 155 | For a SQLite config database, your config will define: | ||
| 156 | |||
| 157 | name=dbrconf | ||
| 158 | class=master | ||
| 159 | dbfile=/path/to/database | ||
| 160 | type=SQLite | ||
| 161 | dbr_bootstrap=1 | ||
| 162 | |||
| 163 | really, all you need to usually do is customize the dbfile path. | ||
| 164 | |||
| 165 | For a MySQL config database, your config will define: | ||
| 166 | |||
| 167 | name=dbrconf | ||
| 168 | class=master | ||
| 169 | hostname=db.host.domain.com | ||
| 170 | database=myapp | ||
| 171 | user=dbusername | ||
| 172 | password=dbpassword | ||
| 173 | type=Mysql | ||
| 174 | dbr_bootstrap=1 | ||
| 175 | |||
| 176 | typically customize just hostname, database, user and password. | ||
| 177 | |||
| 178 | The class could also be "query", for a read-only replicated copy, for example. | ||
| 179 | |||
| 180 | =head2 Bootstrap | ||
| 181 | |||
| 182 | When you create a new() DBR, you typically point it at the above config file. | ||
| 183 | |||
| 184 | From there, all further actions to your application databases are thru the | ||
| 185 | DBR object. | ||
| 186 | |||
| 187 | =head2 Debug Logging | ||
| 188 | |||
| 189 | A common logger instance is required by DBR and shared by all related objects. | ||
| 190 | |||
| 191 | =head1 METADATA | ||
| 192 | |||
| 193 | =head2 Schema | ||
| 194 | |||
| 195 | =head2 Instance | ||
| 196 | |||
| 197 | =head2 Table | ||
| 198 | |||
| 199 | =head2 Field | ||
| 200 | |||
| 201 | =head2 Enumeration | ||
| 202 | |||
| 203 | =head1 API | ||
| 204 | |||
| 205 | Be sure to also check out: | ||
| 206 | |||
| 207 | L<http://code.google.com/p/perl-dbr/wiki/ObjectsAndMethods> | ||
| 208 | |||
| 209 | =head2 DBR | ||
| 210 | |||
| 211 | =head3 new | ||
| 212 | |||
| 213 | =head3 get_instance | ||
| 214 | |||
| 215 | =head3 connect | ||
| 216 | |||
| 217 | =head3 timezone | ||
| 218 | |||
| 219 | =head3 remap | ||
| 220 | |||
| 221 | =head3 unmap | ||
| 222 | |||
| 223 | =head2 Instance Handle | ||
| 224 | |||
| 225 | =head3 TABLENAME | ||
| 226 | |||
| 227 | =head3 begin | ||
| 228 | |||
| 229 | =head3 commit | ||
| 230 | |||
| 231 | =head3 rollback | ||
| 232 | |||
| 233 | =head3 select (v1) | ||
| 234 | |||
| 235 | =head3 insert (v1) | ||
| 236 | |||
| 237 | =head3 update (v1) | ||
| 238 | |||
| 239 | =head3 delete (v1) | ||
| 240 | |||
| 241 | =head2 Object | ||
| 242 | |||
| 243 | =head3 where | ||
| 244 | |||
| 245 | =head3 all | ||
| 246 | |||
| 247 | =head3 get | ||
| 248 | |||
| 249 | =head3 insert | ||
| 250 | |||
| 251 | =head3 enum | ||
| 252 | |||
| 253 | =head3 parse | ||
| 254 | |||
| 255 | =head2 Resultset | ||
| 256 | |||
| 257 | =head3 next | ||
| 258 | |||
| 259 | =head3 each | ||
| 260 | |||
| 261 | =head3 hashmap_multi | ||
| 262 | |||
| 263 | =head3 hashmap_single | ||
| 264 | |||
| 265 | =head3 count | ||
| 266 | |||
| 267 | =head3 values | ||
| 268 | |||
| 269 | =head3 raw_hashrefs | ||
| 270 | |||
| 271 | =head3 raw_arrayrefs | ||
| 272 | |||
| 273 | =head3 raw_keycol | ||
| 274 | |||
| 275 | =head2 Record | ||
| 276 | |||
| 277 | =head3 FIELDNAME | ||
| 278 | |||
| 279 | =head3 get | ||
| 280 | |||
| 281 | =head3 RELATIONNAME | ||
| 282 | |||
| 283 | =head3 gethash | ||
| 284 | |||
| 285 | =head3 set | ||
| 286 | |||
| 287 | =head3 delete | ||
| 288 | |||
| 289 | =head2 Translator | ||
| 290 | |||
| 291 | =head3 Dollars | ||
| 292 | |||
| 293 | =head4 dollars | ||
| 294 | |||
| 295 | =head4 format | ||
| 296 | |||
| 297 | =head4 cents | ||
| 298 | |||
| 299 | =head3 Unixtime | ||
| 300 | |||
| 301 | =head4 date | ||
| 302 | |||
| 303 | =head4 time | ||
| 304 | |||
| 305 | =head4 datetime | ||
| 306 | |||
| 307 | =head4 fancytime | ||
| 308 | |||
| 309 | =head4 fancydate | ||
| 310 | |||
| 311 | =head4 fancydatetime | ||
| 312 | |||
| 313 | =head4 unixtime | ||
| 314 | |||
| 315 | =head4 midnight | ||
| 316 | |||
| 317 | =head4 endofday | ||
| 318 | |||
| 319 | =head3 Enum | ||
| 320 | |||
| 321 | =head4 handle | ||
| 322 | |||
| 323 | =head4 name | ||
| 324 | |||
| 325 | =head4 in | ||
| 326 | |||
| 327 | =head4 id | ||
| 328 | |||
| 329 | =head2 Operators | ||
| 330 | |||
| 331 | use DBR::Util::Operator; | ||
| 332 | |||
| 333 | =head3 GT (greater-than) | ||
| 334 | |||
| 335 | where( 'item.price' => GT 3.59 ); | ||
| 336 | |||
| 337 | =head3 LT (less-than) | ||
| 338 | |||
| 339 | where( 'item.price' => LT 3.59 ); | ||
| 340 | |||
| 341 | =head3 GE (greater-than-or-equal-to) | ||
| 342 | |||
| 343 | where( 'item.price' => GE 3.59 ); | ||
| 344 | |||
| 345 | =head3 LE (less-than-or-equal-to) | ||
| 346 | |||
| 347 | where( 'item.price' => LE 3.59 ); | ||
| 348 | |||
| 349 | =head3 NOT | ||
| 350 | |||
| 351 | =head3 LIKE | ||
| 352 | |||
| 353 | where( name => LIKE( '%ing' ) | ||
| 354 | |||
| 355 | =head3 NOTLIKE | ||
| 356 | |||
| 357 | where( name => LIKE( 'George%' ) | ||
| 358 | |||
| 359 | =head3 BETWEEN | ||
| 360 | |||
| 361 | where( price => BETWEEN( 3.00, 4.50 ) ) | ||
| 362 | |||
| 363 | =head3 NOTBETWEEN | ||
| 364 | |||
| 365 | where( price => NOTBETWEEN( 50, 100 ) ) | ||
| 366 | |||
| 367 | =head2 Logger | ||
| 368 | |||
| 369 | =head3 logErr | ||
| 370 | |||
| 371 | =head3 logWarn | ||
| 372 | |||
| 373 | =head3 logInfo | ||
| 374 | |||
| 375 | =head3 logDebug | ||
| 376 | |||
| 377 | verbose | ||
| 378 | |||
| 379 | =head3 logDebug2 | ||
| 380 | |||
| 381 | very verbose | ||
| 382 | |||
| 383 | =head3 logDebug3 | ||
| 384 | |||
| 385 | really, really verbose | ||
| 386 | |||
| 387 | =head1 TOOLS | ||
| 388 | |||
| 389 | =head2 dbr-admin | ||
| 390 | |||
| 391 | =head2 dbr-load-spec | ||
| 392 | |||
| 393 | =head2 dbr-dump-spec | ||
| 394 | |||
| 395 | =head2 dbr-scan-db | ||
| 396 | |||
| 397 | =head1 TODO | ||
| 398 | |||
| 399 | =over | ||
| 400 | |||
| 401 | =item dbr-admin | ||
| 402 | |||
| 403 | This tool works, but it has various quirks that make it a bit | ||
| 404 | difficult to use at present. | ||
| 405 | |||
| 406 | =item fast metadata browser | ||
| 407 | |||
| 408 | DBR needs a fast and effective browser, especially for looking up | ||
| 409 | the relationship names and enumeration handles. The dbr-admin tool | ||
| 410 | is helpful but clunky, the dbr-spec-dump also can be use for this | ||
| 411 | purpose, but... | ||
| 412 | |||
| 413 | =item cross-module scoping | ||
| 414 | |||
| 415 | DBR's current pre-fetch scoping support only works within a file. | ||
| 416 | It needs to be extended. For now, don't pass DBR objects between | ||
| 417 | packages, unless they are in the same file. | ||
| 418 | |||
| 419 | =item debug support | ||
| 420 | |||
| 421 | Attempting to Dumper data with DBR objects will explode with metadata | ||
| 422 | you never wanted to see. DBR needs dumper support. | ||
| 423 | |||
| 424 | =item OR support | ||
| 425 | |||
| 426 | DBR doesn't want to you to make an OR query. It usually results in | ||
| 427 | an inefficient table scan. In fact, you just can't with DBR! | ||
| 428 | All the constraints in a where() are ANDed together. | ||
| 429 | |||
| 430 | Should you be able to OR? | ||
| 431 | |||
| 432 | =back | ||
| 433 | |||
| 434 | =head1 BUGS | ||
| 435 | |||
| 436 | =head2 MySQL sub-queries | ||
| 437 | |||
| 438 | DBR will magically perform a sub-query when you use the reverse | ||
| 439 | direction of a relationship. For example, given an e-commerce | ||
| 440 | scenario with an Order that has Items, the item has a foreign key | ||
| 441 | to its order, so C<order.items> would be traversing that foreign | ||
| 442 | key in the reverse direction, causing a sub-query of items matching | ||
| 443 | the order's pkey. MySQL handles the sub-query very poorly, and | ||
| 444 | depending on the volume of data, you may need to identify the order pkeys | ||
| 445 | of interest first, and then use C<< where( order_id => $order_ids ) >> | ||
| 446 | explicitly instead. | ||
| 447 | |||
| 448 | =head2 perl-dbr project | ||
| 449 | |||
| 450 | See L<http://code.google.com/p/perl-dbr/issues/list>. | ||
| 451 | |||
| 452 | =head1 SEE ALSO | ||
| 453 | |||
| 454 | Google Code project: L<http://code.google.com/p/perl-dbr> | ||
| 455 | |||
| 456 | API Diagram: L<http://code.google.com/p/perl-dbr/wiki/ObjectsAndMethods> | ||
| 457 | |||
| 458 | =head1 AUTHOR | ||
| 459 | |||
| 460 | Daniel Norman, C<dnorman@drjays.com>. | ||
| 461 | |||
| 462 | =head1 COPYRIGHT AND LICENSE | ||
| 463 | |||
| 464 | Copyright 2009, Daniel Norman | ||
| 465 | |||
| 466 | This library is free software; you can redistribute it and/or modify | ||
| 467 | it under the same terms as Perl itself. | ||
| 468 | |||
| 469 | =cut | ||
| 470 | |||
| 471 | 1; |