| 1 | package CGI::Application::Demo::Basic::Util::Create; | ||
| 2 | |||
| 3 | # Note: | ||
| 4 | # o tab = 4 spaces || die | ||
| 5 | # | ||
| 6 | # Author: | ||
| 7 | # Ron Savage <ron@savage.net.au> | ||
| 8 | # Home page: http://savage.net.au/index.html | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use CGI::Application::Demo::Basic::Util::Config; | ||
| 14 | |||
| 15 | use DBI; | ||
| 16 | |||
| 17 | use DBIx::Admin::CreateTable; | ||
| 18 | |||
| 19 | use File::Slurp; # For read_file. | ||
| 20 | |||
| 21 | use FindBin::Real; | ||
| 22 | |||
| 23 | use Hash::FieldHash qw(:all); | ||
| 24 | |||
| 25 | fieldhash my %config => 'config'; | ||
| 26 | fieldhash my %config_file_name => 'config_file_name'; | ||
| 27 | fieldhash my %creator => 'creator'; | ||
| 28 | fieldhash my %db_vendor => 'db_vendor'; | ||
| 29 | fieldhash my %dbh => 'dbh'; | ||
| 30 | fieldhash my %sequence => 'sequence'; | ||
| 31 | |||
| 32 | our $VERSION = '1.06'; | ||
| 33 | |||
| 34 | # -------------------------------------------------- | ||
| 35 | |||
| 36 | sub create_all_tables | ||
| 37 | { | ||
| 38 | my($self) = @_; | ||
| 39 | |||
| 40 | $self -> create_faculty_table; | ||
| 41 | $self -> create_log_table; | ||
| 42 | $self -> create_sessions_table; | ||
| 43 | |||
| 44 | my($config) = CGI::Application::Demo::Basic::Util::Config -> new($self -> config_file_name) -> config; | ||
| 45 | my($dsn) = $$config{dsn}; | ||
| 46 | $dsn =~ s/.+dbname=(.+)/$1/; | ||
| 47 | |||
| 48 | if ($self -> db_vendor eq 'SQLITE') | ||
| 49 | { | ||
| 50 | `chmod a+w $dsn`; | ||
| 51 | } | ||
| 52 | |||
| 53 | } # End of create_all_tables. | ||
| 54 | |||
| 55 | # -------------------------------------------------- | ||
| 56 | |||
| 57 | sub create_faculty_table | ||
| 58 | { | ||
| 59 | my($self) = @_; | ||
| 60 | my($table_name) = 'faculty'; | ||
| 61 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 62 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 63 | create table faculty | ||
| 64 | ( | ||
| 65 | faculty_id $primary_key, | ||
| 66 | faculty_name varchar(255) not null | ||
| 67 | ) | ||
| 68 | SQL | ||
| 69 | $self -> report($table_name, 'created', $result); | ||
| 70 | |||
| 71 | } # End of create_faculty_table. | ||
| 72 | |||
| 73 | # -------------------------------------------------- | ||
| 74 | |||
| 75 | sub create_log_table | ||
| 76 | { | ||
| 77 | my($self) = @_; | ||
| 78 | my($table_name) = 'log'; | ||
| 79 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 80 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 81 | create table log | ||
| 82 | ( | ||
| 83 | id $primary_key, | ||
| 84 | timestamp timestamp, | ||
| 85 | lvl varchar(9), | ||
| 86 | message varchar(255) | ||
| 87 | ) | ||
| 88 | SQL | ||
| 89 | $self -> report($table_name, 'created', $result); | ||
| 90 | |||
| 91 | } # End of create_log_table. | ||
| 92 | |||
| 93 | # -------------------------------------------------- | ||
| 94 | # Note: The sessions table is a special case. | ||
| 95 | |||
| 96 | sub create_sessions_table | ||
| 97 | { | ||
| 98 | my($self) = @_; | ||
| 99 | my($table_name) = 'sessions'; | ||
| 100 | my($type) = $self -> db_vendor eq 'ORACLE' ? 'long' : 'text'; | ||
| 101 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 102 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 103 | create table sessions | ||
| 104 | ( | ||
| 105 | id char(32) not null primary key, | ||
| 106 | a_session $type not null | ||
| 107 | ) | ||
| 108 | SQL | ||
| 109 | $self -> report($table_name, 'created', $result); | ||
| 110 | |||
| 111 | } # End of create_sessions_table. | ||
| 112 | |||
| 113 | # -------------------------------------------------- | ||
| 114 | |||
| 115 | sub drop_all_tables | ||
| 116 | { | ||
| 117 | my($self) = @_; | ||
| 118 | |||
| 119 | for my $table_name (qw/faculty log sessions/) | ||
| 120 | { | ||
| 121 | $self -> creator -> drop_table($table_name); | ||
| 122 | } | ||
| 123 | |||
| 124 | } # End of drop_all_tables. | ||
| 125 | |||
| 126 | # ----------------------------------------------- | ||
| 127 | |||
| 128 | sub new | ||
| 129 | { | ||
| 130 | my($class, $config_file_name) = @_; | ||
| 131 | my($self) = bless({}, $class); | ||
| 132 | |||
| 133 | die __PACKAGE__ . ". You must supply a value for 'config_file_name'" if (! $config_file_name); | ||
| 134 | |||
| 135 | $self -> config_file_name($config_file_name); | ||
| 136 | |||
| 137 | my($config) = CGI::Application::Demo::Basic::Util::Config -> new($config_file_name) -> config; | ||
| 138 | my($db_vendor) = $$config{'dsn'} =~ /[^:]+:([^:]+):/; | ||
| 139 | |||
| 140 | $self -> db_vendor(uc $db_vendor); | ||
| 141 | $self -> dbh(DBI -> connect | ||
| 142 | ( | ||
| 143 | $$config{dsn}, | ||
| 144 | $$config{username}, | ||
| 145 | $$config{password}, | ||
| 146 | { | ||
| 147 | AutoCommit => $$config{AutoCommit}, | ||
| 148 | LongReadLen => $$config{LongReadLen}, | ||
| 149 | PrintError => $$config{PrintError}, | ||
| 150 | RaiseError => $$config{RaiseError}, | ||
| 151 | }, | ||
| 152 | ) ); | ||
| 153 | |||
| 154 | $self -> creator(DBIx::Admin::CreateTable -> new(dbh => $self -> dbh) ); | ||
| 155 | |||
| 156 | return $self; | ||
| 157 | |||
| 158 | } # End of new. | ||
| 159 | |||
| 160 | # ----------------------------------------------- | ||
| 161 | |||
| 162 | sub populate_all_tables | ||
| 163 | { | ||
| 164 | my($self) = @_; | ||
| 165 | |||
| 166 | $self -> populate_faculty_table; | ||
| 167 | |||
| 168 | } # End of populate_all_tables. | ||
| 169 | |||
| 170 | # ----------------------------------------------- | ||
| 171 | |||
| 172 | sub populate_faculty_table | ||
| 173 | { | ||
| 174 | my($self) = @_; | ||
| 175 | my($data) = $self -> read_a_file('faculty.txt'); | ||
| 176 | my($sql) = $self -> db_vendor eq 'ORACLE' | ||
| 177 | ? 'insert into faculty (faculty_id, faculty_name) values (faculty_seq.nextval, ?)' | ||
| 178 | : 'insert into faculty (faculty_name) values (?)'; | ||
| 179 | my($sth) = $self -> dbh -> prepare($sql); | ||
| 180 | |||
| 181 | $sth -> execute($_) for @$data; | ||
| 182 | $sth -> finish; | ||
| 183 | |||
| 184 | } # End of populate_faculty_table. | ||
| 185 | |||
| 186 | # -------------------------------------------------- | ||
| 187 | # Scripts using this will be command line scripts. | ||
| 188 | # Assumed directory structure: | ||
| 189 | # ./data/faculty.txt | ||
| 190 | # ./scripts/populate.pl | ||
| 191 | |||
| 192 | sub read_a_file | ||
| 193 | { | ||
| 194 | my($self, $input_file_name) = @_; | ||
| 195 | $input_file_name = FindBin::Real::Bin . "/../data/$input_file_name"; | ||
| 196 | |||
| 197 | return [grep{! /^$/ && ! /^#/} map{s/^\s+//; s/\s+$//; $_} read_file($input_file_name)]; | ||
| 198 | |||
| 199 | } # End of read_a_file. | ||
| 200 | |||
| 201 | # ----------------------------------------------- | ||
| 202 | |||
| 203 | sub report | ||
| 204 | { | ||
| 205 | my($self, $table_name, $message, $result) = @_; | ||
| 206 | |||
| 207 | if ($result) | ||
| 208 | { | ||
| 209 | die "Table '$table_name' $result\n"; | ||
| 210 | } | ||
| 211 | else | ||
| 212 | { | ||
| 213 | print "Table '$table_name' $message. \n"; | ||
| 214 | } | ||
| 215 | |||
| 216 | } # End of report. | ||
| 217 | |||
| 218 | # ----------------------------------------------- | ||
| 219 | |||
| 220 | 1; |