| 1 | package VSGDR::StaticData; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use List::Util qw(max); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use POSIX qw(strftime); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use English; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use Win32; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | |||
| 16 | ##TODO 1. Fix multi-column primary/unique keys. | ||
| 17 | ##TODO 2. Check that non-key identity columns are handled correctly when they occur in the final position in the table. | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | VSGDR::StaticData - Static data script support package for SSDT post-deployment steps, Ded MedVed. | ||
| 22 | |||
| 23 | =head1 VERSION | ||
| 24 | |||
| 25 | Version 0.39 | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | our $VERSION = '0.39'; | ||
| 30 | |||
| 31 | |||
| 32 | sub databaseName { | ||
| 33 | |||
| 34 | local $_ = undef ; | ||
| 35 | |||
| 36 | my $dbh = shift ; | ||
| 37 | |||
| 38 | my $sth2 = $dbh->prepare(databaseNameSQL()); | ||
| 39 | my $rs = $sth2->execute(); | ||
| 40 | my $res = $sth2->fetchall_arrayref() ; | ||
| 41 | |||
| 42 | return $$res[0][0] ; | ||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | sub databaseNameSQL { | ||
| 47 | |||
| 48 | return <<"EOF" ; | ||
| 49 | |||
| 50 | select db_name() | ||
| 51 | |||
| 52 | EOF | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | sub dependency { | ||
| 57 | |||
| 58 | local $_ = undef ; | ||
| 59 | |||
| 60 | my $dbh = shift ; | ||
| 61 | |||
| 62 | my $sth2 = $dbh->prepare( dependencySQL()); | ||
| 63 | my $rs = $sth2->execute(); | ||
| 64 | my $res = $sth2->fetchall_arrayref() ; | ||
| 65 | |||
| 66 | if ( scalar @{$res} ) { return $res ; } ; | ||
| 67 | return [] ; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | sub dependencySQL { | ||
| 73 | |||
| 74 | return <<"EOF" ; | ||
| 75 | select distinct | ||
| 76 | tc2.TABLE_CATALOG as to_CATALOG | ||
| 77 | , tc2.TABLE_SCHEMA as to_SCHEMA | ||
| 78 | , tc2.TABLE_NAME as to_NAME | ||
| 79 | , tc1.TABLE_CATALOG as from_CATALOG | ||
| 80 | , tc1.TABLE_SCHEMA as from_SCHEMA | ||
| 81 | , tc1.TABLE_NAME as from_NAME | ||
| 82 | , rc.CONSTRAINT_NAME as to_CONSTRAINT | ||
| 83 | from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc | ||
| 84 | join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc1 | ||
| 85 | on tc1.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA | ||
| 86 | and tc1.CONSTRAINT_CATALOG = rc.CONSTRAINT_CATALOG | ||
| 87 | and tc1.CONSTRAINT_NAME = rc.CONSTRAINT_NAME | ||
| 88 | join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 | ||
| 89 | on tc2.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA | ||
| 90 | and tc2.CONSTRAINT_CATALOG = rc.CONSTRAINT_CATALOG | ||
| 91 | and tc2.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME | ||
| 92 | |||
| 93 | EOF | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | sub generateScript { | ||
| 99 | |||
| 100 | my ${LargeDataSetThreshhold} = 30 ; | ||
| 101 | |||
| 102 | local $_ = undef; | ||
| 103 | |||
| 104 | my $dbh = shift ; | ||
| 105 | my $schema = shift ; | ||
| 106 | my $table = shift ; | ||
| 107 | |||
| 108 | croak "bad arg dbh" unless defined $dbh; | ||
| 109 | croak "bad arg schema" unless defined $schema; | ||
| 110 | croak "bad arg table" unless defined $table; | ||
| 111 | |||
| 112 | $schema = substr $schema, 1, -1 if $schema =~ m/\A \[ .+ \] \Z /msix; | ||
| 113 | $table = substr $table, 1, -1 if $table =~ m/\A \[ .+ \] \Z /msix; | ||
| 114 | my $combinedName = "${schema}.${table}"; | ||
| 115 | my $quotedCombinedName = "[${schema}].[${table}]"; | ||
| 116 | my $tableVarName = "LocalTable_${table}"; | ||
| 117 | |||
| 118 | my $quotedSchema = "[${schema}]"; | ||
| 119 | my $quotedTable = "[${table}]"; | ||
| 120 | |||
| 121 | my $database = databaseName($dbh); | ||
| 122 | |||
| 123 | no warnings; | ||
| 124 | my $userName = $OSNAME eq 'MSWin32' ? Win32::LoginName : ${[getpwuid( $< )]}->[6]; $userName =~ s/,.*//; | ||
| 125 | use warnings; | ||
| 126 | |||
| 127 | my $date = strftime "%d/%m/%Y", localtime; | ||
| 128 | |||
| 129 | my $hasId = has_idCols($dbh,$schema,$table) ; | ||
| 130 | my $idCol = undef ; | ||
| 131 | if ($hasId) { | ||
| 132 | $idCol = idCols($dbh,$schema,$table) ; | ||
| 133 | } | ||
| 134 | #warn Dumper $idCol ; | ||
| 135 | my $set_IDENTITY_INSERT_ON = ""; | ||
| 136 | my $set_IDENTITY_INSERT_OFF = ""; | ||
| 137 | $set_IDENTITY_INSERT_ON = "set IDENTITY_INSERT ${quotedCombinedName} ON" if $hasId; | ||
| 138 | $set_IDENTITY_INSERT_OFF = "set IDENTITY_INSERT ${quotedCombinedName} OFF" if $hasId; | ||
| 139 | |||
| 140 | |||
| 141 | my $ra_columns = columns($dbh,$schema,$table); | ||
| 142 | #warn Dumper $quotedSchema,$quotedTable ; | ||
| 143 | my $ra_pkcolumns = pkcolumns($dbh,$quotedSchema,$quotedTable); | ||
| 144 | |||
| 145 | croak "${quotedCombinedName} doesn't appear to be a valid table" unless scalar @{$ra_columns}; | ||
| 146 | |||
| 147 | #warn Dumper $ra_columns ; | ||
| 148 | #exit ; | ||
| 149 | |||
| 150 | # croak 'No Primary Key defined' unless scalar @{$ra_pkcolumns}; | ||
| 151 | # croak 'Unusable Primary Key defined' unless scalar @{$ra_pkcolumns} == 1; | ||
| 152 | |||
| 153 | my @IsColumnNumeric = map { $_->[1] =~ m{char|text|date}i ? 0 : 1 ; } @{$ra_columns} ; | ||
| 154 | #warn Dumper $ra_columns; | ||
| 155 | #warn Dumper @IsColumnNumeric ; | ||
| 156 | #exit; | ||
| 157 | |||
| 158 | my $primaryKeyCheckClause = ""; | ||
| 159 | my $pk_column = undef ; #$ra_pkcolumns->[0][0]; | ||
| 160 | #my @nonKeyColumns = grep { $_->[0][0] ne $pk_column } @{$ra_columns}; | ||
| 161 | |||
| 162 | my @nonKeyColumns = () ; | ||
| 163 | |||
| 164 | |||
| 165 | my $widest_column_name_len = max ( map { length ($_->[0]); } @{$ra_columns} ) ; | ||
| 166 | my $widest_column_name_padding = int($widest_column_name_len/4) + 4; | ||
| 167 | |||
| 168 | my $flatcolumnlist = "" ; | ||
| 169 | my $flatvariablelist = "" ; | ||
| 170 | foreach my $l (@{$ra_columns}) { | ||
| 171 | do { local $" = ""; $flatcolumnlist .= "[$l->[0]]" ; $flatcolumnlist .= ", "} ; | ||
| 172 | do { local $" = ""; $flatvariablelist .= "@"."$l->[0]" ; $flatvariablelist .= ","} ; | ||
| 173 | } | ||
| 174 | $flatcolumnlist =~ s{ ,\s? \z }{}msx; | ||
| 175 | $flatvariablelist =~ s{ ,\s? \z }{}msx; | ||
| 176 | |||
| 177 | |||
| 178 | #warn Dumper @{$ra_pkcolumns} ; | ||
| 179 | |||
| 180 | my $reportingPKCols = "" ; | ||
| 181 | my $recordExistenceCheckSQL = "" ; | ||
| 182 | if ( ! scalar @{$ra_pkcolumns} ) { | ||
| 183 | my @pk_ColumnsCheck = () ; | ||
| 184 | foreach my $l (@{$ra_columns}) { | ||
| 185 | my $varlen = length($l->[0]) ; | ||
| 186 | my $colpadding = $widest_column_name_padding - (int(($varlen)/4)); | ||
| 187 | my $varpadding = $widest_column_name_padding - (int(($varlen+1)/4)); | ||
| 188 | push @pk_ColumnsCheck , "([$l->[0]]" . "\t"x$varpadding . " = \@$l->[0]" . "\t"x$varpadding . "or ([$l->[0]]". "\t"x$varpadding . " is null and \@$l->[0] ". "\t"x$varpadding . " is null ) ) " ; | ||
| 189 | } | ||
| 190 | #my @pk_ColumnsCheck = map { "( $_->[0]\t\t\t = \@$_->[0] or ( $_->[0]\t\t\t is null and \@$_->[0] is null ) ) " } @{$ra_columns} ; | ||
| 191 | $primaryKeyCheckClause = "where\t" . do { local $" = "\n\t\t\t\tand\t\t"; "@pk_ColumnsCheck" }; | ||
| 192 | |||
| 193 | $recordExistenceCheckSQL = <<"EOF"; | ||
| 194 | if exists | ||
| 195 | ( | ||
| 196 | select $flatvariablelist | ||
| 197 | except | ||
| 198 | select ${flatcolumnlist} | ||
| 199 | from ${quotedCombinedName} | ||
| 200 | ) | ||
| 201 | EOF | ||
| 202 | |||
| 203 | } | ||
| 204 | elsif ( scalar @{$ra_pkcolumns} != 1 ) { | ||
| 205 | |||
| 206 | my @pk_ColumnsCheck = () ; | ||
| 207 | |||
| 208 | foreach my $l (@{$ra_pkcolumns}) { | ||
| 209 | my $varlen = length($l->[0]) ; | ||
| 210 | my $colpadding = $widest_column_name_padding - (int(($varlen)/4)); | ||
| 211 | my $varpadding = $widest_column_name_padding - (int(($varlen+1)/4)); | ||
| 212 | push @pk_ColumnsCheck , "([$l->[0]]" . "\t"x$varpadding . " = \@$l->[0]" . "\t"x$varpadding . "or ([$l->[0]]". "\t"x$varpadding . " is null and \@$l->[0] ". "\t"x$varpadding . " is null ) ) " ; | ||
| 213 | |||
| 214 | my @reportingPKCols = map { "$_->[0]" } @{$ra_columns} ; | ||
| 215 | $reportingPKCols = do {local $" = ", "; "@reportingPKCols"} ; | ||
| 216 | |||
| 217 | # my @pk_ColumnsCheck = map { "$_->[0]\t\t\t = \@$_->[0]" } @{$ra_columns} ; | ||
| 218 | } | ||
| 219 | $primaryKeyCheckClause = "where\t" . do { local $" = "\n\t\t\t\tand\t\t"; "@pk_ColumnsCheck" } ; | ||
| 220 | |||
| 221 | foreach my $col (@{$ra_columns}) { | ||
| 222 | #warn Dumper @{$ra_columns}; | ||
| 223 | #warn Dumper $col; | ||
| 224 | push @nonKeyColumns, $col unless grep {$_->[0] eq $col->[0] } @{$ra_pkcolumns} ; | ||
| 225 | } | ||
| 226 | $recordExistenceCheckSQL = <<"EOF"; | ||
| 227 | if not exists | ||
| 228 | ( | ||
| 229 | select * | ||
| 230 | from ${quotedCombinedName} | ||
| 231 | ${primaryKeyCheckClause} | ||
| 232 | ) | ||
| 233 | EOF | ||
| 234 | } | ||
| 235 | else { | ||
| 236 | $reportingPKCols = $ra_pkcolumns->[0][0]; | ||
| 237 | $pk_column = $ra_pkcolumns->[0][0]; | ||
| 238 | $primaryKeyCheckClause = "where ${pk_column} = \@${pk_column}"; | ||
| 239 | @nonKeyColumns = grep { $_->[0] ne $pk_column } @{$ra_columns}; | ||
| 240 | |||
| 241 | $recordExistenceCheckSQL = <<"EOF"; | ||
| 242 | if not exists | ||
| 243 | ( | ||
| 244 | select ${flatcolumnlist} | ||
| 245 | from ${quotedCombinedName} | ||
| 246 | ${primaryKeyCheckClause} | ||
| 247 | ) | ||
| 248 | EOF | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | my $variabledeclaration = "declare\t" ; | ||
| 253 | my $tabledeclaration = "(\t\tStaticDataPopulationId\t\tint\tnot null identity(1,1)\n\t,\t\t" ; | ||
| 254 | my $selectstatement = "select\t" ; | ||
| 255 | my $insertclause = "insert into ${combinedName}\n\t\t\t\t\t\t("; | ||
| 256 | my $valuesclause = "values("; | ||
| 257 | # my $flatcolumnlist = "" ; | ||
| 258 | my $flatExtractColumnList = "" ; | ||
| 259 | # my $flatvariablelist = "" ; | ||
| 260 | my $updateColumns = "set\t"; | ||
| 261 | my $printStatement = "" ; | ||
| 262 | |||
| 263 | #warn Dumper $widest_column_name_len; | ||
| 264 | #warn Dumper $widest_column_name_padding; | ||
| 265 | #warn Dumper @{$ra_columns}; | ||
| 266 | foreach my $l (@{$ra_columns}) { | ||
| 267 | my $varlen = length($l->[0]) ; | ||
| 268 | my $colpadding = $widest_column_name_padding - (int(($varlen)/4)); | ||
| 269 | my $varpadding = $widest_column_name_padding - (int(($varlen+1)/4)); | ||
| 270 | #warn Dumper $l->[0]; | ||
| 271 | #warn Dumper $varlen; | ||
| 272 | #warn Dumper $padding; | ||
| 273 | #warn Dumper $variabledeclaration; | ||
| 274 | #warn Dumper $varpadding; | ||
| 275 | # do { local $" = "\t"; $variabledeclaration .= "@"."@{$l}[0,1,2,3,5]" ; $variabledeclaration .= "\n\t,\t\t"} ; | ||
| 276 | do { local $" = "\t"; $variabledeclaration .= "@"."@{$l}[0]". "\t"x$varpadding . "$$l[1]" ."@{$l}[2,3]" ; $variabledeclaration .= "\n\t,\t\t"} ; | ||
| 277 | # do { local $" = "\t"; $tabledeclaration .= "@{$l}" ; $tabledeclaration .= "\n\t\t,\t"} ; | ||
| 278 | do { local $" = "\t"; $tabledeclaration .= "[@{$l}[0]]". "\t"x$colpadding . "[$$l[1]]" ."@{$l}[2,3,4,5]" ; $tabledeclaration .= "\n\t,\t\t"} ; | ||
| 279 | # do { local $" = ""; $selectstatement .= "@"."$l->[0]\t\t= $l->[0]" ; $selectstatement .= "\n\t\t,\t\t"} ; | ||
| 280 | do { local $" = ""; $selectstatement .= "@"."$l->[0]" . "\t"x$varpadding ."= [$l->[0]]" ; $selectstatement .= "\n\t\t,\t\t"} ; | ||
| 281 | do { local $" = ""; $insertclause .= "[$l->[0]]" ; $insertclause .= ", "} ; | ||
| 282 | do { local $" = ""; $valuesclause .= "[$l->[0]]" ; $valuesclause .= ", "} ; | ||
| 283 | # do { local $" = ""; $flatcolumnlist .= "[$l->[0]]" ; $flatcolumnlist .= ", "} ; | ||
| 284 | do { local $" = ""; $flatExtractColumnList .= $l->[1] =~ m{\A(?:date|datetime[2]?|smalldatetime)\z}i ? "convert(varchar(30),[$l->[0]],120)" : "[$l->[0]]" ; $flatExtractColumnList .= ", "} ; | ||
| 285 | # do { local $" = ""; $flatvariablelist .= "@"."$l->[0]" ; $flatvariablelist .= ","} ; | ||
| 286 | |||
| 287 | do { local $" = ""; $printStatement .= "' $$l[0]: ' " ; } ; | ||
| 288 | my $printFragment = $$l[1] !~ m{ (?: char ) }ixms | ||
| 289 | ? "cast( \@$$l[0] as varchar(128))" | ||
| 290 | : "\@$$l[0]" ; | ||
| 291 | |||
| 292 | $printFragment = " + case when ${printFragment} is null then 'NULL' else '''' + ${printFragment} + '''' end + " ; ; | ||
| 293 | $printStatement .= $printFragment ; | ||
| 294 | } | ||
| 295 | foreach my $l (@nonKeyColumns) { | ||
| 296 | # create update statement for each non-identity column. | ||
| 297 | if ( ! $hasId || ( $l->[0] ne $idCol ) ) { | ||
| 298 | #warn Dumper $l; | ||
| 299 | do { local $" = ""; $updateColumns .= "$l->[0]\t\t= "."@"."$l->[0]" ; $updateColumns .= "\n\t\t\t\t\t,\t"} ; | ||
| 300 | } | ||
| 301 | elsif($hasId) { | ||
| 302 | do { local $" = ""; $updateColumns .= "/* cannot update this identity column -- $l->[0]\t\t= "."@"."$l->[0]" ; $updateColumns .= "\n\t\t\t\t\t,*/\t"} ; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | # trim off erroneous trailing cruft - better to resign array interpolations above . | ||
| 308 | $variabledeclaration =~ s{ \n\t,\t\t \z }{}msx; | ||
| 309 | $tabledeclaration =~ s{ \n\t,\t\t \z }{}msx; | ||
| 310 | $selectstatement =~ s{ \n\t\t,\t\t \z }{}msx; | ||
| 311 | $updateColumns =~ s{ \n\t\t\t,\t \z }{}msx; | ||
| 312 | $insertclause =~ s{ ,\s? \z }{}msx; | ||
| 313 | $valuesclause =~ s{ ,\s? \z }{}msx; | ||
| 314 | # $flatcolumnlist =~ s{ ,\s? \z }{}msx; | ||
| 315 | $flatExtractColumnList =~ s{ ,\s? \z }{}msx; | ||
| 316 | # $flatvariablelist =~ s{ ,\s? \z }{}msx; | ||
| 317 | $updateColumns =~ s{ \n\t\t\t\t\t,\t \z }{}msx; | ||
| 318 | $printStatement =~ s{ \+\s \z }{}msx; | ||
| 319 | |||
| 320 | |||
| 321 | $tabledeclaration .= "\n\t)"; | ||
| 322 | $insertclause .= ")"; | ||
| 323 | $valuesclause .= ")"; | ||
| 324 | |||
| 325 | my $insertingPrintStatement = "print 'Inserting ${combinedName}:' + " . $printStatement ; | ||
| 326 | my $updatingPrintStatement = "print 'Updating ${combinedName}: ' + " . $printStatement; | ||
| 327 | |||
| 328 | |||
| 329 | # my $ra_data = getCurrentTableData($dbh,$combinedName,$pk_column,$flatcolumnlist); | ||
| 330 | # my $ra_data = getCurrentTableData($dbh,$quotedCombinedName ,$pk_column,$flatcolumnlist); | ||
| 331 | my $ra_data = getCurrentTableData($dbh,$quotedCombinedName ,$pk_column,$flatExtractColumnList); | ||
| 332 | |||
| 333 | my @valuesTable ; | ||
| 334 | my $valuesClause = "values\n\t\t\t"; | ||
| 335 | |||
| 336 | my $lno = 1; | ||
| 337 | foreach my $ra_row (@{$ra_data}){ | ||
| 338 | # warn Dumper $ra_row; | ||
| 339 | my @outVals = undef ; | ||
| 340 | #warn Dumper @{$ra_row} ; | ||
| 341 | #exit; | ||
| 342 | for ( my $i = 0; $i < scalar @{$ra_row}; $i++ ) { | ||
| 343 | #warn Dumper $ra_row->[$i] ; | ||
| 344 | #warn Dumper $IsColumnNumeric[$i] ; | ||
| 345 | # $ra_row->[$i] = ( defined $ra_row->[$i] ) ? $ra_row->[$i] : "null" ; | ||
| 346 | |||
| 347 | if ( ( $IsColumnNumeric[$i] == 1 ) and ( not ( defined ($ra_row->[$i]) ) ) ) { | ||
| 348 | $outVals[$i] = 'null' ; | ||
| 349 | } | ||
| 350 | if ( ( $IsColumnNumeric[$i] == 0 ) and ( not ( defined ($ra_row->[$i]) ) ) ) { | ||
| 351 | $outVals[$i] = 'null' ; | ||
| 352 | } | ||
| 353 | if ( ( $IsColumnNumeric[$i] == 1 ) and ( ( defined ($ra_row->[$i]) ) ) ) { | ||
| 354 | $outVals[$i] = $ra_row->[$i] ; | ||
| 355 | } | ||
| 356 | if ( ( $IsColumnNumeric[$i] == 0 ) and ( ( defined ($ra_row->[$i]) ) ) ) { | ||
| 357 | if (${$ra_columns}[$i][1] =~ m{\A(?:date|datetime[2]?|smalldatetime)\z}i) { | ||
| 358 | $outVals[$i] = "convert(". ${$ra_columns}[$i][1] ."," . $dbh->quote($ra_row->[$i]) . ",120)" ; | ||
| 359 | } | ||
| 360 | else { | ||
| 361 | $outVals[$i] = $dbh->quote($ra_row->[$i]) ; | ||
| 362 | } | ||
| 363 | } | ||
| 364 | } | ||
| 365 | push @valuesTable, \@outVals ; | ||
| 366 | #my @outVals = map { $ColumnNumericity{$_} == 1 ? $_ : $dbh->quote($_) } @{$ra_row}; | ||
| 367 | my $line = do{ local $" = ", "; "@outVals" } ; | ||
| 368 | #$valuesClause .= "(\t" . $line . ")" . "\n\t\t,\t" ; | ||
| 369 | $lno++; | ||
| 370 | } | ||
| 371 | |||
| 372 | my @maxWidth; | ||
| 373 | my $maxCol; | ||
| 374 | |||
| 375 | if ( scalar @valuesTable ) { | ||
| 376 | my @tmp = @{$valuesTable[0]}; | ||
| 377 | $maxCol = scalar @tmp -1 ; | ||
| 378 | for ( my $i = 0; $i <= $maxCol; $i++ ) { | ||
| 379 | push @maxWidth, 0; | ||
| 380 | } | ||
| 381 | for ( my $i = 0; $i < scalar @valuesTable; $i++ ) { | ||
| 382 | my @tmp = @{$valuesTable[$i]}; | ||
| 383 | for ( my $i = 0; $i <= $maxCol; $i++ ) { | ||
| 384 | if (length($tmp[$i]) > $maxWidth[$i] ) { | ||
| 385 | $maxWidth[$i] = length($tmp[$i]) ; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | #warn Dumper @maxWidth ; | ||
| 392 | |||
| 393 | for ( my $i = 0; $i < scalar @valuesTable; $i++ ) { | ||
| 394 | my @tmp = @{$valuesTable[$i]}; | ||
| 395 | my $line = ""; | ||
| 396 | for ( my $j = 0; $j <= $maxCol; $j++ ) { | ||
| 397 | my $val = $tmp[$j]; | ||
| 398 | my $valWidth = length($val); | ||
| 399 | my $PadLength = $maxWidth[$j]-$valWidth; | ||
| 400 | my $padding = " "x$PadLength; | ||
| 401 | $line .= ", ${padding}${val}"; | ||
| 402 | } | ||
| 403 | $line =~ s{ ^,\s}{}msx; | ||
| 404 | $valuesClause .= "(\t" . $line . ")" . "\n\t\t,\t" ; | ||
| 405 | } | ||
| 406 | |||
| 407 | $valuesClause =~ s{ \n\t\t,\t \z }{}msx; | ||
| 408 | |||
| 409 | |||
| 410 | my $noopPrintStatement = "'Nothing to update. Values are unchanged.'"; | ||
| 411 | my $printNoOpStatement = "print ${noopPrintStatement}" ; | ||
| 412 | if ( ${pk_column} ) { | ||
| 413 | $noopPrintStatement = "'Nothing to update. ${combinedName}: Values are unchanged for Primary/Unique Key: '"; | ||
| 414 | $printNoOpStatement = "print ${noopPrintStatement} + cast(\@${reportingPKCols} as varchar(1000)) " | ||
| 415 | } | ||
| 416 | |||
| 417 | my $elsePrintSection = <<"EOF"; | ||
| 418 | else begin | ||
| 419 | ${printNoOpStatement} | ||
| 420 | end | ||
| 421 | EOF | ||
| 422 | |||
| 423 | if ( scalar @{$ra_data} > ${LargeDataSetThreshhold} ){ | ||
| 424 | $insertingPrintStatement = "" ; | ||
| 425 | $updatingPrintStatement = "" ; | ||
| 426 | $elsePrintSection = "" ; | ||
| 427 | } | ||
| 428 | |||
| 429 | my ${elseBlock} = ""; | ||
| 430 | |||
| 431 | if ( scalar @nonKeyColumns ) { | ||
| 432 | ${elseBlock} = <<"EOF"; | ||
| 433 | |||
| 434 | -- if the static data doesn''t match what is already there then update it. | ||
| 435 | -- 'except' handily handles null as equal. Saves some extensive twisted logic. | ||
| 436 | else begin | ||
| 437 | if exists | ||
| 438 | ( | ||
| 439 | select ${flatcolumnlist} | ||
| 440 | from $quotedCombinedName | ||
| 441 | ${primaryKeyCheckClause} | ||
| 442 | except | ||
| 443 | select ${flatvariablelist} | ||
| 444 | ) begin | ||
| 445 | $updatingPrintStatement | ||
| 446 | if \@DeploySwitch = 1 begin | ||
| 447 | update s | ||
| 448 | ${updateColumns} | ||
| 449 | from $quotedCombinedName s | ||
| 450 | ${primaryKeyCheckClause} | ||
| 451 | end | ||
| 452 | set \@ChangedCount += 1 ; | ||
| 453 | end | ||
| 454 | ${elsePrintSection} | ||
| 455 | end | ||
| 456 | EOF | ||
| 457 | } | ||
| 458 | |||
| 459 | |||
| 460 | my $tmp_sv = substr(${table},0,20) ; | ||
| 461 | my $savePointName = "sc_${tmp_sv}_SP"; | ||
| 462 | |||
| 463 | my ${printChangedTotalsSection} = "" ; | ||
| 464 | #warn Dumper @nonKeyColumns ; | ||
| 465 | |||
| 466 | if ( scalar @{$ra_data} > ${LargeDataSetThreshhold} ){ | ||
| 467 | $printChangedTotalsSection = "print 'Total count of inserted records : ' + cast( \@InsertedCount as varchar(10))" ; | ||
| 468 | $printChangedTotalsSection .= "\n\tprint 'Total count of altered records : ' + cast( \@ChangedCount as varchar(10))" ; | ||
| 469 | } | ||
| 470 | |||
| 471 | |||
| 472 | return <<"EOF"; | ||
| 473 | |||
| 474 | /**************************************************************************************** | ||
| 475 | * Database: ${database} | ||
| 476 | * Author : ${userName} | ||
| 477 | * Date : ${date} | ||
| 478 | * Purpose : Static data deployment script for ${combinedName} | ||
| 479 | * | ||
| 480 | * | ||
| 481 | * Version History | ||
| 482 | * --------------- | ||
| 483 | * 1.0.0 ${date} ${userName} | ||
| 484 | * Created. | ||
| 485 | ***************************************************************************************/ | ||
| 486 | |||
| 487 | set nocount on | ||
| 488 | |||
| 489 | declare \@DeployCmd varchar(20) | ||
| 490 | ,\@DeploySwitch bit | ||
| 491 | |||
| 492 | set \@DeployCmd = '\$(StaticDataDeploy)' | ||
| 493 | set \@DeploySwitch = 0 | ||
| 494 | --Check whether a deploy has been stated. | ||
| 495 | if isnull(upper(\@DeployCmd) , '') <> 'DEPLOY' | ||
| 496 | begin | ||
| 497 | set \@DeploySwitch = 0 --FALSE, only run a dummy deploy where no actual data will be modified. | ||
| 498 | print 'Deploy Type: Dummy Deploy (No data will be changed)' | ||
| 499 | end | ||
| 500 | else | ||
| 501 | begin | ||
| 502 | set \@DeploySwitch = 1 --TRUE, run real deploy. | ||
| 503 | print 'Deploy Type: Actual Deploy' | ||
| 504 | end | ||
| 505 | |||
| 506 | |||
| 507 | |||
| 508 | begin try | ||
| 509 | |||
| 510 | -- Declarations | ||
| 511 | declare \@ct int | ||
| 512 | , \@i int | ||
| 513 | , \@InsertedCount int = 0 | ||
| 514 | , \@ChangedCount int = 0 | ||
| 515 | |||
| 516 | declare \@localTransactionStarted bit; | ||
| 517 | |||
| 518 | |||
| 519 | begin transaction | ||
| 520 | save transaction ${savePointName} ; | ||
| 521 | |||
| 522 | set \@localTransactionStarted = 1; | ||
| 523 | |||
| 524 | declare \@${tableVarName} table | ||
| 525 | ${tabledeclaration} | ||
| 526 | |||
| 527 | ; with src as | ||
| 528 | ( | ||
| 529 | select * | ||
| 530 | from ( ${valuesClause} | ||
| 531 | ) AS vtable | ||
| 532 | ( $flatcolumnlist) | ||
| 533 | ) | ||
| 534 | insert into | ||
| 535 | \@${tableVarName} | ||
| 536 | ( ${flatcolumnlist} | ||
| 537 | ) | ||
| 538 | select ${flatcolumnlist} | ||
| 539 | from src | ||
| 540 | |||
| 541 | ${variabledeclaration} | ||
| 542 | |||
| 543 | |||
| 544 | |||
| 545 | |||
| 546 | -- count how many records need to be inserted | ||
| 547 | select \@ct = count(*) from \@${tableVarName} | ||
| 548 | |||
| 549 | set \@i = 1 | ||
| 550 | -- insert the records into the ${table} table if they don't already exist, otherwise update them | ||
| 551 | while \@i <=\@ct begin | ||
| 552 | |||
| 553 | ${selectstatement} | ||
| 554 | from \@${tableVarName} | ||
| 555 | where StaticDataPopulationId\t\t= \@i | ||
| 556 | |||
| 557 | ${recordExistenceCheckSQL} | ||
| 558 | begin | ||
| 559 | $insertingPrintStatement | ||
| 560 | if \@DeploySwitch = 1 begin | ||
| 561 | ${set_IDENTITY_INSERT_ON} | ||
| 562 | ${insertclause} | ||
| 563 | values (${flatvariablelist}) | ||
| 564 | ${set_IDENTITY_INSERT_OFF} | ||
| 565 | end | ||
| 566 | set \@InsertedCount += 1 ; | ||
| 567 | end | ||
| 568 | ${elseBlock} | ||
| 569 | |||
| 570 | set \@i=\@i+1 | ||
| 571 | end | ||
| 572 | |||
| 573 | commit | ||
| 574 | |||
| 575 | |||
| 576 | ${printChangedTotalsSection} | ||
| 577 | |||
| 578 | end try | ||
| 579 | begin catch | ||
| 580 | |||
| 581 | -- if xact_state() = -1 then the transaction isn't recorded in \@\@trancount so check both | ||
| 582 | if \@\@trancount > 0 or xact_state() = -1 begin | ||
| 583 | if xact_state() = -1 | ||
| 584 | rollback; -- we can't do anything else | ||
| 585 | -- Rollback any locally begun transaction to savepoint. Don't fail the whole deployment if it's transactional. | ||
| 586 | -- If our transaction is the only one, then just rollback. | ||
| 587 | if \@\@trancount > 1 begin | ||
| 588 | if \@localTransactionStarted is not null and \@localTransactionStarted = 1 begin | ||
| 589 | rollback transaction ${savePointName}; | ||
| 590 | commit ; -- windback our local transaction completely | ||
| 591 | end; | ||
| 592 | --else it's probably nothing to do with us. but we have to rollback anyway | ||
| 593 | else | ||
| 594 | rollback; | ||
| 595 | end | ||
| 596 | else begin | ||
| 597 | -- final check to see if we need to unwind the transaction | ||
| 598 | if \@\@trancount > 0 | ||
| 599 | rollback; | ||
| 600 | end; | ||
| 601 | end; | ||
| 602 | |||
| 603 | print 'Deployment of ${combinedName} static data failed. This script was rolled back.'; | ||
| 604 | print error_message(); | ||
| 605 | |||
| 606 | ${set_IDENTITY_INSERT_OFF}; | ||
| 607 | |||
| 608 | |||
| 609 | end catch | ||
| 610 | |||
| 611 | go | ||
| 612 | |||
| 613 | |||
| 614 | EOF | ||
| 615 | |||
| 616 | } | ||
| 617 | |||
| 618 | sub generateTestDataScript { | ||
| 619 | |||
| 620 | local $_ = undef; | ||
| 621 | |||
| 622 | my $dbh = shift ; | ||
| 623 | my $schema = shift ; | ||
| 624 | my $table = shift ; | ||
| 625 | my $sql = shift ; | ||
| 626 | my $use_MinimalForm = shift ; | ||
| 627 | |||
| 628 | croak "bad arg dbh" unless defined $dbh; | ||
| 629 | croak "bad arg schema" unless defined $schema; | ||
| 630 | croak "bad arg table" unless defined $table; | ||
| 631 | croak "bad arg sql" unless defined $sql; | ||
| 632 | croak "bad arg minimal form" unless defined $use_MinimalForm; | ||
| 633 | |||
| 634 | $schema = substr $schema, 1, -1 if $schema =~ m/\A \[ .+ \] \Z /msix; | ||
| 635 | $table = substr $table, 1, -1 if $table =~ m/\A \[ .+ \] \Z /msix; | ||
| 636 | my $combinedName = "${schema}.${table}"; | ||
| 637 | my $quotedCombinedName = "[${schema}].[${table}]"; | ||
| 638 | |||
| 639 | my $quotedSchema = "[${schema}]"; | ||
| 640 | my $quotedTable = "[${table}]"; | ||
| 641 | |||
| 642 | my $database = databaseName($dbh); | ||
| 643 | |||
| 644 | my $hasId = has_idCols($dbh,$schema,$table) ; | ||
| 645 | my $idCol = undef ; | ||
| 646 | if ($hasId) { | ||
| 647 | $idCol = idCols($dbh,$schema,$table) ; | ||
| 648 | } | ||
| 649 | #warn Dumper $idCol ; | ||
| 650 | my $set_IDENTITY_INSERT_ON = ""; | ||
| 651 | my $set_IDENTITY_INSERT_OFF = ""; | ||
| 652 | $set_IDENTITY_INSERT_ON = "set IDENTITY_INSERT ${quotedCombinedName} ON" if $hasId; | ||
| 653 | $set_IDENTITY_INSERT_OFF = "set IDENTITY_INSERT ${quotedCombinedName} OFF" if $hasId; | ||
| 654 | |||
| 655 | |||
| 656 | my $ra_columns = columns($dbh,$schema,$table); | ||
| 657 | croak "${quotedCombinedName} doesn't appear to be a valid table" unless scalar @{$ra_columns}; | ||
| 658 | |||
| 659 | |||
| 660 | my @IsColumnNumeric = map { $_->[1] =~ m{char|text|date}i ? 0 : 1 ; } @{$ra_columns} ; | ||
| 661 | my @nonKeyColumns = () ; | ||
| 662 | |||
| 663 | my $widest_column_name_len = max ( map { length ($_->[0]); } @{$ra_columns} ) ; | ||
| 664 | my $widest_column_name_padding = int($widest_column_name_len/4) + 4; | ||
| 665 | |||
| 666 | my $flatvariablelist = "" ; | ||
| 667 | foreach my $l (@{$ra_columns}) { | ||
| 668 | do { local $" = ""; $flatvariablelist .= "@"."$l->[0]" ; $flatvariablelist .= ","} ; | ||
| 669 | } | ||
| 670 | $flatvariablelist =~ s{ ,\s? \z }{}msx; | ||
| 671 | |||
| 672 | foreach my $l (@{$ra_columns}) { | ||
| 673 | my $varlen = length($l->[0]) ; | ||
| 674 | my $colpadding = $widest_column_name_padding - (int(($varlen)/4)); | ||
| 675 | my $varpadding = $widest_column_name_padding - (int(($varlen+1)/4)); | ||
| 676 | } | ||
| 677 | |||
| 678 | my $flatExtractColumnList = "" ; | ||
| 679 | |||
| 680 | foreach my $l (@{$ra_columns}) { | ||
| 681 | my $varlen = length($l->[0]) ; | ||
| 682 | my $colpadding = $widest_column_name_padding - (int(($varlen)/4)); | ||
| 683 | my $varpadding = $widest_column_name_padding - (int(($varlen+1)/4)); | ||
| 684 | do { local $" = ""; $flatExtractColumnList .= $l->[1] =~ m{\A(?:date|datetime[2]?|smalldatetime)\z}i ? "convert(varchar(30),[$l->[0]],120)" : "[$l->[0]]" ; $flatExtractColumnList .= ", "} ; | ||
| 685 | |||
| 686 | } | ||
| 687 | |||
| 688 | $flatExtractColumnList =~ s{ ,\s? \z }{}msx; | ||
| 689 | |||
| 690 | my $ra_metadata = describeTestDataForTable($dbh,$sql); | ||
| 691 | my @cols = map { $$_[0] } @$ra_metadata ; | ||
| 692 | my $colList = do { local $" = "," ; "@cols" } ; | ||
| 693 | |||
| 694 | my $ra_data = getTestDataForTable($dbh,$quotedCombinedName,$flatExtractColumnList,$sql); | ||
| 695 | |||
| 696 | #need to over lay tables with columns aprt from those whihc are 'hidden' | ||
| 697 | my @valuesTable ; | ||
| 698 | my $valuesClause = "values\n\t\t\t"; | ||
| 699 | |||
| 700 | my $lno = 1; | ||
| 701 | foreach my $ra_row (@{$ra_data}){ | ||
| 702 | my @outVals = undef ; | ||
| 703 | for ( my $i = 0; $i < scalar @{$ra_row}; $i++ ) { | ||
| 704 | |||
| 705 | if ( not ( defined ($ra_row->[$i]) ) ) { | ||
| 706 | $outVals[$i] = 'null' ; | ||
| 707 | } | ||
| 708 | else { | ||
| 709 | if (${$ra_metadata}[$i][1] =~ m{\A(?:date|datetime[2]?|smalldatetime)\z}i) { | ||
| 710 | $outVals[$i] = "convert(". ${$ra_columns}[$i][1] ."," . $dbh->quote($ra_row->[$i]) . ",120)" ; | ||
| 711 | } | ||
| 712 | elsif ( ${$ra_metadata}[$i][1] =~ m{(?:char|text|date)}i) { | ||
| 713 | $outVals[$i] = $dbh->quote($ra_row->[$i]) ; | ||
| 714 | } | ||
| 715 | else { | ||
| 716 | $outVals[$i] = $ra_row->[$i] ; | ||
| 717 | } | ||
| 718 | } | ||
| 719 | } | ||
| 720 | push @valuesTable, \@outVals ; | ||
| 721 | my $line = do{ local $" = ", "; "@outVals" } ; | ||
| 722 | $lno++; | ||
| 723 | } | ||
| 724 | |||
| 725 | my @maxWidth; | ||
| 726 | my $maxCol; | ||
| 727 | |||
| 728 | if ( scalar @valuesTable ) { | ||
| 729 | my @tmp = @{$valuesTable[0]}; | ||
| 730 | $maxCol = scalar @tmp -1 ; | ||
| 731 | for ( my $i = 0; $i <= $maxCol; $i++ ) { | ||
| 732 | push @maxWidth, 0; | ||
| 733 | } | ||
| 734 | for ( my $i = 0; $i < scalar @valuesTable; $i++ ) { | ||
| 735 | my @tmp = @{$valuesTable[$i]}; | ||
| 736 | for ( my $i = 0; $i <= $maxCol; $i++ ) { | ||
| 737 | if (length($tmp[$i]) > $maxWidth[$i] ) { | ||
| 738 | $maxWidth[$i] = length($tmp[$i]) ; | ||
| 739 | } | ||
| 740 | } | ||
| 741 | } | ||
| 742 | } | ||
| 743 | |||
| 744 | #warn Dumper @maxWidth ; | ||
| 745 | |||
| 746 | for ( my $i = 0; $i < scalar @valuesTable; $i++ ) { | ||
| 747 | my @tmp = @{$valuesTable[$i]}; | ||
| 748 | my $line = ""; | ||
| 749 | for ( my $j = 0; $j <= $maxCol; $j++ ) { | ||
| 750 | my $val = $tmp[$j]; | ||
| 751 | my $valWidth = length($val); | ||
| 752 | my $PadLength = $maxWidth[$j]-$valWidth; | ||
| 753 | my $padding = " "x$PadLength; | ||
| 754 | $line .= ", ${padding}${val}"; | ||
| 755 | } | ||
| 756 | $line =~ s{ ^,\s}{}msx; | ||
| 757 | $valuesClause .= "(\t" . $line . ")" . "\n\t\t,\t" ; | ||
| 758 | } | ||
| 759 | |||
| 760 | $valuesClause =~ s{ \n\t\t,\t \z }{}msx; | ||
| 761 | |||
| 762 | if ( ! $use_MinimalForm) { | ||
| 763 | return <<"EOF"; | ||
| 764 | |||
| 765 | ${set_IDENTITY_INSERT_ON} | ||
| 766 | ; with src as | ||
| 767 | ( | ||
| 768 | select * | ||
| 769 | from ( ${valuesClause} | ||
| 770 | ) AS vtable | ||
| 771 | ( ${colList}) | ||
| 772 | ) | ||
| 773 | insert into | ||
| 774 | ${quotedCombinedName} | ||
| 775 | ( ${colList} | ||
| 776 | ) | ||
| 777 | select ${colList} | ||
| 778 | from src ; | ||
| 779 | ${set_IDENTITY_INSERT_OFF} | ||
| 780 | |||
| 781 | EOF | ||
| 782 | } | ||
| 783 | else { | ||
| 784 | ${valuesClause} =~ s{\A values\n\t\t\t }{\t\t,\t}msx; | ||
| 785 | return <<"EOF"; | ||
| 786 | ${valuesClause} | ||
| 787 | EOF | ||
| 788 | |||
| 789 | } | ||
| 790 | } | ||
| 791 | |||
| 792 | sub describeTestDataForTable { | ||
| 793 | |||
| 794 | local $_ = undef ; | ||
| 795 | |||
| 796 | my $dbh = shift or croak 'no dbh' ; | ||
| 797 | my $sql = shift or croak 'no sql' ; | ||
| 798 | |||
| 799 | ( my $quoted_sql = $sql ) =~ s{'}{''}g; | ||
| 800 | |||
| 801 | my $metadata_sql = "exec sp_describe_first_result_set N'${quoted_sql}'" ; | ||
| 802 | |||
| 803 | my $sth2 = $dbh->prepare($metadata_sql); | ||
| 804 | my $rs = $sth2->execute(); | ||
| 805 | my $res = $sth2->fetchall_arrayref() ; | ||
| 806 | |||
| 807 | my @ret_res = map { [($$_[2],$$_[5],$$_[3],$$_[1])] } @$res; | ||
| 808 | |||
| 809 | return \@ret_res ; | ||
| 810 | |||
| 811 | } | ||
| 812 | |||
| 813 | sub getTestDataForTable { | ||
| 814 | |||
| 815 | local $_ = undef ; | ||
| 816 | |||
| 817 | my $dbh = shift or croak 'no dbh' ; | ||
| 818 | my $combinedName = shift or croak 'no table' ; | ||
| 819 | my $cols = shift or croak 'no cols list' ; | ||
| 820 | my $sql = shift or croak 'no sql' ; | ||
| 821 | #warn Dumper getCurrentTableDataSQL($combinedName,$pkCol,$cols); | ||
| 822 | |||
| 823 | my $sth2 = $dbh->prepare($sql); | ||
| 824 | my $rs = $sth2->execute(); | ||
| 825 | my $res = $sth2->fetchall_arrayref() ; | ||
| 826 | |||
| 827 | return $res ; | ||
| 828 | |||
| 829 | } | ||
| 830 | |||
| 831 | sub getCurrentTableData { | ||
| 832 | |||
| 833 | local $_ = undef ; | ||
| 834 | |||
| 835 | my $dbh = shift or croak 'no dbh' ; | ||
| 836 | my $combinedName = shift or croak 'no table' ; | ||
| 837 | my $pkCol = shift ; #or croak 'no primary key' ; | ||
| 838 | my $cols = shift ; #or croak 'no primary key' ; | ||
| 839 | |||
| 840 | #warn Dumper getCurrentTableDataSQL($combinedName,$pkCol,$cols); | ||
| 841 | |||
| 842 | my $sth2 = $dbh->prepare(getCurrentTableDataSQL($combinedName,$pkCol,$cols)); | ||
| 843 | my $rs = $sth2->execute(); | ||
| 844 | my $res = $sth2->fetchall_arrayref() ; | ||
| 845 | |||
| 846 | return $res ; | ||
| 847 | |||
| 848 | } | ||
| 849 | |||
| 850 | sub getCurrentTableDataSQL { | ||
| 851 | |||
| 852 | local $_ = undef ; | ||
| 853 | |||
| 854 | my $combinedName = shift or croak 'no table' ; | ||
| 855 | my $pkCol = shift ; #or croak 'no primary key' ; | ||
| 856 | my $cols = shift ; #or croak 'no primary key' ; | ||
| 857 | |||
| 858 | my $orderBy = "" ; | ||
| 859 | |||
| 860 | if ( ! $pkCol ) { | ||
| 861 | $orderBy = "" ; | ||
| 862 | } | ||
| 863 | else { | ||
| 864 | $orderBy = "order by $pkCol" ; | ||
| 865 | } | ||
| 866 | |||
| 867 | return <<"EOF" ; | ||
| 868 | |||
| 869 | select ${cols} | ||
| 870 | from ${combinedName} so | ||
| 871 | ${orderBy} | ||
| 872 | |||
| 873 | EOF | ||
| 874 | |||
| 875 | } | ||
| 876 | |||
| 877 | sub idCols { | ||
| 878 | |||
| 879 | local $_ = undef ; | ||
| 880 | |||
| 881 | my $dbh = shift or croak 'no dbh' ; | ||
| 882 | my $schema = shift or croak 'no schema' ; | ||
| 883 | my $table = shift or croak 'no table' ; | ||
| 884 | |||
| 885 | my $sth2 = $dbh->prepare(idColsSQL()); | ||
| 886 | my $rs = $sth2->execute($schema,$table); | ||
| 887 | my $res = $sth2->fetchall_arrayref() ; | ||
| 888 | |||
| 889 | return $$res[0][0] ; | ||
| 890 | |||
| 891 | } | ||
| 892 | |||
| 893 | sub idColsSQL { | ||
| 894 | |||
| 895 | return <<"EOF" ; | ||
| 896 | |||
| 897 | select sc.name as ID_COL | ||
| 898 | FROM dbo.sysobjects so | ||
| 899 | join dbo.syscolumns sc | ||
| 900 | on so.id = sc.id | ||
| 901 | and sc.colstat & 1 = 1 | ||
| 902 | where schema_name(so.uid) = ? | ||
| 903 | and so.name = ? | ||
| 904 | |||
| 905 | EOF | ||
| 906 | |||
| 907 | } | ||
| 908 | |||
| 909 | sub has_idCols { | ||
| 910 | |||
| 911 | local $_ = undef ; | ||
| 912 | |||
| 913 | my $dbh = shift or croak 'no dbh' ; | ||
| 914 | my $schema = shift or croak 'no schema' ; | ||
| 915 | my $table = shift or croak 'no table' ; | ||
| 916 | |||
| 917 | my $sth2 = $dbh->prepare(has_idColsSQL()); | ||
| 918 | my $rs = $sth2->execute($schema,$table); | ||
| 919 | my $res = $sth2->fetchall_arrayref() ; | ||
| 920 | |||
| 921 | return $$res[0][0] ; | ||
| 922 | |||
| 923 | } | ||
| 924 | |||
| 925 | sub has_idColsSQL { | ||
| 926 | |||
| 927 | return <<"EOF" ; | ||
| 928 | |||
| 929 | select 1 as ID_COL | ||
| 930 | FROM dbo.sysobjects so | ||
| 931 | where schema_name(so.uid) = ? | ||
| 932 | and so.name = ? | ||
| 933 | and exists ( | ||
| 934 | select * | ||
| 935 | from dbo.syscolumns sc | ||
| 936 | where so.id = sc.id | ||
| 937 | and sc.colstat & 1 = 1 | ||
| 938 | ) | ||
| 939 | EOF | ||
| 940 | |||
| 941 | } | ||
| 942 | |||
| 943 | |||
| 944 | sub pkcolumns { | ||
| 945 | |||
| 946 | local $_ = undef ; | ||
| 947 | |||
| 948 | my $dbh = shift or croak 'no dbh' ; | ||
| 949 | my $qtd_schema = shift or croak 'no schema' ; | ||
| 950 | my $qtd_table = shift or croak 'no table' ; | ||
| 951 | |||
| 952 | my $schema = $qtd_schema ; | ||
| 953 | my $table = $qtd_table ; | ||
| 954 | |||
| 955 | $schema =~ s/\A\[(.*)\]\z/$1/; | ||
| 956 | $table =~ s/\A\[(.*)\]\z/$1/; | ||
| 957 | |||
| 958 | my $sth2 = $dbh->prepare( pkcolumnsSQL()); | ||
| 959 | my $rs = $sth2->execute($schema,$table,$schema,$table); | ||
| 960 | my $res = $sth2->fetchall_arrayref() ; | ||
| 961 | |||
| 962 | if ( scalar @{$res} ) { return $res ; } ; | ||
| 963 | return [] ; | ||
| 964 | } | ||
| 965 | |||
| 966 | |||
| 967 | |||
| 968 | sub pkcolumnsSQL { | ||
| 969 | |||
| 970 | return <<"EOF" ; | ||
| 971 | |||
| 972 | ; with ranking as ( | ||
| 973 | select CONSTRAINT_SCHEMA, CONSTRAINT_NAME | ||
| 974 | , row_number() over (order by case when tc.CONSTRAINT_TYPE = 'PRIMARY KEY' then 1 else 2 end, CONSTRAINT_NAME ) as rn | ||
| 975 | from INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc | ||
| 976 | where tc.CONSTRAINT_TYPE in( 'PRIMARY KEY','UNIQUE' ) | ||
| 977 | and tc.TABLE_SCHEMA = ? | ||
| 978 | and tc.TABLE_NAME = ? | ||
| 979 | ) | ||
| 980 | select COLUMN_NAME | ||
| 981 | from INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc | ||
| 982 | join INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu | ||
| 983 | on tc.TABLE_CATALOG = kcu.TABLE_CATALOG | ||
| 984 | and tc.TABLE_SCHEMA = kcu.TABLE_SCHEMA | ||
| 985 | and tc.TABLE_NAME = kcu.TABLE_NAME | ||
| 986 | and tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME | ||
| 987 | join ranking rk | ||
| 988 | on tc.CONSTRAINT_SCHEMA = rk.CONSTRAINT_SCHEMA | ||
| 989 | and tc.CONSTRAINT_NAME = rk.CONSTRAINT_NAME | ||
| 990 | where tc.CONSTRAINT_TYPE in( 'PRIMARY KEY','UNIQUE' ) | ||
| 991 | and tc.TABLE_SCHEMA = ? | ||
| 992 | and tc.TABLE_NAME = ? | ||
| 993 | and rn = 1 | ||
| 994 | order by | ||
| 995 | ORDINAL_POSITION | ||
| 996 | |||
| 997 | EOF | ||
| 998 | |||
| 999 | } | ||
| 1000 | |||
| 1001 | |||
| 1002 | sub columns { | ||
| 1003 | |||
| 1004 | local $_ = undef ; | ||
| 1005 | |||
| 1006 | my $dbh = shift or croak 'no dbh' ; | ||
| 1007 | my $schema = shift or croak 'no schema' ; | ||
| 1008 | my $table = shift or croak 'no table' ; | ||
| 1009 | |||
| 1010 | my $sth2 = $dbh->prepare( columnsSQL()); | ||
| 1011 | my $rs = $sth2->execute($schema,$table,"[${schema}]","[${table}]"); | ||
| 1012 | my $res = $sth2->fetchall_arrayref() ; | ||
| 1013 | |||
| 1014 | if ( scalar @{$res} ) { return $res ; } ; | ||
| 1015 | return [] ; | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | |||
| 1019 | |||
| 1020 | sub columnsSQL { | ||
| 1021 | |||
| 1022 | return <<"EOF" ; | ||
| 1023 | select Column_name | ||
| 1024 | , data_type | ||
| 1025 | , case when character_maximum_length is not null then '('+ case when character_maximum_length = -1 then 'max' else cast(character_maximum_length as varchar(10)) end+')' else '' end | ||
| 1026 | as datasize | ||
| 1027 | , isnull(case when lower(Data_type) = 'float' | ||
| 1028 | then '('+cast(Numeric_precision as varchar(10))+')' | ||
| 1029 | when lower(Data_type) not like '%int%' and Numeric_precision is not null | ||
| 1030 | then '('+cast(Numeric_precision as varchar(10))+','+cast(Numeric_scale as varchar(10))+')' | ||
| 1031 | else '' | ||
| 1032 | end | ||
| 1033 | ,'') | ||
| 1034 | as dataprecision | ||
| 1035 | , case when DATABASEPROPERTYEX(db_name(), 'Collation') != collation_name then 'collate ' + collation_name else '' end | ||
| 1036 | as collation | ||
| 1037 | , case when LOWER(IS_NULLABLE) = 'no' then 'not null' else 'null' end | ||
| 1038 | as datanullabity | ||
| 1039 | from INFORMATION_SCHEMA.COLUMNS | ||
| 1040 | where 1=1 | ||
| 1041 | and TABLE_SCHEMA = ? | ||
| 1042 | and TABLE_NAME = ? | ||
| 1043 | and COLUMNPROPERTY(object_id(?+'.'+?) , COLUMN_NAME,'IsComputed') != 1 | ||
| 1044 | --order by ORDINAL_POSITION | ||
| 1045 | |||
| 1046 | EOF | ||
| 1047 | |||
| 1048 | } | ||
| 1049 | |||
| 1050 | |||
| 1051 | |||
| 1052 | |||
| 1053 | |||
| 1054 | |||
| 1055 | __DATA__ | ||