| 1 | package Oryx::DBM; | ||
| 2 | |||
| 3 | use DBM::Deep; | ||
| 4 | use Oryx::DBM::Class; | ||
| 5 | use Oryx::DBM::Util; | ||
| 6 | use Oryx::Class; | ||
| 7 | |||
| 8 | use base qw(Oryx Oryx::MetaClass); | ||
| 9 | |||
| 10 | __PACKAGE__->mk_classdata("datapath"); | ||
| 11 | |||
| 12 | our $DEBUG = 0; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Oryx::DBM - DBM Storage interface for Oryx | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | my $storage = Oryx::DBM->new; | ||
| 21 | |||
| 22 | $storage->connect([ 'dbm:Deep:datapath=/path/to/datafiles' ]); | ||
| 23 | |||
| 24 | $storage->dbh; | ||
| 25 | $storage->db_name; | ||
| 26 | $storage->ping; | ||
| 27 | $storage->schema; | ||
| 28 | $storage->util; | ||
| 29 | $storage->set_util; | ||
| 30 | $storage->deploy_class; | ||
| 31 | $storage->deploy_schema; | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | DBM Storage interface for Oryx. You should not need to instantiate | ||
| 36 | this directly, use C<< Oryx->connect() >> instead. | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | =over | ||
| 41 | |||
| 42 | =item new | ||
| 43 | |||
| 44 | Simple constructor | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my $class = shift; | ||
| 50 | return bless { }, $class; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | =item dbh | ||
| 55 | |||
| 56 | stub - returns $self | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub dbh { $_[0] } | ||
| 61 | sub commit { } | ||
| 62 | |||
| 63 | =item connect( \@conn, [$schema] ) | ||
| 64 | |||
| 65 | Called by C<< Oryx->connect() >>. You shouldn't need to be doing this. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | # $conn looks like this : ["dbm:Deep:datapath=/path/to/data"] | ||
| 70 | sub connect { | ||
| 71 | my ($self, $conn, $schema) = @_; | ||
| 72 | |||
| 73 | if ($conn->[0] =~ /^dbm:Deep:datapath=(.+)$/) { | ||
| 74 | $self->_croak('ERROR: connect called without a datapath') | ||
| 75 | unless $1; | ||
| 76 | $self->datapath($1); | ||
| 77 | } else { | ||
| 78 | $self->_croak("ERROR: bad dsn $conn->[0]"); | ||
| 79 | } | ||
| 80 | |||
| 81 | $self->catalog(DBM::Deep->new($self->datapath.'/oryx_catalog')); | ||
| 82 | |||
| 83 | $self->init('Oryx::DBM::Class', $conn, $schema); | ||
| 84 | |||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | =item catalog | ||
| 89 | |||
| 90 | L<DBM::Deep> instance for holding the catalog of tables. This is | ||
| 91 | a sort of global internal store for the DBM backend for keeping | ||
| 92 | meta data which it needs. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub catalog { $_[0]->{catalog} = $_[1] if $_[1]; $_[0]->{catalog} } | ||
| 97 | |||
| 98 | =item ping | ||
| 99 | |||
| 100 | ping the database - all this does here is make sure the C<catalog> | ||
| 101 | exists and is a L<DBM::Deep> instance | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub ping { | ||
| 106 | my $self = shift; | ||
| 107 | return UNIVERSAL::isa($self->catalog, 'DBM::Deep'); | ||
| 108 | } | ||
| 109 | |||
| 110 | =item schema | ||
| 111 | |||
| 112 | returns the schema if called with no arguments, otherwise | ||
| 113 | sets if called with a L<Oryx::Schema> instance. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | |||
| 118 | sub schema { | ||
| 119 | my $self = shift; | ||
| 120 | $self->{schema} = shift if @_; | ||
| 121 | $self->{schema}; | ||
| 122 | } | ||
| 123 | |||
| 124 | =item schema | ||
| 125 | |||
| 126 | returns the schema if called with no arguments, otherwise | ||
| 127 | sets if called with a L<Oryx::Schema> instance. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub util { | ||
| 132 | my $self = shift; | ||
| 133 | $self->{util} = shift if @_; | ||
| 134 | $self->{util}; | ||
| 135 | } | ||
| 136 | |||
| 137 | =item set_util | ||
| 138 | |||
| 139 | determines which L<Oryx::DBI::Util> class to instantiate | ||
| 140 | by looking at the dsn passed to C<connect> and sets it | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub set_util { | ||
| 145 | my $self = shift; | ||
| 146 | $self->util( Oryx::DBM::Util->new ); | ||
| 147 | } | ||
| 148 | |||
| 149 | =item deploy_schema( $schema ) | ||
| 150 | |||
| 151 | Takes a L<Oryx::Schema> instance and deploys all classes seen by that | ||
| 152 | schema instance to the database creating all L<DBM::Deep> db files | ||
| 153 | needed for storing your persistent objects. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub deploy_schema { | ||
| 158 | my ($self, $schema) = @_; | ||
| 159 | $schema = $self->schema unless defined $schema; | ||
| 160 | |||
| 161 | $DEBUG && $self->_carp( | ||
| 162 | "deploy_schema $schema : classes => ".join(",\n", $schema->classes) | ||
| 163 | ); | ||
| 164 | unless (-d $self->datapath) { | ||
| 165 | mkdir $self->datapath; | ||
| 166 | } | ||
| 167 | foreach my $class ($schema->classes) { | ||
| 168 | $self->deploy_class($class); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | =item deploy_class( $class ) | ||
| 173 | |||
| 174 | does the work of deploying a given class; called by C<deploy_schema> | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub deploy_class { | ||
| 179 | my ($self, $class) = @_; | ||
| 180 | $DEBUG && $self->_carp("DEPLOYING $class"); | ||
| 181 | $self->util->table_create($self, $class->table); | ||
| 182 | } | ||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | =head1 SEE ALSO | ||
| 187 | |||
| 188 | L<Oryx>, L<Oryx::Class>, L<Oryx::DBM::Util> | ||
| 189 | |||
| 190 | =head1 AUTHOR | ||
| 191 | |||
| 192 | Copyright (C) 2005 Richard Hundt <richard NO SPAM AT protea-systems.com> | ||
| 193 | |||
| 194 | =head1 LICENSE | ||
| 195 | |||
| 196 | This library is free software and may be used under the same terms as Perl itself. | ||
| 197 | |||
| 198 | =cut |