| 1 | #------------------------------------------------------------------------------ | ||
| 2 | # DBO::Visitor::Initialize - Initialize database representation | ||
| 3 | # | ||
| 4 | # DESCRIPTION | ||
| 5 | # A visitor class that performs various checks and initializations on | ||
| 6 | # a DBO database schema representation. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Gareth Rees | ||
| 10 | # | ||
| 11 | # COPYRIGHT | ||
| 12 | # Copyright (c) 1999 Canon Research Centre Europe Ltd/ | ||
| 13 | # | ||
| 14 | # $Id: Initialize.pm,v 1.3 1999/06/29 17:09:31 garethr Exp $ | ||
| 15 | #------------------------------------------------------------------------------ | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | package DBO::Visitor::Initialize; | ||
| 19 | use base qw(DBO::Visitor); | ||
| 20 | use Class::Multimethods qw(visit_database visit_table visit_column); | ||
| 21 | |||
| 22 | multimethod visit_database => | ||
| 23 | qw(DBO::Visitor::Initialize DBO::Database DBO::Handle) => | ||
| 24 | sub { | ||
| 25 | my ($vis, $db, $handle) = @_; | ||
| 26 | # visit_database(superclass($vis), $db, $handle); | ||
| 27 | call_next_method(); | ||
| 28 | foreach my $table (@{$db->{tables}}) { | ||
| 29 | not exists $db->{tables_by_id}{$table->{id}} | ||
| 30 | or die DBO::Exception->new | ||
| 31 | (ID_REUSED => "Two tables have same id '%s'.", $table->{id}); | ||
| 32 | $db->{tables_by_id}{$table->{id}} = $table; | ||
| 33 | } | ||
| 34 | }; | ||
| 35 | |||
| 36 | multimethod visit_table => | ||
| 37 | qw(DBO::Visitor::Initialize DBO::Table DBO::Handle) => | ||
| 38 | sub { | ||
| 39 | my ($vis, $table, $handle) = @_; | ||
| 40 | $table->{name} or die DBO::Exception->new | ||
| 41 | (NO_TABLE_NAME => "No 'name' property for table."); | ||
| 42 | $table->{id} or $table->{id} = $table->{name}; | ||
| 43 | $vis->{keys} = $table->{keys} = []; | ||
| 44 | delete $vis->{autoincrement}; | ||
| 45 | # visit_table(superclass($vis), $table, $handle); | ||
| 46 | call_next_method(); | ||
| 47 | $table->{autoincrement} = $vis->{autoincrement} if $vis->{autoincrement}; | ||
| 48 | foreach my $col (@{$table->{columns}}) { | ||
| 49 | $table->{columns_by_name}{$col->{name}} = $col; | ||
| 50 | } | ||
| 51 | }; | ||
| 52 | |||
| 53 | multimethod visit_column => | ||
| 54 | qw(DBO::Visitor::Initialize DBO::Column::Base DBO::Handle) => | ||
| 55 | sub { | ||
| 56 | my ($vis, $col, $handle) = @_; | ||
| 57 | $col->{name} or die DBO::Exception->new | ||
| 58 | (NO_COLUMN_NAME => "No 'name' property for column."); | ||
| 59 | }; | ||
| 60 | |||
| 61 | multimethod visit_column => | ||
| 62 | qw(DBO::Visitor::Initialize DBO::Column::Modifier DBO::Handle) => | ||
| 63 | sub { | ||
| 64 | my ($vis, $col, $handle) = @_; | ||
| 65 | $col->{base} or die DBO::Exception->new | ||
| 66 | (NO_BASE_COLUMN => "No base column for modifier."); | ||
| 67 | $col->{base}->isa('DBO::Column') or die DBO::Exception->new | ||
| 68 | (BASE_COLUMN_TYPE => "Base column for modifier is %s, not a DBO::Column.", | ||
| 69 | ref $col->{base}); | ||
| 70 | visit_column($vis, $col->{base}, $handle); | ||
| 71 | $col->{name} = $col->{base}{name}; | ||
| 72 | }; | ||
| 73 | |||
| 74 | multimethod visit_column => | ||
| 75 | qw(DBO::Visitor::Initialize DBO::Column::Key DBO::Handle) => | ||
| 76 | sub { | ||
| 77 | my ($vis, $col, $handle) = @_; | ||
| 78 | push @{$vis->{keys}}, $col; | ||
| 79 | # visit_column($vis, superclass($col), $handle); | ||
| 80 | call_next_method(); | ||
| 81 | }; | ||
| 82 | |||
| 83 | multimethod visit_column => | ||
| 84 | qw(DBO::Visitor::Initialize DBO::Column::AutoIncrement DBO::Handle) => | ||
| 85 | sub { | ||
| 86 | my ($vis, $col, $handle) = @_; | ||
| 87 | # visit_column($vis, superclass($col), $handle); | ||
| 88 | call_next_method(); | ||
| 89 | not exists $vis->{autoincrement} or die DBO::Exception | ||
| 90 | (MULTIPLE_AUTOINCREMENT => "Two autoincrement fields (%s and %s) in table are not supported.", $vis->{autoincrement}, $col->{name}); | ||
| 91 | $vis->{autoincrement} = $col->{name}; | ||
| 92 | }; | ||
| 93 | |||
| 94 | multimethod visit_column => | ||
| 95 | qw(DBO::Visitor::Initialize DBO::Column::Char DBO::Handle) => | ||
| 96 | sub { | ||
| 97 | my ($vis, $col, $handle) = @_; | ||
| 98 | # visit_column($vis, superclass($col), $handle); | ||
| 99 | call_next_method(); | ||
| 100 | defined $col->{max_length} or $col->{max_length} = 10; | ||
| 101 | }; | ||
| 102 | |||
| 103 | multimethod visit_column => | ||
| 104 | qw(DBO::Visitor::Initialize DBO::Column::Text DBO::Handle) => | ||
| 105 | sub { | ||
| 106 | my ($vis, $col, $handle) = @_; | ||
| 107 | # visit_column($vis, superclass($col), $handle); | ||
| 108 | call_next_method(); | ||
| 109 | defined $col->{avg_length} or $col->{avg_length} = 100; | ||
| 110 | defined $col->{max_length} or $col->{max_length} = 1000; | ||
| 111 | }; | ||
| 112 | |||
| 113 | multimethod visit_column => | ||
| 114 | qw(DBO::Visitor::Initialize DBO::Column::Time DBO::Handle) => | ||
| 115 | sub { | ||
| 116 | my ($vis, $col, $handle) = @_; | ||
| 117 | $col->{max_length} = 19; | ||
| 118 | # visit_column($vis, superclass($col), $handle); | ||
| 119 | call_next_method(); | ||
| 120 | defined $col->{accuracy} or $col->{accuracy} = 5; | ||
| 121 | }; | ||
| 122 | |||
| 123 | multimethod visit_column => | ||
| 124 | qw(DBO::Visitor::Initialize DBO::Column::Option DBO::Handle) => | ||
| 125 | sub { | ||
| 126 | my ($vis, $col, $handle) = @_; | ||
| 127 | # visit_column($vis, superclass($col), $handle); | ||
| 128 | call_next_method(); | ||
| 129 | defined $col->{values} or die DBO::Exception->new | ||
| 130 | (NO_OPTION_VALUES => "No values property for option column."); | ||
| 131 | ref $col->{values} eq 'ARRAY' or die DBO::Exception->new | ||
| 132 | (OPTION_VALUES_TYPE => "values property is a %s, (should be ARRAY).", | ||
| 133 | ref $col->{values}); | ||
| 134 | }; | ||
| 135 | |||
| 136 | multimethod visit_column => | ||
| 137 | qw(DBO::Visitor::Initialize DBO::Column::ForeignKey DBO::Handle) => | ||
| 138 | sub { | ||
| 139 | my ($vis, $col, $handle) = @_; | ||
| 140 | # visit_column($vis, superclass($col), $handle); | ||
| 141 | call_next_method(); | ||
| 142 | $col->{foreign_table} or die "No foreign_table option for $col->{name}."; | ||
| 143 | $col->{foreign_col} or die "No foreign_col option for $col->{name}."; | ||
| 144 | }; | ||
| 145 | |||
| 146 | 1; |