Revision f7057756
Von Moritz Bunkus vor fast 18 Jahren hinzugefügt
SL/User.pm | ||
---|---|---|
35 | 35 |
package User; |
36 | 36 |
|
37 | 37 |
use SL::DBUpgrade2; |
38 |
use SL::DBUtils; |
|
38 | 39 |
|
39 | 40 |
sub new { |
40 | 41 |
$main::lxdebug->enter_sub(); |
... | ... | |
87 | 88 |
my @language = (); |
88 | 89 |
|
89 | 90 |
# scan the locale directory and read in the LANGUAGE files |
90 |
opendir DIR, "locale";
|
|
91 |
opendir(DIR, "locale");
|
|
91 | 92 |
|
92 |
my @dir = grep !/(^\.\.?$|\..*)/, readdir DIR;
|
|
93 |
my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
|
|
93 | 94 |
|
94 | 95 |
foreach my $dir (@dir) { |
95 | 96 |
next unless open(FH, "locale/$dir/LANGUAGE"); |
... | ... | |
133 | 134 |
} |
134 | 135 |
|
135 | 136 |
do "$userspath/$self->{login}.conf"; |
136 |
$myconfig{dbpasswd} = unpack 'u', $myconfig{dbpasswd};
|
|
137 |
$myconfig{dbpasswd} = unpack('u', $myconfig{dbpasswd});
|
|
137 | 138 |
|
138 | 139 |
# check if database is down |
139 | 140 |
my $dbh = |
... | ... | |
151 | 152 |
|
152 | 153 |
# add login to employee table if it does not exist |
153 | 154 |
# no error check for employee table, ignore if it does not exist |
154 |
$query = qq|SELECT e.id FROM employee e WHERE e.login = '$self->{login}'|; |
|
155 |
$sth = $dbh->prepare($query); |
|
156 |
$sth->execute; |
|
157 |
|
|
158 |
my ($login) = $sth->fetchrow_array; |
|
159 |
$sth->finish; |
|
155 |
$query = qq|SELECT id FROM employee WHERE login = ?|; |
|
156 |
my ($login) = selectrow_query($form, $dbh, $query, $self->{login}); |
|
160 | 157 |
|
161 | 158 |
if (!$login) { |
162 |
$query = qq|INSERT INTO employee (login, name, workphone, role) |
|
163 |
VALUES ('$self->{login}', '$myconfig{name}',
|
|
164 |
'$myconfig{tel}', 'user')|;
|
|
165 |
$dbh->do($query);
|
|
159 |
$query = qq|INSERT INTO employee (login, name, workphone, role)| .
|
|
160 |
qq|VALUES (?, ?, ?, ?)|;
|
|
161 |
my @values = ($self->{login}, $myconfig{name}, $myconfig{tel}, "user");
|
|
162 |
do_query($form, $dbh, $query, @values);
|
|
166 | 163 |
} |
167 | 164 |
|
168 | 165 |
$self->create_schema_info_table($form, $dbh); |
... | ... | |
298 | 295 |
or $form->dberror; |
299 | 296 |
|
300 | 297 |
if ($form->{dbdriver} eq 'Pg') { |
301 |
|
|
302 |
$query = qq|SELECT datname FROM pg_database WHERE NOT ((datname = 'template0') OR (datname = 'template1'))|; |
|
303 |
$sth = $dbh->prepare($query); |
|
304 |
$sth->execute || $form->dberror($query); |
|
298 |
$query = |
|
299 |
qq|SELECT datname FROM pg_database | . |
|
300 |
qq|WHERE NOT datname IN ('template0', 'template1')|; |
|
301 |
$sth = $dbh->prepare($query); |
|
302 |
$sth->execute() || $form->dberror($query); |
|
305 | 303 |
|
306 | 304 |
while (my ($db) = $sth->fetchrow_array) { |
307 | 305 |
|
... | ... | |
314 | 312 |
DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) |
315 | 313 |
or $form->dberror; |
316 | 314 |
|
317 |
$query = qq|SELECT p.tablename FROM pg_tables p
|
|
318 |
WHERE p.tablename = 'defaults'
|
|
319 |
AND p.tableowner = '$form->{dbuser}'|;
|
|
315 |
$query = |
|
316 |
qq|SELECT tablename FROM pg_tables | .
|
|
317 |
qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
|
|
320 | 318 |
my $sth = $dbh->prepare($query); |
321 |
$sth->execute || $form->dberror($query); |
|
319 |
$sth->execute($form->{dbuser}) || |
|
320 |
$form->dberror($query . " ($form->{dbuser})"); |
|
322 | 321 |
|
323 | 322 |
if ($sth->fetchrow_array) { |
324 |
push @dbsources, $db;
|
|
323 |
push(@dbsources, $db);
|
|
325 | 324 |
} |
326 | 325 |
$sth->finish; |
327 | 326 |
$dbh->disconnect; |
328 | 327 |
next; |
329 | 328 |
} |
330 |
push @dbsources, $db;
|
|
329 |
push(@dbsources, $db);
|
|
331 | 330 |
} |
332 | 331 |
} |
333 | 332 |
|
334 | 333 |
if ($form->{dbdriver} eq 'Oracle') { |
335 | 334 |
if ($form->{only_acc_db}) { |
336 |
$query = qq|SELECT o.owner FROM dba_objects o
|
|
337 |
WHERE o.object_name = 'DEFAULTS'
|
|
338 |
AND o.object_type = 'TABLE'|;
|
|
335 |
$query = |
|
336 |
qq|SELECT owner FROM dba_objects | .
|
|
337 |
qq|WHERE object_name = 'DEFAULTS' AND object_type = 'TABLE'|;
|
|
339 | 338 |
} else { |
340 | 339 |
$query = qq|SELECT username FROM dba_users|; |
341 | 340 |
} |
... | ... | |
344 | 343 |
$sth->execute || $form->dberror($query); |
345 | 344 |
|
346 | 345 |
while (my ($db) = $sth->fetchrow_array) { |
347 |
push @dbsources, $db;
|
|
346 |
push(@dbsources, $db);
|
|
348 | 347 |
} |
349 | 348 |
} |
350 | 349 |
|
... | ... | |
366 | 365 |
my $dbh = |
367 | 366 |
DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) |
368 | 367 |
or $form->dberror; |
369 |
|
|
368 |
$form->{db} =~ s/\"//g; |
|
370 | 369 |
my %dbcreate = ( |
371 | 370 |
'Pg' => qq|CREATE DATABASE "$form->{db}"|, |
372 | 371 |
'Oracle' => |
373 |
qq|CREATE USER "$form->{db}" DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP IDENTIFIED BY "$form->{db}"| |
|
372 |
qq|CREATE USER "$form->{db}" DEFAULT TABLESPACE USERS | . |
|
373 |
qq|TEMPORARY TABLESPACE TEMP IDENTIFIED BY "$form->{db}"| |
|
374 | 374 |
); |
375 | 375 |
|
376 | 376 |
my %dboptions = ( |
... | ... | |
385 | 385 |
push(@{$dboptions{"Pg"}}, "TEMPLATE = $dbdefault"); |
386 | 386 |
} |
387 | 387 |
|
388 |
my $query = qq|$dbcreate{$form->{dbdriver}}|;
|
|
388 |
my $query = $dbcreate{$form->{dbdriver}};
|
|
389 | 389 |
$query .= " WITH " . join(" ", @{$dboptions{"Pg"}}) if (@{$dboptions{"Pg"}}); |
390 | 390 |
|
391 |
$dbh->do($query) || $form->dberror($query);
|
|
391 |
do_query($form, $dbh, $query);
|
|
392 | 392 |
|
393 | 393 |
if ($form->{dbdriver} eq 'Oracle') { |
394 |
$query = qq|GRANT CONNECT,RESOURCE TO "$form->{db}"|; |
|
395 |
$dbh->do($query) || $form->dberror($query);
|
|
394 |
$query = qq|GRANT CONNECT, RESOURCE TO "$form->{db}"|;
|
|
395 |
do_query($form, $dbh, $query);
|
|
396 | 396 |
} |
397 | 397 |
$dbh->disconnect; |
398 | 398 |
|
... | ... | |
420 | 420 |
$filename = qq|sql/$form->{chart}-chart.sql|; |
421 | 421 |
$self->process_query($form, $dbh, $filename); |
422 | 422 |
|
423 |
$query = "UPDATE defaults SET coa = " . $dbh->quote($form->{"chart"});
|
|
424 |
$dbh->do($query) || $form->dberror($query);
|
|
423 |
$query = "UPDATE defaults SET coa = ?";
|
|
424 |
do_query($form, $dbh, $query, $form->{chart});
|
|
425 | 425 |
|
426 | 426 |
$dbh->disconnect; |
427 | 427 |
|
... | ... | |
488 | 488 |
|
489 | 489 |
my ($self, $form, $dbh, $filename, $version_or_control) = @_; |
490 | 490 |
|
491 |
# return unless (-f $filename); |
|
492 |
|
|
493 | 491 |
open(FH, "$filename") or $form->error("$filename : $!\n"); |
494 | 492 |
my $query = ""; |
495 | 493 |
my $sth; |
... | ... | |
528 | 526 |
my $errstr = $dbh->errstr; |
529 | 527 |
$sth->finish(); |
530 | 528 |
$dbh->rollback(); |
531 |
$form->dberror("The database update/creation did not succeed. The file ${filename} containing the following query failed:<br>${query}<br>" . |
|
529 |
$form->dberror("The database update/creation did not succeed. " . |
|
530 |
"The file ${filename} containing the following " . |
|
531 |
"query failed:<br>${query}<br>" . |
|
532 | 532 |
"The error message was: ${errstr}<br>" . |
533 | 533 |
"All changes in that file have been reverted."); |
534 | 534 |
} |
... | ... | |
562 | 562 |
$main::lxdebug->enter_sub(); |
563 | 563 |
|
564 | 564 |
my ($self, $form) = @_; |
565 |
|
|
565 |
$form->{db} =~ s/\"//g; |
|
566 | 566 |
my %dbdelete = ('Pg' => qq|DROP DATABASE "$form->{db}"|, |
567 |
'Oracle' => qq|DROP USER $form->{db} CASCADE|);
|
|
567 |
'Oracle' => qq|DROP USER "$form->{db}" CASCADE|);
|
|
568 | 568 |
|
569 | 569 |
$form->{sid} = $form->{dbdefault}; |
570 | 570 |
&dbconnect_vars($form, $form->{dbdefault}); |
571 | 571 |
my $dbh = |
572 | 572 |
DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) |
573 | 573 |
or $form->dberror; |
574 |
my $query = qq|$dbdelete{$form->{dbdriver}}|;
|
|
575 |
$dbh->do($query) || $form->dberror($query);
|
|
574 |
my $query = $dbdelete{$form->{dbdriver}};
|
|
575 |
do_query($form, $dbh, $query);
|
|
576 | 576 |
|
577 | 577 |
$dbh->disconnect; |
578 | 578 |
|
... | ... | |
634 | 634 |
|
635 | 635 |
if ($form->{dbdriver} eq 'Pg') { |
636 | 636 |
|
637 |
$query = qq|SELECT d.datname FROM pg_database d, pg_user u |
|
638 |
WHERE d.datdba = u.usesysid |
|
639 |
AND u.usename = '$form->{dbuser}'|; |
|
640 |
my $sth = $dbh->prepare($query); |
|
641 |
$sth->execute || $form->dberror($query); |
|
637 |
$query = |
|
638 |
qq|SELECT d.datname FROM pg_database d, pg_user u | . |
|
639 |
qq|WHERE d.datdba = u.usesysid AND u.usename = ?|; |
|
640 |
my $sth = prepare_execute_query($form, $dbh, $query, $form->{dbuser}); |
|
642 | 641 |
|
643 | 642 |
while (my ($db) = $sth->fetchrow_array) { |
644 | 643 |
|
... | ... | |
646 | 645 |
|
647 | 646 |
&dbconnect_vars($form, $db); |
648 | 647 |
|
649 |
my $dbh = |
|
648 |
my $dbh2 =
|
|
650 | 649 |
DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) |
651 | 650 |
or $form->dberror; |
652 | 651 |
|
653 |
$query = qq|SELECT t.tablename FROM pg_tables t
|
|
654 |
WHERE t.tablename = 'defaults'|;
|
|
655 |
my $sth = $dbh->prepare($query);
|
|
656 |
$sth->execute || $form->dberror($query);
|
|
652 |
$query = |
|
653 |
qq|SELECT tablename FROM pg_tables | .
|
|
654 |
qq|WHERE tablename = 'defaults'|;
|
|
655 |
my $sth2 = prepare_execute_query($form, $dbh, $query);
|
|
657 | 656 |
|
658 |
if ($sth->fetchrow_array) { |
|
657 |
if ($sth2->fetchrow_array) {
|
|
659 | 658 |
$query = qq|SELECT version FROM defaults|; |
660 |
my $sth = $dbh->prepare($query); |
|
661 |
$sth->execute; |
|
662 |
|
|
663 |
if (my ($version) = $sth->fetchrow_array) { |
|
664 |
$dbsources{$db} = $version; |
|
665 |
} |
|
666 |
$sth->finish; |
|
659 |
my ($version) = selectrow_query($form, $dbh2, $query); |
|
660 |
$dbsources{$db} = $version; |
|
667 | 661 |
} |
668 |
$sth->finish; |
|
669 |
$dbh->disconnect; |
|
662 |
$sth2->finish;
|
|
663 |
$dbh2->disconnect;
|
|
670 | 664 |
} |
671 | 665 |
$sth->finish; |
672 | 666 |
} |
673 | 667 |
|
674 | 668 |
if ($form->{dbdriver} eq 'Oracle') { |
675 |
$query = qq|SELECT o.owner FROM dba_objects o
|
|
676 |
WHERE o.object_name = 'DEFAULTS'
|
|
677 |
AND o.object_type = 'TABLE'|;
|
|
669 |
$query = |
|
670 |
qq|SELECT owner FROM dba_objects |.
|
|
671 |
qq|WHERE object_name = 'DEFAULTS' AND object_type = 'TABLE'|;
|
|
678 | 672 |
|
679 | 673 |
$sth = $dbh->prepare($query); |
680 | 674 |
$sth->execute || $form->dberror($query); |
... | ... | |
708 | 702 |
return %dbsources; |
709 | 703 |
} |
710 | 704 |
|
711 |
## LINET |
|
712 | 705 |
sub calc_version { |
713 | 706 |
$main::lxdebug->enter_sub(2); |
714 | 707 |
|
... | ... | |
750 | 743 |
|
751 | 744 |
return $res_a <=> $res_b; |
752 | 745 |
} |
753 |
## /LINET |
|
754 | 746 |
|
755 | 747 |
sub update_available { |
756 | 748 |
my ($dbdriver, $cur_version) = @_; |
757 | 749 |
|
758 |
opendir SQLDIR, "sql/${dbdriver}-upgrade" or &error("", "sql/${dbdriver}-upgrade: $!"); |
|
750 |
opendir(SQLDIR, "sql/${dbdriver}-upgrade") |
|
751 |
or &error("", "sql/${dbdriver}-upgrade: $!"); |
|
759 | 752 |
my @upgradescripts = |
760 |
grep(/$form->{dbdriver}-upgrade-\Q$cur_version\E.*\.(sql|pl)$/, readdir(SQLDIR)); |
|
761 |
closedir SQLDIR; |
|
753 |
grep(/$form->{dbdriver}-upgrade-\Q$cur_version\E.*\.(sql|pl)$/, |
|
754 |
readdir(SQLDIR)); |
|
755 |
closedir(SQLDIR); |
|
762 | 756 |
|
763 | 757 |
return ($#upgradescripts > -1); |
764 | 758 |
} |
... | ... | |
771 | 765 |
my $query = "SELECT tag FROM schema_info LIMIT 1"; |
772 | 766 |
if (!$dbh->do($query)) { |
773 | 767 |
$query = |
774 |
"CREATE TABLE schema_info (" .
|
|
775 |
" tag text, " .
|
|
776 |
" login text, " .
|
|
777 |
" itime timestamp DEFAULT now(), " .
|
|
778 |
" PRIMARY KEY (tag))";
|
|
768 |
qq|CREATE TABLE schema_info (| .
|
|
769 |
qq| tag text, | .
|
|
770 |
qq| login text, | .
|
|
771 |
qq| itime timestamp DEFAULT now(), | .
|
|
772 |
qq| PRIMARY KEY (tag))|;
|
|
779 | 773 |
$dbh->do($query) || $form->dberror($query); |
780 | 774 |
} |
781 | 775 |
|
... | ... | |
796 | 790 |
if ($form->{dbupdate}) { |
797 | 791 |
|
798 | 792 |
# read update scripts into memory |
799 |
opendir SQLDIR, "sql/" . $form->{dbdriver} . "-upgrade" or &error("", "sql/" . $form->{dbdriver} . "-upgrade : $!");
|
|
800 |
## LINET
|
|
793 |
opendir(SQLDIR, "sql/" . $form->{dbdriver} . "-upgrade")
|
|
794 |
or &error("", "sql/" . $form->{dbdriver} . "-upgrade : $!");
|
|
801 | 795 |
@upgradescripts = |
802 | 796 |
sort(cmp_script_version |
803 |
grep(/$form->{dbdriver}-upgrade-.*?\.(sql|pl)$/, readdir(SQLDIR)));
|
|
804 |
## /LINET
|
|
805 |
closedir SQLDIR;
|
|
797 |
grep(/$form->{dbdriver}-upgrade-.*?\.(sql|pl)$/, |
|
798 |
readdir(SQLDIR)));
|
|
799 |
closedir(SQLDIR);
|
|
806 | 800 |
} |
807 | 801 |
|
808 |
foreach my $db (split / /, $form->{dbupdate}) {
|
|
802 |
foreach my $db (split(/ /, $form->{dbupdate})) {
|
|
809 | 803 |
|
810 | 804 |
next unless $form->{$db}; |
811 | 805 |
|
... | ... | |
819 | 813 |
|
820 | 814 |
# check version |
821 | 815 |
$query = qq|SELECT version FROM defaults|; |
822 |
my $sth = $dbh->prepare($query); |
|
823 |
|
|
824 |
# no error check, let it fall through |
|
825 |
$sth->execute; |
|
826 |
|
|
827 |
my $version = $sth->fetchrow_array; |
|
828 |
$sth->finish; |
|
816 |
my ($version) = selectrow_query($form, $dbh, $query); |
|
829 | 817 |
|
830 | 818 |
next unless $version; |
831 | 819 |
|
832 |
## LINET |
|
833 | 820 |
$version = calc_version($version); |
834 |
## /LINET |
|
835 | 821 |
|
836 | 822 |
foreach my $upgradescript (@upgradescripts) { |
837 | 823 |
my $a = $upgradescript; |
... | ... | |
840 | 826 |
|
841 | 827 |
my ($mindb, $maxdb) = split /-/, $a; |
842 | 828 |
my $str_maxdb = $maxdb; |
843 |
## LINET |
|
844 | 829 |
$mindb = calc_version($mindb); |
845 | 830 |
$maxdb = calc_version($maxdb); |
846 |
## /LINET |
|
847 | 831 |
|
848 | 832 |
next if ($version >= $maxdb); |
849 | 833 |
|
... | ... | |
853 | 837 |
# apply upgrade |
854 | 838 |
$main::lxdebug->message(DEBUG2, "Applying Update $upgradescript"); |
855 | 839 |
if ($file_type eq "sql") { |
856 |
$self->process_query($form, $dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb); |
|
840 |
$self->process_query($form, $dbh, "sql/" . $form->{"dbdriver"} . |
|
841 |
"-upgrade/$upgradescript", $str_maxdb); |
|
857 | 842 |
} else { |
858 |
$self->process_perl_script($form, $dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb); |
|
843 |
$self->process_perl_script($form, $dbh, "sql/" . $form->{"dbdriver"} . |
|
844 |
"-upgrade/$upgradescript", $str_maxdb); |
|
859 | 845 |
} |
860 | 846 |
|
861 | 847 |
$version = $maxdb; |
... | ... | |
899 | 885 |
|
900 | 886 |
map({ $_->{"applied"} = 0; } @upgradescripts); |
901 | 887 |
|
902 |
$query = "SELECT tag FROM schema_info";
|
|
888 |
$query = qq|SELECT tag FROM schema_info|;
|
|
903 | 889 |
$sth = $dbh->prepare($query); |
904 | 890 |
$sth->execute() || $form->dberror($query); |
905 | 891 |
while (($tag) = $sth->fetchrow_array()) { |
... | ... | |
962 | 948 |
|
963 | 949 |
my ($query, $tag, $sth); |
964 | 950 |
|
965 |
$query = "SELECT tag FROM schema_info";
|
|
951 |
$query = qq|SELECT tag FROM schema_info|;
|
|
966 | 952 |
$sth = $dbh->prepare($query); |
967 | 953 |
$sth->execute() || $form->dberror($query); |
968 | 954 |
while (($tag) = $sth->fetchrow_array()) { |
... | ... | |
1095 | 1081 |
my @conf = qw(acs address admin businessnumber charset company countrycode |
1096 | 1082 |
currency dateformat dbconnect dbdriver dbhost dbport dboptions |
1097 | 1083 |
dbname dbuser dbpasswd email fax name numberformat password |
1098 |
printer role sid signature stylesheet tel templates vclimit angebote bestellungen rechnungen |
|
1099 |
anfragen lieferantenbestellungen einkaufsrechnungen taxnumber co_ustid duns menustyle |
|
1100 |
template_format default_media default_printer_id copies show_form_details); |
|
1084 |
printer role sid signature stylesheet tel templates vclimit angebote |
|
1085 |
bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen |
|
1086 |
taxnumber co_ustid duns menustyle template_format default_media |
|
1087 |
default_printer_id copies show_form_details); |
|
1101 | 1088 |
|
1102 | 1089 |
$main::lxdebug->leave_sub(); |
1103 | 1090 |
|
Auch abrufbar als: Unified diff
Keine Form-Variablen direkt in SQL-Queries verwenden. Ein paar kosmetische Änderungen wie Zeilenlängen.