| 1 |
|
| # $Id: Xfer.pm,v 1.42 2001/05/10 13:09:40 spragues Exp spragues $ |
| 2 |
|
| # |
| 3 |
|
| # (c) 1999, 2000 Morgan Stanley Dean Witter and Co. |
| 4 |
|
| # See ..../src/LICENSE for terms of distribution. |
| 5 |
|
| # |
| 6 |
|
| # |
| 7 |
|
| #See pod documentation at the end of this file |
| 8 |
|
| # |
| 9 |
|
| # |
| 10 |
|
| #reminder. when updating documentation: |
| 11 |
|
| # 1) usage |
| 12 |
|
| # 2) short usage |
| 13 |
|
| # 3) pod summary |
| 14 |
|
| # 4) pod detailed |
| 15 |
|
| # |
| 16 |
|
| # |
| 17 |
|
| #public: |
| 18 |
|
| # sub new |
| 19 |
|
| # sub xfer |
| 20 |
|
| # sub done |
| 21 |
|
| # |
| 22 |
|
| #if -error_handling set to 'retry' and -callback_err_batch undefined |
| 23 |
|
| #then this routine becomes the bcp_batch error_handler |
| 24 |
|
| # |
| 25 |
|
| # sub bcp_batch_error_handler { |
| 26 |
|
| # |
| 27 |
|
| #private: |
| 28 |
|
| # sub _grab_from_sybase { |
| 29 |
|
| # sub _grab_from_file { |
| 30 |
|
| # sub _grab_from_perl { |
| 31 |
|
| # sub sx_cleanup { |
| 32 |
|
| # |
| 33 |
|
| #sendrow stuff |
| 34 |
|
| # sub sx_sendrow_bcp { |
| 35 |
|
| # sub sx_sendrow_batch { |
| 36 |
|
| # sub sx_sendrow_batch_of_size { |
| 37 |
|
| # sub sx_sendrow_batch_success { |
| 38 |
|
| # sub sx_sendrow_return { |
| 39 |
|
| # sub sx_sendrow_temp { |
| 40 |
|
| # sub sx_sendrow_final_batch { |
| 41 |
|
| # |
| 42 |
|
| #auto delete stuff |
| 43 |
|
| # sub sx_remove_target_rows { |
| 44 |
|
| # sub sx_auto_delete_setup { |
| 45 |
|
| # |
| 46 |
|
| #option stuff |
| 47 |
|
| # sub _checkargs { |
| 48 |
|
| # sub _prep_xfer { |
| 49 |
|
| # sub sx_verify_options { |
| 50 |
|
| # sub sx_from_file_map { |
| 51 |
|
| # |
| 52 |
|
| #error handling routinues |
| 53 |
|
| # sub sx_sendrow_failure { |
| 54 |
|
| # sub sx_sendrow_batch_failure { |
| 55 |
|
| # sub _print_error_detection { |
| 56 |
|
| # sub sx_error_tidy_msg { |
| 57 |
|
| # sub sx_oversize_error { |
| 58 |
|
| # |
| 59 |
|
| #util stuff |
| 60 |
|
| # sub _open_bcp { |
| 61 |
|
| # |
| 62 |
|
| # sub sx_complain { |
| 63 |
|
| # sub sx_usage { |
| 64 |
|
| # sub sx_message_handler { |
| 65 |
|
| # sub sx_error_handler { |
| 66 |
|
| # sub sx_error_handler_dbi { |
| 67 |
|
| # sub _print { |
| 68 |
|
| # sub sx_debug { |
| 69 |
|
| # sub sx_delete_sql { |
| 70 |
|
| # sub sx_truncate_table { |
| 71 |
|
| # sub sx_delete_rows { |
| 72 |
|
| # sub _drop_indices { |
| 73 |
|
| # sub _drop_indices_setup { |
| 74 |
|
| # sub _create_indices { |
| 75 |
|
| # sub sx_run_shell_cmd { |
| 76 |
|
| # sub sx_parse_user_hash { |
| 77 |
|
| # sub _parse_path { |
| 78 |
|
| # |
| 79 |
|
| # |
| 80 |
|
| |
| 81 |
|
| package Sybase::Xfer; |
| 82 |
|
| |
| 83 |
|
| use 5.6.1; |
| 84 |
|
| |
| 85 |
|
| #set-up package |
| 86 |
|
| use strict; |
| 87 |
|
| |
| 88 |
|
| # use Exporter; |
| 89 |
|
| # use Carp; |
| 90 |
|
| # use vars qw/@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION/; |
| 91 |
|
| # @ISA = qw/Exporter/; |
| 92 |
|
| |
| 93 |
|
| use vars qw/$VERSION/; |
| 94 |
|
| $VERSION = '0.63'; |
| 95 |
|
| |
| 96 |
|
| #RCS version |
| 97 |
|
| my ($RCSVERSION) = do { |
| 98 |
|
| my @r = ( q$Revision: 1.42 $ =~ /\d+/g ); |
| 99 |
|
| sprintf "%d." . "%02d" x $#r, @r; |
| 100 |
|
| }; |
| 101 |
|
| |
| 102 |
|
| #modules |
| 103 |
|
| # use lib '/ms/dist/perl5/PROJ/DBI/1.30-5.6/lib/perl5/'; |
| 104 |
|
| # use DBI; |
| 105 |
|
| |
| 106 |
|
| #Sybase DBD |
| 107 |
|
| # use lib '/ms/dist/perl5/PROJ/DBD-Sybase/0.94-5.6/lib/perl5/'; |
| 108 |
|
| # use lib '/ms/dev/perl5/DBD-Sybase/0.95-5.6/install/.exec/sun4u.sunos64.5.8/lib/perl5'; |
| 109 |
|
| # use lib '/ms/dist/perl5/PROJ/DBD-Sybase/0.95-5.6/lib/perl5/'; |
| 110 |
|
| |
| 111 |
|
| #DB2 |
| 112 |
|
| # use lib '/ms/dist/perl5/PROJ/DBD-DB2/0.76-5.6/lib/perl5/'; |
| 113 |
|
| # use DBD::DB2; |
| 114 |
|
| # use DBD::DB2::Constants; |
| 115 |
|
| |
| 116 |
|
| #BLK routines for Sybase |
| 117 |
|
| use lib '/ms/dist/perl5/PROJ/syb/2.15-5.6/lib/perl5'; |
| 118 |
|
| |
| 119 |
|
| # use lib '/ms/dist/perl5/PROJ/syb/2.14-5.6/lib/perl5'; |
| 120 |
|
| use Sybase::CTlib; |
| 121 |
|
| |
| 122 |
|
| #other stuff |
| 123 |
|
| use FileHandle; |
| 124 |
|
| use File::Basename; |
| 125 |
|
| |
| 126 |
|
| # use Sybase::ObjectInfo; |
| 127 |
|
| use Getopt::Long; |
| 128 |
|
| use Tie::IxHash; |
| 129 |
|
| |
| 130 |
|
| use Sys::Hostname; |
| 131 |
|
| |
| 132 |
|
| #to validate numbers |
| 133 |
|
| use POSIX qw/strtod/; |
| 134 |
|
| |
| 135 |
|
| #constants |
| 136 |
|
| use constant RS_UNRECOV_ERROR => -1; |
| 137 |
|
| use constant RS_INTERRUPT => -2; |
| 138 |
|
| use constant RS_TIMEOUT => -3; |
| 139 |
|
| |
| 140 |
|
| #globals |
| 141 |
|
| use vars qw/$DB_ERROR %opt $DB_ERROR_ONELINE $DB_DEADLOCK $DB_IN_ERR_HANDLER/; |
| 142 |
|
| |
| 143 |
|
| #----------------------------------------------------------------------- |
| 144 |
|
| #constructor |
| 145 |
|
| #----------------------------------------------------------------------- |
| 146 |
|
| sub new { |
| 147 |
|
| |
| 148 |
|
| my $self = shift; |
| 149 |
|
| my $class = ref($self) || $self; |
| 150 |
|
| |
| 151 |
|
| |
| 152 |
|
| #basic option checking, bail out of error |
| 153 |
|
| my %opt = $self->_checkargs(@_); |
| 154 |
|
| return undef if exists $opt{1}; |
| 155 |
|
| |
| 156 |
|
| $self = {%opt}; |
| 157 |
|
| bless $self, $class; |
| 158 |
|
| |
| 159 |
|
| #use CTlib instead of DBI |
| 160 |
|
| $self->{CTLib} = 1; |
| 161 |
|
| |
| 162 |
|
| #other initializations |
| 163 |
|
| $DB_ERROR = (); |
| 164 |
|
| $Sybase::CTlib::nsql_strip_whitespace++ if $self->{trim_whitespace}; |
| 165 |
|
| |
| 166 |
|
| #set the stage. delete/truncate; define source. if error return. |
| 167 |
|
| $self->_prep_xfer() && return undef; |
| 168 |
|
| |
| 169 |
|
| #squirrel away a few counts |
| 170 |
|
| $self->{sx_send_count} = 0; |
| 171 |
|
| $self->{sx_succeed_count} = 0; |
| 172 |
|
| $self->{sx_err_count} = 0; |
| 173 |
|
| $self->{sx_resend_count} = 0; |
| 174 |
|
| $self->{sx_send_count_batch} = 0; |
| 175 |
|
| $self->{sx_succeed_count_batch} = 0; |
| 176 |
|
| $self->{sx_err_count_batch} = 0; |
| 177 |
|
| $self->{sx_resend_count_batch} = 0; |
| 178 |
|
| |
| 179 |
|
| #install interrupt signal handler |
| 180 |
|
| $SIG{INT} = sub { return sx_interrupt_handler($self) }; |
| 181 |
|
| |
| 182 |
|
| |
| 183 |
|
| #send it back |
| 184 |
|
| return $self; |
| 185 |
|
| } |
| 186 |
|
| |
| 187 |
|
| #--------- |
| 188 |
|
| #interrupt handler |
| 189 |
|
| #---------- |
| 190 |
|
| sub sx_interrupt_handler { |
| 191 |
|
| my $self = shift; |
| 192 |
|
| |
| 193 |
|
| #dummy up error message for sx_cleanup |
| 194 |
|
| $DB_ERROR = "Interrupt detected. Terminating transfer!"; |
| 195 |
|
| |
| 196 |
|
| #print |
| 197 |
|
| $self->_print("\n"); |
| 198 |
|
| $self->_print("------\n"); |
| 199 |
|
| $self->_print("$DB_ERROR\n"); |
| 200 |
|
| $self->_print("------\n"); |
| 201 |
|
| |
| 202 |
|
| $self->{GOT_INTERRUPT} = RS_INTERRUPT; |
| 203 |
|
| die 'interrupt'; |
| 204 |
|
| |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| #---------- |
| 208 |
|
| #timeout handler |
| 209 |
|
| #---------- |
| 210 |
|
| sub sx_timeout_handler { |
| 211 |
|
| my $self = shift; |
| 212 |
|
| |
| 213 |
|
| #dummy up error message for sx_cleanup |
| 214 |
|
| $DB_ERROR = "Timeout detected ($self->{timeout} seconds). Terminating transfer!"; |
| 215 |
|
| |
| 216 |
|
| #print |
| 217 |
|
| $self->_print("------\n"); |
| 218 |
|
| $self->_print("$DB_ERROR\n"); |
| 219 |
|
| $self->_print("------\n"); |
| 220 |
|
| |
| 221 |
|
| $self->{GOT_TIMEOUT} = RS_TIMEOUT; |
| 222 |
|
| die 'timeout'; |
| 223 |
|
| |
| 224 |
|
| } |
| 225 |
|
| |
| 226 |
|
| #----------------------------------------------------------------------- |
| 227 |
|
| #destroy the object |
| 228 |
|
| #----------------------------------------------------------------------- |
| 229 |
|
| sub done { |
| 230 |
|
| my $self = shift; |
| 231 |
|
| my $class = ref($self) || $self; |
| 232 |
|
| |
| 233 |
|
| #return current handlers |
| 234 |
|
| ct_callback( CS_SERVERMSG_CB, $self->{current_err_handler} ); |
| 235 |
|
| ct_callback( CS_CLIENTMSG_CB, $self->{current_msg_handler} ); |
| 236 |
|
| |
| 237 |
|
| my $num_success = $self->{sx_succeed_count}; |
| 238 |
|
| my $num_err = $self->{sx_err_count}; |
| 239 |
|
| my $num_resend = $self->{sx_resend_count}; |
| 240 |
|
| my $num_fails = $num_err - $num_resend; |
| 241 |
|
| |
| 242 |
|
| $self = undef; |
| 243 |
|
| |
| 244 |
|
| return wantarray ? ( $num_success, $num_err, $num_resend, $num_fails ) : $num_success; |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
|
| #----------------------------------------------------------------------- |
| 248 |
|
| #xfer the data |
| 249 |
|
| #----------------------------------------------------------------------- |
| 250 |
|
| sub xfer { |
| 251 |
|
| |
| 252 |
|
| my $self = shift; |
| 253 |
|
| my $class = ref($self) || $self; |
| 254 |
|
| |
| 255 |
|
| #set return type |
| 256 |
|
| my $return = $_[0]; |
| 257 |
|
| my %rstyle; |
| 258 |
|
| if ( ref $return ) { |
| 259 |
|
| %rstyle = %$return; |
| 260 |
|
| } |
| 261 |
|
| elsif ($return) { |
| 262 |
|
| %rstyle = @_; |
| 263 |
|
| } |
| 264 |
|
| else { |
| 265 |
|
| %rstyle = ( -return => 'ARRAY' ); |
| 266 |
|
| } |
| 267 |
|
| my $val = $rstyle{"-return"} || $rstyle{return}; |
| 268 |
|
| sx_complain("unknown return key passed to xfer: <$return>\n"), return 1 unless $val; |
| 269 |
|
| unless ( ref $val eq "HASH" || uc $val eq "HASH" || ref $val eq "ARRAY" || uc $val eq "ARRAY" ) { |
| 270 |
|
| sx_complain("-return must be HASH, ARRAY, {} or []\n") && return 1; |
| 271 |
|
| } |
| 272 |
|
| $self->{return} ||= $val; |
| 273 |
|
| |
| 274 |
|
| |
| 275 |
|
| |
| 276 |
|
| |
| 277 |
|
| #no buffering on stdout |
| 278 |
|
| $|++; |
| 279 |
|
| |
| 280 |
|
| #timeout signal |
| 281 |
|
| if ( $self->{timeout} ) { |
| 282 |
|
| alarm $self->{timeout}; |
| 283 |
|
| $SIG{ALRM} = sub { return sx_timeout_handler($self) }; |
| 284 |
|
| } |
| 285 |
|
| |
| 286 |
|
| |
| 287 |
|
| |
| 288 |
|
| #grab data from one of three possible genre of sources |
| 289 |
|
| my $rs = 0; |
| 290 |
|
| eval { |
| 291 |
|
| SWITCH: { |
| 292 |
|
| |
| 293 |
|
| #perl code source |
| 294 |
|
| if ( ref( $self->{sx_xfer_source} ) =~ /^(CODE|ARRAY)/ ) { |
| 295 |
|
| $rs = $self->_grab_from_perl(); |
| 296 |
|
| last; |
| 297 |
|
| } |
| 298 |
|
| |
| 299 |
|
| #file-like source |
| 300 |
|
| if ( UNIVERSAL::isa( $self->{sx_xfer_source}, 'IO::File' ) ) { |
| 301 |
|
| $rs = $self->_grab_from_file(); |
| 302 |
|
| last; |
| 303 |
|
| } |
| 304 |
|
| |
| 305 |
|
| #db source |
| 306 |
|
| $rs = $self->_grab_from_db(); |
| 307 |
|
| last; |
| 308 |
|
| } |
| 309 |
|
| |
| 310 |
|
| }; |
| 311 |
|
| |
| 312 |
|
| #check return |
| 313 |
|
| if ($@) { |
| 314 |
|
| $self->_print("main eval error: $@\n"); |
| 315 |
|
| $rs = $self->{GOT_INTERRUPT} || $self->{GOT_TIMEOUT}; |
| 316 |
|
| } |
| 317 |
|
| |
| 318 |
|
| #summarize, restore env |
| 319 |
|
| my @rc = sx_cleanup( $self, $rs ); |
| 320 |
|
| |
| 321 |
|
| #return with proper context |
| 322 |
|
| my $rstyle = $self->{return}; |
| 323 |
|
| |
| 324 |
|
| #hash return |
| 325 |
|
| my %rs = ( rows_read => $rc[1], |
| 326 |
|
| rows_transferred => $rc[2], |
| 327 |
|
| last_error_msg => $rc[3], |
| 328 |
|
| scalar_return => $rc[0], |
| 329 |
|
| ok => $rc[0] >= 0 ? 1 : 0, #1=>all rows transferred, 0=>problems |
| 330 |
|
| ); |
| 331 |
|
| |
| 332 |
|
| #array return |
| 333 |
|
| my @rs = @rc[ 1 .. 3, 0 ]; |
| 334 |
|
| $rs[4] = $rc[0] >= 0 ? 1 : 0; |
| 335 |
|
| |
| 336 |
|
| #scalar return |
| 337 |
|
| $rs = $rc[0]; |
| 338 |
|
| |
| 339 |
|
| #send it back |
| 340 |
|
| if ( $rstyle eq "HASH" || ref $rstyle eq "HASH" ) { |
| 341 |
|
| return wantarray ? %rs : $rs; |
| 342 |
|
| |
| 343 |
|
| } |
| 344 |
|
| elsif ( $rstyle eq "ARRAY" || ref $rstyle eq "ARRAY" ) { |
| 345 |
|
| return wantarray ? @rs : $rs; |
| 346 |
|
| |
| 347 |
|
| #backward compatibility requires array if not specified |
| 348 |
|
| } |
| 349 |
|
| else { |
| 350 |
|
| return wantarray ? @rs : $rs; |
| 351 |
|
| |
| 352 |
|
| } |
| 353 |
|
| } |
| 354 |
|
| |
| 355 |
|
| |
| 356 |
|
| #----------------------------------------------------------------------- |
| 357 |
|
| #cleanup & exit |
| 358 |
|
| #----------------------------------------------------------------------- |
| 359 |
|
| sub sx_cleanup { |
| 360 |
|
| |
| 361 |
|
| my ( $self, $grab_rs ) = @_; |
| 362 |
|
| |
| 363 |
|
| #resave db error if not saved already |
| 364 |
|
| $self->{sx_save_db_error} = $DB_ERROR if $DB_ERROR && !$self->{sx_save_db_error}; |
| 365 |
|
| $self->{sx_save_db_error_oneline} = $DB_ERROR_ONELINE if $DB_ERROR_ONELINE && !$self->{sx_save_db_error_oneline}; |
| 366 |
|
| |
| 367 |
|
| #close 'from' and 'to' connections |
| 368 |
|
| $self->{sx_dbh_from} = undef if ref $self->{sx_dbh_from}; |
| 369 |
|
| |
| 370 |
|
| # $self->{sx_dbh_to_bcp}->blk_drop() if ref $self->{sx_dbh_to_bcp}; |
| 371 |
|
| $self->{sx_dbh_to_bcp} = undef if ref $self->{sx_dbh_to_bcp}; |
| 372 |
|
| |
| 373 |
|
| #if indices were dropped on 'to' table then recreate them now. |
| 374 |
|
| $self->{drop_and_recreate_indices} && $self->_create_indices() && return 1; |
| 375 |
|
| |
| 376 |
|
| #close the non-bcp 'to' connection |
| 377 |
|
| $self->{sx_dbh_to_non_bcp} = undef if $self->{sx_dbh_to_non_bcp}; |
| 378 |
|
| |
| 379 |
|
| #if number of rows sent is zero and an error condition, then sx_sendrow_bcp NEVER |
| 380 |
|
| #called to field an error. So we have to do it here. |
| 381 |
|
| if ($grab_rs) { |
| 382 |
|
| if ( $self->{sx_send_count} == 0 || $self->{GOT_INTERRUPT} || $self->{GOT_TIMEOUT} ) { |
| 383 |
|
| $self->_print("--------------\n"); |
| 384 |
|
| $self->_print("Error detected. (non-recoverable)\n"); |
| 385 |
|
| sx_error_tidy_msg($self); |
| 386 |
|
| $self->_print("--------------\n"); |
| 387 |
|
| } |
| 388 |
|
| $self->{GOT_UNRECOV_ERROR} = 1; |
| 389 |
|
| } |
| 390 |
|
| |
| 391 |
|
| #return current handlers |
| 392 |
|
| ct_callback( CS_SERVERMSG_CB, $self->{current_err_handler} ); |
| 393 |
|
| ct_callback( CS_CLIENTMSG_CB, $self->{current_msg_handler} ); |
| 394 |
|
| |
| 395 |
|
| #restore nsql deadlock retry logic |
| 396 |
|
| $Sybase::CTlib::nsql_deadlock_retrycount = $self->{save_deadlock_retry}; |
| 397 |
|
| $Sybase::CTlib::nsql_deadlock_retrysleep = $self->{save_deadlock_sleep}; |
| 398 |
|
| $Sybase::CTlib::nsql_deadlock_verbose = $self->{save_deadlock_verbose}; |
| 399 |
|
| |
| 400 |
|
| #restore alarm settings |
| 401 |
|
| alarm(0) if $opt{timeout}; |
| 402 |
|
| |
| 403 |
|
| #last error saved if retry or continue |
| 404 |
|
| my $err_oneline = $self->{sx_save_db_error_oneline} || $DB_ERROR_ONELINE; |
| 405 |
|
| my $err = $self->{sx_save_db_error} || $DB_ERROR; |
| 406 |
|
| |
| 407 |
|
| #notification |
| 408 |
|
| # my $num_fails = $self->{sx_err_count} - $self->{sx_resend_count}; |
| 409 |
|
| my $num_fails = $self->{sx_send_count} - $self->{sx_succeed_count}; |
| 410 |
|
| unless ( $self->{silent} ) { |
| 411 |
|
| |
| 412 |
|
| $self->_print("\n"); |
| 413 |
|
| $self->_print("Xfer summary:\n"); |
| 414 |
|
| $self->_print(" $self->{sx_send_count} rows read from source\n"); |
| 415 |
|
| $self->_print(" $self->{sx_succeed_count} rows transferred to target\n"); |
| 416 |
|
| |
| 417 |
|
| #8/29/01. too misleading when retry is on. |
| 418 |
|
| ## $self->_print( " $self->{sx_err_count} rows had errors\n" ) if $self->{sx_err_count} > 0; |
| 419 |
|
| ## $self->_print( " $self->{sx_resend_count} total rows resent\n" ) if $self->{sx_resend_count} > 0; |
| 420 |
|
| |
| 421 |
|
| #only if retry on |
| 422 |
|
| if ( $num_fails > 0 && $self->{error_handling} =~ /^retry/ ) { |
| 423 |
|
| $self->_print(" $num_fails total unsuccessful retries\n"); |
| 424 |
|
| } |
| 425 |
|
| |
| 426 |
|
| $self->_print(" last known error message encountered: $err_oneline\n") if $err_oneline; |
| 427 |
|
| } |
| 428 |
|
| |
| 429 |
|
| #get rid of empty error file |
| 430 |
|
| my $elf = $self->{sx_error_data_file_fh}; |
| 431 |
|
| my $fn = $self->{error_data_file}; |
| 432 |
|
| if ($elf) { |
| 433 |
|
| $elf->close(); |
| 434 |
|
| unlink $fn unless $fn && -s $fn; |
| 435 |
|
| } |
| 436 |
|
| |
| 437 |
|
| #set return values |
| 438 |
|
| |
| 439 |
|
| #scalar return value |
| 440 |
|
| # 0 = success w/o any hitches |
| 441 |
|
| # >0 = success with n number of recoverable failures |
| 442 |
|
| # -1 = unrecovable errors (eg. bad sql, xfer aborted) |
| 443 |
|
| # -2 = got interrupt |
| 444 |
|
| # -3 = got timeout |
| 445 |
|
| # |
| 446 |
|
| my $scalar_return = 0; |
| 447 |
|
| $scalar_return ||= $self->{GOT_INTERRUPT} || $self->{GOT_TIMEOUT} || -1 * defined( $self->{GOT_UNRECOV_ERROR} ); |
| 448 |
|
| if ( $num_fails >= 1 ) { |
| 449 |
|
| $scalar_return = -1.0 * abs($num_fails); |
| 450 |
|
| } |
| 451 |
|
| |
| 452 |
|
| my $num_rows_read = $self->{sx_send_count}; |
| 453 |
|
| my $num_rows_xferred = $self->{sx_succeed_count}; |
| 454 |
|
| |
| 455 |
|
| # print "scalar return=$scalar_return, num_fails=$num_fails\n"; #DEBUG |
| 456 |
|
| return ( $scalar_return, $num_rows_read, $num_rows_xferred, $err ); |
| 457 |
|
| |
| 458 |
|
| } |
| 459 |
|
| |
| 460 |
|
| #----------------------------------------------------------------------- |
| 461 |
|
| #routine to manage fetching one record at time from the 'from' source |
| 462 |
|
| #----------------------------------------------------------------------- |
| 463 |
|
| sub sx_get_row_at_a_time { |
| 464 |
|
| |
| 465 |
|
| my ( $self, $sql, $cb ) = @_; |
| 466 |
|
| |
| 467 |
|
| if ( $self->{from_source} =~ /^dbd/i ) { |
| 468 |
|
| |
| 469 |
|
| my $dbh = $self->{sx_dbh_from}; |
| 470 |
|
| |
| 471 |
|
| # my $sth = $dbh->do($sql); |
| 472 |
|
| |
| 473 |
|
| #send sql |
| 474 |
|
| my $sth; |
| 475 |
|
| unless ( $sth = $dbh->prepare($sql) ) { |
| 476 |
|
| $DB_ERROR = $dbh->errstr; |
| 477 |
|
| $self->_print( "error in dbi.prepare: " . $DB_ERROR . "\n" ); |
| 478 |
|
| return 1; |
| 479 |
|
| } |
| 480 |
|
| |
| 481 |
|
| #run sql |
| 482 |
|
| my $re; |
| 483 |
|
| unless ( $re = $sth->execute ) { |
| 484 |
|
| $DB_ERROR = $sth->errstr; |
| 485 |
|
| $self->_print( "error in dbi.execute: <" . $DB_ERROR . ">\n" ); |
| 486 |
|
| return 1; |
| 487 |
|
| } |
| 488 |
|
| |
| 489 |
|
| #field 'rows' one at a time |
| 490 |
|
| { |
| 491 |
|
| my ( @results, $set ); |
| 492 |
|
| local $SIG{__WARN__} = sub { warn "SJS: $_[0]" if $_[0] !~ /no statement executing/; }; |
| 493 |
|
| do { |
| 494 |
|
| $cb->( $sth->fetchall_array( [] ) ); |
| 495 |
|
| } while ( $sth->{syb_more_results} ); |
| 496 |
|
| } |
| 497 |
|
| return 0; |
| 498 |
|
| } |
| 499 |
|
| } |
| 500 |
|
| |
| 501 |
|
| #----------------------------------------------------------------------- |
| 502 |
|
| #grab data from dbi source to push to 'to' server |
| 503 |
|
| #----------------------------------------------------------------------- |
| 504 |
|
| sub _grab_from_db { |
| 505 |
|
| |
| 506 |
|
| my $self = shift; |
| 507 |
|
| |
| 508 |
|
| #remove the rows from target table |
| 509 |
|
| $self->{auto_delete} && $self->sx_remove_target_rows() && return 1; |
| 510 |
|
| |
| 511 |
|
| #get bcp connection |
| 512 |
|
| my ( $db, $tab ) = @{$self}{qw/sx_to_database sx_to_table/}; |
| 513 |
|
| my $nc = scalar @{ $self->{sx_to_info}->{$db}->{$tab}->{__COLNAMES__} }; |
| 514 |
|
| $self->{sx_dbh_to_bcp} = $self->_open_bcp( $self->{to_table}, $nc ); |
| 515 |
|
| return 1 if $self->{sx_dbh_to_bcp} == 1; |
| 516 |
|
| |
| 517 |
|
| #see if -from_file_map is specified. This could result in the re-ordering of fields |
| 518 |
|
| $self->sx_from_file_map() && return 1; |
| 519 |
|
| |
| 520 |
|
| #define cb for use with nsql |
| 521 |
|
| my $cb = sub { return sx_sendrow_bcp( \@_, $self ) }; |
| 522 |
|
| |
| 523 |
|
| #run nsql on 'from' server |
| 524 |
|
| $self->_print("transferring rows to $self->{to_server} : $self->{to_table}\n") if ( $self->{echo} ); |
| 525 |
|
| my $sql = $self->{sx_xfer_source}; |
| 526 |
|
| |
| 527 |
|
| #progress log |
| 528 |
|
| if ( $self->{progress_log} ) { |
| 529 |
|
| $self->_print("\n"); |
| 530 |
|
| $self->_print("Progress log\n"); |
| 531 |
|
| } |
| 532 |
|
| |
| 533 |
|
| #hack. peppler needs to fix... |
| 534 |
|
| local $SIG{__WARN__} = sub { warn "$_[0]" if $_[0] !~ /no statement executing/; }; |
| 535 |
|
| |
| 536 |
|
| #$sql will be an arrayref when there are multiple batches in the from_sql/from_script |
| 537 |
|
| if ( ref($sql) eq "ARRAY" ) { |
| 538 |
|
| for my $each_sql ( @{$sql} ) { |
| 539 |
|
| $self->_print("SQL:\n$each_sql\n") if $self->{echo}; |
| 540 |
|
| $self->_runsql( $self->{sx_dbh_from}, $each_sql, [], $cb ); |
| 541 |
|
| |
| 542 |
|
| #! $self->{sx_dbh_from}->func( $each_sql, [], $cb, 'nsql' ); |
| 543 |
|
| last if $DB_ERROR; |
| 544 |
|
| } |
| 545 |
|
| } |
| 546 |
|
| else { |
| 547 |
|
| $self->_print("SQL:\n$sql\n") if $self->{echo}; |
| 548 |
|
| $self->_runsql( $self->{sx_dbh_from}, $sql, [], $cb ); |
| 549 |
|
| |
| 550 |
|
| #! $self->{sx_dbh_from}->func( $sql, [], $cb, 'nsql' ); |
| 551 |
|
| } |
| 552 |
|
| |
| 553 |
|
| #commit last set of rows |
| 554 |
|
| return !$DB_ERROR ? $self->sx_sendrow_final_batch() : 1; |
| 555 |
|
| } |
| 556 |
|
| |
| 557 |
|
| #----------------------------------------------------------------------- |
| 558 |
|
| #grab data from file and push to 'to' server |
| 559 |
|
| #----------------------------------------------------------------------- |
| 560 |
|
| sub _grab_from_file { |
| 561 |
|
| |
| 562 |
|
| my $self = shift; |
| 563 |
|
| |
| 564 |
|
| #remove the rows from target table, if called for. |
| 565 |
|
| $self->{auto_delete} && sx_remove_target_rows($self) && return 1; |
| 566 |
|
| |
| 567 |
|
| #bcp init |
| 568 |
|
| my ( $db, $tab ) = @{$self}{qw/sx_to_database sx_to_table/}; |
| 569 |
|
| my $nc = scalar @{ $self->{sx_to_info}->{$db}->{$tab}->{__COLNAMES__} }; |
| 570 |
|
| $self->{sx_dbh_to_bcp} = $self->_open_bcp( $self->{to_table}, $nc ); |
| 571 |
|
| return 1 if $self->{sx_dbh_to_bcp} == 1; |
| 572 |
|
| |
| 573 |
|
| #see if -from_file_map is specified. This could result in the re-ordering of fields |
| 574 |
|
| sx_from_file_map($self) && return 1; |
| 575 |
|
| my ( $map, %map ) = (); |
| 576 |
|
| if ( $self->{sx_from_file_map} ) { |
| 577 |
|
| %map = %{ $self->{sx_from_file_map} }; |
| 578 |
|
| $map++; |
| 579 |
|
| } |
| 580 |
|
| |
| 581 |
|
| #log |
| 582 |
|
| if ( $self->{progress_log} ) { |
| 583 |
|
| $self->_print("\n"); |
| 584 |
|
| $self->_print("Progress log\n"); |
| 585 |
|
| } |
| 586 |
|
| |
| 587 |
|
| #transfer the data by reading the file |
| 588 |
|
| my $delim = $self->{from_file_delimiter}; |
| 589 |
|
| my $fixed = $self->{from_file_pack_format}; |
| 590 |
|
| my $fh = $self->{sx_xfer_source}; |
| 591 |
|
| my @colnames = @{ $self->{sx_to_colnames} }; |
| 592 |
|
| my ( $status, $line ) = ( 1, 0 ); |
| 593 |
|
| while ( defined( $line = <$fh> ) && $status ) { |
| 594 |
|
| chomp $line; |
| 595 |
|
| my $r_data = (); |
| 596 |
|
| |
| 597 |
|
| if ( $delim && $map ) { |
| 598 |
|
| $r_data = [ ( split /$delim/, $line, -1 ) ]; #[ @map{@colnames} ] ]; |
| 599 |
|
| } |
| 600 |
|
| elsif ( $delim && !$map ) { |
| 601 |
|
| $r_data = [ split /$delim/, $line, -1 ]; |
| 602 |
|
| } |
| 603 |
|
| elsif ( $fixed && $map ) { |
| 604 |
|
| $r_data = [ ( unpack( $self->{from_file_pack_format}, $line ) ) ]; |
| 605 |
|
| } |
| 606 |
|
| elsif ( $fixed && !$map ) { |
| 607 |
|
| $r_data = [ unpack( $self->{from_file_pack_format}, $line ) ]; |
| 608 |
|
| } |
| 609 |
|
| |
| 610 |
|
| $status = sx_sendrow_bcp( $r_data, $self ) #returns 1==good, 0==bad; |
| 611 |
|
| } |
| 612 |
|
| |
| 613 |
|
| #return |
| 614 |
|
| return $status ? sx_sendrow_final_batch($self) : 1; |
| 615 |
|
| |
| 616 |
|
| } |
| 617 |
|
| |
| 618 |
|
| #----------------------------------------------------------------------- |
| 619 |
|
| #grab data from perl source |
| 620 |
|
| #----------------------------------------------------------------------- |
| 621 |
|
| sub _grab_from_perl { |
| 622 |
|
| |
| 623 |
|
| my $opt = shift; |
| 624 |
|
| |
| 625 |
|
| #remove the rows from target table, if called for. |
| 626 |
|
| $opt->{auto_delete} && sx_remove_target_rows($opt) && return 1; |
| 627 |
|
| |
| 628 |
|
| #bcp init |
| 629 |
|
| my ( $db, $tab ) = @{$opt}{qw/sx_to_database sx_to_table/}; |
| 630 |
|
| my $nc = scalar @{ $opt->{sx_to_info}->{$db}->{$tab}->{__COLNAMES__} }; |
| 631 |
|
| $opt->{sx_dbh_to_bcp} = $opt->_open_bcp( $opt->{to_table}, $nc ); |
| 632 |
|
| return 1 if $opt->{sx_dbh_to_bcp} == 1; |
| 633 |
|
| |
| 634 |
|
| #progress log |
| 635 |
|
| if ( $opt->{progress_log} ) { |
| 636 |
|
| $opt->_print("\n"); |
| 637 |
|
| $opt->_print("Progress log\n"); |
| 638 |
|
| } |
| 639 |
|
| |
| 640 |
|
| my $bcp_status = 1; |
| 641 |
|
| |
| 642 |
|
| #PERL - CODEREF |
| 643 |
|
| if ( ref $opt->{from_perl} eq 'CODE' ) { |
| 644 |
|
| |
| 645 |
|
| #transfer the data by calling perl code ref |
| 646 |
|
| #return status of user function: |
| 647 |
|
| # < 0 => skip |
| 648 |
|
| # == 0 => quit |
| 649 |
|
| # > 0 => keep going |
| 650 |
|
| my ( $status_getrow, $r_data ) = $opt->{sx_xfer_source}->(); |
| 651 |
|
| while ($status_getrow) { |
| 652 |
|
| if ( $status_getrow >= 1 ) { |
| 653 |
|
| $bcp_status = sx_sendrow_bcp( $r_data, $opt ); |
| 654 |
|
| } |
| 655 |
|
| else { |
| 656 |
|
| $bcp_status = 1; |
| 657 |
|
| } |
| 658 |
|
| last unless $bcp_status; |
| 659 |
|
| ( $status_getrow, $r_data ) = $opt->{sx_xfer_source}->(); |
| 660 |
|
| } |
| 661 |
|
| |
| 662 |
|
| #PERL - ARRAYREF |
| 663 |
|
| } |
| 664 |
|
| elsif ( ref $opt->{from_perl} eq 'ARRAY' ) { |
| 665 |
|
| for my $row ( @{ $opt->{from_perl} } ) { |
| 666 |
|
| $bcp_status = sx_sendrow_bcp( $row, $opt ); |
| 667 |
|
| last unless $bcp_status; |
| 668 |
|
| } |
| 669 |
|
| |
| 670 |
|
| } |
| 671 |
|
| |
| 672 |
|
| #set return |
| 673 |
|
| return $bcp_status ? sx_sendrow_final_batch($opt) : 1; |
| 674 |
|
| |
| 675 |
|
| } |
| 676 |
|
| |
| 677 |
|
| #---- |
| 678 |
|
| # |
| 679 |
|
| #---- |
| 680 |
|
| sub _runsql { |
| 681 |
|
| |
| 682 |
|
| my ( $self, $dbh, $sql, $type, $cb ) = @_; |
| 683 |
|
| |
| 684 |
|
| #ctlib connection |
| 685 |
|
| if ( $self->{CTLib} ) { |
| 686 |
|
| my @res = (); |
| 687 |
|
| eval { @res = $dbh->nsql( $sql, $type, $cb ) }; |
| 688 |
|
| print "** eval: [$@] **\n" if $@; |
| 689 |
|
| return \@res; |
| 690 |
|
| |
| 691 |
|
| #dbi connection |
| 692 |
|
| } |
| 693 |
|
| else { |
| 694 |
|
| if ($type) { |
| 695 |
|
| my @res = (); |
| 696 |
|
| eval { @res = $dbh->func( $sql, $type, $cb, 'nsql' ); }; |
| 697 |
|
| print "** eval: [$@] **\n" if $@; |
| 698 |
|
| return \@res; |
| 699 |
|
| } |
| 700 |
|
| else { |
| 701 |
|
| my $sth = $dbh->prepare($sql); |
| 702 |
|
| $sth->execute; |
| 703 |
|
| while ( $sth->{syb_more_results} ) { $sth->fetch() } |
| 704 |
|
| } |
| 705 |
|
| } |
| 706 |
|
| |
| 707 |
|
| } |
| 708 |
|
| |
| 709 |
|
| #----------------------------------------------------------------------- |
| 710 |
|
| #print opts |
| 711 |
|
| #----------------------------------------------------------------------- |
| 712 |
|
| sub sx_debug { |
| 713 |
|
| |
| 714 |
|
| #db handles |
| 715 |
|
| # $opt{sx_dbh_from} |
| 716 |
|
| # $opt{sx_dbh_to_bcp} |
| 717 |
|
| # $opt{sx_dbh_to_bcp_temp} |
| 718 |
|
| # $opt{sx_dbh_to_non_bcp} |
| 719 |
|
| # |
| 720 |
|
| # $opt{sx_xfer_source} |
| 721 |
|
| # |
| 722 |
|
| #auto delete stuff |
| 723 |
|
| # $opt{sx_ad_create_table} |
| 724 |
|
| # $opt{sx_ad_create_index} |
| 725 |
|
| # $opt{sx_ad_delete_cmd} |
| 726 |
|
| # $opt{sx_ad_delete_join} |
| 727 |
|
| # $opt{sx_ad_upd_stat} |
| 728 |
|
| # $opt{sx_ad_temp_table} |
| 729 |
|
| # $opt{sx_ad_temp_tab_count} |
| 730 |
|
| # $opt{sx_ad_col_num} |
| 731 |
|
| # $opt{sx_ad_rows_deleted} |
| 732 |
|
| # $opt{sx_ad_upd_stat} |
| 733 |
|
| # $opt{scratch_db} |
| 734 |
|
| # $opt{auto_delete} |
| 735 |
|
| # $opt{auto_delete_batchsize} |
| 736 |
|
| # |
| 737 |
|
| # $opt{current_msg_handler} |
| 738 |
|
| # $opt{current_err_handler} |
| 739 |
|
| # |
| 740 |
|
| # $opt{sx_to_database} |
| 741 |
|
| # $opt{sx_to_table} |
| 742 |
|
| # |
| 743 |
|
| # |
| 744 |
|
| #drop/recreate indices |
| 745 |
|
| # $opt{sx_drop_indices_sql} |
| 746 |
|
| # $opt{sx_create_indices_sql} |
| 747 |
|
| # |
| 748 |
|
| #sendrow stuff |
| 749 |
|
| # $opt{sx_send_count} |
| 750 |
|
| # $opt{sx_send_count_batch} |
| 751 |
|
| # |
| 752 |
|
| # $opt{sx_succeed_count} |
| 753 |
|
| # $opt{sx_succeed_count_batch} |
| 754 |
|
| # |
| 755 |
|
| # $opt{sx_err_count} |
| 756 |
|
| # $opt{sx_err_count_batch} |
| 757 |
|
| # |
| 758 |
|
| # $opt{sx_resend_count} |
| 759 |
|
| # $opt{sx_resend_count_batch} |
| 760 |
|
| |
| 761 |
|
| } |
| 762 |
|
| |
| 763 |
|
| #----------------------------------------------------------------------- |
| 764 |
|
| #auto delete |
| 765 |
|
| #----------------------------------------------------------------------- |
| 766 |
|
| sub sx_remove_target_rows { |
| 767 |
|
| |
| 768 |
|
| my $opt = shift; |
| 769 |
|
| |
| 770 |
|
| return 0 unless ( $opt->{auto_delete} ); |
| 771 |
|
| |
| 772 |
|
| if ( $opt->{progress_log} ) { |
| 773 |
|
| $opt->_print("Auto delete log\n"); |
| 774 |
|
| } |
| 775 |
|
| |
| 776 |
|
| # check target table to see if there's any rows in there |
| 777 |
|
| sx_check_target( $opt, "auto_delete" ) || return 0; |
| 778 |
|
| |
| 779 |
|
| #create temp table |
| 780 |
|
| my ( $dbh_to, $dbh_from, $sql ) = @{$opt}{qw/sx_dbh_to_non_bcp sx_dbh_from sx_ad_create_table/}; |
| 781 |
|
| $opt->_runsql( $dbh_to, $sql, {}, undef ); |
| 782 |
|
| |
| 783 |
|
| #! $opt->{sx_dbh_to_non_bcp}->func( $opt->{sx_ad_create_table}, {}, undef, 'nsql' ); |
| 784 |
|
| $DB_ERROR && sx_complain("unable to create temp table\n$DB_ERROR\n") && return 1; |
| 785 |
|
| |
| 786 |
|
| #create a bcp connection to it |
| 787 |
|
| my $nc = scalar @{ $opt->{sx_ad_col_num} }; |
| 788 |
|
| $opt->{sx_dbh_to_bcp_temp} = $opt->_open_bcp( $opt->{sx_ad_temp_table}, $nc ); |
| 789 |
|
| return 1 if $opt->{sx_dbh_to_bcp_temp} == 1; |
| 790 |
|
| |
| 791 |
|
| #callback for bcp |
| 792 |
|
| my %ad_seen = (); |
| 793 |
|
| my $cb = sub { return sx_sendrow_temp( \@_, $opt, \%ad_seen ) }; |
| 794 |
|
| |
| 795 |
|
| #do it. callback counts rows |
| 796 |
|
| $sql = $opt->{sx_xfer_source}; |
| 797 |
|
| $opt->_print(" bcping keys to $opt->{to_server} : $opt->{sx_ad_temp_table}\n") if $opt->{progress_log}; |
| 798 |
|
| $opt->_runsql( $dbh_from, $sql, [], $cb ); |
| 799 |
|
| |
| 800 |
|
| #! $opt->{sx_dbh_from}->func( $opt->{sx_xfer_source}, [], $cb, 'nsql' ); |
| 801 |
|
| $DB_ERROR && sx_complain("error in bcp'ing to temp table (a)\n$DB_ERROR\n") && return 1; |
| 802 |
|
| |
| 803 |
|
| #final batch |
| 804 |
|
| my $numRowsAffected; |
| 805 |
|
| $opt->{sx_dbh_to_bcp_temp}->blk_done( CS_BLK_ALL, $numRowsAffected ); |
| 806 |
|
| $DB_ERROR && sx_complain("error in bcp'ing to temp table (b)\n$DB_ERROR\n") && return 1; |
| 807 |
|
| |
| 808 |
|
| #log message |
| 809 |
|
| if ( $opt->{progress_log} ) { |
| 810 |
|
| $opt->_print(" $opt->{sx_ad_temp_tab_count} keys transferred\n"); |
| 811 |
|
| $opt->_print(" creating index on temp table\n"); |
| 812 |
|
| } |
| 813 |
|
| |
| 814 |
|
| #create index temp table |
| 815 |
|
| $sql = $opt->{sx_ad_create_index}; |
| 816 |
|
| $opt->_runsql( $dbh_to, $sql, {}, undef ); |
| 817 |
|
| |
| 818 |
|
| #! $opt->{sx_dbh_to_non_bcp}->func( $opt->{sx_ad_create_index}, {}, undef, 'nsql' ); |
| 819 |
|
| $DB_ERROR && sx_complain("unable to create temp table index\n$DB_ERROR\n") && return 1; |
| 820 |
|
| |
| 821 |
|
| #run the delete |
| 822 |
|
| if ( $opt->{progress_log} ) { |
| 823 |
|
| $opt->_print(" auto_deleting rows in $opt->{to_server} : $opt->{to_table}\n"); |
| 824 |
|
| $opt->_print(" $opt->{sx_ad_delete_join}\n") if $opt->{echo}; |
| 825 |
|
| } |
| 826 |
|
| $sql = $opt->{sx_ad_delete_cmd}; |
| 827 |
|
| my @res = @{ $opt->_runsql( $dbh_to, $sql, {}, undef ) }; |
| 828 |
|
| |
| 829 |
|
| #! my @res = $opt->{sx_dbh_to_non_bcp}->func( $opt->{sx_ad_delete_cmd}, {}, undef, 'nsql' ); |
| 830 |
|
| $DB_ERROR && sx_complain("error in deleting rows\n$DB_ERROR\n") && return 1; |
| 831 |
|
| |
| 832 |
|
| $opt->{sx_ad_rows_deleted} = $res[0]->{tot_rows}; |
| 833 |
|
| my $loop = $res[0]->{loop}; |
| 834 |
|
| if ( $opt->{progress_log} ) { |
| 835 |
|
| $opt->_print(" $opt->{sx_ad_rows_deleted} rows deleted\n"); # if $opt->{echo} && $loop>1; |
| 836 |
|
| } |
| 837 |
|
| |
| 838 |
|
| #destroy the temp table |
| 839 |
|
| $sql = "drop table $opt->{sx_ad_temp_table}"; |
| 840 |
|
| @res = @{ $opt->_runsql( $dbh_to, $sql, [], undef ) }; |
| 841 |
|
| |
| 842 |
|
| #! @res = $opt->{sx_dbh_to_non_bcp}->func("drop table $opt->{sx_ad_temp_table}", [], undef, 'nsql' ); |
| 843 |
|
| $DB_ERROR && sx_complain("error in dropping temp table\n$DB_ERROR\n") && return 1; |
| 844 |
|
| |
| 845 |
|
| if ( $opt->{progress_log} ) { |
| 846 |
|
| $opt->_print(" auto_delete successful\n\n"); # if $opt->{echo} && $loop>1; |
| 847 |
|
| } |
| 848 |
|
| |
| 849 |
|
| # my $db = $opt->{sx_to_database}; |
| 850 |
|
| # my $tab = $opt->{sx_to_table}; |
| 851 |
|
| # my $xxx = scalar @{ $opt->{sx_to_info}->{$db}->{$tab}->{__COLNAMES__} }; |
| 852 |
|
| # print "3. db=$db, tab=$tab, xxx=$xxx\n"; |
| 853 |
|
| |
| 854 |
|
| #success |
| 855 |
|
| return 0; |
| 856 |
|
| } |
| 857 |
|
| |
| 858 |
|
| #---- |
| 859 |
|
| #parse a user hash |
| 860 |
|
| #----- |
| 861 |
|
| sub sx_parse_user_hash { |
| 862 |
|
| |
| 863 |
|
| #this is a string like the following: |
| 864 |
|
| # |
| 865 |
|
| # '(key=>val, key=>val)' |
| 866 |
|
| # '{key=>val, key=>val}' |
| 867 |
|
| # real perl hashref |
| 868 |
|
| # 'filename' that contains hash |
| 869 |
|
| # |
| 870 |
|
| my ( $uh, $option ) = ( shift, shift ); |
| 871 |
|
| my %uhash = (); |
| 872 |
|
| my $err = ''; |
| 873 |
|
| |
| 874 |
|
| #real perl hash reference |
| 875 |
|
| if ( ref $uh eq 'HASH' ) { |
| 876 |
|
| %uhash = %$uh; |
| 877 |
|
| |
| 878 |
|
| #char list |
| 879 |
|
| } |
| 880 |
|
| elsif ( $uh =~ /^\s*\(/ ) { |
| 881 |
|
| { |
| 882 |
|
| no strict; |
| 883 |
|
| local $SIG{__WARN__} = sub { warn "$_[0]\n" }; |
| 884 |
|
| %uhash = eval "$uh"; |
| 885 |
|
| } |
| 886 |
|
| $err = $@; |
| 887 |
|
| |
| 888 |
|
| #maybe char ref |
| 889 |
|
| } |
| 890 |
|
| elsif ( $uh =~ /^\s*\{/ ) { |
| 891 |
|
| my $s = (); |
| 892 |
|
| { |
| 893 |
|
| no strict; |
| 894 |
|
| local $SIG{__WARN__} = sub { }; |
| 895 |
|
| $s = eval "$uh"; |
| 896 |
|
| } |
| 897 |
|
| $err = $@; |
| 898 |
|
| unless ($err) { |
| 899 |
|
| %uhash = %$s; |
| 900 |
|
| } |
| 901 |
|
| |
| 902 |
|
| #see if it's a file |
| 903 |
|
| } |
| 904 |
|
| elsif ( $uh =~ /[^\,\=]/ ) { |
| 905 |
|
| open( F, "<$uh" ) or $err = "couldn't open <$uh>: $!"; |
| 906 |
|
| if ( !$err ) { |
| 907 |
|
| my @lines = <F>; |
| 908 |
|
| my $lines = join '', @lines; |
| 909 |
|
| $lines = '(' . $lines if $lines !~ /^\s*\(/; |
| 910 |
|
| $lines .= ')' if $lines !~ /\)\s*$/; |
| 911 |
|
| { |
| 912 |
|
| local $SIG{__WARN__} = sub { }; |
| 913 |
|
| %uhash = eval "$lines"; |
| 914 |
|
| } |
| 915 |
|
| $err = $@; |
| 916 |
|
| close(F); |
| 917 |
|
| } |
| 918 |
|
| |
| 919 |
|
| #something's wrong |
| 920 |
|
| } |
| 921 |
|
| else { |
| 922 |
|
| $err = "can't parse $option"; |
| 923 |
|
| |
| 924 |
|
| } |
| 925 |
|
| |
| 926 |
|
| return ( \%uhash, $err ); |
| 927 |
|
| } |
| 928 |
|
| |
| 929 |
|
| #--- |
| 930 |
|
| # |
| 931 |
|
| #--- |
| 932 |
|
| sub sx_from_file_map { |
| 933 |
|
| |
| 934 |
|
| my $self = shift; |
| 935 |
|
| my $p = $self->{from_file_map}; |
| 936 |
|
| |
| 937 |
|
| my $db = $self->{sx_to_database}; |
| 938 |
|
| my $tab = $self->{sx_to_table}; |
| 939 |
|
| my @cols = @{ $self->{sx_to_colnames} }; |
| 940 |
|
| |
| 941 |
|
| my %map = (); |
| 942 |
|
| |
| 943 |
|
| #check if user specified a map. can be hashref, char string of list, for file |
| 944 |
|
| if ($p) { |
| 945 |
|
| |
| 946 |
|
| #parse the value. verify options has already verified no parsing error |
| 947 |
|
| my ( $m, $err ) = sx_parse_user_hash( $p, '-from_file_map' ); |
| 948 |
|
| %map = %$m; |
| 949 |
|
| |
| 950 |
|
| #check for accuracy |
| 951 |
|
| my @nf = grep { !exists $map{$_} } @cols; |
| 952 |
|
| if (@nf) { |
| 953 |
|
| sx_complain("the following fields are in -to_table but are not mapped: @nf\n"); |
| 954 |
|
| return 1; |
| 955 |
|
| } |
| 956 |
|
| |
| 957 |
|
| #make sure all the columns in -to_table are mapped |
| 958 |
|
| my %cols = (); |
| 959 |
|
| @cols{@cols} = (); |
| 960 |
|
| @nf = grep { !exists $cols{$_} } keys %map; |
| 961 |
|
| if (@nf) { |
| 962 |
|
| sx_complain("the following fields are in the map but not in -to_table: @nf\n"); |
| 963 |
|
| return 1; |
| 964 |
|
| } |
| 965 |
|
| |
| 966 |
|
| #it's quite ok not to map certain fields in the -from_source |
| 967 |
|
| # @cols{values %map} = (); |
| 968 |
|
| # @nf = grep { !exists $cols{$_} } 0 .. $#cols; |
| 969 |
|
| # if (@nf) { |
| 970 |
|
| # sx_complain("the following column numbers were not mapped (first column is 0): @nf\n"); |
| 971 |
|
| # return 1; |
| 972 |
|
| # } |
| 973 |
|
| |
| 974 |
|
| $self->{sx_from_file_map} = \%map; |
| 975 |
|
| $self->{sx_from_file_map_indices} = [ @map{@cols} ]; |
| 976 |
|
| |
| 977 |
|
| } |
| 978 |
|
| else { |
| 979 |
|
| $self->{sx_from_file_map} = 0; |
| 980 |
|
| $self->{sx_from_file_map_indices} = 0; |
| 981 |
|
| } |
| 982 |
|
| |
| 983 |
|
| return 0; |
| 984 |
|
| } |
| 985 |
|
| |
| 986 |
|
| #------- |
| 987 |
|
| # |
| 988 |
|
| #------- |
| 989 |
|
| sub sx_sendrow_final_batch { |
| 990 |
|
| |
| 991 |
|
| my $self = shift; |
| 992 |
|
| |
| 993 |
|
| #commit last set of rows |
| 994 |
|
| my $rc = 0; |
| 995 |
|
| if ( $self->{sx_send_count_batch} > 0 ) { |
| 996 |
|
| $rc = sx_sendrow_batch($self); #<0 == bad, >=0 == good |
| 997 |
|
| $rc = $rc == &CS_FAIL ? 1 : 0; |
| 998 |
|
| } |
| 999 |
|
| |
| 1000 |
|
| #1 == error, 0 == success |
| 1001 |
|
| return $rc; |
| 1002 |
|
| } |
| 1003 |
|
| |
| 1004 |
|
| #----------------------------------------------------------------------- |
| 1005 |
|
| #auto_delete callback |
| 1006 |
|
| #must return 1 on success (nsql requirement) |
| 1007 |
|
| #----------------------------------------------------------------------- |
| 1008 |
|
| sub sx_sendrow_temp { |
| 1009 |
|
| |
| 1010 |
|
| #args |
| 1011 |
|
| my ( $r_row, $opt, $ad_seen ) = @_; |
| 1012 |
|
| |
| 1013 |
|
| #pull out the key columns only |
| 1014 |
|
| my @bcp_row = @{$r_row}[ @{ $opt->{sx_ad_col_num} } ]; |
| 1015 |
|
| |
| 1016 |
|
| #only do unique rows |
| 1017 |
|
| my $distinct = join '', @bcp_row; |
| 1018 |
|
| return 1 if $ad_seen->{$distinct}; |
| 1019 |
|
| $ad_seen->{$distinct} = 1; |
| 1020 |
|
| |
| 1021 |
|
| #row count |
| 1022 |
|
| $opt->{sx_ad_temp_tab_count}++; |
| 1023 |
|
| |
| 1024 |
|
| #send the row |
| 1025 |
|
| my $status_send = $opt->{sx_dbh_to_bcp_temp}->blk_rowxfer( \@bcp_row ); |
| 1026 |
|
| $DB_ERROR && sx_complain("$DB_ERROR\n") && return 0; |
| 1027 |
|
| |
| 1028 |
|
| #commit the row |
| 1029 |
|
| if ( $opt->{sx_ad_temp_tab_count} % $opt->{batchsize} == 0 ) { |
| 1030 |
|
| my $numRowsAffected; |
| 1031 |
|
| $opt->_print( ' ' . $opt->{sx_ad_temp_tab_count} . " keys transferred\n" ) if $opt->{echo}; |
| 1032 |
|
| $opt->{sx_dbh_to_bcp_temp}->blk_done( CS_BLK_BATCH, $numRowsAffected ); |
| 1033 |
|
| $DB_ERROR && sx_complain("$DB_ERROR\n") && return 0; |
| 1034 |
|
| } |
| 1035 |
|
| |
| 1036 |
|
| return 1; |
| 1037 |
|
| } |
| 1038 |
|
| |
| 1039 |
|
| #----------------------------------------------------------------------- |
| 1040 |
|
| #the actual callback - bcp version |
| 1041 |
|
| #must return 1 on success (nsql requirement) |
| 1042 |
|
| #----------------------------------------------------------------------- |
| 1043 |
|
| sub sx_sendrow_bcp { |
| 1044 |
|
| |
| 1045 |
|
| #args |
| 1046 |
|
| my ( $r_row, $opt ) = @_; |
| 1047 |
|
| |
| 1048 |
|
| my $dbh = $opt->{sx_dbh_to_bcp}; |
| 1049 |
|
| my ( $status_cb_pre, $status_cb_err_send, $status_cb_err_batch ) = (); |
| 1050 |
|
| my ( $status_send, $status_batch ) = (); |
| 1051 |
|
| |
| 1052 |
|
| |
| 1053 |
|
| #--- |
| 1054 |
|
| #user-defined callback pre send |
| 1055 |
|
| #--- |
| 1056 |
|
| $status_cb_pre = 1; |
| 1057 |
|
| if ( ref( $opt->{callback_pre_send} ) eq 'CODE' ) { |
| 1058 |
|
| my $r_user_row = (); |
| 1059 |
|
| ( $status_cb_pre, $r_user_row ) = $opt->{callback_pre_send}->($r_row); |
| 1060 |
|
| |
| 1061 |
|
| #>0 => use @row returned by user |
| 1062 |
|
| if ( $status_cb_pre > 0 ) { |
| 1063 |
|
| |
| 1064 |
|
| $r_row = $r_user_row; |
| 1065 |
|
| } |
| 1066 |
|
| |
| 1067 |
|
| #<0 => skip row altogether |
| 1068 |
|
| elsif ( $status_cb_pre < 0 ) { |
| 1069 |
|
| $opt->{sx_send_count}++; |
| 1070 |
|
| $opt->{sx_send_count_batch}++; |
| 1071 |
|
| return 1; |
| 1072 |
|
| |
| 1073 |
|
| #==0 => abort based on user |
| 1074 |
|
| } |
| 1075 |
|
| else { |
| 1076 |
|
| sx_complain("User-defined '-callback_pre_send' failed ($opt->{callback_pre_send}).\n"); |
| 1077 |
|
| return 0; |
| 1078 |
|
| } |
| 1079 |
|
| |
| 1080 |
|
| } |
| 1081 |
|
| |
| 1082 |
|
| #debug |
| 1083 |
|
| if ( ( $opt->{debug} & 0x02 ) != 0 ) { |
| 1084 |
|
| my $i = 0; |
| 1085 |
|
| for my $y (@$r_row) { $i++; $opt->_print("$i:\t<$y>\n"); } |
| 1086 |
|
| } |
| 1087 |
|
| |
| 1088 |
|
| #--- |
| 1089 |
|
| #if -map is speficied, this will call for a possible re-ordering of fields |
| 1090 |
|
| #--- |
| 1091 |
|
| if ( $opt->{sx_from_file_map} ) { |
| 1092 |
|
| $r_row = [ @{$r_row}[ @{ $opt->{sx_from_file_map_indices} } ] ]; |
| 1093 |
|
| } |
| 1094 |
|
| |
| 1095 |
|
| #--- |
| 1096 |
|
| #null out identity columns if server is going to set them |
| 1097 |
|
| #--- |
| 1098 |
|
| if (exists $opt->{identity_column}) { |
| 1099 |
|
| $r_row->[ $opt->{identity_column} ] = undef; |
| 1100 |
|
| } |
| 1101 |
|
| |
| 1102 |
|
| #--- |
| 1103 |
|
| #image columns are returned as strings in perl. #eg '151c2001ff34ac' is |
| 1104 |
|
| #obviously a string in hex - however when it comes back from perl via a |
| 1105 |
|
| #select statement it comes back in the form of a *character string* of |
| 1106 |
|
| #hex digits. this code changes to its native chars. |
| 1107 |
|
| #--- |
| 1108 |
|
| for my $c ( @{ $opt->{binary_columns} } ) { |
| 1109 |
|
| |
| 1110 |
|
| #lose the 0x business |
| 1111 |
|
| $r_row->[$c] = substr($r_row->[$c],2) if substr($r_row->[$c],0,2) eq '0x'; |
| 1112 |
|
| |
| 1113 |
|
| #convert to char |
| 1114 |
|
| $r_row->[$c] = pack("H*", $r_row->[$c]); |
| 1115 |
|
| } |
| 1116 |
|
| |
| 1117 |
|
| |
| 1118 |
|
| |
| 1119 |
|
| |
| 1120 |
|
| |
| 1121 |
|
| #--- |
| 1122 |
|
| #send row - will snag client errors |
| 1123 |
|
| #--- |
| 1124 |
|
| #status_send will == 0 on errors as well as DB_ERROR being set. |
| 1125 |
|
| $DB_ERROR = (); |
| 1126 |
|
| $opt->{sx_send_count}++; |
| 1127 |
|
| $opt->{sx_send_count_batch}++; |
| 1128 |
|
| $status_send = 1; |
| 1129 |
|
| |
| 1130 |
|
| #confirm number of columns in the source matches the target. ctlib v2.15 is presently core'ing on mismatch |
| 1131 |
|
| my ($l1, $l2) = (scalar @$r_row, $opt->{sx_to_numcols}); |
| 1132 |
|
| |
| 1133 |
|
| #push undef to end of record data record less than target table. |
| 1134 |
|
| if($l1 < $l2) { |
| 1135 |
|
| push @$r_row, undef for 1 .. ($l2 - $l1); |
| 1136 |
|
| } |
| 1137 |
|
| |
| 1138 |
|
| #signal error if data record greater than target table |
| 1139 |
|
| if( $l1 > $l2 ) { |
| 1140 |
|
| $DB_ERROR="number of columns in data record does not match number of columns in target table\n"; |
| 1141 |
|
| $DB_ERROR_ONELINE=$DB_ERROR; |
| 1142 |
|
| } |
| 1143 |
|
| |
| 1144 |
|
| #make the call to send the data |
| 1145 |
|
| if(!$DB_ERROR) { |
| 1146 |
|
| if ( $dbh->blk_rowxfer($r_row) == CS_FAIL ) { |
| 1147 |
|
| $status_send = 0; |
| 1148 |
|
| } |
| 1149 |
|
| } |
| 1150 |
|
| $status_send = 0 if $DB_ERROR; |
| 1151 |
|
| |
| 1152 |
|
| |
| 1153 |
|
| #save the row iff error_handling == 'retry' |
| 1154 |
|
| if ( $opt->{error_handling} =~ /^retry/i ) { |
| 1155 |
|
| push @{ $opt->{data_rows} }, { |
| 1156 |
|
| rn => $opt->{sx_send_count}, |
| 1157 |
|
| row => $r_row, |
| 1158 |
|
| }; |
| 1159 |
|
| } |
| 1160 |
|
| |
| 1161 |
|
| #check for failure on send |
| 1162 |
|
| if ( !$status_send ) { |
| 1163 |
|
| $opt->{sx_err_count}++; |
| 1164 |
|
| $opt->{sx_err_count_batch}++; |
| 1165 |
|
| $status_send = sx_sendrow_failure( $opt, $r_row ); |
| 1166 |
|
| |
| 1167 |
|
| } |
| 1168 |
|
| else { |
| 1169 |
|
| $opt->{sx_succeed_count_batch}++; |
| 1170 |
|
| } |
| 1171 |
|
| |
| 1172 |
|
| #commit |
| 1173 |
|
| $status_batch = 1; |
| 1174 |
|
| if ( $opt->{sx_send_count} % $opt->{batchsize} == 0 ) { |
| 1175 |
|
| $status_batch = sx_sendrow_batch($opt); |
| 1176 |
|
| $opt->{sx_send_count_batch} = 0; |
| 1177 |
|
| $opt->{sx_resend_count_batch} = 0; |
| 1178 |
|
| $opt->{sx_succeed_count_batch} = 0; |
| 1179 |
|
| $opt->{sx_err_count_batch} = 0; |
| 1180 |
|
| } |
| 1181 |
|
| |
| 1182 |
|
| #set return code |
| 1183 |
|
| return sx_sendrow_return( $opt, $status_send, $status_batch, $status_cb_pre ); |
| 1184 |
|
| |
| 1185 |
|
| } |
| 1186 |
|
| |
| 1187 |
|
| #----------------------------------------------------------------------- |
| 1188 |
|
| #sendrow failure processing |
| 1189 |
|
| #----------------------------------------------------------------------- |
| 1190 |
|
| sub sx_sendrow_failure { |
| 1191 |
|
| |
| 1192 |
|
| my $opt = shift; |
| 1193 |
|
| my $r_row = shift; |
| 1194 |
|
| |
| 1195 |
|
| my $status_send = (); |
| 1196 |
|
| my $pissant_warning = 0; |
| 1197 |
|
| |
| 1198 |
|
| #squirrel the errors away |
| 1199 |
|
| $opt->{sx_save_db_error} = $DB_ERROR; |
| 1200 |
|
| $opt->{sx_save_db_error_oneline} = $DB_ERROR_ONELINE; |
| 1201 |
|
| |
| 1202 |
|
| #a) err send is a cb |
| 1203 |
|
| if ( ref( $opt->{callback_err_send} ) eq 'CODE' && $opt->{error_handling} !~ /^abort/i ) { |
| 1204 |
|
| my ( $status_cb_err_send, $u_row ) = $opt->{callback_err_send}->( |
| 1205 |
|
| DB_ERROR => $DB_ERROR, |
| 1206 |
|
| row_num => $opt->{sx_send_count}, |
| 1207 |
|
| row_ptr => $r_row |
| 1208 |
|
| ); |
| 1209 |
|
| |
| 1210 |
|
| #if user indicated retry status- then send the (fixed-up) row again! |
| 1211 |
|
| if ($status_cb_err_send) { |
| 1212 |
|
| $opt->{sx_resend_count}++; |
| 1213 |
|
| $opt->{sx_resend_count_batch}++; |
| 1214 |
|
| my $rs = sx_sendrow_bcp( $u_row, $opt ); |
| 1215 |
|
| $DB_ERROR = (); #clean-up error |
| 1216 |
|
| $status_send = 1; #force success |
| 1217 |
|
| } |
| 1218 |
|
| else { |
| 1219 |
|
| $status_send = 0; |
| 1220 |
|
| } |
| 1221 |
|
| |
| 1222 |
|
| #b) err send is a HASH |
| 1223 |
|
| } |
| 1224 |
|
| elsif ( ref( $opt->{callback_err_send} ) eq 'HASH' && $opt->{error_handling} !~ /^abort$/i ) { |
| 1225 |
|
| ${ $opt->{callback_err_send} }{ $opt->{sx_send_count} }->{msg} = $DB_ERROR; |
| 1226 |
|
| ${ $opt->{callback_err_send} }{ $opt->{sx_send_count} }->{row} = $r_row; |
| 1227 |
|
| $DB_ERROR = (); |
| 1228 |
|
| $status_send = 1; |
| 1229 |
|
| |
| 1230 |
|
| #c) no err cb - print offending row |
| 1231 |
|
| } |
| 1232 |
|
| else { |
| 1233 |
|
| |
| 1234 |
|
| #regardless of -error_handling write to edf if specified; otherwise stderr |
| 1235 |
|
| if ( $opt->{error_data_file} ) { |
| 1236 |
|
| my $rn = $opt->{sx_send_count}; |
| 1237 |
|
| my $elf = $opt->{sx_error_data_file_fh}; |
| 1238 |
|
| local ($") = "|"; |
| 1239 |
|
| print $elf "#recnum=$rn, reason=$DB_ERROR_ONELINE\n@{$r_row}\n"; |
| 1240 |
|
| |
| 1241 |
|
| #print out the row smartly to stderr |
| 1242 |
|
| } |
| 1243 |
|
| else { |
| 1244 |
|
| $opt->{override_silent}++; |
| 1245 |
|
| my $n = $opt->{sx_send_count}; |
| 1246 |
|
| sx_oversize_error( $opt, $r_row, $n, $DB_ERROR ); |
| 1247 |
|
| $pissant_warning++ if $DB_ERROR_ONELINE =~ /oversized row/; |
| 1248 |
|
| } |
| 1249 |
|
| |
| 1250 |
|
| #set return status |
| 1251 |
|
| # continue => plow ahead and skip this row |
| 1252 |
|
| # retry => since callback defined, skip this row |
| 1253 |
|
| # abort => stop now |
| 1254 |
|
| |
| 1255 |
|
| if ($pissant_warning) { |
| 1256 |
|
| $status_send = 1; |
| 1257 |
|
| } |
| 1258 |
|
| else { |
| 1259 |
|
| if ( $opt->{error_handling} =~ /^abort/i ) { |
| 1260 |
|
| $opt->{GOT_UNRECOV_ERROR} = 1; |
| 1261 |
|
| $status_send = 0; |
| 1262 |
|
| } |
| 1263 |
|
| else { |
| 1264 |
|
| $status_send = 1; |
| 1265 |
|
| } |
| 1266 |
|
| } |
| 1267 |
|
| |
| 1268 |
|
| } |
| 1269 |
|
| |
| 1270 |
|
| return $status_send; |
| 1271 |
|
| |
| 1272 |
|
| } |
| 1273 |
|
| |
| 1274 |
|
| #----------------------------------------------------------------------- |
| 1275 |
|
| #print sybase error nicely |
| 1276 |
|
| #----------------------------------------------------------------------- |
| 1277 |
|
| sub sx_error_tidy_msg { |
| 1278 |
|
| my $self = shift; |
| 1279 |
|
| |
| 1280 |
|
| # if (substr($DB_ERROR, 0,-1) !~ /\n/s ) { |
| 1281 |
|
| $self->_print(" error_message : $DB_ERROR_ONELINE\n"); |
| 1282 |
|
| |
| 1283 |
|
| # } else { |
| 1284 |
|
| # $self->_print(" error_message : $DB_ERROR_ONELINE"); |
| 1285 |
|
| # } |
| 1286 |
|
| } |
| 1287 |
|
| |
| 1288 |
|
| #----------------------------------------------------------------------- |
| 1289 |
|
| #print error detection status |
| 1290 |
|
| #----------------------------------------------------------------------- |
| 1291 |
|
| sub _print_error_detection { |
| 1292 |
|
| my $self = shift; |
| 1293 |
|
| $self->_print("--------------\n"); |
| 1294 |
|
| $self->_print("Error detected\n"); |
| 1295 |
|
| $self->_print(" error_handling : $self->{error_handling}\n"); |
| 1296 |
|
| $self->_print(" error_data_file: $self->{error_data_file}\n") if $self->{error_data_file}; |
| 1297 |
|
| $self->_print(" rows read : $self->{sx_send_count}\n"); |
| 1298 |
|
| $self->sx_error_tidy_msg(); |
| 1299 |
|
| $self->_print(" callback_err_send = $self->{callback_err_send}\n") if defined $self->{callback_err_send}; |
| 1300 |
|
| $self->_print(" callback_err_batch = $self->{callback_err_batch}\n") if defined $self->{callback_err_batch}; |
| 1301 |
|
| $self->_print("--------------\n"); |
| 1302 |
|
| } |
| 1303 |
|
| |
| 1304 |
|
| #----------------------------------------------------------------------- |
| 1305 |
|
| #what to do on send batch failure |
| 1306 |
|
| #----------------------------------------------------------------------- |
| 1307 |
|
| sub sx_sendrow_batch_failure { |
| 1308 |
|
| |
| 1309 |
|
| my $opt = shift; |
| 1310 |
|
| |
| 1311 |
|
| my $status_batch = (); |
| 1312 |
|
| |
| 1313 |
|
| #squirrel the errors away |
| 1314 |
|
| $opt->{sx_save_db_error} = $DB_ERROR; |
| 1315 |
|
| $opt->{sx_save_db_error_oneline} = $DB_ERROR_ONELINE; |
| 1316 |
|
| |
| 1317 |
|
| #ABORT |
| 1318 |
|
| if ( $opt->{error_handling} =~ /^abort$/i ) { |
| 1319 |
|
| $opt->_print_error_detection(); |
| 1320 |
|
| $status_batch = 0; |
| 1321 |
|
| $opt->{GOT_UNRECOV_ERROR} = 1; |
| 1322 |
|
| |
| 1323 |
|
| #CONTINUE OR RETRY |
| 1324 |
|
| } |
| 1325 |
|
| elsif ( $opt->{error_handling} =~ /^continue/i || $opt->{error_handling} =~ /^retry/i ) { |
| 1326 |
|
| |
| 1327 |
|
| #a) callback exists. returns status(1=resend batch, 0=abort batch) and a ref to the rows |
| 1328 |
|
| my $cb = $opt->{callback_err_batch}; |
| 1329 |
|
| if ( ref($cb) eq 'CODE' ) { |
| 1330 |
|
| my ( $cb_status, $ref_row ) = $cb->( |
| 1331 |
|
| DB_ERROR => $DB_ERROR, |
| 1332 |
|
| row_num => $opt->{sx_send_count}, |
| 1333 |
|
| rows => \@{ $opt->{data_rows} }, |
| 1334 |
|
| xfer_self => $opt |
| 1335 |
|
| ); |
| 1336 |
|
| |
| 1337 |
|
| if ( $cb_status == 0 ) { |
| 1338 |
|
| $status_batch = 0; |
| 1339 |
|
| } |
| 1340 |
|
| elsif ( $cb_status > 0 ) { |
| 1341 |
|
| $status_batch = sx_sendrow_batch_of_size( $opt, $ref_row, $cb_status ); |
| 1342 |
|
| } |
| 1343 |
|
| else { |
| 1344 |
|
| } |
| 1345 |
|
| $DB_ERROR = (); |
| 1346 |
|
| |
| 1347 |
|
| #b) no callback |
| 1348 |
|
| } |
| 1349 |
|
| else { |
| 1350 |
|
| $status_batch = 0; |
| 1351 |
|
| $opt->_print_error_detection(); |
| 1352 |
|
| |
| 1353 |
|
| #@ sx_error_tidy_msg($opt); |
| 1354 |
|
| #@ $DB_ERROR && $opt->_print( "$DB_ERROR\n"); |
| 1355 |
|
| $opt->_print( |
| 1356 |
|
| "blk_done error, -error_handling=$opt->{error_handling}, but no " . "-callback_err_batch defined\n" ); |
| 1357 |
|
| } |
| 1358 |
|
| } |
| 1359 |
|
| |
| 1360 |
|
| return $status_batch; |
| 1361 |
|
| } |
| 1362 |
|
| |
| 1363 |
|
| #----------------------------------------------------------------------- |
| 1364 |
|
| #commit the rows |
| 1365 |
|
| #----------------------------------------------------------------------- |
| 1366 |
|
| sub sx_sendrow_batch { |
| 1367 |
|
| |
| 1368 |
|
| # returns -1 for failure, >=0 for success |
| 1369 |
|
| |
| 1370 |
|
| my $opt = $_[0]; |
| 1371 |
|
| my $dbh = $opt->{sx_dbh_to_bcp}; |
| 1372 |
|
| my $status_batch = -99; |
| 1373 |
|
| |
| 1374 |
|
| #reset error flags before call to blk_done |
| 1375 |
|
| ( $DB_ERROR, $DB_ERROR_ONELINE, $DB_DEADLOCK ) = (); |
| 1376 |
|
| |
| 1377 |
|
| #returns number of rows when it works, -1 on failure , or zero. |
| 1378 |
|
| my $numRowsAffected; |
| 1379 |
|
| $status_batch = $dbh->blk_done( CS_BLK_BATCH, $numRowsAffected ); |
| 1380 |
|
| |
| 1381 |
|
| # print "****strange case status_batch=$status_batch\n" if $DB_ERROR && $status_batch == 0; |
| 1382 |
|
| |
| 1383 |
|
| if ( $status_batch != CS_SUCCEED || $DB_ERROR ) { |
| 1384 |
|
| $status_batch = sx_sendrow_batch_failure($opt); |
| 1385 |
|
| } |
| 1386 |
|
| else { |
| 1387 |
|
| $status_batch = sx_sendrow_batch_success( $opt, $numRowsAffected ); |
| 1388 |
|
| } |
| 1389 |
|
| |
| 1390 |
|
| #give back the storage |
| 1391 |
|
| print scalar localtime() . " undefing\n" if $opt->{debug}; |
| 1392 |
|
| @{ $opt->{data_rows} } = undef if $status_batch > 0; #if $opt->{error_handling} =~ /^retry/i; |
| 1393 |
|
| print scalar localtime() . " done undefing\n" if $opt->{debug}; |
| 1394 |
|
| |
| 1395 |
|
| return $numRowsAffected; |
| 1396 |
|
| } |
| 1397 |
|
| |
| 1398 |
|
| #--- |
| 1399 |
|
| # |
| 1400 |
|
| #--- |
| 1401 |
|
| sub bcp_batch_error_handler { |
| 1402 |
|
| |
| 1403 |
|
| #pull the args |
| 1404 |
|
| my %h = @_; |
| 1405 |
|
| my ( $err, $line_num, $row_ref, $opt ) = @h{qw/DB_ERROR row_num rows xfer_self/}; |
| 1406 |
|
| |
| 1407 |
|
| #return code definitions: |
| 1408 |
|
| # rc = 0 : abort |
| 1409 |
|
| # rc = 1 : resend and batch a record at a time |
| 1410 |
|
| # rc > 1 : resend but in one batch |
| 1411 |
|
| # |
| 1412 |
|
| my $rc = 0; |
| 1413 |
|
| my ( $st, $md, $v ) = @{$opt}{qw/retry_deadlock_sleep retry_max retry_verbose/}; |
| 1414 |
|
| |
| 1415 |
|
| #deadlock error. |
| 1416 |
|
| my $deadlock = 1 if $err =~ /1205/m; |
| 1417 |
|
| |
| 1418 |
|
| # my $deadlock = 1 if $err =~ /2601/m; #testing. simulate deadlock error with dupe key error |
| 1419 |
|
| if ($deadlock) { |
| 1420 |
|
| my $nd = ++$opt->{sx_num_retry}; |
| 1421 |
|
| if ( $nd > $md ) { |
| 1422 |
|
| $opt->_print("------\n"); |
| 1423 |
|
| $opt->_print("max retries. aborting only this batch.\n\n"); |
| 1424 |
|
| $opt->_print("------\n"); |
| 1425 |
|
| $rc = 0; |
| 1426 |
|
| } |
| 1427 |
|
| else { |
| 1428 |
|
| |
| 1429 |
|
| if ($v) { |
| 1430 |
|
| $opt->_print("------\n"); |
| 1431 |
|
| $opt->_print("Error on blk_done. It's a deadlock error! Sleeping for $st seconds\n"); |
| 1432 |
|
| $opt->_print(" $DB_ERROR_ONELINE\n"); |
| 1433 |
|
| $opt->_print("------\n"); |
| 1434 |
|
| } |
| 1435 |
|
| sleep $st; |
| 1436 |
|
| $rc = 2; #force no one-by-one error reporting |
| 1437 |
|
| } |
| 1438 |
|
| |
| 1439 |
|
| #not a deadlock error. |
| 1440 |
|
| } |
| 1441 |
|
| else { |
| 1442 |
|
| if ($v) { |
| 1443 |
|
| $opt->_print("------\n"); |
| 1444 |
|
| $opt->_print( |
| 1445 |
|
| "Error on blk_done. It's NOT a deadlock error. Setting batch-size to one and resending batch.\n"); |
| 1446 |
|
| $opt->_print(" $DB_ERROR_ONELINE\n"); |
| 1447 |
|
| $opt->_print("------\n"); |
| 1448 |
|
| } |
| 1449 |
|
| $rc = 1; #one at a time |
| 1450 |
|
| |
| 1451 |
|
| # $rc = 2; #testing |
| 1452 |
|
| |
| 1453 |
|
| } |
| 1454 |
|
| return ( $rc, $row_ref ); |
| 1455 |
|
| } |
| 1456 |
|
| |
| 1457 |
|
| #---- |
| 1458 |
|
| #resubmit the row |
| 1459 |
|
| #----- |
| 1460 |
|
| sub sx_sendrow_batch_of_size { |
| 1461 |
|
| |
| 1462 |
|
| # |
| 1463 |
|
| #returns number of rows sent, >=1 success, <=0 bad |
| 1464 |
|
| # |
| 1465 |
|
| my $opt = shift; |
| 1466 |
|
| my $r_rows = shift; |
| 1467 |
|
| my $new_batchsize = shift; |
| 1468 |
|
| |
| 1469 |
|
| my $x = scalar @$r_rows; |
| 1470 |
|
| |
| 1471 |
|
| my $rcount = 0; |
| 1472 |
|
| my $elf = $opt->{sx_error_data_file_fh}; |
| 1473 |
|
| my $dbh = $opt->{sx_dbh_to_bcp}; |
| 1474 |
|
| foreach my $hp ( @{$r_rows} ) { |
| 1475 |
|
| my ( $rn, $row ) = ( $hp->{rn}, $hp->{row} ); |
| 1476 |
|
| next unless $row; #for some reason an undefined row is being stored |
| 1477 |
|
| $rcount++; |
| 1478 |
|
| |
| 1479 |
|
| $opt->{sx_resend_count}++; |
| 1480 |
|
| $opt->{sx_resend_count_batch}++; |
| 1481 |
|
| |
| 1482 |
|
| #send row again |
| 1483 |
|
| if ( $dbh->blk_rowxfer($row) == CS_FAIL ) { |
| 1484 |
|
| local ($") = "|"; |
| 1485 |
|
| eval { print $elf "#recnum=$rn, reason=$DB_ERROR_ONELINE\n@$row\n"; }; |
| 1486 |
|
| print "eval error=$@\n" if $@; |
| 1487 |
|
| |
| 1488 |
|
| next; |
| 1489 |
|
| } |
| 1490 |
|
| |
| 1491 |
|
| #batch one by one only if batchsize is 1 |
| 1492 |
|
| if ( $new_batchsize == 1 ) { |
| 1493 |
|
| my $numRowsAffected; |
| 1494 |
|
| my $status = $dbh->blk_done( CS_BLK_BATCH, $numRowsAffected ); |
| 1495 |
|
| if ( $numRowsAffected != $new_batchsize ) { |
| 1496 |
|
| $opt->{sx_err_count}++; |
| 1497 |
|
| $opt->{sx_err_count_batch}++; |
| 1498 |
|
| local ($") = "|"; |
| 1499 |
|
| print $elf "#recnum=$rn, reason=$DB_ERROR_ONELINE\n@$row\n"; |
| 1500 |
|
| } |
| 1501 |
|
| } |
| 1502 |
|
| } |
| 1503 |
|
| |
| 1504 |
|
| my $num_good = (); |
| 1505 |
|
| if ( $new_batchsize == 1 ) { |
| 1506 |
|
| $num_good = $opt->{sx_resend_count_batch} - $opt->{sx_err_count_batch}; |
| 1507 |
|
| |
| 1508 |
|
| # $DB_IN_ERR_HANDLER = 1; |
| 1509 |
|
| sx_sendrow_batch_success( $opt, $num_good ); #log message |
| 1510 |
|
| # $DB_IN_ERR_HANDLER = 0; |
| 1511 |
|
| |
| 1512 |
|
| } |
| 1513 |
|
| else { |
| 1514 |
|
| $num_good = sx_sendrow_batch($opt); |
| 1515 |
|
| |
| 1516 |
|
| $num_good = 0 if $num_good < 0; |
| 1517 |
|
| $DB_IN_ERR_HANDLER = 1; |
| 1518 |
|
| sx_sendrow_batch_success( $opt, $num_good ); #log message |
| 1519 |
|
| $DB_IN_ERR_HANDLER = 0; |
| 1520 |
|
| |
| 1521 |
|
| } |
| 1522 |
|
| |
| 1523 |
|
| #log message |
| 1524 |
|
| return $num_good; |
| 1525 |
|
| |
| 1526 |
|
| # return sx_sendrow_batch_success($opt, $num_good); |
| 1527 |
|
| } |
| 1528 |
|
| |
| 1529 |
|
| #----- |
| 1530 |
|
| # |
| 1531 |
|
| #----- |
| 1532 |
|
| sub sx_sendrow_batch_success { |
| 1533 |
|
| |
| 1534 |
|
| my $opt = shift; |
| 1535 |
|
| my $status_batch = shift; |
| 1536 |
|
| |
| 1537 |
|
| return $status_batch if $DB_IN_ERR_HANDLER; |
| 1538 |
|
| |
| 1539 |
|
| my $send_count = $opt->{sx_send_count}; |
| 1540 |
|
| $opt->{sx_succeed_count} += $status_batch; |
| 1541 |
|
| if ( $opt->{progress_log} ) { |
| 1542 |
|
| my $suc = $opt->{sx_succeed_count}; |
| 1543 |
|
| my $res = $opt->{sx_resend_count}; |
| 1544 |
|
| my $fal = $opt->{sx_err_count} - $res; |
| 1545 |
|
| my $bsuc = $opt->{sx_succeed_count_batch}; |
| 1546 |
|
| my $bres = $opt->{sx_resend_count_batch}; |
| 1547 |
|
| my $bfal = $opt->{sx_err_count_batch}; |
| 1548 |
|
| |
| 1549 |
|
| #if errors encountered then give a different log message. otherwise keep it simple |
| 1550 |
|
| if ($res) { |
| 1551 |
|
| $opt->{override_silent} = 1; |
| 1552 |
|
| $opt->_print(" $status_batch rows committed [$suc/$send_count] (retries=$bres, fails=$bfal)\n"); |
| 1553 |
|
| $opt->{override_silent} = 0; |
| 1554 |
|
| } |
| 1555 |
|
| else { |
| 1556 |
|
| $opt->_print(" $status_batch rows committed [$suc]\n"); |
| 1557 |
|
| } |
| 1558 |
|
| } |
| 1559 |
|
| |
| 1560 |
|
| return $status_batch; |
| 1561 |
|
| } |
| 1562 |
|
| |
| 1563 |
|
| #----------------------------------------------------------------------- |
| 1564 |
|
| #set the return code from bcp_sendow |
| 1565 |
|
| #must return 1 for success (nsql thingy) |
| 1566 |
|
| #----------------------------------------------------------------------- |
| 1567 |
|
| sub sx_sendrow_return { |
| 1568 |
|
| |
| 1569 |
|
| my $opt = shift; |
| 1570 |
|
| my ( $status_send, $status_batch, $status_cb_pre ) = @_; |
| 1571 |
|
| |
| 1572 |
|
| my $error = !( $status_send && $status_batch && $status_cb_pre ); |
| 1573 |
|
| if ($error) { |
| 1574 |
|
| |
| 1575 |
|
| #abort |
| 1576 |
|
| if ( $opt->{error_handling} =~ /^abort$/i ) { |
| 1577 |
|
| |
| 1578 |
|
| #batch failure already logged message |
| 1579 |
|
| $status_batch && $opt->_print_error_detection(); |
| 1580 |
|
| return 0; |
| 1581 |
|
| |
| 1582 |
|
| #plow thru errrors if so set |
| 1583 |
|
| } |
| 1584 |
|
| else { |
| 1585 |
|
| $DB_ERROR = (); |
| 1586 |
|
| return 1; |
| 1587 |
|
| } |
| 1588 |
|
| |
| 1589 |
|
| #clean return |
| 1590 |
|
| } |
| 1591 |
|
| else { |
| 1592 |
|
| return 1; |
| 1593 |
|
| |
| 1594 |
|
| } |
| 1595 |
|
| } |
| 1596 |
|
| |
| 1597 |
|
| #----------------------------------------------------------------------- |
| 1598 |
|
| #where to put messages |
| 1599 |
|
| #----------------------------------------------------------------------- |
| 1600 |
|
| sub _print { |
| 1601 |
|
| |
| 1602 |
|
| my ( $self, $line ) = @_; |
| 1603 |
|
| |
| 1604 |
|
| #we don't want to suppress print statements from SQL |
| 1605 |
|
| my ($pack2) = caller(2); |
| 1606 |
|
| |
| 1607 |
|
| #some conditions must be overrieded like errors |
| 1608 |
|
| my $force = $self->{override_silent}; |
| 1609 |
|
| |
| 1610 |
|
| if ( $pack2 eq "Sybase::CTlib" || !$self->{silent} || $force ) { |
| 1611 |
|
| ref( $self->{callback_print} ) eq 'CODE' ? $self->{callback_print}->($line) : print "$line"; |
| 1612 |
|
| } |
| 1613 |
|
| } |
| 1614 |
|
| |
| 1615 |
|
| #----- |
| 1616 |
|
| # |
| 1617 |
|
| #----- |
| 1618 |
|
| sub _dbiConnect { |
| 1619 |
|
| |
| 1620 |
|
| my $self = shift; |
| 1621 |
|
| my $fromOrTo = shift; |
| 1622 |
|
| |
| 1623 |
|
| my ( $dbih, $dbiDSN, $dbd, $where ); |
| 1624 |
|
| my ( $u, $p, $s, $d, $a ); |
| 1625 |
|
| |
| 1626 |
|
| #FROM side |
| 1627 |
|
| if ( $fromOrTo =~ /from/i ) { |
| 1628 |
|
| $where = 'FROM'; |
| 1629 |
|
| |
| 1630 |
|
| if ( $self->{from_dsn} =~ /^DBD/i ) { |
| 1631 |
|
| ( $u, $p, $s, $d, $a ) = @{$self}{qw/from_user from_password from_server from_database app_name/}; |
| 1632 |
|
| $dbd = ( split /\./, $self->{from_dsn} )[1]; |
| 1633 |
|
| } |
| 1634 |
|
| else { |
| 1635 |
|
| sx_complain("brain-damage. can't make dbi connection on non-DBI FROM source\n"); |
| 1636 |
|
| return undef; |
| 1637 |
|
| } |
| 1638 |
|
| |
| 1639 |
|
| #TO side |
| 1640 |
|
| } |
| 1641 |
|
| elsif ( $fromOrTo =~ /to/i ) { |
| 1642 |
|
| $where = 'TO'; |
| 1643 |
|
| ( $u, $p, $s, $d, $a ) = @{$self}{qw/to_user to_password to_server to_database app_name/}; |
| 1644 |
|
| $dbd = 'Sybase'; #sjs needs to be changed! I know. I know. |
| 1645 |
|
| |
| 1646 |
|
| #error |
| 1647 |
|
| } |
| 1648 |
|
| else { |
| 1649 |
|
| sx_complain("brain-damage. bad dbiConnect call\n"); |
| 1650 |
|
| return undef; |
| 1651 |
|
| |
| 1652 |
|
| } |
| 1653 |
|
| |
| 1654 |
|
| $a .= "_" . substr( $where, 0, 1 ); |
| 1655 |
|
| |
| 1656 |
|
| #set the dbi dsn value |
| 1657 |
|
| if ( $dbd =~ /^Sybase$/i ) { |
| 1658 |
|
| $dbiDSN = "dbi:$dbd:server=$s;hostname=$ENV{HOSTNAME}"; |
| 1659 |
|
| $dbiDSN .= ";scriptName=$a"; |
| 1660 |
|
| $dbiDSN .= ";database=$d" if $d; |
| 1661 |
|
| } |
| 1662 |
|
| elsif ( $dbd =~ /^DB2$/i ) { |
| 1663 |
|
| $dbiDSN = "dbi:$dbd:$s"; |
| 1664 |
|
| } |
| 1665 |
|
| |
| 1666 |
|
| #make the connection via DBI - we'll print our own error message |
| 1667 |
|
| $dbih = DBI->connect( $dbiDSN, $u, $p, { RaiseError => 0, PrintError => 0 } ); |
| 1668 |
|
| $dbih || ( sx_complain("$where login error:\n$DB_ERROR(User=[$u], Pwd=[$p], Server=[$s])\n") && return undef ); |
| 1669 |
|
| if ( $dbd =~ /Sybase/i ) { |
| 1670 |
|
| $dbih->{syb_err_handler} = sub { return $self->sx_error_handler_dbi(@_) }; |
| 1671 |
|
| $dbih->{syb_show_sql}++; |
| 1672 |
|
| $dbih->{syb_show_eed}++; |
| 1673 |
|
| $dbih->do("set flushmessage on"); |
| 1674 |
|
| $dbih->func( 'LONG', '_date_fmt' ); #need seconds + microseconds to xfer properly |
| 1675 |
|
| #set imagesize if asked to |
| 1676 |
|
| my $imagesize = $self->{textsize} || 10_000_000; |
| 1677 |
|
| $dbih->do("set textsize $imagesize"); |
| 1678 |
|
| |
| 1679 |
|
| #use the above. this seems a little esoteric |
| 1680 |
|
| # $dbih->ct_options(CS_SET, CS_OPT_TEXTSIZE, $imagesize, CS_INT_TYPE); |
| 1681 |
|
| |
| 1682 |
|
| } |
| 1683 |
|
| |
| 1684 |
|
| #turn this back on for paranoia sake |
| 1685 |
|
| $dbih->{PrintError} = 0; |
| 1686 |
|
| $dbih->{RaiseError} = 1; |
| 1687 |
|
| |
| 1688 |
|
| return $dbih; |
| 1689 |
|
| } |
| 1690 |
|
| |
| 1691 |
|
| #--- |
| 1692 |
|
| # |
| 1693 |
|
| #--- |
| 1694 |
|
| sub _ctConnect { |
| 1695 |
|
| |
| 1696 |
|
| my ( $self, $fromOrTo ) = @_; |
| 1697 |
|
| my ( $u, $p, $s, $d, $a ); |
| 1698 |
|
| |
| 1699 |
|
| if ( $fromOrTo =~ /from/i ) { |
| 1700 |
|
| ( $u, $p, $s, $d, $a ) = @{$self}{qw/from_user from_password from_server from_database app_name/}; |
| 1701 |
|
| $a .= '_F'; |
| 1702 |
|
| } |
| 1703 |
|
| else { |
| 1704 |
|
| ( $u, $p, $s, $d, $a ) = @{$self}{qw/to_user to_password to_server to_database app_name/}; |
| 1705 |
|
| $a .= '_T'; |
| 1706 |
|
| } |
| 1707 |
|
| |
| 1708 |
|
| #specific error handlers |
| 1709 |
|
| $self->{current_err_handler} = ct_callback( CS_SERVERMSG_CB, sub { $self->sx_error_handler(@_) } ); |
| 1710 |
|
| $self->{current_msg_handler} = ct_callback( CS_CLIENTMSG_CB, sub { $self->sx_message_handler(@_) } ); |
| 1711 |
|
| |
| 1712 |
|
| #charset. this effects all open connections, alas. |
| 1713 |
|
| # ? |
| 1714 |
|
| |
| 1715 |
|
| #only set the hostname property for now. we may want to set packetsize later. |
| 1716 |
|
| my $props = { CON_PROPS => { CS_HOSTNAME => Sys::Hostname::hostname() } }; |
| 1717 |
|
| |
| 1718 |
|
| my $dbh = Sybase::CTlib->new( $u, $p, $s, $a, $props ); |
| 1719 |
|
| $DB_ERROR && sx_complain("[$fromOrTo] login error:\n$DB_ERROR(User=$u, Server=$s)\n") && return 1; |
| 1720 |
|
| |
| 1721 |
|
| #use the right database if specified |
| 1722 |
|
| if($d) { |
| 1723 |
|
| $dbh->nsql("use $d"); |
| 1724 |
|
| $DB_ERROR && sx_complain("unable to use database [$d].\n$DB_ERROR\n") && return 1; |
| 1725 |
|
| } |
| 1726 |
|
| |
| 1727 |
|
| #binary/image/text size |
| 1728 |
|
| my $imagesize = $self->{textsize} || 32_000; |
| 1729 |
|
| my $r1 =$dbh->ct_options( CS_SET, CS_OPT_TEXTSIZE, $imagesize, CS_INT_TYPE ); |
| 1730 |
|
| if($r1 != CS_SUCCEED) { |
| 1731 |
|
| sx_complain("unable to set textsize. text/image data may truncated\n"); |
| 1732 |
|
| } |
| 1733 |
|
| |
| 1734 |
|
| #date format - make it long so as milliseconds are present in the xfer |
| 1735 |
|
| my $r2 = cs_dt_info(CS_SET, CS_DT_CONVFMT, CS_UNUSED, CS_DATES_LONG); |
| 1736 |
|
| if ($r2 != CS_SUCCEED) { |
| 1737 |
|
| sx_complain("error setting datetime format to 'LONG'. Datetime precision will be lost\n"); |
| 1738 |
|
| } |
| 1739 |
|
| |
| 1740 |
|
| #numerics greater than 2 gig do not translate to int nor float correctly so we keep them in |
| 1741 |
|
| #their native format. |
| 1742 |
|
| $dbh->{UseNumeric} = 1; |
| 1743 |
|
| |
| 1744 |
|
| |
| 1745 |
|
| return $dbh; |
| 1746 |
|
| |
| 1747 |
|
| } |
| 1748 |
|
| |
| 1749 |
|
| #--- |
| 1750 |
|
| # |
| 1751 |
|
| #--- |
| 1752 |
|
| sub _connectNonBcp { |
| 1753 |
|
| my $self = shift; |
| 1754 |
|
| |
| 1755 |
|
| #allows connect via CTlib for now since there seems to be some conflict |
| 1756 |
|
| #using DBD::Sybase and Sybase::CTlib at the same time |
| 1757 |
|
| |
| 1758 |
|
| if ( $self->{CTLib} ) { |
| 1759 |
|
| return $self->_ctConnect(@_); |
| 1760 |
|
| |
| 1761 |
|
| } |
| 1762 |
|
| else { |
| 1763 |
|
| return $self->_ctConnect(@_); |
| 1764 |
|
| |
| 1765 |
|
| # return $self->_dbiConnect(@_); |
| 1766 |
|
| } |
| 1767 |
|
| |
| 1768 |
|
| } |
| 1769 |
|
| |
| 1770 |
|
| #--- |
| 1771 |
|
| # |
| 1772 |
|
| #--- |
| 1773 |
|
| sub _tableDef { |
| 1774 |
|
| |
| 1775 |
|
| my ( $self, $dbih, $table ) = @_; |
| 1776 |
|
| |
| 1777 |
|
| #for each line in the table file |
| 1778 |
|
| my ( $db_name, $own_name, $tab_name ) = split ( /\./, $table ); |
| 1779 |
|
| $db_name =~ s/\s+//g; |
| 1780 |
|
| $own_name =~ s/\s+//g; |
| 1781 |
|
| $tab_name =~ s/\s+//g; |
| 1782 |
|
| |
| 1783 |
|
| #create the sql |
| 1784 |
|
| my $sql_string = <<EOF; |
| 1785 |
|
| select col_name = c.name, |
| 1786 |
|
| type_name = t.name, |
| 1787 |
|
| col_len = c.length, |
| 1788 |
|
| col_order = c.colid, |
| 1789 |
|
| ctype = c.type, |
| 1790 |
|
| utype = t.usertype, |
| 1791 |
|
| allownulls = convert(bit, (c.status & 8)), |
| 1792 |
|
| prec = c.prec, |
| 1793 |
|
| isIdentity = convert(bit, (c.status & 0x80)) |
| 1794 |
|
| from $db_name.dbo.syscolumns c, |
| 1795 |
|
| $db_name.dbo.systypes t |
| 1796 |
|
| where c.id = object_id('$db_name.$own_name.$tab_name') |
| 1797 |
|
| and c.usertype *= t.usertype |
| 1798 |
|
| order by c.colid |
| 1799 |
|
| EOF |
| 1800 |
|
| |
| 1801 |
|
| #run the sql |
| 1802 |
|
| my @h_pointers = $self->_runsql( $dbih, $sql_string, {}, undef ); |
| 1803 |
|
| $DB_ERROR && return undef; |
| 1804 |
|
| |
| 1805 |
|
| #re-organize |
| 1806 |
|
| my %info = (); |
| 1807 |
|
| for my $col ( @{ $h_pointers[0] } ) { |
| 1808 |
|
| my $col_name = $col->{col_name}; |
| 1809 |
|
| $info{$db_name}->{$tab_name}->{$col_name}->{col_type} = $col->{type_name}; |
| 1810 |
|
| $info{$db_name}->{$tab_name}->{$col_name}->{col_len} = $col->{col_len}; |
| 1811 |
|
| $info{$db_name}->{$tab_name}->{$col_name}->{col_id} = $col->{col_order}; |
| 1812 |
|
| $info{$db_name}->{$tab_name}->{$col_name}->{col_allownulls} = $col->{allownulls}; |
| 1813 |
|
| $info{$db_name}->{$tab_name}->{$col_name}->{col_prec} = $col->{prec}; |
| 1814 |
|
| $info{$db_name}->{$tab_name}->{$col_name}->{col_identity} = $col->{isIdentity}; |
| 1815 |
|
| |
| 1816 |
|
| #if user-defined type, get underlying |
| 1817 |
|
| if ( $col->{utype} > 100 ) { |
| 1818 |
|
| |
| 1819 |
|
| #i pulled this sql from sp_help |
| 1820 |
|
| my $sql = <<EOF; |
| 1821 |
|
| select name |
| 1822 |
|
| from $db_name.$own_name.systypes |
| 1823 |
|
| where usertype < 100 |
| 1824 |
|
| and type=$col->{ctype} |
| 1825 |
|
| and name not in ("sysname", "nchar", "nvarchar") |
| 1826 |
|
| EOF |
| 1827 |
|
| my @name = $self->_runsql( $dbih, $sql, {}, undef ); |
| 1828 |
|
| $DB_ERROR && return undef; |
| 1829 |
|
| |
| 1830 |
|
| $info{$db_name}->{$tab_name}->{$col_name}->{col_type} = $name[0]->[0]->{name}; |
| 1831 |
|
| $info{$db_name}->{$tab_name}->{$col_name}->{col_usertype} = $col->{type_name}; |
| 1832 |
|
| |
| 1833 |
|
| #not user-defined type |
| 1834 |
|
| } |
| 1835 |
|
| else { |
| 1836 |
|
| $info{$db_name}->{$tab_name}->{$col_name}->{col_usertype} = undef; |
| 1837 |
|
| |
| 1838 |
|
| } |
| 1839 |
|
| |
| 1840 |
|
| } |
| 1841 |
|
| |
| 1842 |
|
| #add the list column names in the right order |
| 1843 |
|
| @{ $info{$db_name}->{$tab_name}->{__COLNAMES__} } = |
| 1844 |
|
| sort { $info{$db_name}->{$tab_name}->{$a}->{col_id} <=> $info{$db_name}->{$tab_name}->{$b}->{col_id} } |
| 1845 |
|
| keys %{ $info{$db_name}->{$tab_name} }; |
| 1846 |
|
| |
| 1847 |
|
| return \%info; |
| 1848 |
|
| } |
| 1849 |
|
| |
| 1850 |
|
| #----------------------------------------------------------------------- |
| 1851 |
|
| #prep for xfer |
| 1852 |
|
| #----------------------------------------------------------------------- |
| 1853 |
|
| sub _prep_xfer { |
| 1854 |
|
| |
| 1855 |
|
| #args |
| 1856 |
|
| my $self = shift; |
| 1857 |
|
| |
| 1858 |
|
| #little sub to split on keyword GO |
| 1859 |
|
| my $split_on_go = sub { |
| 1860 |
|
| my $sql = shift; |
| 1861 |
|
| my $re = qr/^\s*GO\s*\n/mi; |
| 1862 |
|
| my @s = split $re, $sql; |
| 1863 |
|
| return @s > 1 ? \@s : $sql; |
| 1864 |
|
| }; |
| 1865 |
|
| |
| 1866 |
|
| #log into <from> db if appropriate |
| 1867 |
|
| my $dbh_from = (); |
| 1868 |
|
| if ( $self->{from_dsn} =~ /^DBD/i ) { |
| 1869 |
|
| |
| 1870 |
|
| # $dbh_from = $self->_dbiConnect('FROM'); |
| 1871 |
|
| $dbh_from = $self->_connectNonBcp('FROM'); |
| 1872 |
|
| $dbh_from || return 1; |
| 1873 |
|
| } |
| 1874 |
|
| |
| 1875 |
|
| #squirrel it away |
| 1876 |
|
| $self->{sx_dbh_from} = $dbh_from; |
| 1877 |
|
| |
| 1878 |
|
| #select from 'from' source |
| 1879 |
|
| my ($xfer_source) = (); |
| 1880 |
|
| |
| 1881 |
|
| #from sql |
| 1882 |
|
| if ( $self->{from_sql} ) { |
| 1883 |
|
| $xfer_source = $split_on_go->( $self->{from_sql} ); #allow multiple batches |
| 1884 |
|
| |
| 1885 |
|
| #file containing any sql |
| 1886 |
|
| } |
| 1887 |
|
| elsif ( $self->{from_script} ) { |
| 1888 |
|
| my $fn = $self->{from_script}; |
| 1889 |
|
| open( FH1, "<$fn" ) or ( sx_complain("unable to open script: <$fn>, $!\n") && return 1 ); |
| 1890 |
|
| $xfer_source = $split_on_go->( join '', <FH1> ); #allow multiple batches |
| 1891 |
|
| close(FH1); |
| 1892 |
|
| |
| 1893 |
|
| #table |
| 1894 |
|
| } |
| 1895 |
|
| elsif ( $self->{from_table} ) { |
| 1896 |
|
| my $wc = $self->{where_clause} ? "where $self->{where_clause}" : ''; |
| 1897 |
|
| $xfer_source = "select * from $self->{from_table} $self->{holdlock} $wc"; |
| 1898 |
|
| |
| 1899 |
|
| #perl codeRef or ArrayRef |
| 1900 |
|
| } |
| 1901 |
|
| elsif ( $self->{from_perl} ) { |
| 1902 |
|
| $xfer_source = $self->{from_perl}; |
| 1903 |
|
| |
| 1904 |
|
| #data file |
| 1905 |
|
| } |
| 1906 |
|
| elsif ( $self->{from_file} ) { |
| 1907 |
|
| my $fn = $self->{from_file}; |
| 1908 |
|
| if ( !ref($fn) ) { |
| 1909 |
|
| if ( $fn =~ /\.gz$|\.zip$/i ) { |
| 1910 |
|
| $xfer_source = new FileHandle "zcat $fn |" or sx_complain("unable to open file: <$fn>, $!\n") |
| 1911 |
|
| and return 1; |
| 1912 |
|
| } |
| 1913 |
|
| else { |
| 1914 |
|
| $xfer_source = new FileHandle "<$fn" or sx_complain("unable to open file: <$fn>, $!\n") and return 1; |
| 1915 |
|
| } |
| 1916 |
|
| } |
| 1917 |
|
| else { |
| 1918 |
|
| $xfer_source = $fn; |
| 1919 |
|
| } |
| 1920 |
|
| |
| 1921 |
|
| } |
| 1922 |
|
| |
| 1923 |
|
| #squirrel it away |
| 1924 |
|
| $self->{sx_xfer_source} = $xfer_source; |
| 1925 |
|
| |
| 1926 |
|
| #log into 'to' server (NON-BCP) |
| 1927 |
|
| my %to_info = (); |
| 1928 |
|
| |
| 1929 |
|
| # my $dbh_to = $self->_dbiConnect('TO'); |
| 1930 |
|
| my $dbh_to = $self->_connectNonBcp('TO'); |
| 1931 |
|
| $dbh_to || return 1; |
| 1932 |
|
| |
| 1933 |
|
| #header report |
| 1934 |
|
| if ( $self->{progress_log} ) { |
| 1935 |
|
| $self->_print("Xfer info\n"); |
| 1936 |
|
| $self->_print(" -from => $self->{from_dsn}\n"); |
| 1937 |
|
| $self->_print( " -to => dbd." . "$self->{to_source}.$self->{to_server}.$self->{to_table}\n" ); |
| 1938 |
|
| } |
| 1939 |
|
| |
| 1940 |
|
| #check that -to_table exists |
| 1941 |
|
| my @path = split ( /\./, $self->{to_table} ); |
| 1942 |
|
| my $chk = "select count(*) from $path[0]..sysobjects where name = '$path[2]'"; |
| 1943 |
|
| my @res = @{ $self->_runsql( $dbh_to, $chk, [], undef ) }; |
| 1944 |
|
| ( $DB_ERROR || !$res[0] ) && sx_complain("Can't find to_table <$self->{to_table}>\n$DB_ERROR\n") && return 1; |
| 1945 |
|
| |
| 1946 |
|
| #get to_table object info |
| 1947 |
|
| my $toInfo = $self->_tableDef( $dbh_to, $self->{to_table} ); |
| 1948 |
|
| $self->{sx_to_info} = $toInfo; |
| 1949 |
|
| ( $self->{sx_to_database} ) = keys %{$toInfo}; |
| 1950 |
|
| ( $self->{sx_to_table} ) = keys %{ $toInfo->{ $self->{sx_to_database} } }; |
| 1951 |
|
| my @cols = @{ $self->{sx_to_info}->{ $self->{sx_to_database} }->{ $self->{sx_to_table} }->{__COLNAMES__} }; |
| 1952 |
|
| sx_complain("brain-damage. No columns found in target table\n"), return 1 unless @cols; |
| 1953 |
|
| $self->{sx_to_colnames} = [@cols]; |
| 1954 |
|
| $self->{sx_to_numcols} = scalar @cols; |
| 1955 |
|
| |
| 1956 |
|
| #use right db |
| 1957 |
|
| if ( $self->{sx_to_database} ) { |
| 1958 |
|
| my $sql = "use $self->{sx_to_database}"; |
| 1959 |
|
| my $rs = $self->_runsql( $dbh_to, $sql, undef, undef ); |
| 1960 |
|
| |
| 1961 |
|
| #! my $rs = $dbh_to_non_bcp->func("use $self->{sx_to_database}", [], undef, 'nsql' ); |
| 1962 |
|
| $DB_ERROR && sx_complain("$DB_ERROR\n") && return 1; |
| 1963 |
|
| } |
| 1964 |
|
| |
| 1965 |
|
| #check to charset for unicode and iso |
| 1966 |
|
| $self->{sx_toCharSet} = $self->_checkCharSet( $dbh_to, 'TO'); |
| 1967 |
|
| $self->{sx_fromCharSet} = $self->_checkCharSet( $dbh_from, 'FROM'); |
| 1968 |
|
| |
| 1969 |
|
| |
| 1970 |
|
| #squirrel away |
| 1971 |
|
| $self->{sx_dbh_to_non_bcp} = $dbh_to; |
| 1972 |
|
| |
| 1973 |
|
| #check if delete flag specified |
| 1974 |
|
| $self->{delete_flag} && sx_delete_rows {$self} && return 1; |
| 1975 |
|
| |
| 1976 |
|
| #check if truncate flag specified |
| 1977 |
|
| $self->{truncate_flag} && sx_truncate_table($self) && return 1; |
| 1978 |
|
| |
| 1979 |
|
| #create auto_delete commands |
| 1980 |
|
| $self->{auto_delete} && sx_auto_delete_setup($self) && return 1; |
| 1981 |
|
| |
| 1982 |
|
| #drop/recreate indices |
| 1983 |
|
| $self->{drop_and_recreate_indices} && $self->_drop_indices() && return 1; |
| 1984 |
|
| |
| 1985 |
|
| #debug. calc num rows to be xferred |
| 1986 |
|
| if ( $self->{debug} && $self->{from_table} ) { |
| 1987 |
|
| $self->_print("calculating number for rows to transfer.\n"); |
| 1988 |
|
| my $wc = "where $self->{where_clause}" if $self->{where_clause}; |
| 1989 |
|
| my $sql_string = "select count(*) from $self->{from_table} $wc"; |
| 1990 |
|
| $self->_print("$sql_string\n") if $self->{echo}; |
| 1991 |
|
| my @status = @{ $self->_runsql( $dbh_from, $sql_string, [], undef ) }; |
| 1992 |
|
| |
| 1993 |
|
| #! my @status = $dbh_from->func( $sql_string, [], undef, 'nsql' ); |
| 1994 |
|
| $DB_ERROR && sx_complain("$DB_ERROR\n") && return 1; |
| 1995 |
|
| $self->_print("num rows: $status[0]\n"); |
| 1996 |
|
| } |
| 1997 |
|
| |
| 1998 |
|
| return; |
| 1999 |
|
| } |
| 2000 |
|
| |
| 2001 |
|
| #----- |
| 2002 |
|
| # |
| 2003 |
|
| #----- |
| 2004 |
|
| sub _checkCharSet { |
| 2005 |
|
| my ( $self, $dbh, $where) = @_; |
| 2006 |
|
| |
| 2007 |
|
| #assume iso_1 if from source is not a database |
| 2008 |
|
| return 'iso_1' unless $dbh; |
| 2009 |
|
| |
| 2010 |
|
| my $sql = qq/sp_configure "default character set id"/; |
| 2011 |
|
| my @data = $self->_runsql( $dbh, $sql, {}, undef ); |
| 2012 |
|
| my $val = $data[0]->[0]->{'Run Value'}; |
| 2013 |
|
| my $str = 0; |
| 2014 |
|
| if ( $val == 190 ) { |
| 2015 |
|
| $str = 'utf8'; |
| 2016 |
|
| } |
| 2017 |
|
| elsif ( $val == 1 ) { |
| 2018 |
|
| $str = 'iso_1' |
| 2019 |
|
| } |
| 2020 |
|
| else { |
| 2021 |
|
| my $swi = $where eq 'TO' ? '--to_server' : '--from_server'; |
| 2022 |
|
| $self->_print("unknown charset on $swi: [$val]. Using iso_1\n"); |
| 2023 |
|
| $str = 'iso_1'; |
| 2024 |
|
| } |
| 2025 |
|
| return $str; |
| 2026 |
|
| } |
| 2027 |
|
| |
| 2028 |
|
| #----------------------------------------------------------------------- |
| 2029 |
|
| #make bcp connection |
| 2030 |
|
| #----------------------------------------------------------------------- |
| 2031 |
|
| sub _open_bcp { |
| 2032 |
|
| |
| 2033 |
|
| my ( $self, $tab, $num_cols ) = @_; |
| 2034 |
|
| |
| 2035 |
|
| my $dbh = (); |
| 2036 |
|
| |
| 2037 |
|
| #specific error handlers |
| 2038 |
|
| ct_callback( CS_SERVERMSG_CB, sub { $self->sx_error_handler(@_) } ); |
| 2039 |
|
| ct_callback( CS_CLIENTMSG_CB, sub { $self->sx_message_handler(@_) } ); |
| 2040 |
|
| |
| 2041 |
|
| my ( $u, $p, $s, $a, $cs ) = @{$self}{qw/to_user to_password to_server app_name sx_toCharSet/}; |
| 2042 |
|
| |
| 2043 |
|
| #bulk login connection property |
| 2044 |
|
| my $props = {}; |
| 2045 |
|
| $props->{CS_BULK_LOGIN} = CS_TRUE; |
| 2046 |
|
| |
| 2047 |
|
| #character set property. eg. iso vs. unicode |
| 2048 |
|
| $props->{CS_SYB_CHARSET} = $cs; |
| 2049 |
|
| |
| 2050 |
|
| #used for increasing the peformance of xferring text/image columns |
| 2051 |
|
| $props->{CS_PACKETSIZE} = $self->{packetsize} if $self->{packetsize}; |
| 2052 |
|
| |
| 2053 |
|
| #hostname |
| 2054 |
|
| $props->{CS_HOSTNAME} = Sys::Hostname::hostname(); |
| 2055 |
|
| |
| 2056 |
|
| #program name. 'T' for 'To' or 'Target'. |
| 2057 |
|
| my $programName = $a . '_T'; |
| 2058 |
|
| |
| 2059 |
|
| #make connection |
| 2060 |
|
| $dbh = Sybase::CTlib->new( $u, $p, $s, $programName, { CON_PROPS => $props } ); |
| 2061 |
|
| $DB_ERROR && sx_complain("TO login error:\n$DB_ERROR(User=$u, Server=$s)\n") && return 1; |
| 2062 |
|
| |
| 2063 |
|
| my ($val, $rs); |
| 2064 |
|
| if($self->{sx_fromCharSet} =~ /iso_1/i && $self->{sx_toCharSet} =~ /utf8/i) { |
| 2065 |
|
| my $rs = $dbh->ct_con_props(CS_GET, CS_CHARSETCNV, $val, CS_INT_TYPE); |
| 2066 |
|
| } |
| 2067 |
|
| |
| 2068 |
|
| #special checking for identity and image |
| 2069 |
|
| my %to_info = %{ $self->{sx_to_info} }; |
| 2070 |
|
| my ( $idb, $itab ) = @{$self}{qw/sx_to_database sx_to_table/}; |
| 2071 |
|
| my @sorted_fields = @{ $to_info{$idb}->{$itab}->{__COLNAMES__} }; |
| 2072 |
|
| |
| 2073 |
|
| |
| 2074 |
|
| #set imagesize |
| 2075 |
|
| my $imagesize = $self->{textsize} || 32_000; |
| 2076 |
|
| $dbh->ct_options( CS_SET, CS_OPT_TEXTSIZE, $imagesize, CS_INT_TYPE ); |
| 2077 |
|
| #--- |
| 2078 |
|
| #image columns are returned as a *string* of hex digits in perl when using |
| 2079 |
|
| #a select statement. we'll have to convert it back to chars on the sendrow. |
| 2080 |
|
| #this code documents what columns to convert back. |
| 2081 |
|
| #--- |
| 2082 |
|
| my @sorted_binary = grep {$to_info{$idb}->{$itab}->{$_}->{col_type} =~ /(image|binary|timestamp)/ } @sorted_fields; |
| 2083 |
|
| my @binary_col_num = (); |
| 2084 |
|
| for my $c (@sorted_binary) { |
| 2085 |
|
| push @binary_col_num, grep { $sorted_fields[$_] eq $c } 0 .. $#sorted_fields; |
| 2086 |
|
| } |
| 2087 |
|
| $self->{binary_columns} = [ @binary_col_num ]; |
| 2088 |
|
| |
| 2089 |
|
| |
| 2090 |
|
| #check identity column stuff |
| 2091 |
|
| my @sorted_identity = grep { $to_info{$idb}->{$itab}->{$_}->{col_identity} } @sorted_fields; |
| 2092 |
|
| my @identity_col_num = (); |
| 2093 |
|
| for my $c (@sorted_identity) { |
| 2094 |
|
| push @identity_col_num, grep { $sorted_fields[$_] eq $c } 0 .. $#sorted_fields; |
| 2095 |
|
| } |
| 2096 |
|
| |
| 2097 |
|
| |
| 2098 |
|
| #first one. remember id_column starts at one |
| 2099 |
|
| my ( $has_identity, $id_column ) = ( undef, undef ); |
| 2100 |
|
| |
| 2101 |
|
| #we want to carry over identity columns |
| 2102 |
|
| if ( scalar(@identity_col_num) > 0 && $self->{set_identity_insert} ) { |
| 2103 |
|
| ( $has_identity, $id_column ) = ( 1, $identity_col_num[0] + 1 ); |
| 2104 |
|
| |
| 2105 |
|
| } |
| 2106 |
|
| |
| 2107 |
|
| #we want server to set new identity columns |
| 2108 |
|
| elsif ( scalar(@identity_col_num) > 0 && !$self->{set_identity_insert} ) { |
| 2109 |
|
| ( $has_identity, $id_column ) = ( 0, $identity_col_num[0] + 1 ); |
| 2110 |
|
| |
| 2111 |
|
| #if this is set then we will ensure this column is set to undef on the blk_xfer. column number is base 0. |
| 2112 |
|
| $self->{identity_column} = $identity_col_num[0]; |
| 2113 |
|
| |
| 2114 |
|
| } |
| 2115 |
|
| |
| 2116 |
|
| #no identity column but the switch is set. that's okay... |
| 2117 |
|
| elsif ( !(scalar(@identity_col_num) > 0) && $self->{set_identity_insert} ) { |
| 2118 |
|
| # sx_complain("Identity column not found, but switch set_identity_insert specified\n") && return 1; |
| 2119 |
|
| } |
| 2120 |
|
| |
| 2121 |
|
| |
| 2122 |
|
| #no identity and the switch isn't set and that's just fine |
| 2123 |
|
| elsif ( !(scalar(@identity_col_num) > 0) && !$self->{set_identity_insert} ) { |
| 2124 |
|
| |
| 2125 |
|
| #no-op |
| 2126 |
|
| } |
| 2127 |
|
| |
| 2128 |
|
| |
| 2129 |
|
| #initialize |
| 2130 |
|
| print "init: <$tab> <$num_cols>, <$has_identity>, <$id_column>\n" if $self->{debug}; |
| 2131 |
|
| my $rc = $dbh->blk_init( $tab, $num_cols, $has_identity, $id_column ); |
| 2132 |
|
| if ( $rc == CS_FAIL || $DB_ERROR ) { |
| 2133 |
|
| sx_complain("$DB_ERROR\n") && return 1; |
| 2134 |
|
| } |
| 2135 |
|
| |
| 2136 |
|
| return $dbh; |
| 2137 |
|
| |
| 2138 |
|
| } |
| 2139 |
|
| |
| 2140 |
|
| #----------------------------------------------------------------------- |
| 2141 |
|
| #create the auto_delete string |
| 2142 |
|
| #----------------------------------------------------------------------- |
| 2143 |
|
| sub sx_auto_delete_setup { |
| 2144 |
|
| |
| 2145 |
|
| #args |
| 2146 |
|
| my $opt = shift; |
| 2147 |
|
| return 0 unless $opt->{auto_delete}; |
| 2148 |
|
| |
| 2149 |
|
| #pull the necessary info off the options hash |
| 2150 |
|
| my %to_info = %{ $opt->{sx_to_info} }; |
| 2151 |
|
| my ($db) = keys %to_info; |
| 2152 |
|
| my ($table) = keys %{ $to_info{$db} }; |
| 2153 |
|
| |
| 2154 |
|
| my $temp_db = $opt->{scratch_db}; |
| 2155 |
|
| my $del_batchsize = $opt->{auto_delete_batchsize}; |
| 2156 |
|
| my $tmp_tab = $opt->{sx_ad_temp_table} = "$temp_db..sybxfer$$"; |
| 2157 |
|
| |
| 2158 |
|
| my $del_one_line = "delete $db..$table where"; |
| 2159 |
|
| my $del_join = "delete $db..$table from $db..$table a, $tmp_tab b where"; |
| 2160 |
|
| |
| 2161 |
|
| #create sql prefix |
| 2162 |
|
| my $crt_sql = "create table $tmp_tab("; |
| 2163 |
|
| |
| 2164 |
|
| #get the columns specified by the user |
| 2165 |
|
| my @cols = split ( /[\ ,]+/, $opt->{auto_delete} ); |
| 2166 |
|
| |
| 2167 |
|
| my $columns = (); |
| 2168 |
|
| |
| 2169 |
|
| #loop thru the columns |
| 2170 |
|
| for my $c (@cols) { |
| 2171 |
|
| my ( $val, $ctype ) = (); |
| 2172 |
|
| |
| 2173 |
|
| #if $c is all digits then its a column position |
| 2174 |
|
| my $cname = (); |
| 2175 |
|
| if ( $c =~ /^\d+$/ ) { |
| 2176 |
|
| ($cname) = |
| 2177 |
|
| grep { $to_info{$db}->{$table}->{$_}->{col_id} == $c } @{ $to_info{$db}->{$table}->{__COLNAMES__} }; |
| 2178 |
|
| sx_complain("couldn't find column #$c in $table\n"), return 1 unless defined $cname; |
| 2179 |
|
| } |
| 2180 |
|
| else { |
| 2181 |
|
| $cname = $c; |
| 2182 |
|
| } |
| 2183 |
|
| |
| 2184 |
|
| #get datatype |
| 2185 |
|
| my $type = $to_info{$db}->{$table}->{$cname}->{col_type} || sx_complain("unknown column: <$cname>\n"); |
| 2186 |
|
| my $col_num = $to_info{$db}->{$table}->{$cname}->{col_id} || sx_complain("unknown column: <$cname>\n"); |
| 2187 |
|
| my $col_len = $to_info{$db}->{$table}->{$cname}->{col_len} || sx_complain("unknown column: <$cname>\n"); |
| 2188 |
|
| |
| 2189 |
|
| #list of columns |
| 2190 |
|
| $columns .= "$cname,"; |
| 2191 |
|
| |
| 2192 |
|
| #perl is zero indexed |
| 2193 |
|
| $col_num--; |
| 2194 |
|
| push @{ $opt->{sx_ad_col_num} }, $col_num; |
| 2195 |
|
| |
| 2196 |
|
| #add delimiters |
| 2197 |
|
| if ( $type =~ /date|time/i ) { |
| 2198 |
|
| $val = qq/$cname = '\${row[$col_num]}'/; |
| 2199 |
|
| $ctype = $type; |
| 2200 |
|
| } |
| 2201 |
|
| elsif ( $type =~ /char/i ) { |
| 2202 |
|
| $val = qq/$cname = '\${row[$col_num]}'/; |
| 2203 |
|
| $ctype = "$type($col_len)"; |
| 2204 |
|
| } |
| 2205 |
|
| elsif ( $type =~ /binary/i ) { |
| 2206 |
|
| $val = qq/$cname = 0x\${row[$col_num]}/; |
| 2207 |
|
| $ctype = "$type($col_len)"; |
| 2208 |
|
| } |
| 2209 |
|
| else { |
| 2210 |
|
| $val = qq/$cname = \${row[$col_num]}/; |
| 2211 |
|
| $ctype = $type; |
| 2212 |
|
| } |
| 2213 |
|
| |
| 2214 |
|
| #make cmds |
| 2215 |
|
| $crt_sql .= " $cname $ctype null,"; |
| 2216 |
|
| $del_join .= " a.$cname = b.$cname and"; |
| 2217 |
|
| $del_one_line .= " $val and"; |
| 2218 |
|
| } |
| 2219 |
|
| |
| 2220 |
|
| #remove trailing syntax |
| 2221 |
|
| $columns = substr( $columns, 0, -1 ); |
| 2222 |
|
| $crt_sql = substr( $crt_sql, 0, -1 ) . ") "; |
| 2223 |
|
| $del_join = substr( $del_join, 0, -4 ); |
| 2224 |
|
| $del_one_line = substr( $del_one_line, 0, -4 ); |
| 2225 |
|
| my $crt_idx = "create index ix_$$ on $tmp_tab ( $columns ) "; |
| 2226 |
|
| my $upd_stat = "update statistics $tmp_tab "; |
| 2227 |
|
| |
| 2228 |
|
| #create the sql to delete the rows |
| 2229 |
|
| my $del_sql = sx_delete_sql( $del_join, $opt->{batchsize}, $opt->{silent} ); |
| 2230 |
|
| |
| 2231 |
|
| $opt->{sx_ad_create_table} = $crt_sql; |
| 2232 |
|
| $opt->{sx_ad_create_index} = $crt_idx; |
| 2233 |
|
| $opt->{sx_ad_delete_cmd} = $del_sql; |
| 2234 |
|
| $opt->{sx_ad_delete_join} = $del_join; |
| 2235 |
|
| $opt->{sx_ad_upd_stat} = $upd_stat; |
| 2236 |
|
| |
| 2237 |
|
| return 0; |
| 2238 |
|
| } |
| 2239 |
|
| |
| 2240 |
|
| #----------------------------------------------------------------------- |
| 2241 |
|
| #sql to delete rows |
| 2242 |
|
| #----------------------------------------------------------------------- |
| 2243 |
|
| sub sx_delete_sql { |
| 2244 |
|
| |
| 2245 |
|
| my ( $del_line, $batchsize, $silent ) = @_; |
| 2246 |
|
| |
| 2247 |
|
| return <<EOF; |
| 2248 |
|
| set rowcount $batchsize |
| 2249 |
|
| declare \@loop int, \@tot_rows int, \@n int |
| 2250 |
|
| select \@loop=0, \@tot_rows=0, \@n=0 |
| 2251 |
|
| while (\@loop = 0 or \@n > 0) |
| 2252 |
|
| begin |
| 2253 |
|
| select \@loop=\@loop+1 |
| 2254 |
|
| $del_line |
| 2255 |
|
| select \@n=\@\@rowcount |
| 2256 |
|
| select \@tot_rows=\@tot_rows+\@n |
| 2257 |
|
| if ( \@n > 0 and $silent <> 1) print " \%1! rows deleted.", \@n |
| 2258 |
|
| end |
| 2259 |
|
| select loop=\@loop-1, tot_rows=\@tot_rows |
| 2260 |
|
| set rowcount 0 |
| 2261 |
|
| EOF |
| 2262 |
|
| } |
| 2263 |
|
| |
| 2264 |
|
| #----------------------------------------------------------------------- |
| 2265 |
|
| #oversize row info trace |
| 2266 |
|
| #----------------------------------------------------------------------- |
| 2267 |
|
| sub sx_oversize_error { |
| 2268 |
|
| |
| 2269 |
|
| my $opt = shift; |
| 2270 |
|
| my @row = @{ +shift }; |
| 2271 |
|
| my $row_num = shift; |
| 2272 |
|
| my $error = shift; |
| 2273 |
|
| |
| 2274 |
|
| #ObjectInfo structure |
| 2275 |
|
| my %to_info = %{ $opt->{sx_to_info} }; |
| 2276 |
|
| |
| 2277 |
|
| my ( $db, $tab ) = @{$opt}{qw/sx_to_database sx_to_table/}; |
| 2278 |
|
| |
| 2279 |
|
| #sort the fields by column order |
| 2280 |
|
| my @sorted_fields = @{ $to_info{$db}->{$tab}->{__COLNAMES__} }; |
| 2281 |
|
| my @sorted_len = map { $to_info{$db}->{$tab}->{$_}->{col_len} } @sorted_fields; |
| 2282 |
|
| my @sorted_type = map { $to_info{$db}->{$tab}->{$_}->{col_type} } @sorted_fields; |
| 2283 |
|
| my @sorted_prec = map { $to_info{$db}->{$tab}->{$_}->{col_prec} } @sorted_fields; |
| 2284 |
|
| |
| 2285 |
|
| #check num columns in each |
| 2286 |
|
| my $to_nc = scalar @sorted_fields; |
| 2287 |
|
| my $from_nc = scalar @row; |
| 2288 |
|
| |
| 2289 |
|
| $opt->_print("------------------\n"); |
| 2290 |
|
| $opt->_print("Expanded error log\n"); |
| 2291 |
|
| $opt->_print(" Error: $error"); |
| 2292 |
|
| $opt->_print(" note : num columns in source ($from_nc) != num columns in target ($to_nc)\n") if $from_nc != $to_nc; |
| 2293 |
|
| $opt->_print(" row : $row_num\n"); |
| 2294 |
|
| $opt->_print(" table: $db.$tab definitions with values on right.\n"); |
| 2295 |
|
| |
| 2296 |
|
| #loop thru all the fields |
| 2297 |
|
| my $max = $to_nc > $from_nc ? $to_nc : $from_nc; |
| 2298 |
|
| for ( my $i = 0 ; $i < $max ; $i++ ) { |
| 2299 |
|
| |
| 2300 |
|
| #from data |
| 2301 |
|
| my ( $val, $act_len, $form ) = (); |
| 2302 |
|
| if ( $i < $from_nc ) { |
| 2303 |
|
| $val = $row[$i]; |
| 2304 |
|
| $act_len = length $row[$i]; |
| 2305 |
|
| $form = " %2i: %-20s\t%-10s\t<%s>\n"; |
| 2306 |
|
| } |
| 2307 |
|
| else { |
| 2308 |
|
| $val = "__not supplied__"; |
| 2309 |
|
| $act_len = -1; |
| 2310 |
|
| $form = " %2i: %-20s\t%-10s\t%s\n"; |
| 2311 |
|
| } |
| 2312 |
|
| |
| 2313 |
|
| #to data |
| 2314 |
|
| my ( $fld_name, $dec_type, $dec_len ) = (); |
| 2315 |
|
| if ( $i < $to_nc ) { |
| 2316 |
|
| $fld_name = $sorted_fields[$i]; |
| 2317 |
|
| $dec_type = $sorted_type[$i]; |
| 2318 |
|
| $dec_len = $dec_type =~ /numeric/ ? $sorted_prec[$i] : $sorted_len[$i]; |
| 2319 |
|
| } |
| 2320 |
|
| else { |
| 2321 |
|
| $fld_name = "__missing__"; |
| 2322 |
|
| $dec_type = "__missing__ "; |
| 2323 |
|
| $dec_len = " "; |
| 2324 |
|
| } |
| 2325 |
|
| |
| 2326 |
|
| #expanded messages |
| 2327 |
|
| my $msg = (); |
| 2328 |
|
| if ( $dec_type =~ /char/i && $act_len > $dec_len && $act_len >= 0 ) { |
| 2329 |
|
| $msg = " *Column #" . ( $i + 1 ) . " actual length [$act_len] > declared length [$dec_len]\n"; |
| 2330 |
|
| $opt->_print("$msg"); |
| 2331 |
|
| |
| 2332 |
|
| } |
| 2333 |
|
| elsif ( $dec_type =~ /(int|float)/ && $act_len >= 0 ) { |
| 2334 |
|
| local $! = 0; |
| 2335 |
|
| my ( $test, $err ) = POSIX::strtod($val); |
| 2336 |
|
| if ($err) { |
| 2337 |
|
| $msg = " *Column #" . ( $i + 1 ) . " contains non-numeric data\n"; |
| 2338 |
|
| $opt->_print("$msg"); |
| 2339 |
|
| } |
| 2340 |
|
| } |
| 2341 |
|
| |
| 2342 |
|
| $dec_len = '(' . $dec_len . ')' if $dec_type !~ /missing/i; |
| 2343 |
|
| my $line = sprintf $form, $i + 1, $fld_name, $dec_type . $dec_len, $val; |
| 2344 |
|
| $opt->_print($line); |
| 2345 |
|
| } |
| 2346 |
|
| $opt->_print("------------------\n"); |
| 2347 |
|
| $opt->_print("\n"); |
| 2348 |
|
| return 0; |
| 2349 |
|
| } |
| 2350 |
|
| |
| 2351 |
|
| #----------------------------------------------------------------------- |
| 2352 |
|
| #check arguments |
| 2353 |
|
| #----------------------------------------------------------------------- |
| 2354 |
|
| sub _checkargs { |
| 2355 |
|
| |
| 2356 |
|
| my $self = shift; |
| 2357 |
|
| my @user_options = @_; |
| 2358 |
|
| |
| 2359 |
|
| #verify the options |
| 2360 |
|
| my %opt = sx_verify_options(@user_options); |
| 2361 |
|
| return 1 if exists $opt{1}; |
| 2362 |
|
| |
| 2363 |
|
| #if help or no options then give usage and bail |
| 2364 |
|
| if ( defined $opt{help} || ( $opt{sybxfer} && keys %opt == 1 ) ) { |
| 2365 |
|
| sx_usage( \%opt ), return 1; |
| 2366 |
|
| } |
| 2367 |
|
| |
| 2368 |
|
| #set some defaults |
| 2369 |
|
| #program_name in master..sysprocesses only 16 bytes long. need last two for sybxfer. |
| 2370 |
|
| $opt{app_name} = basename($0) unless defined $opt{app_name}; |
| 2371 |
|
| $opt{app_name} = substr( $opt{app_name}, 0, 14 ); |
| 2372 |
|
| $opt{progress_log} = 1 unless defined $opt{progress_log}; |
| 2373 |
|
| $opt{holdlock} = $opt{holdlock} ? 'HOLDLOCK' : ''; |
| 2374 |
|
| $opt{trim_whitespace} ||= 0; |
| 2375 |
|
| $opt{silent} ||= 0; |
| 2376 |
|
| |
| 2377 |
|
| #if "U" specified, then make from and to equal to "user" |
| 2378 |
|
| $opt{from_user} ||= $opt{user} || $ENV{USER}; |
| 2379 |
|
| $opt{to_user} ||= $opt{user} || $ENV{USER}; |
| 2380 |
|
| |
| 2381 |
|
| #if "P" specified, then make from and to equal to "password" |
| 2382 |
|
| $opt{from_password} ||= $opt{password} || $opt{from_user}; |
| 2383 |
|
| $opt{to_password} ||= $opt{password} || $opt{to_user}; |
| 2384 |
|
| |
| 2385 |
|
| #if "S" specified, then make from and to server equal to "server" |
| 2386 |
|
| $opt{from_server} ||= $opt{server} || $ENV{DSQUERY}; |
| 2387 |
|
| $opt{to_server} ||= $opt{server} || $ENV{DSQUERY}; |
| 2388 |
|
| |
| 2389 |
|
| #if "T" specified, then set from and to tables |
| 2390 |
|
| if ( $opt{table} ) { |
| 2391 |
|
| $opt{from_table} ||= $opt{table}; |
| 2392 |
|
| $opt{to_table} ||= $opt{table}; |
| 2393 |
|
| } |
| 2394 |
|
| |
| 2395 |
|
| #if "D" specified, then set database |
| 2396 |
|
| if ( $opt{database} ) { |
| 2397 |
|
| $opt{from_database} ||= $opt{database}; |
| 2398 |
|
| $opt{to_database} ||= $opt{database}; |
| 2399 |
|
| } |
| 2400 |
|
| |
| 2401 |
|
| #parse the --from_source switch |
| 2402 |
|
| my $rc; |
| 2403 |
|
| ( $rc, $opt{from_dsn} ) = $self->_parse_from_source( \%opt ); |
| 2404 |
|
| $rc && return 1; |
| 2405 |
|
| |
| 2406 |
|
| #if batchsize not specified then force it to 1000 |
| 2407 |
|
| $opt{batchsize} = 1000 unless $opt{batchsize}; |
| 2408 |
|
| |
| 2409 |
|
| #make sure no ambiguity/problems with -to_table |
| 2410 |
|
| if ( $opt{to_table} ) { |
| 2411 |
|
| my ( $ok, $source, $server, $db, $own, $tab ) = $self->_parse_path( \%opt, $opt{to_table} ); |
| 2412 |
|
| if ($ok) { |
| 2413 |
|
| if ( !$opt{to_database} && !$db ) { |
| 2414 |
|
| sx_complain("-to_table should be of the form [db.][owner.]table if no -to_database specified\n") |
| 2415 |
|
| && return 1; |
| 2416 |
|
| } |
| 2417 |
|
| |
| 2418 |
|
| if ( !$opt{to_server} && !$server ) { |
| 2419 |
|
| sx_complain("-to_table should be of the form [server.][db.][owner.]table if no -to_server specified\n") |
| 2420 |
|
| && return 1; |
| 2421 |
|
| } |
| 2422 |
|
| |
| 2423 |
|
| #we'll get to this later |
| 2424 |
|
| # if (!$opt{to_source} && !$source) { |
| 2425 |
|
| # sx_complain("-to_table should be of the form [source.][server.][db.][owner.]table if no -to_source specified\n") |
| 2426 |
|
| # && return 1; |
| 2427 |
|
| # } |
| 2428 |
|
| |
| 2429 |
|
| $opt{to_database} = $db || $opt{to_database}; |
| 2430 |
|
| $opt{to_server} = $server || $opt{to_server}; |
| 2431 |
|
| $opt{to_source} = $source || $opt{source} || 'Sybase'; |
| 2432 |
|
| if ($own) { |
| 2433 |
|
| $opt{to_table} = "$opt{to_database}.$own.$tab"; |
| 2434 |
|
| } |
| 2435 |
|
| else { |
| 2436 |
|
| $opt{to_table} = "$opt{to_database}..$tab"; |
| 2437 |
|
| } |
| 2438 |
|
| } |
| 2439 |
|
| else { |
| 2440 |
|
| sx_complain("couldn't parse -to_table. form should be [db.][owner.]table\n") && return 1; |
| 2441 |
|
| } |
| 2442 |
|
| } |
| 2443 |
|
| else { |
| 2444 |
|
| sx_complain("-to_table MUST be specified\n") && return 1; |
| 2445 |
|
| } |
| 2446 |
|
| |
| 2447 |
|
| #make sure no ambiguity/problems with -from_table |
| 2448 |
|
| if ( $opt{from_table} ) { |
| 2449 |
|
| my ( $ok, $source, $server, $db, $own, $tab ) = $self->_parse_path( \%opt, $opt{from_table} ); |
| 2450 |
|
| if ($ok) { |
| 2451 |
|
| if ( !$opt{from_database} && !$db ) { |
| 2452 |
|
| sx_complain("-from_table should be of the form [db.][owner.]table if no -from_database specified\n") |
| 2453 |
|
| && return 1; |
| 2454 |
|
| } |
| 2455 |
|
| |
| 2456 |
|
| if ( !$opt{from_server} && !$server ) { |
| 2457 |
|
| sx_complain( |
| 2458 |
|
| "-from_table should be of the form [server.][db.][owner.]table if no -from_server specified\n") |
| 2459 |
|
| && return 1; |
| 2460 |
|
| } |
| 2461 |
|
| |
| 2462 |
|
| #we'll get to this later |
| 2463 |
|
| # if (!$opt{from_source} && !$source) { |
| 2464 |
|
| # sx_complain("-from_table should be of the form source.server.db.[owner].table if no -from_source specified\n") |
| 2465 |
|
| # && return 1; |
| 2466 |
|
| # } |
| 2467 |
|
| |
| 2468 |
|
| #@ $opt{from_database} = $db || $opt{from_database}; |
| 2469 |
|
| #@ $opt{from_server} = $server || $opt{from_server}; |
| 2470 |
|
| #@ $opt{from_source} = $source || $opt{from_source} || 'Sybase'; |
| 2471 |
|
| #@ if ($own) { |
| 2472 |
|
| #@ $opt{from_table} = "$opt{from_database}.$own.$tab"; |
| 2473 |
|
| #@ } |
| 2474 |
|
| #@ else { |
| 2475 |
|
| #@ $opt{from_table} = "$opt{from_database}..$tab"; |
| 2476 |
|
| #@ } |
| 2477 |
|
| } |
| 2478 |
|
| else { |
| 2479 |
|
| sx_complain("couldn't parse -from_table. form should be [db.][owner.]table\n") && return 1; |
| 2480 |
|
| } |
| 2481 |
|
| } |
| 2482 |
|
| |
| 2483 |
|
| #error handling |
| 2484 |
|
| if ( defined $opt{error_handling} && !$opt{error_handling} =~ m/^(continue|abort|retry)/i ) { |
| 2485 |
|
| sx_complain("if -error_handling is specified it must be either abort/continue/retry\n") && return 1; |
| 2486 |
|
| } |
| 2487 |
|
| $opt{error_handling} = 'abort' unless defined $opt{error_handling}; |
| 2488 |
|
| if ( $opt{error_handling} =~ /retry/i ) { |
| 2489 |
|
| |
| 2490 |
|
| if ( !$opt{callback_err_batch} ) { |
| 2491 |
|
| $opt{callback_err_batch} = \&bcp_batch_error_handler; |
| 2492 |
|
| sx_complain("Must specify -error_data_file if -error_handling = 'retry'\n"), return 1 |
| 2493 |
|
| unless ( $opt{error_data_file} ); |
| 2494 |
|
| } |
| 2495 |
|
| } |
| 2496 |
|
| my $fn = $opt{error_data_file}; |
| 2497 |
|
| |
| 2498 |
|
| if ($fn) { |
| 2499 |
|
| $opt{sx_error_data_file_fh} = new FileHandle ">$fn"; |
| 2500 |
|
| sx_complain("Can't open <$fn> $!\n"), return 1 unless $opt{sx_error_data_file_fh}; |
| 2501 |
|
| } |
| 2502 |
|
| |
| 2503 |
|
| #checks |
| 2504 |
|
| if ( $opt{error_handling} =~ /abort/i && ( $opt{retry_max} || $opt{retry_verbose} || $opt{retry_deadlock_sleep} ) ) { |
| 2505 |
|
| sx_complain( |
| 2506 |
|
| "when -error_handling is 'abort', -retry_max, -retry_verbose, -retry_deadlock_sleep cannot be used\n"); |
| 2507 |
|
| } |
| 2508 |
|
| |
| 2509 |
|
| #default scratch db |
| 2510 |
|
| $opt{scratch_db} = 'tempdb' unless $opt{scratch_db}; |
| 2511 |
|
| $opt{auto_delete_batchsize} = 3000 unless defined $opt{auto_delete_batchsize}; |
| 2512 |
|
| |
| 2513 |
|
| #-- |
| 2514 |
|
| #deadlock retry |
| 2515 |
|
| #-- |
| 2516 |
|
| $opt{retry_max} = 3 unless defined $opt{retry_max}; |
| 2517 |
|
| $opt{retry_deadlock_sleep} = 120 unless defined $opt{retry_deadlock_sleep}; |
| 2518 |
|
| $opt{retry_verbose} = 1 unless defined $opt{retry_verbose}; |
| 2519 |
|
| $opt{save_deadlock_retry} = $Sybase::CTlib::nsql_deadlock_retrycount; |
| 2520 |
|
| $opt{save_deadlock_sleep} = $Sybase::CTlib::nsql_deadlock_retrysleep; |
| 2521 |
|
| $opt{save_deadlock_verbose} = $Sybase::CTlib::nsql_deadlock_verbose; |
| 2522 |
|
| $Sybase::CTlib::nsql_deadlock_retrycount = defined $opt{retry_max} ? $opt{retry_max} : 3; |
| 2523 |
|
| $Sybase::CTlib::nsql_deadlock_retrysleep = defined $opt{retry_deadlock_sleep} ? $opt{retry_deadlock_sleep} : 120; |
| 2524 |
|
| $Sybase::CTlib::nsql_deadlock_verbose = defined $opt{verbose} ? $opt{verbose} : 1; |
| 2525 |
|
| |
| 2526 |
|
| #checks |
| 2527 |
|
| unless ( $opt{to_server} ) { |
| 2528 |
|
| sx_complain("Must specify -to server\n") && return 1; |
| 2529 |
|
| } |
| 2530 |
|
| |
| 2531 |
|
| unless ( $opt{to_table} ) { |
| 2532 |
|
| sx_complain("Must specify -to table, use db.[owner].table syntax for safety.\n") && return 1; |
| 2533 |
|
| } |
| 2534 |
|
| |
| 2535 |
|
| unless ( $opt{from_table} || $opt{from_script} || $opt{from_sql} || $opt{from_perl} || $opt{from_file} ) { |
| 2536 |
|
| sx_complain("Must specify -from table, -from_script, -from_sql, -from_perl or -from_file\n"); |
| 2537 |
|
| return 1; |
| 2538 |
|
| } |
| 2539 |
|
| |
| 2540 |
|
| # if ( $opt{from_table} || $opt{from_script} || $opt{from_sql} ) { $opt{from_source} ||= 'Sybase' } |
| 2541 |
|
| |
| 2542 |
|
| if ( $opt{from_perl} ) { |
| 2543 |
|
| $opt{from_source} = 'PERL-CODEREF'; |
| 2544 |
|
| } |
| 2545 |
|
| |
| 2546 |
|
| if ( $opt{from_file} && !ref( $opt{from_file} ) ) { $opt{from_source} = 'FLAT-FILE' } |
| 2547 |
|
| if ( $opt{from_file} && ref( $opt{from_file} ) ) { $opt{from_source} = 'FILE-HANDLE' } |
| 2548 |
|
| |
| 2549 |
|
| my $c = grep { defined $_ } @opt{qw/from_table from_script from_perl from_file/}; |
| 2550 |
|
| if ( $c > 1 ) { |
| 2551 |
|
| sx_complain("-from_table, -from_script, -from_perl, -from_file are mutually exclusive\n") && return 1; |
| 2552 |
|
| } |
| 2553 |
|
| |
| 2554 |
|
| if ( ( $opt{from_table} || $opt{from_script} || $opt{from_sql} ) && !$opt{from_server} ) { |
| 2555 |
|
| sx_complain("Must specify -from_server if -from_table, -from_script or -from_sql is specified\n"); |
| 2556 |
|
| return 1; |
| 2557 |
|
| } |
| 2558 |
|
| |
| 2559 |
|
| if ( $opt{from_file} && !$opt{from_file_delimiter} && !$opt{from_file_pack_format} ) { |
| 2560 |
|
| sx_complain("Must specify either -from_file_delimiter or -from_file_pack_format if -from_file is specified\n") |
| 2561 |
|
| && return; |
| 2562 |
|
| } |
| 2563 |
|
| $opt{from_file_delimiter} = quotemeta $opt{from_file_delimiter} if length $opt{from_file_delimiter} == 1; |
| 2564 |
|
| |
| 2565 |
|
| if ( $opt{from_file_delimiter} && !$opt{from_file} ) { |
| 2566 |
|
| sx_complain("Must specify -from_file if -from_file_delimiter is specified\n") && return 1; |
| 2567 |
|
| } |
| 2568 |
|
| |
| 2569 |
|
| if ( $opt{from_file_pack_format} && !$opt{from_file} ) { |
| 2570 |
|
| sx_complain("Must specify -from_file if -from_file_pack_format is specified\n") && return 1; |
| 2571 |
|
| } |
| 2572 |
|
| $c = grep { defined $_ } @opt{qw/from_file_delimiter from_file_pack_format/}; |
| 2573 |
|
| if ( $c > 1 ) { |
| 2574 |
|
| sx_complain("-from_file_delimiter, -from_file_pack_format are mutually exclusive\n") && return 1; |
| 2575 |
|
| } |
| 2576 |
|
| |
| 2577 |
|
| $c = grep { defined $_ } @opt{qw/auto_delete delete_flag truncate_flag/}; |
| 2578 |
|
| if ( $c > 1 ) { |
| 2579 |
|
| sx_complain("-auto_delete, -delete_flag and -truncate_flag are mutually exclusive\n") && return 1; |
| 2580 |
|
| } |
| 2581 |
|
| |
| 2582 |
|
| #---- |
| 2583 |
|
| #check -from_file_map syntax |
| 2584 |
|
| #---- |
| 2585 |
|
| my $err = (); |
| 2586 |
|
| $opt{from_file_map} and ( undef, $err ) = sx_parse_user_hash( $opt{from_file_map}, '-from_file_map' ); |
| 2587 |
|
| if ($err) { |
| 2588 |
|
| sx_complain(<<EOF); |
| 2589 |
|
| error parsing -from_file_map, err=$err |
| 2590 |
|
| |
| 2591 |
|
| syntax is: -from_file_map [hashref | string | filename | 0] |
| 2592 |
|
| * hashref => is a perl hash ref |
| 2593 |
|
| |
| 2594 |
|
| * string => is of the form '(col=>pos, col=>pos, ...)' |
| 2595 |
|
| where col is column name in -to_table and |
| 2596 |
|
| pos is field number in input stream |
| 2597 |
|
| first position is zero NOT one |
| 2598 |
|
| |
| 2599 |
|
| * filename => is name of file containing above string |
| 2600 |
|
| |
| 2601 |
|
| * 0 => same as -NOfrom_file_map |
| 2602 |
|
| |
| 2603 |
|
| one of the above values must be specified if this switch |
| 2604 |
|
| is used |
| 2605 |
|
| EOF |
| 2606 |
|
| return 1; |
| 2607 |
|
| } |
| 2608 |
|
| |
| 2609 |
|
| #--- |
| 2610 |
|
| #check -drop_and_recreate_indices |
| 2611 |
|
| #--- |
| 2612 |
|
| my ( %dari, $dari ) = (); |
| 2613 |
|
| if ( defined $opt{drop_and_recreate_indices} && $opt{drop_and_recreate_indices} !~ /^[01]$/ ) { |
| 2614 |
|
| ( $dari, $err ) = sx_parse_user_hash( $opt{drop_and_recreate_indices}, '-drop_and_recreate_indices' ); |
| 2615 |
|
| } |
| 2616 |
|
| |
| 2617 |
|
| if ( !$err && $dari ) { |
| 2618 |
|
| %dari = %$dari; |
| 2619 |
|
| !exists $dari{syts} and $err = "key 'syts' not found"; |
| 2620 |
|
| $dari{syts} && !exists $dari{source} and $err = "key 'source' not found"; |
| 2621 |
|
| |
| 2622 |
|
| #defaults |
| 2623 |
|
| $dari{logfile} = 'stdout' unless defined $dari{logfile}; |
| 2624 |
|
| $dari{logfile} = '/dev/null' if $dari{logfile} eq '0'; |
| 2625 |
|
| $dari{sytsUser} = $opt{to_user} unless defined $dari{sytsUser}; |
| 2626 |
|
| $dari{sytsPwd} = $dari{sytsUser} unless defined $dari{sytsPwd}; |
| 2627 |
|
| $dari{withSortedData} = 0 unless defined $dari{withSortedData}; |
| 2628 |
|
| } |
| 2629 |
|
| |
| 2630 |
|
| #print error |
| 2631 |
|
| if ($err) { |
| 2632 |
|
| sx_complain(<<EOF); |
| 2633 |
|
| error parsing -drop_and_recreate_indices, err=$err |
| 2634 |
|
| |
| 2635 |
|
| syntax is: -drop_and_recreate_indices [hashref | string | filename | 0 | 1] |
| 2636 |
|
| * 1 => same as -drop_and_recreate_indices |
| 2637 |
|
| |
| 2638 |
|
| * 0 => same as -NOdrop_and_recreate_indices |
| 2639 |
|
| |
| 2640 |
|
| * hashref => is a perl hash ref |
| 2641 |
|
| |
| 2642 |
|
| * string => is of the form: |
| 2643 |
|
| '(syts=>1/0, source=>"server.database", logfile=>"filename", sytsUser=>user, sytsPwd=>pwd, withSortedData=>0/1)' |
| 2644 |
|
| syts indicates to use syts |
| 2645 |
|
| server.database is where the -to_table exists with indices. |
| 2646 |
|
| filename is the logfile. omit if stdout desired. |
| 2647 |
|
| user is the syts user. default is -to_user. |
| 2648 |
|
| pwd is the syts pwd. default is user. |
| 2649 |
|
| |
| 2650 |
|
| EOF |
| 2651 |
|
| return 1; |
| 2652 |
|
| } |
| 2653 |
|
| |
| 2654 |
|
| #squirrel away |
| 2655 |
|
| if (%dari) { |
| 2656 |
|
| $opt{sx_drop_and_recreate_indices} = \%dari; |
| 2657 |
|
| } |
| 2658 |
|
| else { |
| 2659 |
|
| $opt{sx_drop_and_recreate_indices} = { syts => 0 }; |
| 2660 |
|
| } |
| 2661 |
|
| |
| 2662 |
|
| #auto delete |
| 2663 |
|
| if ( $opt{auto_delete} && !$opt{from_table} ) { |
| 2664 |
|
| sx_complain("switch -auto_delete can only be used with -from_table\n"); |
| 2665 |
|
| return 1; |
| 2666 |
|
| } |
| 2667 |
|
| |
| 2668 |
|
| return %opt; |
| 2669 |
|
| } |
| 2670 |
|
| |
| 2671 |
|
| #------------------------------------------------------- |
| 2672 |
|
| #parse source path |
| 2673 |
|
| #------------------------------------------------------- |
| 2674 |
|
| sub _parse_path { |
| 2675 |
|
| my ( $self, $opt, $path ) = @_; |
| 2676 |
|
| |
| 2677 |
|
| my @parts = split /\./, $path; |
| 2678 |
|
| my ( $ok, $source, $server, $db, $own, $tab ); |
| 2679 |
|
| if ( @parts <= 5 ) { |
| 2680 |
|
| ( $source, $server, $db, $own, $tab ) = @parts if @parts == 5; |
| 2681 |
|
| ( $server, $db, $own, $tab ) = @parts if @parts == 4; |
| 2682 |
|
| ( $db, $own, $tab ) = @parts if @parts == 3; |
| 2683 |
|
| ( $own, $tab ) = @parts if @parts == 2; |
| 2684 |
|
| ($tab) = @parts if @parts == 1; |
| 2685 |
|
| $ok = 1; |
| 2686 |
|
| } |
| 2687 |
|
| else { |
| 2688 |
|
| $ok = 0; |
| 2689 |
|
| } |
| 2690 |
|
| return ( $ok, $source, $server, $db, $own, $tab ); |
| 2691 |
|
| |
| 2692 |
|
| } |
| 2693 |
|
| |
| 2694 |
|
| #----------------------------------------------------------------------- |
| 2695 |
|
| #confirm options and load massaged options |
| 2696 |
|
| #----------------------------------------------------------------------- |
| 2697 |
|
| sub sx_verify_options { |
| 2698 |
|
| |
| 2699 |
|
| my @user_settings = @_; |
| 2700 |
|
| |
| 2701 |
|
| #need to preserve order for options processing |
| 2702 |
|
| my %user_settings = (); |
| 2703 |
|
| tie %user_settings, "Tie::IxHash"; |
| 2704 |
|
| |
| 2705 |
|
| my $i = 0; |
| 2706 |
|
| while ( $i < @user_settings ) { |
| 2707 |
|
| my ( $k, $v ) = ( $user_settings[$i], $user_settings[ $i + 1 ] ); |
| 2708 |
|
| |
| 2709 |
|
| #this means flag style option if the next option starts with a '-' |
| 2710 |
|
| if ( $v =~ /^\-\w/ || $i == $#user_settings && $k =~ /^\-\w/ ) { |
| 2711 |
|
| $v = 1; |
| 2712 |
|
| $i++; |
| 2713 |
|
| } |
| 2714 |
|
| else { |
| 2715 |
|
| $i += 2; |
| 2716 |
|
| } |
| 2717 |
|
| $user_settings{"$k"} = $v; |
| 2718 |
|
| |
| 2719 |
|
| } |
| 2720 |
|
| |
| 2721 |
|
| #the list of options |
| 2722 |
|
| my @valid_options = |
| 2723 |
|
| qw/ |
| 2724 |
|
| help|h|?:s |
| 2725 |
|
| |
| 2726 |
|
| from_source|fx=s |
| 2727 |
|
| from_server|fs=s |
| 2728 |
|
| from_user|fu=s |
| 2729 |
|
| from_password|fp=s |
| 2730 |
|
| from_database|fd=s |
| 2731 |
|
| |
| 2732 |
|
| from_table|ft=s |
| 2733 |
|
| from_script=s |
| 2734 |
|
| from_sql=s |
| 2735 |
|
| from_perl=s |
| 2736 |
|
| from_file|ff=s |
| 2737 |
|
| from_file_delimiter|ffd=s |
| 2738 |
|
| from_file_map|ffm|map:s |
| 2739 |
|
| from_file_pack_format|ffpf=s |
| 2740 |
|
| |
| 2741 |
|
| to_server|ts=s |
| 2742 |
|
| to_user|tu=s |
| 2743 |
|
| to_password|tp=s |
| 2744 |
|
| to_table|tt=s |
| 2745 |
|
| to_database|td=s |
| 2746 |
|
| |
| 2747 |
|
| user|U=s |
| 2748 |
|
| password|P=s |
| 2749 |
|
| server|S=s |
| 2750 |
|
| table|T=s |
| 2751 |
|
| database|D=s |
| 2752 |
|
| |
| 2753 |
|
| delete_flag|df:s |
| 2754 |
|
| truncate_flag|tf:s |
| 2755 |
|
| where_clause|wc=s |
| 2756 |
|
| batchsize|bs=i |
| 2757 |
|
| holdlock|hl:s |
| 2758 |
|
| trim_whitespace|tw:s |
| 2759 |
|
| timeout|to=i |
| 2760 |
|
| drop_and_recreate_indices|dari:s |
| 2761 |
|
| copy_source_indices|csi! |
| 2762 |
|
| set_identity_insert|sii:s |
| 2763 |
|
| textsize|txs:s |
| 2764 |
|
| packetsize|ps:s |
| 2765 |
|
| |
| 2766 |
|
| scratch_db=s |
| 2767 |
|
| auto_delete=s |
| 2768 |
|
| auto_delete_batchsize|adb=i |
| 2769 |
|
| |
| 2770 |
|
| debug=s |
| 2771 |
|
| echo:s |
| 2772 |
|
| silent:s |
| 2773 |
|
| progress_log|verbose:s |
| 2774 |
|
| app_name|an=s |
| 2775 |
|
| |
| 2776 |
|
| error_handling|eh=s |
| 2777 |
|
| error_data_file|edf=s |
| 2778 |
|
| retry_max=s |
| 2779 |
|
| retry_deadlock_sleep|rds=s |
| 2780 |
|
| retry_verbose|rv=s |
| 2781 |
|
| |
| 2782 |
|
| callback_err_send=s |
| 2783 |
|
| callback_err_batch=s |
| 2784 |
|
| callback_pre_send=s |
| 2785 |
|
| callback_print=s |
| 2786 |
|
| |
| 2787 |
|
| sybxfer:s |
| 2788 |
|
| return:s |
| 2789 |
|
| method:s |
| 2790 |
|
| /; |
| 2791 |
|
| |
| 2792 |
|
| use vars qw/%real_options/; |
| 2793 |
|
| |
| 2794 |
|
| do { |
| 2795 |
|
| local $SIG{__WARN__} = sub { sx_complain("$_[0]") }; |
| 2796 |
|
| |
| 2797 |
|
| #load up ARGV for GetOptions |
| 2798 |
|
| %real_options = (); |
| 2799 |
|
| local @ARGV = %user_settings; |
| 2800 |
|
| Getopt::Long::Configure(qw/no_ignore_case/); |
| 2801 |
|
| my $rs = GetOptions( \%real_options, @valid_options ); |
| 2802 |
|
| $rs || return 1; |
| 2803 |
|
| }; |
| 2804 |
|
| |
| 2805 |
|
| return %real_options; |
| 2806 |
|
| } |
| 2807 |
|
| |
| 2808 |
|
| #----------------------------------------------------------------------- |
| 2809 |
|
| #complain |
| 2810 |
|
| #----------------------------------------------------------------------- |
| 2811 |
|
| sub sx_complain { |
| 2812 |
|
| |
| 2813 |
|
| my $msg = shift; |
| 2814 |
|
| warn "----------------\n"; |
| 2815 |
|
| warn "$msg"; |
| 2816 |
|
| warn "----------------\n"; |
| 2817 |
|
| return 1; |
| 2818 |
|
| |
| 2819 |
|
| } |
| 2820 |
|
| |
| 2821 |
|
| #----------------------------------------------------------------------- |
| 2822 |
|
| #this is copied from nsql_error_handler. I might change it later. |
| 2823 |
|
| #----------------------------------------------------------------------- |
| 2824 |
|
| sub sx_error_handler { |
| 2825 |
|
| my $self = shift; |
| 2826 |
|
| shift; #dbh not needed. |
| 2827 |
|
| return $self->sx_error_handler_dbi(@_); |
| 2828 |
|
| } |
| 2829 |
|
| |
| 2830 |
|
| #----- |
| 2831 |
|
| # |
| 2832 |
|
| #----- |
| 2833 |
|
| sub sx_error_handler_dbi { |
| 2834 |
|
| |
| 2835 |
|
| my ( $self, $number, $sev, $state, $line, $server, $proc, $msg ) = @_; |
| 2836 |
|
| |
| 2837 |
|
| $DB_DEADLOCK = 1 if $number == 1205 ? 1 : undef; |
| 2838 |
|
| $DB_ERROR = undef; |
| 2839 |
|
| $DB_ERROR_ONELINE = undef; |
| 2840 |
|
| |
| 2841 |
|
| #ignore these |
| 2842 |
|
| # 5701 #changed db context |
| 2843 |
|
| # 5703 #changed language |
| 2844 |
|
| # 5704 #changed character set |
| 2845 |
|
| |
| 2846 |
|
| return 1 if grep { $_ == $number } ( 5701, 5703, 5704 ); |
| 2847 |
|
| |
| 2848 |
|
| #print messages are severity 10 |
| 2849 |
|
| if ( $sev == 10 ) { |
| 2850 |
|
| print "$msg\n"; |
| 2851 |
|
| |
| 2852 |
|
| } |
| 2853 |
|
| else { |
| 2854 |
|
| chomp($msg); |
| 2855 |
|
| $DB_ERROR = <<EOF; |
| 2856 |
|
| Sybase::Xfer [Error Handler] |
| 2857 |
|
| Server : $server |
| 2858 |
|
| Number : $number |
| 2859 |
|
| Severity : $sev |
| 2860 |
|
| Line : $line |
| 2861 |
|
| Text : $msg |
| 2862 |
|
| EOF |
| 2863 |
|
| $DB_ERROR_ONELINE = "SybaseError:server=$server:number=$number:text=$msg"; |
| 2864 |
|
| } |
| 2865 |
|
| return 1; |
| 2866 |
|
| } |
| 2867 |
|
| |
| 2868 |
|
| #----------------------------------------------------------------------- |
| 2869 |
|
| #prints sql 'print' code via _print |
| 2870 |
|
| #----------------------------------------------------------------------- |
| 2871 |
|
| sub sx_message_handler { |
| 2872 |
|
| |
| 2873 |
|
| # my ( $opt, $db, $message, $state, $severity, $text, $server, $procedure, $line ) = @_; |
| 2874 |
|
| |
| 2875 |
|
| my ( $self, $layer, $origin, $severity, $number, $msg, $osmsg, $dbh ) = @_; |
| 2876 |
|
| |
| 2877 |
|
| #ignore these |
| 2878 |
|
| # 5701 #changed db context |
| 2879 |
|
| # 5703 #changed language |
| 2880 |
|
| # 5704 #changed character set |
| 2881 |
|
| |
| 2882 |
|
| return CS_SUCCEED if grep { $_ == $number } ( 5701, 5703, 5704 ); |
| 2883 |
|
| |
| 2884 |
|
| #set DB_ERROR |
| 2885 |
|
| $DB_ERROR .= "\nOpen Client Message:\n"; |
| 2886 |
|
| $DB_ERROR .= sprintf( "Message number: LAYER = (%ld) ORIGIN = (%ld) ", $layer, $origin ); |
| 2887 |
|
| $DB_ERROR .= sprintf( "SEVERITY = (%ld) NUMBER = (%ld)\n", $severity, $number ); |
| 2888 |
|
| $DB_ERROR .= "Message String: $msg\n"; |
| 2889 |
|
| $DB_ERROR .= sprintf( "Operating System Error: %s\n", $osmsg ) if defined $osmsg; |
| 2890 |
|
| $DB_ERROR_ONELINE = "$number $msg"; |
| 2891 |
|
| |
| 2892 |
|
| #force nosilent on errors. |
| 2893 |
|
| $self->{override_silent}++; |
| 2894 |
|
| $self->_print("$msg\n"); |
| 2895 |
|
| |
| 2896 |
|
| CS_SUCCEED; |
| 2897 |
|
| } |
| 2898 |
|
| |
| 2899 |
|
| #----- |
| 2900 |
|
| # |
| 2901 |
|
| #----- |
| 2902 |
|
| sub _drop_indices_setup { |
| 2903 |
|
| |
| 2904 |
|
| my $self = shift; |
| 2905 |
|
| my ($dbh, $db, $tab) = @_; |
| 2906 |
|
| |
| 2907 |
|
| my %dari = %{ $self->{sx_drop_and_recreate_indices} }; |
| 2908 |
|
| |
| 2909 |
|
| my $sql = <<EOF . "\n"; |
| 2910 |
|
| declare \@db varchar(40), \@objname varchar(100), \@withSortedData int |
| 2911 |
|
| select \@db='$db', \@objname='$tab' |
| 2912 |
|
| select \@withSortedData=$dari{withSortedData} |
| 2913 |
|
| EOF |
| 2914 |
|
| |
| 2915 |
|
| $sql = $sql . <<'EOF'; |
| 2916 |
|
| set nocount on |
| 2917 |
|
| declare @keys varchar(255), @indid int, @nk int |
| 2918 |
|
| declare @inddesc varchar(255), @idx_name varchar(40), @cluster varchar(20), @options varchar(50) |
| 2919 |
|
| declare @uniq varchar(12), @dev varchar(40) |
| 2920 |
|
| select @nk=0 |
| 2921 |
|
| |
| 2922 |
|
| -- |
| 2923 |
|
| -- this select will set @indid to the index id of the first index. |
| 2924 |
|
| -- |
| 2925 |
|
| select @indid = min(indid) |
| 2926 |
|
| from sysindexes |
| 2927 |
|
| where id = object_id(@objname) and indid > 0 and indid < 255 |
| 2928 |
|
| |
| 2929 |
|
| -- |
| 2930 |
|
| -- If no indexes, return. |
| 2931 |
|
| -- |
| 2932 |
|
| --if @indid is NULL print "17640, Object does not have any indexes." |
| 2933 |
|
| |
| 2934 |
|
| |
| 2935 |
|
| -- |
| 2936 |
|
| --foreach index |
| 2937 |
|
| -- |
| 2938 |
|
| while @indid is not NULL |
| 2939 |
|
| begin |
| 2940 |
|
| select @idx_name=null, @uniq=null, @cluster=null, @options=null, @dev=null |
| 2941 |
|
| select @nk=@nk+1 |
| 2942 |
|
| |
| 2943 |
|
| -- first we'll figure out what the keys are. |
| 2944 |
|
| declare @i int, @thiskey varchar(30), @sorder char(4), @lastindid int |
| 2945 |
|
| select @keys = "", @i = 1 |
| 2946 |
|
| |
| 2947 |
|
| --foreach key |
| 2948 |
|
| set nocount on |
| 2949 |
|
| while @i <= 31 |
| 2950 |
|
| begin |
| 2951 |
|
| select @thiskey = index_col(@objname, @indid, @i) |
| 2952 |
|
| if (@thiskey is NULL) break |
| 2953 |
|
| if @i > 1 select @keys = @keys + "," |
| 2954 |
|
| select @keys = @keys + @thiskey |
| 2955 |
|
| |
| 2956 |
|
| --sort order |
| 2957 |
|
| select @sorder = index_colorder(@objname, @indid, @i) |
| 2958 |
|
| if (@sorder = "DESC") select @keys = @keys + "-" + @sorder |
| 2959 |
|
| |
| 2960 |
|
| select @i = @i + 1 |
| 2961 |
|
| end |
| 2962 |
|
| select @keys=ltrim(@keys) |
| 2963 |
|
| |
| 2964 |
|
| --idx: clustered or nonclustered |
| 2965 |
|
| if @indid = 1 select @cluster = "clustered" |
| 2966 |
|
| |
| 2967 |
|
| if @indid > 1 begin |
| 2968 |
|
| if exists (select * from sysindexes i |
| 2969 |
|
| where status2 & 512 = 512 and i.indid = @indid and i.id = object_id(@objname)) |
| 2970 |
|
| select @cluster = "clustered" |
| 2971 |
|
| else |
| 2972 |
|
| select @cluster = "nonclustered" |
| 2973 |
|
| end |
| 2974 |
|
| |
| 2975 |
|
| --idx: unique or not |
| 2976 |
|
| if exists (select * from master.dbo.spt_values v, sysindexes i |
| 2977 |
|
| where i.status & v.number = v.number |
| 2978 |
|
| and v.type = "I" |
| 2979 |
|
| and v.number = 2 |
| 2980 |
|
| and i.id = object_id(@objname) |
| 2981 |
|
| and i.indid = @indid) begin |
| 2982 |
|
| |
| 2983 |
|
| select @uniq = 'unique' |
| 2984 |
|
| |
| 2985 |
|
| select @inddesc = @inddesc + ", " + v.name |
| 2986 |
|
| from master.dbo.spt_values v, sysindexes i |
| 2987 |
|
| where i.status & v.number = v.number |
| 2988 |
|
| and v.type = "I" |
| 2989 |
|
| and v.number = 2 |
| 2990 |
|
| and i.id = object_id(@objname) |
| 2991 |
|
| and i.indid = @indid |
| 2992 |
|
| end |
| 2993 |
|
| |
| 2994 |
|
| -- if this is a nonunique clustered index on dol tables |
| 2995 |
|
| else |
| 2996 |
|
| -- |
| 2997 |
|
| -- allow_dup_rows |
| 2998 |
|
| -- |
| 2999 |
|
| if exists ( |
| 3000 |
|
| select * from sysindexes i |
| 3001 |
|
| where status2 & 512 = 512 |
| 3002 |
|
| and i.indid = @indid |
| 3003 |
|
| and i.id = object_id(@objname)) begin |
| 3004 |
|
| |
| 3005 |
|
| select @options='allow_dup_rows' |
| 3006 |
|
| |
| 3007 |
|
| select @inddesc = @inddesc + ", " + v.name |
| 3008 |
|
| from master.dbo.spt_values v, sysindexes i |
| 3009 |
|
| where v.type = "I" |
| 3010 |
|
| and v.number = 64 |
| 3011 |
|
| and i.id = object_id(@objname) |
| 3012 |
|
| and i.indid = @indid |
| 3013 |
|
| end |
| 3014 |
|
| |
| 3015 |
|
| |
| 3016 |
|
| -- |
| 3017 |
|
| -- ignore_dupkey (0x01). |
| 3018 |
|
| -- |
| 3019 |
|
| if exists ( |
| 3020 |
|
| select * from master.dbo.spt_values v, sysindexes i |
| 3021 |
|
| where i.status & v.number = v.number |
| 3022 |
|
| and v.type = "I" |
| 3023 |
|
| and v.number = 1 |
| 3024 |
|
| and i.id = object_id(@objname) |
| 3025 |
|
| and i.indid = @indid) begin |
| 3026 |
|
| |
| 3027 |
|
| select @options='ignore_dup_key' |
| 3028 |
|
| |
| 3029 |
|
| select @inddesc = @inddesc + ", " + v.name |
| 3030 |
|
| from master.dbo.spt_values v, sysindexes i |
| 3031 |
|
| where i.status & v.number = v.number |
| 3032 |
|
| and v.type = "I" |
| 3033 |
|
| and v.number = 1 |
| 3034 |
|
| and i.id = object_id(@objname) |
| 3035 |
|
| and i.indid = @indid |
| 3036 |
|
| end |
| 3037 |
|
| |
| 3038 |
|
| -- |
| 3039 |
|
| -- See if the index is ignore_dup_row (0x04). |
| 3040 |
|
| -- |
| 3041 |
|
| if exists ( |
| 3042 |
|
| select * from master.dbo.spt_values v, sysindexes i |
| 3043 |
|
| where i.status & v.number = v.number |
| 3044 |
|
| and v.type = "I" |
| 3045 |
|
| and v.number = 4 |
| 3046 |
|
| and i.id = object_id(@objname) |
| 3047 |
|
| and i.indid = @indid) begin |
| 3048 |
|
| |
| 3049 |
|
| select @options='ignore_dup_row' |
| 3050 |
|
| |
| 3051 |
|
| select @inddesc = @inddesc + ", " + v.name |
| 3052 |
|
| from master.dbo.spt_values v, sysindexes i |
| 3053 |
|
| where i.status & v.number = v.number |
| 3054 |
|
| and v.type = "I" |
| 3055 |
|
| and v.number = 4 |
| 3056 |
|
| and i.id = object_id(@objname) |
| 3057 |
|
| and i.indid = @indid |
| 3058 |
|
| end |
| 3059 |
|
| |
| 3060 |
|
| -- |
| 3061 |
|
| -- See if the index is allow_dup_row (0x40). |
| 3062 |
|
| -- |
| 3063 |
|
| if exists ( |
| 3064 |
|
| select * from master.dbo.spt_values v, sysindexes i |
| 3065 |
|
| where i.status & v.number = v.number |
| 3066 |
|
| and v.type = "I" |
| 3067 |
|
| and v.number = 64 |
| 3068 |
|
| and i.id = object_id(@objname) |
| 3069 |
|
| and i.indid = @indid) begin |
| 3070 |
|
| |
| 3071 |
|
| select @options='allow_dup_row' |
| 3072 |
|
| |
| 3073 |
|
| select @inddesc = @inddesc + ", " + v.name |
| 3074 |
|
| from master.dbo.spt_values v, sysindexes i |
| 3075 |
|
| where i.status & v.number = v.number |
| 3076 |
|
| and v.type = "I" |
| 3077 |
|
| and v.number = 64 |
| 3078 |
|
| and i.id = object_id(@objname) |
| 3079 |
|
| and i.indid = @indid |
| 3080 |
|
| end |
| 3081 |
|
| |
| 3082 |
|
| -- device |
| 3083 |
|
| select @dev = s.name |
| 3084 |
|
| from syssegments s, sysindexes i |
| 3085 |
|
| where s.segment = i.segment |
| 3086 |
|
| and i.id = object_id(@objname) |
| 3087 |
|
| and i.indid = @indid |
| 3088 |
|
| |
| 3089 |
|
| |
| 3090 |
|
| -- index name |
| 3091 |
|
| select @idx_name=name |
| 3092 |
|
| from sysindexes |
| 3093 |
|
| where id = object_id(@objname) and indid = @indid |
| 3094 |
|
| |
| 3095 |
|
| if(@withSortedData <> 0 and @cluster = 'clustered') |
| 3096 |
|
| BEGIN |
| 3097 |
|
| -- print "before = [%1!]", @options |
| 3098 |
|
| select @options=case when @options <> null |
| 3099 |
|
| then @options + ', sorted_data' |
| 3100 |
|
| else 'sorted_data' |
| 3101 |
|
| end |
| 3102 |
|
| -- print "after = [%1!]", @options |
| 3103 |
|
| END |
| 3104 |
|
| |
| 3105 |
|
| if @options <> null select @options='with ' + @options |
| 3106 |
|
| |
| 3107 |
|
| --debug |
| 3108 |
|
| -- print "create %1! %2! index %3! on %4!..%5!(%6!) %7! on %8!", |
| 3109 |
|
| -- @uniq, @cluster, @idx_name, @db, @objname, @keys, @options, @dev |
| 3110 |
|
| |
| 3111 |
|
| |
| 3112 |
|
| -- |
| 3113 |
|
| -- return create_index, drop_index and idx_name |
| 3114 |
|
| -- |
| 3115 |
|
| select create_index='create '+@uniq+' '+@cluster+' index '+@idx_name+' on '+ |
| 3116 |
|
| @db+'..'+@objname+'('+@keys+') '+@options+' on '+"'"+@dev+"'", |
| 3117 |
|
| |
| 3118 |
|
| drop_index='drop index '+@objname+'.'+@idx_name, |
| 3119 |
|
| |
| 3120 |
|
| idx_name=@idx_name |
| 3121 |
|
| |
| 3122 |
|
| -- |
| 3123 |
|
| -- Now move @indid to the next index. |
| 3124 |
|
| -- |
| 3125 |
|
| select @lastindid = @indid |
| 3126 |
|
| select @indid = NULL |
| 3127 |
|
| select @indid = min(indid) from sysindexes |
| 3128 |
|
| where id = object_id(@objname) and indid > @lastindid and indid < 255 |
| 3129 |
|
| end |
| 3130 |
|
| --select num_indices=@nk |
| 3131 |
|
| EOF |
| 3132 |
|
| |
| 3133 |
|
| #run the sql |
| 3134 |
|
| my @idx = @{ $self->_runsql( $dbh, $sql, {}, undef ) }; |
| 3135 |
|
| $DB_ERROR && sx_complain("$DB_ERROR\n") && return 1; |
| 3136 |
|
| |
| 3137 |
|
| |
| 3138 |
|
| #get some info |
| 3139 |
|
| my ( $user, $pwd, $db, $server ) = @{$self}{qw/to_user to_password sx_to_database to_server/}; |
| 3140 |
|
| my ( $from_server, $from_database ) = @{$self}{qw/from_server sx_from_database/}; |
| 3141 |
|
| my ($tab) = keys %{ $self->{sx_to_info}->{$db} }; |
| 3142 |
|
| @{ $self->{sx_to_table_indices}->{_names} } = (); |
| 3143 |
|
| |
| 3144 |
|
| #check option settings |
| 3145 |
|
| my $dari = $self->{sx_drop_and_recreate_indices}; |
| 3146 |
|
| my %dari = %$dari; |
| 3147 |
|
| |
| 3148 |
|
| #rip thru the results of the sql depending if syts is specified |
| 3149 |
|
| if ( $dari{syts} ) { |
| 3150 |
|
| my $src = $dari{source}; |
| 3151 |
|
| my $syts_drop_base = "syts -T$server.$db -U$dari{sytsUser} -P$dari{sytsPwd} -a 'drop index' "; |
| 3152 |
|
| my $syts_create_base = "syts -S$src -D$server.$db -U$dari{sytsUser} -P$dari{sytsPwd} -a 'create index' "; |
| 3153 |
|
| my $li = (); |
| 3154 |
|
| |
| 3155 |
|
| for my $index (@idx) { |
| 3156 |
|
| my ( $ci, $di, $in ) = @{$index}{qw/create_index drop_index idx_name/}; |
| 3157 |
|
| $li .= "$tab.$in,"; |
| 3158 |
|
| push @{ $self->{sx_to_table_indices}->{_names} }, $in; |
| 3159 |
|
| } |
| 3160 |
|
| $li = substr( $li, 0, -1 ); |
| 3161 |
|
| $self->{sx_to_table_indices}->{syts}->{create} = $syts_create_base . "-O'$li'"; |
| 3162 |
|
| $self->{sx_to_table_indices}->{syts}->{drop} = $syts_drop_base . "-O'$li'"; |
| 3163 |
|
| |
| 3164 |
|
| } |
| 3165 |
|
| else { |
| 3166 |
|
| for my $index (@idx) { |
| 3167 |
|
| my ( $ci, $di, $in ) = @{$index}{qw/create_index drop_index idx_name/}; |
| 3168 |
|
| $self->{sx_to_table_indices}->{$in}->{create_sql} = $ci; |
| 3169 |
|
| $self->{sx_to_table_indices}->{$in}->{drop_sql} = $di; |
| 3170 |
|
| push @{ $self->{sx_to_table_indices}->{_names} }, $in; |
| 3171 |
|
| } |
| 3172 |
|
| } |
| 3173 |
|
| |
| 3174 |
|
| return 0; |
| 3175 |
|
| } |
| 3176 |
|
| |
| 3177 |
|
| |
| 3178 |
|
| #---- |
| 3179 |
|
| # |
| 3180 |
|
| #---- |
| 3181 |
|
| sub sx_usage { |
| 3182 |
|
| |
| 3183 |
|
| my $self = shift; |
| 3184 |
|
| |
| 3185 |
|
| #if not called from sybxfer don't bother. |
| 3186 |
|
| return 0 unless $self->{sybxfer}; |
| 3187 |
|
| |
| 3188 |
|
| my $short_usage = <<EOF; |
| 3189 |
|
| $0 usage: |
| 3190 |
|
| --help, -h for this help |
| 3191 |
|
| perldoc Xfer.pm for the full doc |
| 3192 |
|
| |
| 3193 |
|
| from sources |
| 3194 |
|
| --from_table, -ft tablename in db..table syntax containing data |
| 3195 |
|
| --from_script file containing sql to run on from_server to get data |
| 3196 |
|
| --from_sql sql to run on from_server to get data |
| 3197 |
|
| --from_perl perl coderef to run to get data |
| 3198 |
|
| --from_file, -ff flat-file containing delimited fields |
| 3199 |
|
| |
| 3200 |
|
| from login information |
| 3201 |
|
| --from_server, -fs defaults to DSQUERY |
| 3202 |
|
| --from_user, -fu defaults to USER |
| 3203 |
|
| --from_password, -fp defaults to -from_user |
| 3204 |
|
| --from_database, -fd no default |
| 3205 |
|
| |
| 3206 |
|
| to login information |
| 3207 |
|
| --to_server, -ts defaults to DSQUERY |
| 3208 |
|
| --to_user, -tu defaults to USER |
| 3209 |
|
| --to_password, -tp defaults to -to_user |
| 3210 |
|
| --to_database, -td no default. |
| 3211 |
|
| --to_table, -tt no default. [SERVER.][DATABASE.][OWNER.]TABLE syntax supported |
| 3212 |
|
| |
| 3213 |
|
| from and to login information |
| 3214 |
|
| --server, -S sets -from_server and -to_server |
| 3215 |
|
| --user, -U sets -from_user and -to_user |
| 3216 |
|
| --password, -P sets -from_password and -to_password |
| 3217 |
|
| --database, -D sets -from_database and -to_database |
| 3218 |
|
| --table, -T sets -from_table and to_table. Use 'db..table' syntax |
| 3219 |
|
| |
| 3220 |
|
| if using from_file |
| 3221 |
|
| --from_file_delimiter, -ffd <regex> eg '\\s+' or '|' |
| 3222 |
|
| --from_file_pack_format, -ffpf <pack string> eg 'a2 x4 a23' |
| 3223 |
|
| |
| 3224 |
|
| more options |
| 3225 |
|
| --timeout,-to timeout value in seconds |
| 3226 |
|
| --where_clause, -wc where_clause to append to query if -from_table |
| 3227 |
|
| --delete_flag, -df delete (with where_clause) from -to_table first |
| 3228 |
|
| --batchsize, -bs batchsize of deletes and bcp commit size |
| 3229 |
|
| --holdlock, -hl appends a holdlock to sql if -from_table |
| 3230 |
|
| --trim_whitespace, -tw trims whitepspace before bcp |
| 3231 |
|
| --app_name, -an set program_name in sysprocesses |
| 3232 |
|
| --echo echo sql commands that run under the covers |
| 3233 |
|
| --silent quiet mode |
| 3234 |
|
| --progress_log, -verbose print a log for every -batchsize |
| 3235 |
|
| --debug programmer debug mode. use with caution. |
| 3236 |
|
| --set_identity_insert, -sii use the identity values in the -source |
| 3237 |
|
| --packetsize, -ps packet size value. overrides target server config value. |
| 3238 |
|
| --textsize, -txs textsize value. overrides target server config value. |
| 3239 |
|
| |
| 3240 |
|
| |
| 3241 |
|
| --from_file_map, -ffm <string | hash ref | file> |
| 3242 |
|
| map table columns to field positions |
| 3243 |
|
| as string eg. '(cusip=>0, matdate=>2, name=>4)' |
| 3244 |
|
| |
| 3245 |
|
| --drop_and_recreate_indices, -dari [ <string | hash ref | file> ] |
| 3246 |
|
| drop indices pre-transfer; recreate post-transfer |
| 3247 |
|
| the value indicates to use MorganStanley's syts app. |
| 3248 |
|
| |
| 3249 |
|
| --truncate_flag,-tf [<string | hash ref | file> ] |
| 3250 |
|
| truncate -to_table first |
| 3251 |
|
| the value indicates to use MorganStanley's syts app. |
| 3252 |
|
| |
| 3253 |
|
| autodelete info |
| 3254 |
|
| --auto_delete delete only records to be inserted |
| 3255 |
|
| --scratch_db scratch_db to hold temp tables. default=tempdb |
| 3256 |
|
| --auto_delete_batchsize, -adb batchsize of autodelete |
| 3257 |
|
| |
| 3258 |
|
| error handling |
| 3259 |
|
| --error_handling, -eh continue | abort | retry - how to handle errors |
| 3260 |
|
| --error_data_file, -edf file containing data and error msg of rows not xferred |
| 3261 |
|
| --retry_max how many times to retry the batch |
| 3262 |
|
| --retry_verbose, -rv whether or not notification desired on server errors when retrying |
| 3263 |
|
| --retry_deadlock_sleep, -rds if deadlock server error (1205) how long to sleep between retries |
| 3264 |
|
| |
| 3265 |
|
| api only options |
| 3266 |
|
| --callback_err_send coderef code to call if error on bcp_sendrow |
| 3267 |
|
| --callback_err_batch coderef code to call if error on bcp_batch (commit) |
| 3268 |
|
| --callback_pre_send coderef code to call before row is sent to bcp_sendrow |
| 3269 |
|
| --callback_print coderef code to call when printing to stdout |
| 3270 |
|
| EOF |
| 3271 |
|
| |
| 3272 |
|
| warn $short_usage; |
| 3273 |
|
| return 1; |
| 3274 |
|
| } |
| 3275 |
|
| |
| 3276 |
|
| #----- |
| 3277 |
|
| #truncate table |
| 3278 |
|
| #----- |
| 3279 |
|
| sub sx_truncate_table { |
| 3280 |
|
| |
| 3281 |
|
| my $self = shift; |
| 3282 |
|
| |
| 3283 |
|
| #special sql to use 'syts' to truncate table |
| 3284 |
|
| my ( $user, $pwd, $db, $server ) = @{$self}{qw/to_user to_password sx_to_database to_server/}; |
| 3285 |
|
| my ($tab) = keys %{ $self->{sx_to_info}->{$db} }; |
| 3286 |
|
| $self->_print(" -truncate_flag option set, truncating target table\n") if ( $self->{progress_log} ); |
| 3287 |
|
| my $tv = $self->{truncate_flag}; |
| 3288 |
|
| |
| 3289 |
|
| my ( %trunc, $err, $hp ) = (); |
| 3290 |
|
| if ( $tv && $tv =~ /^.$/ ) { |
| 3291 |
|
| %trunc = ( syts => 0 ); |
| 3292 |
|
| } |
| 3293 |
|
| else { |
| 3294 |
|
| ( $hp, $err ) = sx_parse_user_hash($tv); |
| 3295 |
|
| %trunc = %$hp if ref($hp) eq "HASH"; |
| 3296 |
|
| } |
| 3297 |
|
| |
| 3298 |
|
| #check if necessary. |
| 3299 |
|
| sx_check_target( $self, 'truncate' ) || return 0; |
| 3300 |
|
| |
| 3301 |
|
| #some defaults |
| 3302 |
|
| $trunc{logfile} = '/dev/null' if $trunc{logfile} eq '0'; |
| 3303 |
|
| $trunc{logfile} = 'stdout' unless defined $trunc{logfile}; |
| 3304 |
|
| $trunc{sytsUser} = $user unless defined $trunc{sytsUser}; |
| 3305 |
|
| $trunc{sytsPwd} = $trunc{sytsUser} unless defined $trunc{sytsPwd}; |
| 3306 |
|
| |
| 3307 |
|
| #check for errors |
| 3308 |
|
| my @s = keys %trunc; |
| 3309 |
|
| if ( $err || @s != 4 || !defined $trunc{syts} || !defined $trunc{logfile} ) { |
| 3310 |
|
| sx_complain( "couldn't understand -truncate_flag.\n" |
| 3311 |
|
| . "Use syntax: -truncate_flag [ '(syts=>1, logfile=>filename), sytsUser=>User, sytsPwd=>Pwd' ] \n" ); |
| 3312 |
|
| return 1; |
| 3313 |
|
| } |
| 3314 |
|
| |
| 3315 |
|
| #truncate via syts |
| 3316 |
|
| if ( $trunc{syts} ) { |
| 3317 |
|
| my $out = $trunc{logfile} eq 'stdout' ? '' : ">$trunc{logfile}"; |
| 3318 |
|
| my $syts_cmd = qq/syts -T$server.$db -U$trunc{sytsUser} -P$trunc{sytsPwd}/; |
| 3319 |
|
| my $ksh_string = "$syts_cmd -a 'truncate table' -O '$tab' $out"; |
| 3320 |
|
| return sx_run_shell_cmd($ksh_string); |
| 3321 |
|
| |
| 3322 |
|
| #standard sql to truncate table |
| 3323 |
|
| } |
| 3324 |
|
| else { |
| 3325 |
|
| my $dbh_to = $self->{sx_dbh_to_non_bcp}; |
| 3326 |
|
| my $sql_string = "truncate table $tab"; |
| 3327 |
|
| my @status = @{ $self->_runsql( $dbh_to, $sql_string, [], undef ) }; |
| 3328 |
|
| |
| 3329 |
|
| #! my @status = $dbh_to_non_bcp->func($sql_string, [], undef, 'nsql' ); |
| 3330 |
|
| $DB_ERROR && sx_complain("$DB_ERROR\n") && return 1; |
| 3331 |
|
| return 0; |
| 3332 |
|
| |
| 3333 |
|
| } |
| 3334 |
|
| } |
| 3335 |
|
| |
| 3336 |
|
| #---- |
| 3337 |
|
| # |
| 3338 |
|
| #---- |
| 3339 |
|
| sub sx_check_target { |
| 3340 |
|
| |
| 3341 |
|
| my $self = shift; |
| 3342 |
|
| my $which = shift; |
| 3343 |
|
| |
| 3344 |
|
| my $dbh_to = $self->{sx_dbh_to_non_bcp}; |
| 3345 |
|
| my $zerosql = "select cnt=count(*) from $self->{to_table}"; |
| 3346 |
|
| my @res = @{ $self->_runsql( $dbh_to, $zerosql, {}, undef ) }; |
| 3347 |
|
| |
| 3348 |
|
| #! my @res = $self->{sx_dbh_to_non_bcp}->func($zerosql, {}, undef, 'nsql' ); |
| 3349 |
|
| $DB_ERROR && sx_complain("unable to get count on target table\n$DB_ERROR\n") && return 1; |
| 3350 |
|
| my $numrows = $res[0]->{cnt}; |
| 3351 |
|
| if ( $numrows > 0 ) { |
| 3352 |
|
| return 1; |
| 3353 |
|
| } |
| 3354 |
|
| else { |
| 3355 |
|
| $self->_print(" target table empty. skipping $which.\n") if $self->{progress_log}; |
| 3356 |
|
| return 0; |
| 3357 |
|
| } |
| 3358 |
|
| } |
| 3359 |
|
| |
| 3360 |
|
| #----------------------------------------------------------------------- |
| 3361 |
|
| #parse the --from_source value |
| 3362 |
|
| #----------------------------------------------------------------------- |
| 3363 |
|
| sub _parse_from_source { |
| 3364 |
|
| |
| 3365 |
|
| my ( $self, $opt ) = @_; |
| 3366 |
|
| |
| 3367 |
|
| #examples |
| 3368 |
|
| # |
| 3369 |
|
| #--from_source \ |
| 3370 |
|
| # |
| 3371 |
|
| # {source => dbd:Sybase:<server>:<user>:<pwd>, from => table=<tablename>, whereClause => wc, holdlock => 0/1 } |
| 3372 |
|
| # {source => dbd:Sybase:<server>:<user>:<pwd>, from => sql=<sql> } |
| 3373 |
|
| # {source => dbd:Sybase:<server>:<user>:<pwd>, from => file=<filename> } |
| 3374 |
|
| # |
| 3375 |
|
| # {source => dbd:DB2:<server>:<user>:<pwd>, from => table=<tablename>, whereClause => wc, holdlock => 0/1 } |
| 3376 |
|
| # {source => dbd:DB2:<server>:<user>:<pwd>, from => sql=<sql> } |
| 3377 |
|
| # {source => dbd:DB2:<server>:<user>:<pwd>, from => file=<filename> } |
| 3378 |
|
| # |
| 3379 |
|
| # {source => Perl, from => \&code } |
| 3380 |
|
| # {source => Perl, from => \*fh } |
| 3381 |
|
| # {source => Perl, from => \@array } |
| 3382 |
|
| # |
| 3383 |
|
| # {source => File, from => xxx.bcp } |
| 3384 |
|
| # |
| 3385 |
|
| # |
| 3386 |
|
| # |
| 3387 |
|
| #returns a dsn like string |
| 3388 |
|
| # |
| 3389 |
|
| # dbd.Sybase.<server>.<table> |
| 3390 |
|
| # dbd.Sybase.<server>.SQL.inline |
| 3391 |
|
| # dbd.Sybase.<server>.SQL.inFile.$fn |
| 3392 |
|
| # dbd.DB2.<server>.<table> |
| 3393 |
|
| # dbd.DB2.<server>.SQL.inline |
| 3394 |
|
| # dbd.DB2.<server>.SQL.inFile.$fn |
| 3395 |
|
| # Perl.CodeRef.<ref> |
| 3396 |
|
| # Perl.FHRef.<ref> |
| 3397 |
|
| # Perl.ArrayRef.<ref> |
| 3398 |
|
| # File.Name.<$fn> |
| 3399 |
|
| # Sql.Inline.<$sql> |
| 3400 |
|
| # Sql.File.Name.<$fn> |
| 3401 |
|
| |
| 3402 |
|
| #parse the value. verify options has already verified no parsing error |
| 3403 |
|
| my $dsn = (); |
| 3404 |
|
| if ( $opt->{from_source} ) { |
| 3405 |
|
| my ( $fs, $err ) = sx_parse_user_hash( $opt->{from_source}, '-from_source' ); |
| 3406 |
|
| |
| 3407 |
|
| #look for key 'source' |
| 3408 |
|
| my $pref = "err: --from_source."; |
| 3409 |
|
| sx_complain("unable to parse --from_source. see documentation.") && return 1 if $err; |
| 3410 |
|
| sx_complain("$pref 'source' must be specified") && return 1 if !$fs->{source}; |
| 3411 |
|
| sx_complain("$pref 'source' value error. Only dbd, Perl, and File are supported.") && return 1 |
| 3412 |
|
| if $fs->{source} !~ /^(dbd|Perl|File)/; |
| 3413 |
|
| |
| 3414 |
|
| #key source =~ ^dbd |
| 3415 |
|
| my @s = (); |
| 3416 |
|
| |
| 3417 |
|
| #DBD |
| 3418 |
|
| if ( $fs->{source} =~ /^dbd/i ) { |
| 3419 |
|
| |
| 3420 |
|
| my (@s) = split /[:=]/, $fs->{source}; |
| 3421 |
|
| |
| 3422 |
|
| #key from |
| 3423 |
|
| my ( $from, $fromValue ) = $fs->{from} =~ /(t|s|f)(?:\w*)[:=](.*)/i; |
| 3424 |
|
| $from = lc $from; |
| 3425 |
|
| if ( $fromValue =~ s/^\s*['"]// ) { $fromValue =~ s/['"]\s*$//; } |
| 3426 |
|
| my (@f) = split /[:=]/, $fromValue; |
| 3427 |
|
| |
| 3428 |
|
| #backward compatiblity |
| 3429 |
|
| $opt->{from_server} = $s[2]; |
| 3430 |
|
| $opt->{from_user} = $s[3]; |
| 3431 |
|
| $opt->{from_password} = $s[4]; |
| 3432 |
|
| $opt->{from_scriptName} = $s[5]; |
| 3433 |
|
| |
| 3434 |
|
| if ( $from eq 't' ) { |
| 3435 |
|
| $opt->{from_table} = $fromValue; |
| 3436 |
|
| $dsn = "$s[0].$s[1].$s[2].sql.table=$fromValue"; |
| 3437 |
|
| } |
| 3438 |
|
| elsif ( $from eq 's' ) { |
| 3439 |
|
| $opt->{from_sql} = $fromValue; |
| 3440 |
|
| $dsn = "$s[0].$s[1].$s[2].sql.inline=<not echoed>"; |
| 3441 |
|
| } |
| 3442 |
|
| elsif ( $from eq 'f' ) { |
| 3443 |
|
| $opt->{from_script} = $fromValue; |
| 3444 |
|
| $dsn = "$s[0].$s[1].$s[2].sql.inFile=$fromValue"; |
| 3445 |
|
| } |
| 3446 |
|
| |
| 3447 |
|
| #PERL |
| 3448 |
|
| } |
| 3449 |
|
| elsif ( $fs->{source} =~ /^Perl/i ) { |
| 3450 |
|
| $opt->{from_perl} = $fs->{from}; #arrayRef or codeRef |
| 3451 |
|
| if ( ref $fs->{from} eq 'CODE' ) { |
| 3452 |
|
| $dsn = "Perl.CodeRef.$fs->{from}"; |
| 3453 |
|
| } |
| 3454 |
|
| elsif ( ref $fs->{from} eq 'ARRAY' ) { |
| 3455 |
|
| $dsn = "Perl.ArrayRef.$fs->{from}"; |
| 3456 |
|
| } |
| 3457 |
|
| elsif ( ref $fs->{from} eq 'GLOB' ) { |
| 3458 |
|
| $dsn = "Perl.FHRef.$fs->{from}"; |
| 3459 |
|
| $opt->{from_file} = $opt->{from_perl}; |
| 3460 |
|
| delete $opt->{from_perl}; |
| 3461 |
|
| } |
| 3462 |
|
| else { |
| 3463 |
|
| sx_complain( |
| 3464 |
|
| "When -source is 'Perl', -from can only be codeRef, ArrayRef or FHref. bad value=<$fs->{source}>"); |
| 3465 |
|
| } |
| 3466 |
|
| |
| 3467 |
|
| #FILE |
| 3468 |
|
| } |
| 3469 |
|
| elsif ( $fs->{source} =~ /^File/i ) { |
| 3470 |
|
| $opt->{from_file} = $fs->{source}; |
| 3471 |
|
| $dsn = "Perl.File.$opt->{from_file}"; |
| 3472 |
|
| if ( ref $opt->{from_file} eq 'GLOB' ) { |
| 3473 |
|
| $opt->{from_perl} = $opt->{from_file}; |
| 3474 |
|
| delete $opt->{from_file}; |
| 3475 |
|
| $dsn = "Perl.CodeRef.$opt->{from_perl}"; |
| 3476 |
|
| } |
| 3477 |
|
| } |
| 3478 |
|
| return ( 0, $dsn ); |
| 3479 |
|
| |
| 3480 |
|
| #--- |
| 3481 |
|
| #backward compatibility. these options used with Sybase::Xfer |
| 3482 |
|
| #--- |
| 3483 |
|
| } |
| 3484 |
|
| elsif ( ref( $opt->{from_perl} ) eq 'CODE' ) { |
| 3485 |
|
| $dsn = "Perl.CodeRef.$opt->{from_perl}"; |
| 3486 |
|
| return ( 0, $dsn ); |
| 3487 |
|
| } |
| 3488 |
|
| elsif ( ref( $opt->{from_perl} ) eq 'ARRAY' ) { |
| 3489 |
|
| $dsn = "Perl.ArrayRef.$opt->{from_perl}"; |
| 3490 |
|
| return ( 0, $dsn ); |
| 3491 |
|
| } |
| 3492 |
|
| elsif ( $opt->{from_table} ) { |
| 3493 |
|
| $dsn = "dbd.Sybase.$opt->{from_server}.$opt->{from_table}"; |
| 3494 |
|
| return ( 0, $dsn ); |
| 3495 |
|
| } |
| 3496 |
|
| elsif ( $opt->{from_file} ) { |
| 3497 |
|
| $dsn = "File.Name.$opt->{from_file}"; |
| 3498 |
|
| return ( 0, $dsn ); |
| 3499 |
|
| |
| 3500 |
|
| } |
| 3501 |
|
| elsif ( $opt->{from_sql} ) { |
| 3502 |
|
| $dsn = "dbd.Sybase.$opt->{from_server}.sql.inline=<not echoed>"; |
| 3503 |
|
| return ( 0, $dsn ); |
| 3504 |
|
| |
| 3505 |
|
| } |
| 3506 |
|
| elsif ( $opt->{from_script} ) { |
| 3507 |
|
| $dsn = "dbd.Sybase.$opt->{from_server}.sql.inFile=$opt->{from_script}"; |
| 3508 |
|
| return ( 0, $dsn ); |
| 3509 |
|
| |
| 3510 |
|
| } |
| 3511 |
|
| return ( 0, undef ); |
| 3512 |
|
| |
| 3513 |
|
| } |
| 3514 |
|
| |
| 3515 |
|
| #----- |
| 3516 |
|
| # |
| 3517 |
|
| #----- |
| 3518 |
|
| sub sx_delete_rows { |
| 3519 |
|
| |
| 3520 |
|
| my $self = shift; |
| 3521 |
|
| |
| 3522 |
|
| my ( $dbh_to, $tab, $wc, $bs, $silent ) = @{$self}{qw/sx_dbh_to_non_bcp to_table where_clause batchsize silent/}; |
| 3523 |
|
| my ( $log, $echo ) = @{$self}{qw/progress_log echo/}; |
| 3524 |
|
| my $del_line = "delete $tab"; |
| 3525 |
|
| $del_line .= " where $wc" if $wc; |
| 3526 |
|
| |
| 3527 |
|
| my $sql_string = sx_delete_sql( $del_line, $bs, $silent ); |
| 3528 |
|
| $self->_print(" -delete_flag option set, deleting rows from target table\n") if $log; |
| 3529 |
|
| $self->_print(" $del_line (in a loop)\n") if $echo; |
| 3530 |
|
| |
| 3531 |
|
| #check if any rows in target; |
| 3532 |
|
| sx_check_target( $self, "delete" ) || return 0; |
| 3533 |
|
| |
| 3534 |
|
| my $a = $Sybase::CTlib::nsql_deadlock_retrycount; |
| 3535 |
|
| my $b = $Sybase::CTlib::nsql_deadlock_retrysleep; |
| 3536 |
|
| my $c = $Sybase::CTlib::nsql_deadlock_verbose; |
| 3537 |
|
| |
| 3538 |
|
| $Sybase::CTlib::nsql_deadlock_retrycount = 100; |
| 3539 |
|
| $Sybase::CTlib::nsql_deadlock_retrysleep = 60; |
| 3540 |
|
| $Sybase::CTlib::nsql_deadlock_verbose = 1; |
| 3541 |
|
| |
| 3542 |
|
| my @status = @{ $self->_runsql( $dbh_to, $sql_string, [], undef ) }; |
| 3543 |
|
| |
| 3544 |
|
| # my @status = $dbh_to_non_bcp->func($sql_string, [], undef, 'nsql' ); |
| 3545 |
|
| $DB_ERROR && sx_complain("$DB_ERROR\n") && return 1; |
| 3546 |
|
| |
| 3547 |
|
| $Sybase::CTlib::nsql_deadlock_retrycount = $a; |
| 3548 |
|
| $Sybase::CTlib::nsql_deadlock_retrysleep = $b; |
| 3549 |
|
| $Sybase::CTlib::nsql_deadlock_verbose = $c; |
| 3550 |
|
| |
| 3551 |
|
| return 0; |
| 3552 |
|
| } |
| 3553 |
|
| |
| 3554 |
|
| #----- |
| 3555 |
|
| # |
| 3556 |
|
| #----- |
| 3557 |
|
| sub _drop_indices { |
| 3558 |
|
| |
| 3559 |
|
| my $self = shift; |
| 3560 |
|
| |
| 3561 |
|
| #find the indices |
| 3562 |
|
| my $dbh = $self->{sx_dbh_to_non_bcp}; |
| 3563 |
|
| my $db = $self->{sx_to_database}; |
| 3564 |
|
| my $tab = $self->{sx_to_table}; |
| 3565 |
|
| $self->_drop_indices_setup($dbh, $db, $tab) && return 1; |
| 3566 |
|
| |
| 3567 |
|
| #log |
| 3568 |
|
| my ( $server, $tab, $log, $dbh_to ) = @{$self}{qw/to_server to_table progress_log sx_dbh_to_non_bcp/}; |
| 3569 |
|
| my @idx_names = @{ $self->{sx_to_table_indices}->{_names} }; |
| 3570 |
|
| local $" = ','; #so the array list separates with commas. |
| 3571 |
|
| if (@idx_names) { |
| 3572 |
|
| $self->_print(" -drop_and_recreate_indices option set, dropping indices (@idx_names) on target table\n") |
| 3573 |
|
| if $log; |
| 3574 |
|
| } |
| 3575 |
|
| else { |
| 3576 |
|
| $self->_print(" -drop_and_recreate_indices option set, no indices to drop on target table\n") if $log; |
| 3577 |
|
| } |
| 3578 |
|
| |
| 3579 |
|
| my %dari = %{ $self->{sx_drop_and_recreate_indices} }; |
| 3580 |
|
| |
| 3581 |
|
| #check if syts specified |
| 3582 |
|
| if ( $dari{syts} ) { |
| 3583 |
|
| my $out = $dari{logfile} eq 'stdout' ? '' : ">$dari{logfile}"; |
| 3584 |
|
| my $ksh_cmd = $self->{sx_to_table_indices}->{syts}->{drop} . " $out"; |
| 3585 |
|
| return sx_run_shell_cmd($ksh_cmd); |
| 3586 |
|
| |
| 3587 |
|
| } |
| 3588 |
|
| else { |
| 3589 |
|
| my $drop_sql = ''; |
| 3590 |
|
| for my $in (@idx_names) { |
| 3591 |
|
| $drop_sql .= $self->{sx_to_table_indices}->{$in}->{drop_sql} . "\n"; |
| 3592 |
|
| } |
| 3593 |
|
| |
| 3594 |
|
| if ($drop_sql) { |
| 3595 |
|
| $self->_runsql( $dbh_to, $drop_sql, undef, undef ); |
| 3596 |
|
| $DB_ERROR && sx_complain("$DB_ERROR\n") && return 1; |
| 3597 |
|
| } |
| 3598 |
|
| } |
| 3599 |
|
| return 0; |
| 3600 |
|
| } |
| 3601 |
|
| |
| 3602 |
|
| #---- |
| 3603 |
|
| # |
| 3604 |
|
| #---- |
| 3605 |
|
| sub _create_indices { |
| 3606 |
|
| |
| 3607 |
|
| my $self = shift; |
| 3608 |
|
| |
| 3609 |
|
| my @idx_names = @{ $self->{sx_to_table_indices}->{_names} }; |
| 3610 |
|
| if ( $self->{progress_log} ) { |
| 3611 |
|
| $self->_print("\n"); |
| 3612 |
|
| $self->_print("Post xfer processing\n"); |
| 3613 |
|
| local $" = ','; #so the array list separates with commas. |
| 3614 |
|
| $self->_print(" -drop_and_recreate_indices option set, creating indices (@idx_names) on target table\n"); |
| 3615 |
|
| } |
| 3616 |
|
| |
| 3617 |
|
| #this was setup in drop_indices |
| 3618 |
|
| my %dari = %{ $self->{sx_drop_and_recreate_indices} }; |
| 3619 |
|
| |
| 3620 |
|
| #syts |
| 3621 |
|
| if ( $dari{syts} ) { |
| 3622 |
|
| my $out = $dari{logfile} eq 'stdout' ? '' : ">>$dari{logfile}"; |
| 3623 |
|
| my $ksh_cmd = $self->{sx_to_table_indices}->{syts}->{create} . " $out"; |
| 3624 |
|
| return sx_run_shell_cmd($ksh_cmd); |
| 3625 |
|
| |
| 3626 |
|
| #not syts |
| 3627 |
|
| } |
| 3628 |
|
| else { |
| 3629 |
|
| my $dbh_to = $self->{sx_dbh_to_non_bcp}; |
| 3630 |
|
| for my $in (@idx_names) { |
| 3631 |
|
| my $sql = $self->{sx_to_table_indices}->{$in}->{create_sql}; |
| 3632 |
|
| $self->_print(" creating index $in : $sql\n") if $self->{progress_log}; |
| 3633 |
|
| $self->_runsql( $dbh_to, $sql, undef, undef ); |
| 3634 |
|
| $DB_ERROR && sx_complain("Create Index error:\n$DB_ERROR\n") && return 1; |
| 3635 |
|
| } |
| 3636 |
|
| } |
| 3637 |
|
| |
| 3638 |
|
| return 0; |
| 3639 |
|
| } |
| 3640 |
|
| |
| 3641 |
|
| #------ |
| 3642 |
|
| #run a shell script |
| 3643 |
|
| #------ |
| 3644 |
|
| sub sx_run_shell_cmd { |
| 3645 |
|
| return system( +shift ); |
| 3646 |
|
| } |
| 3647 |
|
| |
| 3648 |
|
| #================================================================ |
| 3649 |
|
| #================================================================ |
| 3650 |
|
| #================================================================ |
| 3651 |
|
| #================================================================ |
| 3652 |
|
| |
| 3653 |
|
| __END__ |