| 1 | #------------------------------------------------------------------------------ | ||
| 2 | # DBO - Database Objects | ||
| 3 | # | ||
| 4 | # DESCRIPTION | ||
| 5 | # An object-oriented database abstraction layer. | ||
| 6 | # | ||
| 7 | # AUTHOR | ||
| 8 | # Gareth Rees | ||
| 9 | # | ||
| 10 | # COPYRIGHT | ||
| 11 | # Copyright (c) 1999 Canon Research Centre Europe Ltd/ | ||
| 12 | # | ||
| 13 | # $Id: DBO.pm,v 1.4 1999/06/29 17:09:30 garethr Exp $ | ||
| 14 | #------------------------------------------------------------------------------ | ||
| 15 | |||
| 16 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | package DBO; | ||
| 18 | 4 | ▶ | use base 'Exporter'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 4 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 4 | ▶ | use UNIVERSAL 'isa'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | use Class::Multimethods qw(visit_database visit_table); | ||
| 22 | use vars qw($VERSION $DEBUG @EXPORT_OK %EXPORT_TAGS); | ||
| 23 | |||
| 24 | $VERSION = '0.01'; | ||
| 25 | $DEBUG = 0; | ||
| 26 | @EXPORT_OK = qw(Database Table Key Option ForeignKey Char Text Integer Unsigned AutoIncrement Time); | ||
| 27 | %EXPORT_TAGS = (constructors => [qw(Database Table Key Option ForeignKey Char Text Integer Unsigned AutoIncrement Time)]); | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my $class = shift; | ||
| 31 | my $self = bless { @_ }, $class; | ||
| 32 | |||
| 33 | # Check that the schema argument is a DBO::Database. | ||
| 34 | isa($self->{schema},'DBO::Database') | ||
| 35 | or croak(DBO::Exception->new | ||
| 36 | (SCHEMA => "'schema' must be a DBO::Database, not %s.", | ||
| 37 | ref $self->{schema})); | ||
| 38 | |||
| 39 | # Check that the handle argument is a DBO::Handle. | ||
| 40 | isa($self->{handle},'DBO::Handle') | ||
| 41 | or croak(DBO::Exception->new | ||
| 42 | (HANDLE => "'handle' must be a DBO::Handle, not %s.", | ||
| 43 | ref $self->{handle})); | ||
| 44 | |||
| 45 | # Apply the Initialize visitor. | ||
| 46 | require DBO::Visitor::Initialize; | ||
| 47 | $self->apply_to_database('DBO::Visitor::Initialize'); | ||
| 48 | |||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub DESTROY { | ||
| 53 | my $self = shift; | ||
| 54 | $self->{dbh}->disconnect; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub apply_to_database { | ||
| 58 | my $self = shift; | ||
| 59 | my $vis = shift; | ||
| 60 | $vis = $vis->new(@_) unless ref $vis; | ||
| 61 | visit_database($vis, $self->{schema}, $self->{handle}); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub apply_to_table { | ||
| 65 | my $self = shift; | ||
| 66 | my $id = shift; | ||
| 67 | my $table = $self->{schema}->lookup_table($id) | ||
| 68 | or die DBO::Exception->new(NO_SUCH_TABLE => "No such table: %s", $id); | ||
| 69 | my $vis = shift; | ||
| 70 | $vis = $vis->new(@_) unless ref $vis; | ||
| 71 | visit_table($vis, $table, $self->{handle}); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub error { | ||
| 75 | my $self = shift; | ||
| 76 | $self->{error} = shift; | ||
| 77 | } | ||
| 78 | |||
| 79 | #------------------------------------------------------------------------------ | ||
| 80 | # Constructor functions (for convenience) | ||
| 81 | #------------------------------------------------------------------------------ | ||
| 82 | |||
| 83 | sub Database { DBO::Database->new(@_) } | ||
| 84 | sub Table { DBO::Table->new(@_) } | ||
| 85 | sub Key { DBO::Column::Key->new(@_) } | ||
| 86 | sub Option { DBO::Column::Option->new(@_) } | ||
| 87 | sub ForeignKey { DBO::Column::ForeignKey->new(@_) } | ||
| 88 | sub Char { DBO::Column::Char->new(@_) } | ||
| 89 | sub Text { DBO::Column::Text->new(@_) } | ||
| 90 | sub Integer { DBO::Column::Integer->new(@_) } | ||
| 91 | sub Unsigned { DBO::Column::Unsigned->new(@_) } | ||
| 92 | sub AutoIncrement { DBO::Column::AutoIncrement->new(@_) } | ||
| 93 | sub Time { DBO::Column::Time->new(@_) } | ||
| 94 | |||
| 95 | |||
| 96 | #------------------------------------------------------------------------------ | ||
| 97 | # DBO::Handle - handle to a database | ||
| 98 | #------------------------------------------------------------------------------ | ||
| 99 | |||
| 100 | package DBO::Handle; | ||
| 101 | |||
| 102 | package DBO::Handle::DBI; | ||
| 103 | use base 'DBO::Handle'; | ||
| 104 | use vars '$AUTOLOAD'; | ||
| 105 | |||
| 106 | sub connect { | ||
| 107 | my $class = shift; | ||
| 108 | require DBI; | ||
| 109 | my $dbh = DBI->connect(@_) or return; | ||
| 110 | bless \$dbh, $class; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub dosql { | ||
| 114 | my $self = shift; | ||
| 115 | my $sql = join ' ', @_; | ||
| 116 | $$self->do($sql) | ||
| 117 | or croak(DBO::Exception->new | ||
| 118 | (SQL => "Failed to execute SQL statement %s: %s.", | ||
| 119 | $sql, $$self->errstr)); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub AUTOLOAD { | ||
| 123 | my $self = shift; | ||
| 124 | my $method = $AUTOLOAD; | ||
| 125 | $method =~ s/.*://; | ||
| 126 | $$self->$method(@_); | ||
| 127 | } | ||
| 128 | |||
| 129 | package DBO::Handle::DBI::mSQL; | ||
| 130 | use base 'DBO::Handle::DBI'; | ||
| 131 | |||
| 132 | package DBO::Handle::DBI::mysql; | ||
| 133 | use base 'DBO::Handle::DBI'; | ||
| 134 | |||
| 135 | |||
| 136 | #------------------------------------------------------------------------------ | ||
| 137 | # DBO::Exception - class of exceptions | ||
| 138 | #------------------------------------------------------------------------------ | ||
| 139 | |||
| 140 | package DBO::Exception; | ||
| 141 | |||
| 142 | sub new { | ||
| 143 | my $class = shift; | ||
| 144 | my $exception = shift; | ||
| 145 | my $default = shift; | ||
| 146 | my $self = bless { exception => $exception, | ||
| 147 | default => $default, | ||
| 148 | args => [ @_ ] }, $class; | ||
| 149 | warn $self->format if $DBO::DEBUG; | ||
| 150 | return $self; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub format { | ||
| 154 | my $self = shift; | ||
| 155 | sprintf $self->{default}, @{$self->{args}}; | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | #------------------------------------------------------------------------------ | ||
| 160 | # DBO::Database - abstract representation of a database | ||
| 161 | #------------------------------------------------------------------------------ | ||
| 162 | |||
| 163 | package DBO::Database; | ||
| 164 | use Class::Multimethods 'visit_database'; | ||
| 165 | |||
| 166 | sub new { | ||
| 167 | my $class = shift; | ||
| 168 | my $self = bless { @_ }, $class; | ||
| 169 | return $self; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub lookup_table { | ||
| 173 | my $self = shift; | ||
| 174 | my $id = shift; | ||
| 175 | $self->{tables_by_id}{$id}; | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | #------------------------------------------------------------------------------ | ||
| 180 | # DBO::Table - abstract representation of a database table | ||
| 181 | #------------------------------------------------------------------------------ | ||
| 182 | |||
| 183 | package DBO::Table; | ||
| 184 | use Class::Multimethods 'visit_table'; | ||
| 185 | |||
| 186 | sub new { | ||
| 187 | my $class = shift; | ||
| 188 | my $self = bless { @_ }, $class; | ||
| 189 | return $self; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub lookup_column { | ||
| 193 | my $self = shift; | ||
| 194 | my $id = shift; | ||
| 195 | $self->{columns_by_id}{$id}; | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | #------------------------------------------------------------------------------ | ||
| 200 | # DBO::Column - abstract representation of a database column | ||
| 201 | #------------------------------------------------------------------------------ | ||
| 202 | |||
| 203 | package DBO::Column; | ||
| 204 | use Class::Multimethods 'visit_column'; | ||
| 205 | |||
| 206 | sub new { | ||
| 207 | my $class = shift; | ||
| 208 | my $self = bless { @_ }, $class; | ||
| 209 | return $self; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub visit { | ||
| 213 | my $self = shift; | ||
| 214 | my $visitor = shift; | ||
| 215 | visit_column($visitor, $self); | ||
| 216 | } | ||
| 217 | |||
| 218 | package DBO::Column::Modifier; | ||
| 219 | use base 'DBO::Column'; | ||
| 220 | |||
| 221 | package DBO::Column::Key; | ||
| 222 | use base 'DBO::Column::Modifier'; | ||
| 223 | |||
| 224 | package DBO::Column::Option; | ||
| 225 | use base 'DBO::Column::Modifier'; | ||
| 226 | |||
| 227 | package DBO::Column::ForeignKey; | ||
| 228 | use base 'DBO::Column::Modifier'; | ||
| 229 | |||
| 230 | package DBO::Column::Base; | ||
| 231 | use base 'DBO::Column'; | ||
| 232 | |||
| 233 | package DBO::Column::Number; | ||
| 234 | use base 'DBO::Column::Base'; | ||
| 235 | |||
| 236 | package DBO::Column::String; | ||
| 237 | use base 'DBO::Column::Base'; | ||
| 238 | |||
| 239 | package DBO::Column::Char; | ||
| 240 | use base 'DBO::Column::String'; | ||
| 241 | |||
| 242 | package DBO::Column::Text; | ||
| 243 | use base 'DBO::Column::String'; | ||
| 244 | |||
| 245 | package DBO::Column::Integer; | ||
| 246 | use base 'DBO::Column::Number'; | ||
| 247 | |||
| 248 | package DBO::Column::Unsigned; | ||
| 249 | use base 'DBO::Column::Integer'; | ||
| 250 | |||
| 251 | package DBO::Column::AutoIncrement; | ||
| 252 | use base 'DBO::Column::Unsigned'; | ||
| 253 | |||
| 254 | package DBO::Column::Time; | ||
| 255 | use base 'DBO::Column::Char'; | ||
| 256 | |||
| 257 | |||
| 258 | #------------------------------------------------------------------------------ | ||
| 259 | # DBO::Visitor - an action on a database | ||
| 260 | #------------------------------------------------------------------------------ | ||
| 261 | |||
| 262 | package DBO::Visitor; | ||
| 263 | use Class::Multimethods; | ||
| 264 | |||
| 265 | sub new { | ||
| 266 | my $class = shift; | ||
| 267 | my $self = bless { @_ }, $class; | ||
| 268 | return $self; | ||
| 269 | } | ||
| 270 | |||
| 271 | multimethod visit_database => | ||
| 272 | qw(DBO::Visitor DBO::Database DBO::Handle) => | ||
| 273 | sub { | ||
| 274 | my ($vis, $database, $handle) = @_; | ||
| 275 | foreach my $table (@{$database->{tables}}) { | ||
| 276 | visit_table($vis, $table, $handle); | ||
| 277 | } | ||
| 278 | }; | ||
| 279 | |||
| 280 | multimethod visit_table => | ||
| 281 | qw(DBO::Visitor DBO::Table DBO::Handle) => | ||
| 282 | sub { | ||
| 283 | my ($vis, $table, $handle) = @_; | ||
| 284 | foreach my $col (@{$table->{columns}}) { | ||
| 285 | visit_column($vis, $col, $handle); | ||
| 286 | } | ||
| 287 | }; | ||
| 288 | |||
| 289 | multimethod visit_column => | ||
| 290 | qw(DBO::Visitor DBO::Column::Base DBO::Handle) => | ||
| 291 | sub { | ||
| 292 | # my ($vis, $col, $handle) = @_; | ||
| 293 | }; | ||
| 294 | |||
| 295 | multimethod visit_column => | ||
| 296 | qw(DBO::Visitor DBO::Column::Modifier DBO::Handle) => | ||
| 297 | sub { | ||
| 298 | my ($vis, $col, $handle) = @_; | ||
| 299 | visit_column($vis, $col->{base}, $handle); | ||
| 300 | }; | ||
| 301 | |||
| 302 | 1; | ||
| 303 | |||
| 304 | __END__ | ||