| 1 | # $Id: ESQL.pm,v 1.20 2001/06/05 12:45:05 matt Exp $ | ||
| 2 | |||
| 3 | package AxKit::XSP::ESQL; | ||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use vars qw/@ISA $VERSION $NS @RESULTS @NAMES @STH @COUNT/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | @ISA = ('Apache::AxKit::Language::XSP'); | ||
| 8 | |||
| 9 | $VERSION = "1.4"; | ||
| 10 | $NS = "http://apache.org/xsp/SQL/v2"; | ||
| 11 | |||
| 12 | 0 | ▶ | use Apache::AxKit::Language::XSP; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use DBI; | ||
| 14 | use AxKit::XSP::Util; | ||
| 15 | |||
| 16 | AxKit::XSP::Util->register(); | ||
| 17 | |||
| 18 | # DBI->trace(1); | ||
| 19 | |||
| 20 | sub new_query { | ||
| 21 | unshift @RESULTS, {}; | ||
| 22 | unshift @NAMES, []; | ||
| 23 | unshift @STH, undef; | ||
| 24 | unshift @COUNT, 0; | ||
| 25 | # warn "new_query: ", scalar @STH, "\n"; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub end_query { | ||
| 29 | shift @RESULTS; | ||
| 30 | shift @NAMES; | ||
| 31 | my $sth = shift @STH; | ||
| 32 | $sth->finish(); | ||
| 33 | shift @COUNT; | ||
| 34 | # warn "end_query: ", scalar @STH, "\n"; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub prepare { | ||
| 38 | my ($dbh, $query) = @_; | ||
| 39 | $STH[0] = $dbh->prepare($query); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub execute { | ||
| 43 | my (@params) = set_null_params(@_); | ||
| 44 | my $rv = $STH[0]->execute(@params); | ||
| 45 | $NAMES[0] = $STH[0]->{NAME_lc}; | ||
| 46 | my %hash; | ||
| 47 | my $rc = $STH[0]->bind_columns(\@hash{ @{$NAMES[0]} }); | ||
| 48 | $RESULTS[0] = \%hash; | ||
| 49 | return $rv; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub execute_from_update { | ||
| 53 | my (@params) = set_null_params(@_); | ||
| 54 | return $STH[0]->execute(@params); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub set_null_params { | ||
| 58 | map { $_ eq 'NULL' ? undef : $_ } @_; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub get_sth { | ||
| 62 | my ($ancestor) = @_; | ||
| 63 | $ancestor ||= 0; | ||
| 64 | $STH[$ancestor]; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub get_row { | ||
| 68 | my ($ancestor) = @_; | ||
| 69 | $ancestor ||= 0; | ||
| 70 | my $res = $STH[$ancestor]->fetch; | ||
| 71 | $COUNT[$ancestor]++ if $res; | ||
| 72 | return $res; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub get_column { | ||
| 76 | my ($column, $ancestor) = @_; | ||
| 77 | $ancestor ||= 0; | ||
| 78 | if (DBI::looks_like_number($column)) { | ||
| 79 | return $RESULTS[$ancestor]{ $NAMES[$ancestor][$column - 1] }; | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | return $RESULTS[$ancestor]{$column}; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | sub column_name { | ||
| 87 | my ($column, $ancestor) = @_; | ||
| 88 | $ancestor ||= 0; | ||
| 89 | return $STH[$ancestor]->{NAME}->[column_number($column)]; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub column_number { | ||
| 93 | my ($col, $ancestor) = @_; | ||
| 94 | $ancestor ||= 0; | ||
| 95 | if (DBI::looks_like_number($col)) { | ||
| 96 | return $col - 1; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | my $num = 0; | ||
| 100 | for (@{$NAMES[$ancestor]}) { | ||
| 101 | last if $_ eq $col; | ||
| 102 | $num++; | ||
| 103 | } | ||
| 104 | return $num; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | sub get_columns { | ||
| 109 | my ($ancestor) = @_; | ||
| 110 | $ancestor ||= 0; | ||
| 111 | return @{$NAMES[$ancestor]}; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub get_count { | ||
| 115 | my ($ancestor) = @_; | ||
| 116 | $ancestor ||= 0; | ||
| 117 | warn("get_count: returning " . $COUNT[$ancestor] . "\n"); | ||
| 118 | return $COUNT[$ancestor]; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub parse_char { | ||
| 122 | my ($e, $text) = @_; | ||
| 123 | |||
| 124 | if ($e->current_element() ne 'query') { | ||
| 125 | $text =~ s/^\s*//; | ||
| 126 | $text =~ s/\s*$//; | ||
| 127 | } | ||
| 128 | |||
| 129 | return '' unless $text; | ||
| 130 | |||
| 131 | $text =~ s/\|/\\\|/g; | ||
| 132 | return ". q|$text|"; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub parse_start { | ||
| 136 | my ($e, $tag, %attribs) = @_; | ||
| 137 | |||
| 138 | if ($tag eq 'connection') { | ||
| 139 | $e->manage_text(0); | ||
| 140 | return "{\nmy (\$dbh, \$connect_count, \$driver, \$transactions, \$dburl, \$user, \$pass);\n"; | ||
| 141 | } | ||
| 142 | elsif ($tag eq 'driver') { | ||
| 143 | my $transactions = 1; | ||
| 144 | if (lc($attribs{transactions}) eq 'no') { | ||
| 145 | $transactions = 0; | ||
| 146 | } | ||
| 147 | elsif (lc($attribs{transactions}) eq 'yes') { | ||
| 148 | $transactions = 1; | ||
| 149 | } | ||
| 150 | elsif (exists($attribs{transactions})) { | ||
| 151 | die "<esql:driver transactions='$attribs{transactions}'> is invalid. Use 'yes' or 'no'"; | ||
| 152 | } | ||
| 153 | |||
| 154 | return '$transactions = ' . $transactions . ';$driver = "dbi:"'; | ||
| 155 | } | ||
| 156 | elsif ($tag eq 'dburl') { | ||
| 157 | return '$dburl = ""'; | ||
| 158 | } | ||
| 159 | elsif ($tag eq 'username') { | ||
| 160 | return '$user = ""'; | ||
| 161 | } | ||
| 162 | elsif ($tag eq 'password') { | ||
| 163 | return '$pass = ""'; | ||
| 164 | } | ||
| 165 | elsif ($tag eq 'pool') { | ||
| 166 | die "esql:pool not supported"; | ||
| 167 | } | ||
| 168 | elsif ($tag eq 'execute-query') { | ||
| 169 | $e->manage_text(0); | ||
| 170 | return <<'EOT'; | ||
| 171 | $dbh = DBI->connect($driver . ($dburl ? ":$dburl" : ''), | ||
| 172 | $user, $pass, | ||
| 173 | { | ||
| 174 | PrintError => 0, | ||
| 175 | AutoCommit => $transactions ? 0 : 1, | ||
| 176 | RaiseError => 1, | ||
| 177 | }) unless $connect_count; | ||
| 178 | $connect_count++; | ||
| 179 | AxKit::XSP::ESQL::new_query(); | ||
| 180 | { | ||
| 181 | my ($query, $max_rows, $skip_rows, @params, $rv); | ||
| 182 | $max_rows = 0; $skip_rows = 0; | ||
| 183 | EOT | ||
| 184 | } | ||
| 185 | elsif ($tag eq 'max-rows') { | ||
| 186 | return '$max_rows = ""'; | ||
| 187 | } | ||
| 188 | elsif ($tag eq 'skip-rows') { | ||
| 189 | return '$skip_rows = ""'; | ||
| 190 | } | ||
| 191 | elsif ($tag eq 'query') { | ||
| 192 | return '$query = ""'; | ||
| 193 | } | ||
| 194 | elsif ($tag eq 'parameter') { | ||
| 195 | return ". '?'; push(\@params, ''"; | ||
| 196 | } | ||
| 197 | elsif ($tag eq 'results') { | ||
| 198 | $e->manage_text(0); | ||
| 199 | return <<'EOT'; | ||
| 200 | { | ||
| 201 | AxKit::XSP::ESQL::prepare($dbh, $query); | ||
| 202 | $rv = AxKit::XSP::ESQL::execute(@params); | ||
| 203 | my ($col, $ancestor, $format); | ||
| 204 | if ($skip_rows) { | ||
| 205 | 1 while (AxKit::XSP::ESQL::get_count() < $skip_rows && AxKit::XSP::ESQL::get_row()); | ||
| 206 | } | ||
| 207 | if (AxKit::XSP::ESQL::get_row()) { | ||
| 208 | EOT | ||
| 209 | } | ||
| 210 | elsif ($tag eq 'row-results') { | ||
| 211 | $e->manage_text(0); | ||
| 212 | return <<'EOT'; | ||
| 213 | do { | ||
| 214 | EOT | ||
| 215 | } | ||
| 216 | elsif ($tag eq 'get-columns') { | ||
| 217 | my $ancestor = $attribs{ancestor} || 0; | ||
| 218 | my $case = $attribs{'tag-case'}; | ||
| 219 | my $function = ''; | ||
| 220 | if ($case eq 'upper') { $function = 'uc'; } | ||
| 221 | if ($case eq 'lower') { $function = 'lc'; } | ||
| 222 | $e->append_to_script("for my \$col (AxKit::XSP::ESQL::get_columns($ancestor)) {\nmy \$ancestor = $ancestor;\n"); | ||
| 223 | my $xsp_element = { | ||
| 224 | Name => 'element', | ||
| 225 | NamespaceURI => $AxKit::XSP::Core::NS, | ||
| 226 | Attributes => [], | ||
| 227 | }; | ||
| 228 | $e->start_element($xsp_element); | ||
| 229 | my $xsp_name = { | ||
| 230 | Name => 'name', | ||
| 231 | NamespaceURI => $AxKit::XSP::Core::NS, | ||
| 232 | Attributes => [], | ||
| 233 | # Parent => $xsp_element, | ||
| 234 | }; | ||
| 235 | $e->start_element($xsp_name); | ||
| 236 | my $xsp_expr = { | ||
| 237 | Name => 'expr', | ||
| 238 | NamespaceURI => $AxKit::XSP::Core::NS, | ||
| 239 | Attributes => [], | ||
| 240 | # Parent => $xsp_name, | ||
| 241 | }; | ||
| 242 | $e->start_element($xsp_expr); | ||
| 243 | return '$col'; | ||
| 244 | } | ||
| 245 | elsif ($tag eq 'encoding') { | ||
| 246 | # not supported yet! | ||
| 247 | die "esql:encoding not supported"; | ||
| 248 | } | ||
| 249 | elsif ($tag eq 'column') { | ||
| 250 | return '$col = ""'; | ||
| 251 | } | ||
| 252 | elsif ($tag =~ /^get-(column|string|boolean|double|float|int|long|short)$/) { | ||
| 253 | $e->start_expr($tag); | ||
| 254 | my $code = '$col = ""; $ancestor = 0; $format = "";'; | ||
| 255 | if (my $col = lc($attribs{column})) { | ||
| 256 | $code .= '$col = q|' . $col . '|;'; | ||
| 257 | } | ||
| 258 | $code .= '$ancestor = ' . ($attribs{ancestor} || 0) . ';'; | ||
| 259 | return $code; | ||
| 260 | } | ||
| 261 | elsif ($tag =~ /^get-(date|time|timestamp)$/) { | ||
| 262 | $e->start_expr($tag); | ||
| 263 | my $code = '$col = ""; $ancestor = 0; $format = "";'; | ||
| 264 | if (my $col = lc($attribs{column})) { | ||
| 265 | $code .= '$col = q|' . $col . '|;'; | ||
| 266 | } | ||
| 267 | if (my $format = $attribs{format}) { | ||
| 268 | $code .= '$format = q|' . $format . '|;'; | ||
| 269 | } | ||
| 270 | $code .= '$ancestor = ' . ($attribs{ancestor} || 0) . ';'; | ||
| 271 | return $code; | ||
| 272 | } | ||
| 273 | elsif ($tag eq 'get-xml') { | ||
| 274 | my $code = '$col = ""; $ancestor = 0; $format = "";'; | ||
| 275 | if (my $col = lc($attribs{column})) { | ||
| 276 | $code .= '$col = q|' . $col . '|;'; | ||
| 277 | } | ||
| 278 | $code .= '$ancestor = ' . ($attribs{ancestor} || 0) . ';'; | ||
| 279 | return $code; | ||
| 280 | } | ||
| 281 | elsif ($tag eq 'get-row-position') { | ||
| 282 | $e->start_expr($tag); | ||
| 283 | my $code = '$col = ""; $ancestor = 0; $format = "";'; | ||
| 284 | $code .= '$ancestor = ' . ($attribs{ancestor} || 0) . ';'; | ||
| 285 | return $code; | ||
| 286 | } | ||
| 287 | elsif ($tag eq 'get-column-name') { | ||
| 288 | $e->start_expr($tag); | ||
| 289 | my $code = '$col = ""; $ancestor = 0; $format = "";'; | ||
| 290 | if (my $col = lc($attribs{column})) { | ||
| 291 | $code .= '$col = q|' . $col . '|;'; | ||
| 292 | } | ||
| 293 | $code .= '$ancestor = ' . ($attribs{ancestor} || 0) . ';'; | ||
| 294 | return $code; | ||
| 295 | } | ||
| 296 | elsif ($tag eq 'get-column-label') { | ||
| 297 | $e->start_expr($tag); | ||
| 298 | my $code = '$col = ""; $ancestor = 0; $format = "";'; | ||
| 299 | if (my $col = lc($attribs{column})) { | ||
| 300 | $code .= '$col = q|' . $col . '|;'; | ||
| 301 | } | ||
| 302 | $code .= '$ancestor = ' . ($attribs{ancestor} || 0) . ';'; | ||
| 303 | return $code; | ||
| 304 | } | ||
| 305 | elsif ($tag eq 'get-column-type-name') { | ||
| 306 | $e->start_expr($tag); | ||
| 307 | my $code = '$col = ""; $ancestor = 0; $format = "";'; | ||
| 308 | if (my $col = lc($attribs{column})) { | ||
| 309 | $code .= '$col = q|' . $col . '|;'; | ||
| 310 | } | ||
| 311 | $code .= '$ancestor = ' . ($attribs{ancestor} || 0) . ';'; | ||
| 312 | return $code; | ||
| 313 | } | ||
| 314 | elsif ($tag eq 'no-results') { | ||
| 315 | $e->manage_text(0); | ||
| 316 | return <<'EOT'; | ||
| 317 | if (AxKit::XSP::ESQL::get_count() == 0) { | ||
| 318 | my ($col, $ancestor, $format); | ||
| 319 | EOT | ||
| 320 | } | ||
| 321 | elsif ($tag eq 'update-results') { | ||
| 322 | $e->manage_text(0); | ||
| 323 | return <<'EOT'; | ||
| 324 | { | ||
| 325 | AxKit::XSP::ESQL::prepare($dbh, $query); | ||
| 326 | $rv = AxKit::XSP::ESQL::execute_from_update(@params); | ||
| 327 | if ($rv) { | ||
| 328 | EOT | ||
| 329 | } | ||
| 330 | else { | ||
| 331 | die "Unknown ESQL tag: $tag"; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | sub parse_end { | ||
| 336 | my ($e, $tag) = @_; | ||
| 337 | if ($tag eq 'connection') { | ||
| 338 | return "\n} # /connection\n"; | ||
| 339 | } | ||
| 340 | elsif ($tag eq 'driver') { } | ||
| 341 | elsif ($tag eq 'dburl') { } | ||
| 342 | elsif ($tag eq 'username') { } | ||
| 343 | elsif ($tag eq 'password') { } | ||
| 344 | elsif ($tag eq 'pool') { } | ||
| 345 | elsif ($tag eq 'execute-query') { | ||
| 346 | return <<'EOT'; | ||
| 347 | } # </execute-query> | ||
| 348 | AxKit::XSP::ESQL::end_query(); | ||
| 349 | $connect_count--; | ||
| 350 | unless ($connect_count) { | ||
| 351 | $dbh->disconnect(); | ||
| 352 | undef $dbh; | ||
| 353 | } | ||
| 354 | EOT | ||
| 355 | } | ||
| 356 | elsif ($tag eq 'max-rows') { } | ||
| 357 | elsif ($tag eq 'skip-rows') { } | ||
| 358 | elsif ($tag eq 'query') { | ||
| 359 | return ";\n"; | ||
| 360 | } | ||
| 361 | elsif ($tag eq 'parameter') { | ||
| 362 | return ");\n\$query .= ''"; | ||
| 363 | } | ||
| 364 | elsif ($tag eq 'results') { | ||
| 365 | return <<'EOT'; | ||
| 366 | } # end - if (rows existed) | ||
| 367 | } # </results> | ||
| 368 | $dbh->commit if $transactions; | ||
| 369 | EOT | ||
| 370 | } | ||
| 371 | elsif ($tag eq 'row-results') { | ||
| 372 | return <<'EOT'; | ||
| 373 | if ($max_rows && AxKit::XSP::ESQL::get_count() >= $max_rows) { | ||
| 374 | last; | ||
| 375 | } | ||
| 376 | } while (AxKit::XSP::ESQL::get_row()); # while(get_row) </row-results> | ||
| 377 | EOT | ||
| 378 | } | ||
| 379 | elsif ($tag eq 'get-columns') { | ||
| 380 | my $xsp_element = { | ||
| 381 | Name => 'element', | ||
| 382 | NamespaceURI => $AxKit::XSP::Core::NS, | ||
| 383 | Attributes => [], | ||
| 384 | }; | ||
| 385 | my $xsp_name = { | ||
| 386 | Name => 'name', | ||
| 387 | NamespaceURI => $AxKit::XSP::Core::NS, | ||
| 388 | Attributes => [], | ||
| 389 | Parent => $xsp_element, | ||
| 390 | }; | ||
| 391 | my $xsp_expr = { | ||
| 392 | Name => 'expr', | ||
| 393 | NamespaceURI => $AxKit::XSP::Core::NS, | ||
| 394 | Attributes => [], | ||
| 395 | Parent => $xsp_name, | ||
| 396 | }; | ||
| 397 | $e->end_element($xsp_expr); | ||
| 398 | $e->end_element($xsp_name); | ||
| 399 | delete $xsp_expr->{Parent}; | ||
| 400 | $e->start_element($xsp_expr); | ||
| 401 | $e->append_to_script('AxKit::XSP::ESQL::get_column($col, $ancestor)'); | ||
| 402 | $e->end_element($xsp_expr); | ||
| 403 | $e->end_element($xsp_element); | ||
| 404 | return "\n} # </get-columns>\n"; | ||
| 405 | } | ||
| 406 | elsif ($tag eq 'encoding') { | ||
| 407 | } | ||
| 408 | elsif ($tag eq 'column') { | ||
| 409 | } | ||
| 410 | elsif ($tag =~ /^get-(column|string)$/) { | ||
| 411 | $e->append_to_script('AxKit::XSP::ESQL::get_column($col, $ancestor)'); | ||
| 412 | $e->end_expr(); | ||
| 413 | return ''; | ||
| 414 | } | ||
| 415 | elsif ($tag =~ /^get-(date|time|timestamp)$/) { | ||
| 416 | $e->append_to_script('AxKit::XSP::ESQL::get_column($col, $ancestor)'); | ||
| 417 | $e->end_expr(); | ||
| 418 | return ''; | ||
| 419 | } | ||
| 420 | elsif ($tag eq 'get-boolean') { | ||
| 421 | $e->append_to_script('AxKit::XSP::ESQL::get_column($col, $ancestor) ? XML::XPath::Boolean->True : XML::XPath::Boolean->False'); | ||
| 422 | $e->end_expr(); | ||
| 423 | return ''; | ||
| 424 | } | ||
| 425 | elsif ($tag =~ /^get-(double|float)$/) { | ||
| 426 | $e->append_to_script('sprintf("%e", AxKit::XSP::ESQL::get_column($col, $ancestor))'); | ||
| 427 | $e->end_expr(); | ||
| 428 | return ''; | ||
| 429 | } | ||
| 430 | elsif ($tag =~ /^get-(int|long|short)$/) { | ||
| 431 | $e->append_to_script('sprintf("%d", AxKit::XSP::ESQL::get_column($col, $ancestor))'); | ||
| 432 | $e->end_expr(); | ||
| 433 | return ''; | ||
| 434 | } | ||
| 435 | elsif ($tag eq 'get-xml') { | ||
| 436 | my $util_include_expr = { | ||
| 437 | Name => 'include-expr', | ||
| 438 | NamespaceURI => $AxKit::XSP::Util::NS, | ||
| 439 | Attributes => [], | ||
| 440 | }; | ||
| 441 | my $xsp_expr = { | ||
| 442 | Name => 'expr', | ||
| 443 | NamespaceURI => $AxKit::XSP::Core::NS, | ||
| 444 | Attributes => [], | ||
| 445 | }; | ||
| 446 | $e->start_element($util_include_expr); | ||
| 447 | $e->start_element($xsp_expr); | ||
| 448 | $e->append_to_script('AxKit::XSP::ESQL::get_column($col, $ancestor)'); | ||
| 449 | $e->end_element($xsp_expr); | ||
| 450 | $e->end_element($util_include_expr); | ||
| 451 | return ''; | ||
| 452 | } | ||
| 453 | elsif ($tag eq 'get-row-position') { | ||
| 454 | $e->append_to_script('AxKit::XSP::ESQL::get_count($ancestor)'); | ||
| 455 | $e->end_expr(); | ||
| 456 | return ''; | ||
| 457 | } | ||
| 458 | elsif ($tag eq 'get-column-name') { | ||
| 459 | $e->append_to_script('lc(AxKit::XSP::ESQL::column_name($col, $ancestor))'); | ||
| 460 | $e->end_expr(); | ||
| 461 | return ''; | ||
| 462 | } | ||
| 463 | elsif ($tag eq 'get-column-label') { | ||
| 464 | $e->append_to_script('AxKit::XSP::ESQL::column_name($col, $ancestor)'); | ||
| 465 | $e->end_expr(); | ||
| 466 | return ''; | ||
| 467 | } | ||
| 468 | elsif ($tag eq 'get-column-type-name') { | ||
| 469 | $e->append_to_script('$dbh->type_info(AxKit::XSP::ESQL::get_sth($ancestor)->{TYPE}->[AxKit::XSP::ESQL::column_number($col)])->{TYPE_NAME}'); | ||
| 470 | $e->end_expr(); | ||
| 471 | return ''; | ||
| 472 | } | ||
| 473 | elsif ($tag eq 'no-results') { | ||
| 474 | $e->{ESQL_NodeMode} = 0; | ||
| 475 | return "\n} # </no-results>\n"; | ||
| 476 | } | ||
| 477 | elsif ($tag eq 'update-results') { | ||
| 478 | $e->{ESQL_NodeMode} = 0; | ||
| 479 | return <<'EOT'; | ||
| 480 | } # end - if (update occured) | ||
| 481 | } # </update-results> | ||
| 482 | $dbh->commit if $transactions; | ||
| 483 | EOT | ||
| 484 | } | ||
| 485 | |||
| 486 | return ";"; | ||
| 487 | } | ||
| 488 | |||
| 489 | 1; | ||
| 490 | __END__ | ||