| 1 | package OpenInteract::SQLInstall; | ||
| 2 | |||
| 3 | # $Id: SQLInstall.pm,v 1.22 2003/01/07 13:46:54 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Class::Date; | ||
| 7 | use Data::Dumper qw( Dumper ); | ||
| 8 | use OpenInteract::Package; | ||
| 9 | use SPOPS::SQLInterface; | ||
| 10 | |||
| 11 | @OpenInteract::SQLInstall::ISA = qw(); | ||
| 12 | $OpenInteract::SQLInstall::VERSION = sprintf("%d.%02d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/); | ||
| 13 | |||
| 14 | use constant DEBUG => 0; | ||
| 15 | |||
| 16 | |||
| 17 | # Ensure that the installer is 'require'd -- put the package dir(s) | ||
| 18 | # into @INC and read the class name from the package itself. | ||
| 19 | |||
| 20 | sub require_package_installer { | ||
| 21 | my ( $class, $pkg_info ) = @_; | ||
| 22 | |||
| 23 | # Ensure that the necessary directories from the package are in @INC | ||
| 24 | |||
| 25 | OpenInteract::Package->add_to_inc( $pkg_info ); | ||
| 26 | my $installer_class = $pkg_info->{sql_installer}; | ||
| 27 | return undef unless ( $installer_class ); | ||
| 28 | |||
| 29 | DEBUG && _w( 1, "Trying to require ($installer_class) from ($class) for ($pkg_info->{name})" ); | ||
| 30 | eval "require $installer_class"; | ||
| 31 | die "Cannot include installer ($installer_class) to system. Error: $@\n" if ( $@ ); | ||
| 32 | return $installer_class; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | # TODO: This whole status thing sucks. We should create an | ||
| 37 | # OpenInteract::Manage::Status object that can hold all this crap | ||
| 38 | |||
| 39 | # Use this to apply a particular action | ||
| 40 | # | ||
| 41 | # Parameters: | ||
| 42 | # action: action you want to do (create_structure|install_data|install_security) | ||
| 43 | # db: DBI database handle (in correct db, RaiseError on) | ||
| 44 | # config: config object/hashref | ||
| 45 | # package: package for which you're installing | ||
| 46 | # status: 'raw' (get back arrayref of raw status items) (optional) | ||
| 47 | |||
| 48 | sub apply { | ||
| 49 | my ( $class, $p ) = @_; | ||
| 50 | |||
| 51 | # TODO: we assume the db driver is the one from the 'main' | ||
| 52 | # datasource for now -- we'll probably have to revisit this | ||
| 53 | |||
| 54 | my $driver_name = $class->find_database_driver( $p->{config}, 'main' ); | ||
| 55 | my $action = $p->{action_code}; | ||
| 56 | unless ( $action ) { | ||
| 57 | DEBUG && _w( 1, "Finding coderef for $p->{action}" ); | ||
| 58 | my $handlers = $class->read_db_handlers( $driver_name ); | ||
| 59 | $action = $handlers->{ $p->{action} }; | ||
| 60 | } | ||
| 61 | unless ( $action ) { | ||
| 62 | my $msg = "$p->{action} for $driver_name: no action taken"; | ||
| 63 | return ( $p->{status} eq 'raw' ) | ||
| 64 | ? [ { ok => 1, msg => $msg } ] | ||
| 65 | : $msg; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $status = []; | ||
| 69 | |||
| 70 | # Do this when the first item in the handler definition is the | ||
| 71 | # subroutine, next is the hashref of arguments | ||
| 72 | |||
| 73 | if ( ref $action eq 'ARRAY' ) { | ||
| 74 | my $routine = shift @{ $action }; | ||
| 75 | my $extra_args = ( ref $action->[0] eq 'HASH' ) ? $action->[0] : {}; | ||
| 76 | my $args = { package => $p->{package}, | ||
| 77 | db => $p->{db}, | ||
| 78 | config => $p->{config}, | ||
| 79 | %{ $extra_args } }; | ||
| 80 | $status = $class->$routine( $args ); | ||
| 81 | } | ||
| 82 | |||
| 83 | # Do this when there is actual code to run | ||
| 84 | |||
| 85 | elsif ( ref $action eq 'CODE' ) { | ||
| 86 | $status = $action->( $class, { package => $p->{package}, | ||
| 87 | db => $p->{db}, | ||
| 88 | config => $p->{config} }); | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | my $msg = 'Action is not of expected type, so nothing run.'; | ||
| 92 | return ( $p->{status} eq 'raw' ) | ||
| 93 | ? [ { ok => 1, msg => $msg } ] | ||
| 94 | : $msg; | ||
| 95 | } | ||
| 96 | return $status if ( $p->{status} eq 'raw' ); | ||
| 97 | return $class->format_status( { action => $p->{action}, | ||
| 98 | driver_name => $driver_name }, | ||
| 99 | $status ); | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | # Read in a set of SQL files and execute them, doing necessary | ||
| 105 | # replacements before doing so; returns an arrayref of status hashrefs | ||
| 106 | |||
| 107 | sub create_structure { | ||
| 108 | my ( $class, $p ) = @_; | ||
| 109 | unless ( ref $p->{table_file_list} eq 'ARRAY' ) { | ||
| 110 | return [{ type => 'structure', | ||
| 111 | ok => 0, | ||
| 112 | msg => 'No files given from which to read structures!' }]; | ||
| 113 | } | ||
| 114 | |||
| 115 | my $pkg_info = $p->{package}; | ||
| 116 | |||
| 117 | # TODO: we assume the db driver is the one from the 'main' | ||
| 118 | # datasource for now -- we'll probably have to revisit this | ||
| 119 | |||
| 120 | my $driver_name = $class->find_database_driver( $p->{config}, 'main' ); | ||
| 121 | my $struct_dir = join( '/', $pkg_info->{website_dir}, $pkg_info->{package_dir}, 'struct' ); | ||
| 122 | my @status = (); | ||
| 123 | foreach my $table_file ( @{ $p->{table_file_list} } ) { | ||
| 124 | my $this_status = { type => 'structure', name => $table_file, ok => 1 }; | ||
| 125 | my $table_sql = eval { $class->read_file( "$struct_dir/$table_file" ) }; | ||
| 126 | if ( $@ ) { | ||
| 127 | $this_status->{ok} = 0; | ||
| 128 | $this_status->{msg} = "Cannot open/read file for table SQL ($struct_dir/$table_file): $@"; | ||
| 129 | DEBUG && _w( 1, " -- $this_status->{msg}" ); | ||
| 130 | } | ||
| 131 | else { | ||
| 132 | $table_sql = $class->sql_modify_increment( $p->{config}, $driver_name, $table_sql ); | ||
| 133 | $table_sql = $class->sql_modify_increment_type( $p->{config}, $driver_name, $table_sql ); | ||
| 134 | $table_sql = $class->sql_modify_usertype( $p->{config}, $driver_name, $table_sql ); | ||
| 135 | $table_sql = $class->sql_modify_grouptype( $p->{config}, $driver_name, $table_sql ); | ||
| 136 | $table_sql = $class->sql_modify_datetime( $p->{config}, $driver_name, $table_sql ); | ||
| 137 | eval { $p->{db}->do( $table_sql ) }; | ||
| 138 | if ( $@ ) { | ||
| 139 | $this_status->{ok} = 0; | ||
| 140 | $this_status->{msg} = "Cannot execute table SQL\n$table_sql\nError: $@"; | ||
| 141 | DEBUG && _w( 1, " -- $this_status->{msg}" ); | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | $this_status->{msg} = "Created table from ($table_file): ok"; | ||
| 145 | DEBUG && _w( 1, "Structure (from $table_file) ok!" ); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | push @status, $this_status; | ||
| 149 | } | ||
| 150 | return \@status; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | # Takes a set of filenames of data and processes each of them; returns | ||
| 156 | # an arrayref of status hashrefs | ||
| 157 | |||
| 158 | sub install_data { | ||
| 159 | my ( $class, $p ) = @_; | ||
| 160 | unless ( ref $p->{data_file_list} eq 'ARRAY' ) { | ||
| 161 | return [ { type => $p->{type}, | ||
| 162 | ok => 0, | ||
| 163 | msg => 'No files given from which to read data!' } ]; | ||
| 164 | } | ||
| 165 | |||
| 166 | my $pkg_info = $p->{package}; | ||
| 167 | my $data_dir = $p->{data_dir} || join( '/', $pkg_info->{website_dir}, $pkg_info->{package_dir}, 'data' ); | ||
| 168 | my @status = (); | ||
| 169 | my %args = ( db => $p->{db}, config => $p->{config} ); | ||
| 170 | foreach my $data_file ( @{ $p->{data_file_list} } ) { | ||
| 171 | my $this_status = { type => $p->{type}, name => $data_file, ok => 1 }; | ||
| 172 | my $process_status = eval { $class->process_data_file({ %args, | ||
| 173 | filename => "$data_dir/$data_file" }) }; | ||
| 174 | if ( $@ ) { | ||
| 175 | $this_status->{ok} = 0; | ||
| 176 | $this_status->{msg} = "Error: $@"; | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | $this_status->{msg} = ( $process_status->{msg} ) | ||
| 180 | ? "$process_status->{msg} (from $data_file): ok" | ||
| 181 | : "Processed data (from $data_file): ok"; | ||
| 182 | } | ||
| 183 | push @status, $this_status; | ||
| 184 | } | ||
| 185 | return \@status; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | # Processes a single .dat data file | ||
| 191 | |||
| 192 | # TODO: Split out the actual processing so we can feed the data and | ||
| 193 | # action in and get the same result whether we generate the data | ||
| 194 | # ourselves or read it from a file | ||
| 195 | |||
| 196 | sub process_data_file { | ||
| 197 | my ( $class, $p ) = @_; | ||
| 198 | unless ( -f $p->{filename} ) { | ||
| 199 | die "Cannot process data file without valid filename! ", | ||
| 200 | "(Filename given: <$p->{filename}>)\n"; | ||
| 201 | } | ||
| 202 | my $info = $class->read_perl_file( $p->{filename} ); | ||
| 203 | return {} unless ( ref $info eq 'ARRAY' and ref $info->[0] eq 'HASH' ); | ||
| 204 | my $action = shift @{ $info }; | ||
| 205 | |||
| 206 | # Transform all the data at once | ||
| 207 | |||
| 208 | my $trans_data = $class->transform_data( $action, $info, $p ); | ||
| 209 | |||
| 210 | # Something defined in 'sql_type' signifies that we're going to | ||
| 211 | # process a given SQL statement for as many data entries as there | ||
| 212 | # are | ||
| 213 | |||
| 214 | my ( $process_sql, $save_object ); | ||
| 215 | if ( my $sql_type = $action->{sql_type} ) { | ||
| 216 | DEBUG && _w( 1, "Reading data for prepared SQL statements" ); | ||
| 217 | my $types = undef; | ||
| 218 | foreach my $data ( @{ $trans_data } ) { | ||
| 219 | eval { | ||
| 220 | if ( $sql_type eq 'insert' ) { | ||
| 221 | SPOPS::SQLInterface->db_insert({ | ||
| 222 | db => $p->{db}, | ||
| 223 | table => $action->{sql_table}, | ||
| 224 | field => $action->{field_order}, | ||
| 225 | value => $data, | ||
| 226 | dbi_type_info => $action->{field_type} }); | ||
| 227 | $process_sql++; | ||
| 228 | } | ||
| 229 | if ( $sql_type eq 'update' ) { | ||
| 230 | SPOPS::SQLInterface->db_update({ | ||
| 231 | db => $p->{db}, | ||
| 232 | table => $action->{sql_table}, | ||
| 233 | where => $action->{sql_where}, | ||
| 234 | value => $data, | ||
| 235 | dbi_type_info => $action->{field_type} }); | ||
| 236 | $process_sql++; | ||
| 237 | } | ||
| 238 | if ( $sql_type eq 'delete' ) { | ||
| 239 | SPOPS::SQLInterface->db_delete({ | ||
| 240 | db => $p->{db}, | ||
| 241 | table => $action->{sql_table}, | ||
| 242 | where => $action->{sql_where}, | ||
| 243 | value => $data, | ||
| 244 | dbi_type_info => $action->{field_type} }); | ||
| 245 | $process_sql++; | ||
| 246 | } | ||
| 247 | }; | ||
| 248 | if ( $@ ) { | ||
| 249 | die "Error executing SQL statement type $sql_type\n", | ||
| 250 | "Data: ((", join( ', ', @{ $data } ), "))\nError: $@\n"; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | # If a 'spops_class' exists, we want to create an object for each | ||
| 256 | # item of data | ||
| 257 | |||
| 258 | elsif ( my $object_class = $action->{spops_class} ) { | ||
| 259 | my $website_name = $p->{config}{server_info}{website_name} || $p->{config}{website_name}; | ||
| 260 | $object_class = $class->sql_class_to_website( $website_name, $object_class ); | ||
| 261 | DEBUG && _w( 1, "Reading data for class $object_class" ); | ||
| 262 | my $fields = $action->{field_order}; | ||
| 263 | my $num_fields = scalar @{ $fields }; | ||
| 264 | foreach my $data ( @{ $trans_data } ) { | ||
| 265 | my $obj = $object_class->new; | ||
| 266 | foreach my $i ( 0 .. ( $num_fields - 1 ) ) { | ||
| 267 | $obj->{ $fields->[ $i ] } = $data->[ $i ]; | ||
| 268 | } | ||
| 269 | eval { $obj->save({ db => $p->{db}, | ||
| 270 | is_add => 1, | ||
| 271 | skip_log => 1, | ||
| 272 | skip_security => 1, | ||
| 273 | skip_cache => 1, | ||
| 274 | DEBUG => DEBUG }) }; | ||
| 275 | if ( $@ ) { | ||
| 276 | my $ei = SPOPS::Error->get; | ||
| 277 | die "Cannot create SPOPS object!\nBasic: $@\n", | ||
| 278 | "Error: $ei->{system_msg}\n"; | ||
| 279 | } | ||
| 280 | else { | ||
| 281 | $save_object++; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | } | ||
| 285 | if ( $process_sql ) { | ||
| 286 | return { ok => 1, msg => "Processed ($process_sql) SQL statements." }; | ||
| 287 | } | ||
| 288 | elsif ( $save_object ) { | ||
| 289 | return { ok => 1, msg => "Created ($save_object) SPOPS objects" }; | ||
| 290 | } | ||
| 291 | return { ok => 1 }; | ||
| 292 | } | ||
| 293 | |||
| 294 | |||
| 295 | # Find the driver to use | ||
| 296 | |||
| 297 | sub find_database_driver { | ||
| 298 | my ( $class, $config, $connect_key ) = @_; | ||
| 299 | my $driver = $config->{db_info}{ $connect_key }{sql_install} || | ||
| 300 | $config->{db_info}{ $connect_key }{driver_name}; | ||
| 301 | unless ( $driver ) { | ||
| 302 | die "Cannot find database driver given connection key ($connect_key)"; | ||
| 303 | } | ||
| 304 | return $driver; | ||
| 305 | } | ||
| 306 | |||
| 307 | |||
| 308 | # Get the hash of handlers from an installer class | ||
| 309 | |||
| 310 | sub read_db_handlers { | ||
| 311 | my ( $class, $driver_name ) = @_; | ||
| 312 | no strict 'refs'; | ||
| 313 | my %handlers = %{ $class . '::HANDLERS' }; | ||
| 314 | my $db_handlers = {}; | ||
| 315 | foreach my $action ( keys %handlers ) { | ||
| 316 | $db_handlers->{ $action } = $handlers{ $action }{ $driver_name } || | ||
| 317 | $handlers{ $action }{'_default_'}; | ||
| 318 | } | ||
| 319 | return $db_handlers; | ||
| 320 | } | ||
| 321 | |||
| 322 | |||
| 323 | ######################################## | ||
| 324 | # DATA TRANSFORMATION | ||
| 325 | ######################################## | ||
| 326 | |||
| 327 | # Transform all of the data from whatever source | ||
| 328 | |||
| 329 | sub transform_data { | ||
| 330 | my ( $class, $action, $data_list, $p ) = @_; | ||
| 331 | |||
| 332 | # Setup the field order as a hash for the implementors | ||
| 333 | |||
| 334 | for ( my $i = 0; $i < scalar @{ $action->{field_order} }; $i++ ) { | ||
| 335 | $p->{field_order}{ $action->{field_order}->[ $i ] } = $i; | ||
| 336 | } | ||
| 337 | |||
| 338 | # If an action specifies to change a field TO a particular website | ||
| 339 | |||
| 340 | if ( ref $action->{transform_class_to_website} eq 'ARRAY' ) { | ||
| 341 | $data_list = $class->_transform_class_to_website( $action, $data_list, $p ); | ||
| 342 | } | ||
| 343 | |||
| 344 | # If an action specifies to change a field FROM a particular website | ||
| 345 | |||
| 346 | if ( ref $action->{transform_class_to_oi} eq 'ARRAY' ) { | ||
| 347 | $data_list = $class->_transform_class_to_oi( $action, $data_list, $p ); | ||
| 348 | } | ||
| 349 | |||
| 350 | # If an action specifies lookup a field in the 'default_objects' | ||
| 351 | # server configuration key | ||
| 352 | |||
| 353 | if ( ref $action->{transform_default_to_id} eq 'ARRAY' ) { | ||
| 354 | $data_list = $class->_transform_default_to_id( $action, $data_list, $p ); | ||
| 355 | } | ||
| 356 | |||
| 357 | # If an action specifies set the value of a particular field to a | ||
| 358 | # date string representing right now. | ||
| 359 | |||
| 360 | if ( ref $action->{transform_to_now} eq 'ARRAY' ) { | ||
| 361 | $data_list = $class->_transform_to_now( $action, $data_list, $p ); | ||
| 362 | } | ||
| 363 | |||
| 364 | return $data_list; | ||
| 365 | } | ||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | sub _transform_class_to_website { | ||
| 370 | my ( $class, $action, $data_list, $p ) = @_; | ||
| 371 | my $website_name = $p->{config}{server_info}{website_name} || $p->{config}{website_name}; | ||
| 372 | foreach my $data ( @{ $data_list } ) { | ||
| 373 | foreach my $website_field ( @{ $action->{transform_class_to_website} } ) { | ||
| 374 | my $idx = $p->{field_order}{ $website_field }; | ||
| 375 | $data->[ $idx ] = $class->sql_class_to_website( $website_name, $data->[ $idx ] ); | ||
| 376 | } | ||
| 377 | } | ||
| 378 | return $data_list; | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | |||
| 383 | sub _transform_class_to_oi { | ||
| 384 | my ( $class, $action, $data_list, $p ) = @_; | ||
| 385 | my $website_name = $p->{config}{server_info}{website_name} || $p->{config}{website_name}; | ||
| 386 | foreach my $data ( @{ $data_list } ) { | ||
| 387 | foreach my $website_field ( @{ $action->{transform_class_to_oi} } ) { | ||
| 388 | my $idx = $p->{field_order}{ $website_field }; | ||
| 389 | $data->[ $idx ] = $class->sql_class_to_oi( $website_name, $data->[ $idx ] ); | ||
| 390 | } | ||
| 391 | } | ||
| 392 | return $data_list; | ||
| 393 | } | ||
| 394 | |||
| 395 | |||
| 396 | sub _transform_default_to_id { | ||
| 397 | my ( $class, $action, $data_list, $p ) = @_; | ||
| 398 | my $CONFIG = OpenInteract::Request->instance->CONFIG; | ||
| 399 | foreach my $data ( @{ $data_list } ) { | ||
| 400 | foreach my $field ( @{ $action->{transform_default_to_id} } ) { | ||
| 401 | my $idx = $p->{field_order}{ $field }; | ||
| 402 | $data->[ $idx ] = $class->sql_default_to_id( $CONFIG->{default_objects}, $data->[ $idx ] ); | ||
| 403 | } | ||
| 404 | } | ||
| 405 | return $data_list; | ||
| 406 | } | ||
| 407 | |||
| 408 | |||
| 409 | sub _transform_to_now { | ||
| 410 | my ( $class, $action, $data_list, $p ) = @_; | ||
| 411 | my $now_string = Class::Date->now->strftime( '%Y-%m-%d %H:%M:%S' ); | ||
| 412 | foreach my $data ( @{ $data_list } ) { | ||
| 413 | foreach my $field ( @{ $action->{transform_to_now} } ) { | ||
| 414 | my $idx = $p->{field_order}{ $field }; | ||
| 415 | $data->[ $idx ] = $now_string; | ||
| 416 | } | ||
| 417 | } | ||
| 418 | return $data_list; | ||
| 419 | } | ||
| 420 | |||
| 421 | |||
| 422 | |||
| 423 | |||
| 424 | ######################################## | ||
| 425 | # SQL MODIFICATIONS | ||
| 426 | ######################################## | ||
| 427 | |||
| 428 | sub sql_default_to_id { | ||
| 429 | my ( $class, $default, @text ) = @_; | ||
| 430 | my @modified = (); | ||
| 431 | foreach my $field ( @text ) { | ||
| 432 | push @modified, ( $default->{ $field } ) ? $default->{ $field } : $field; | ||
| 433 | } | ||
| 434 | return wantarray ? @modified : $modified[0]; | ||
| 435 | } | ||
| 436 | |||
| 437 | |||
| 438 | # Translate OpenInteract::Blah::Blah -> MyWebsite::Blah::Blah | ||
| 439 | |||
| 440 | sub sql_class_to_website { | ||
| 441 | my ( $class, $website, @text ) = @_; | ||
| 442 | foreach ( @text ) { | ||
| 443 | s/OpenInteract/$website/g; | ||
| 444 | } | ||
| 445 | return wantarray ? @text : $text[0]; | ||
| 446 | } | ||
| 447 | |||
| 448 | |||
| 449 | |||
| 450 | # Translate MyWebsite::Blah::Blah -> OpenInteract::Blah::Blah | ||
| 451 | |||
| 452 | sub sql_class_to_oi { | ||
| 453 | my ( $class, $website, @text ) = @_; | ||
| 454 | foreach ( @text ) { | ||
| 455 | s/$website/OpenInteract/g; | ||
| 456 | } | ||
| 457 | return wantarray ? @text : $text[0]; | ||
| 458 | } | ||
| 459 | |||
| 460 | |||
| 461 | |||
| 462 | # Translate %%INCREMENT%% to a db-specific method for finding an | ||
| 463 | # auto_increment value; we should probably have an 'else' just | ||
| 464 | # assigning every other db driver to 'int not null'... | ||
| 465 | |||
| 466 | sub sql_modify_increment { | ||
| 467 | my ( $class, $config, $driver_name, @sql ) = @_; | ||
| 468 | foreach ( @sql ) { | ||
| 469 | if ( $driver_name eq 'mysql' ) { | ||
| 470 | s/%%INCREMENT%%/INT NOT NULL AUTO_INCREMENT/g; | ||
| 471 | } | ||
| 472 | elsif ( $driver_name eq 'Sybase' or | ||
| 473 | $driver_name eq 'ASAny' or | ||
| 474 | $driver_name eq 'FreeTDS' ) { | ||
| 475 | s/%%INCREMENT%%/NUMERIC( 10, 0 ) IDENTITY NOT NULL/g; | ||
| 476 | } | ||
| 477 | elsif ( $driver_name eq 'Pg' ) { | ||
| 478 | s/%%INCREMENT%%/INT NOT NULL/g; | ||
| 479 | } | ||
| 480 | elsif ( $driver_name eq 'Oracle' ) { | ||
| 481 | s/%%INCREMENT%%/INT NOT NULL/g; | ||
| 482 | } | ||
| 483 | elsif ( $driver_name eq 'SQLite' ) { | ||
| 484 | s/%%INCREMENT%%/INTEGER NOT NULL/g; | ||
| 485 | } | ||
| 486 | elsif ( $driver_name eq 'InterBase' ) { | ||
| 487 | s/%%INCREMENT%%/INT NOT NULL/g; | ||
| 488 | } | ||
| 489 | } | ||
| 490 | return wantarray ? @sql : $sql[0]; | ||
| 491 | } | ||
| 492 | |||
| 493 | |||
| 494 | # Translate %%INCREMENT_TYPE%% to a db-specific datatype -- note that | ||
| 495 | # NULL/NOT NULL are not handled here | ||
| 496 | |||
| 497 | sub sql_modify_increment_type { | ||
| 498 | my ( $class, $config, $driver_name, @to_change ) = @_; | ||
| 499 | foreach ( @to_change ) { | ||
| 500 | if ( $driver_name eq 'mysql' ) { | ||
| 501 | s/%%INCREMENT_TYPE%%/INT/g; | ||
| 502 | } | ||
| 503 | elsif ( $driver_name eq 'Sybase' or | ||
| 504 | $driver_name eq 'ASAny' or | ||
| 505 | $driver_name eq 'FreeTDS' ) { | ||
| 506 | s/%%INCREMENT_TYPE%%/NUMERIC( 10, 0 )/g; | ||
| 507 | } | ||
| 508 | elsif ( $driver_name eq 'Pg' ) { | ||
| 509 | s/%%INCREMENT_TYPE%%/INT/g; | ||
| 510 | } | ||
| 511 | elsif ( $driver_name eq 'Oracle' ) { | ||
| 512 | s/%%INCREMENT_TYPE%%/INT/g; | ||
| 513 | } | ||
| 514 | elsif ( $driver_name eq 'SQLite' ) { | ||
| 515 | s/%%INCREMENT_TYPE%%/INTEGER/g; | ||
| 516 | } | ||
| 517 | elsif ( $driver_name eq 'InterBase' ) { | ||
| 518 | s/%%INCREMENT_TYPE%%/INT/g; | ||
| 519 | } | ||
| 520 | } | ||
| 521 | return wantarray ? @to_change : $to_change[0]; | ||
| 522 | } | ||
| 523 | |||
| 524 | |||
| 525 | sub sql_modify_datetime { | ||
| 526 | my ( $class, $config, $driver_name, @to_change ) = @_; | ||
| 527 | foreach ( @to_change ) { | ||
| 528 | if ( $driver_name eq 'mysql' ) { | ||
| 529 | s/%%DATETIME%%/DATETIME/g; | ||
| 530 | } | ||
| 531 | elsif ( $driver_name eq 'Sybase' or | ||
| 532 | $driver_name eq 'ASAny' or | ||
| 533 | $driver_name eq 'FreeTDS' ) { | ||
| 534 | s/%%DATETIME%%/DATETIME/g; | ||
| 535 | } | ||
| 536 | elsif ( $driver_name eq 'Pg' ) { | ||
| 537 | s/%%DATETIME%%/TIMESTAMP/g; | ||
| 538 | } | ||
| 539 | elsif ( $driver_name eq 'Oracle' ) { | ||
| 540 | s/%%DATETIME%%/DATE/g; | ||
| 541 | } | ||
| 542 | elsif ( $driver_name eq 'SQLite' ) { | ||
| 543 | s/%%DATETIME%%/TIMESTAMP/g; | ||
| 544 | } | ||
| 545 | elsif ( $driver_name eq 'InterBase' ) { | ||
| 546 | s/%%DATETIME%%/TIMESTAMP/g; | ||
| 547 | } | ||
| 548 | } | ||
| 549 | return wantarray ? @to_change : $to_change[0]; | ||
| 550 | } | ||
| 551 | |||
| 552 | # Translate %%USERID_TYPE%% to a datatype specified in the server | ||
| 553 | # configuration. This is so we can have user IDs as characters or | ||
| 554 | # integers. | ||
| 555 | |||
| 556 | sub sql_modify_usertype { | ||
| 557 | my ( $class, $config, $driver_name, @to_change ) = @_; | ||
| 558 | my $type = $config->{id}{user_type} || 'int'; | ||
| 559 | foreach ( @to_change ) { | ||
| 560 | if ( $type eq 'char' ) { | ||
| 561 | s/%%USERID_TYPE%%/VARCHAR(25)/g; | ||
| 562 | } | ||
| 563 | elsif ( $type eq 'int' ) { | ||
| 564 | s/%%USERID_TYPE%%/INT/g; | ||
| 565 | } | ||
| 566 | } | ||
| 567 | return wantarray ? @to_change : $to_change[0]; | ||
| 568 | } | ||
| 569 | |||
| 570 | |||
| 571 | sub sql_modify_grouptype { | ||
| 572 | my ( $class, $config, $driver_name, @to_change ) = @_; | ||
| 573 | my $type = $config->{id}{group_type} || 'int'; | ||
| 574 | foreach ( @to_change ) { | ||
| 575 | if ( $type eq 'char' ) { | ||
| 576 | s/%%GROUPID_TYPE%%/VARCHAR(25)/g; | ||
| 577 | } | ||
| 578 | elsif ( $type eq 'int' ) { | ||
| 579 | s/%%GROUPID_TYPE%%/INT/g; | ||
| 580 | } | ||
| 581 | } | ||
| 582 | return wantarray ? @to_change : $to_change[0]; | ||
| 583 | } | ||
| 584 | |||
| 585 | |||
| 586 | # This is pretty generic (and not used yet) | ||
| 587 | |||
| 588 | sub sql_default { | ||
| 589 | my ( $class, @to_change ) = @_; | ||
| 590 | foreach ( @to_change ) { | ||
| 591 | if ( /%%DEFAULT=(.*)%%/ ) { | ||
| 592 | my $default_value = $1; | ||
| 593 | s/%%DEFAULT.*%%/NOT NULL DEFAULT $default_value/; | ||
| 594 | } | ||
| 595 | } | ||
| 596 | return wantarray ? @to_change : $to_change[0]; | ||
| 597 | } | ||
| 598 | |||
| 599 | |||
| 600 | # This is pretty generic (and not used yet) | ||
| 601 | |||
| 602 | sub sql_primary_key { | ||
| 603 | my ( $class, @to_change ) = @_; | ||
| 604 | foreach ( @to_change ) { | ||
| 605 | if ( /%%PRIMARY_KEY=(.*)%%/ ) { | ||
| 606 | my $key_specify = join( ', ', split /\s*,\s*/, $1 ); | ||
| 607 | s/%%PRIMARY KEY=.*%%/PRIMARY KEY( $key_specify )/; | ||
| 608 | } | ||
| 609 | } | ||
| 610 | return wantarray ? @to_change : $to_change[0]; | ||
| 611 | } | ||
| 612 | |||
| 613 | |||
| 614 | # This is pretty generic (and not used yet) | ||
| 615 | |||
| 616 | sub sql_unique { | ||
| 617 | my ( $class, @to_change ) = @_; | ||
| 618 | foreach ( @to_change ) { | ||
| 619 | if (/%%UNIQUE=(.*)%%/ ) { | ||
| 620 | my $key_specify = join( ', ', split /\s*,\s*/, $1 ); | ||
| 621 | s/%%UNIQUE=.*%%/UNIQUE( $key_specify )/; | ||
| 622 | } | ||
| 623 | } | ||
| 624 | return wantarray ? @to_change : $to_change[0]; | ||
| 625 | } | ||
| 626 | |||
| 627 | |||
| 628 | ######################################## | ||
| 629 | # I/O | ||
| 630 | ######################################## | ||
| 631 | |||
| 632 | # Read in a file and evaluate it as perl. | ||
| 633 | |||
| 634 | sub read_perl_file { | ||
| 635 | my ( $class, $filename ) = @_; | ||
| 636 | my $raw = $class->read_file( $filename ); | ||
| 637 | my $data = undef; | ||
| 638 | { | ||
| 639 | no strict 'vars'; | ||
| 640 | $data = eval $raw; | ||
| 641 | } | ||
| 642 | die "Cannot parse data file ($filename): $@\n" if ( $@ ); | ||
| 643 | return $data; | ||
| 644 | } | ||
| 645 | |||
| 646 | |||
| 647 | |||
| 648 | # Read in a file and return the contents | ||
| 649 | |||
| 650 | sub read_file { | ||
| 651 | my ( $class, $filename ) = @_; | ||
| 652 | die "Cannot read data file: ($filename) does not exist!\n" unless ( -f $filename ); | ||
| 653 | DEBUG && _w( 1, "Reading file $filename" ); | ||
| 654 | open( DF, $filename ) || die "Cannot read data file: $!\n"; | ||
| 655 | local $/ = undef; | ||
| 656 | my $raw = <DF>; | ||
| 657 | close( DF ); | ||
| 658 | return $raw; | ||
| 659 | } | ||
| 660 | |||
| 661 | |||
| 662 | ######################################## | ||
| 663 | # STATUS | ||
| 664 | ######################################## | ||
| 665 | |||
| 666 | # Format a status information hashref: | ||
| 667 | # | ||
| 668 | # type => (structure|data|security) | ||
| 669 | # name => name of file/whatnot | ||
| 670 | # ok => 1 (ok); not true (not ok) | ||
| 671 | # msg => Description of how ok everything is (optional) or description of error | ||
| 672 | |||
| 673 | sub format_status { | ||
| 674 | my ( $class, $info, $status_list ) = @_; | ||
| 675 | my $output = "$info->{action} for $info->{driver_name}\n"; | ||
| 676 | foreach my $status ( @{ $status_list } ) { | ||
| 677 | $output .= qq( Name: $status->{name} -- ); | ||
| 678 | $output .= 'ok' if ( $status->{ok} ); | ||
| 679 | $output .= 'error' if ( ! $status->{ok} ); | ||
| 680 | $output .= "\n -- $status->{msg}" if ( $status->{msg} ); | ||
| 681 | $output .= "\n"; | ||
| 682 | } | ||
| 683 | return $output; | ||
| 684 | } | ||
| 685 | |||
| 686 | sub _w { | ||
| 687 | return unless ( DEBUG >= shift ); | ||
| 688 | my ( $pkg, $file, $line ) = caller; | ||
| 689 | my @ci = caller(1); | ||
| 690 | warn "$ci[3] ($line) >> ", join( ' ', @_ ), "\n"; | ||
| 691 | } | ||
| 692 | |||
| 693 | 1; | ||
| 694 | |||
| 695 | __END__ |