From bf6beb5f4374938b20af65af76003376d9ea0ffd Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Fri, 9 Nov 2007 15:54:04 +0000 Subject: * started to work on configDB-tests git-svn-id: http://svn.openslx.org/svn/openslx/trunk@1407 95ad53e4-c205-0410-b2fa-d234c58c8868 --- config-db/OpenSLX/ConfigDB.pm | 130 +++++++++++++++---------------------- config-db/OpenSLX/MetaDB/DBI.pm | 20 +++--- config-db/OpenSLX/MetaDB/SQLite.pm | 32 ++++++++- 3 files changed, 92 insertions(+), 90 deletions(-) (limited to 'config-db/OpenSLX') diff --git a/config-db/OpenSLX/ConfigDB.pm b/config-db/OpenSLX/ConfigDB.pm index ed86c8e1..e286a4d7 100644 --- a/config-db/OpenSLX/ConfigDB.pm +++ b/config-db/OpenSLX/ConfigDB.pm @@ -93,8 +93,6 @@ use OpenSLX::Basics; use OpenSLX::DBSchema; use OpenSLX::Utils; -our $configDBInstance; - =head1 Methods =head2 Basic Methods @@ -112,25 +110,12 @@ new will return the *same* object =cut sub new -{ # TODO: it would be better to allow new() to do what any caller expects and - # rename this to instance() or anything similar ... +{ my $class = shift; - if ($configDBInstance) { - return $configDBInstance; - } - - $configDBInstance = { - connectedCount => 0, + my $self = { }; - return bless $configDBInstance, $class; -} - -sub DESTROY -{ - my $self = shift; - - $configDBInstance = undef; + return bless $self, $class; } =item C @@ -165,49 +150,45 @@ sub connect ## no critic (ProhibitBuiltinHomonyms) # hash-ref with any additional info that might be required by # specific metadb-module (not used yet) - if (!$self->{connectedCount}) { - - my $dbType = $openslxConfig{'db-type'}; - # name of underlying database module... - - my $dbModuleName = "OpenSLX/MetaDB/$dbType.pm"; - my $dbModule = "OpenSLX::MetaDB::$dbType"; - unless (eval { require $dbModuleName } ) { - if ($! == 2) { - die _tr( - "Unable to load DB-module <%s>\nthat database type is not supported (yet?)\n", - $dbModuleName - ); - } else { - die _tr("Unable to load DB-module <%s> (%s)\n", $dbModuleName, $@); - } - } - my $metaDB = $dbModule->new(); - if (!$metaDB->connect($dbParams)) { - warn _tr("Unable to connect to DB-module <%s>\n%s", $dbModuleName, $@); - warn _tr("These DB-modules seem to work ok:"); - foreach my $dbMod ('mysql', 'SQLite') { - my $fullDbModName = "DBD/$dbMod.pm"; - if (eval { require $fullDbModName }) { - vlog(0, "\t$dbMod\n"); - } - } + my $dbType = $openslxConfig{'db-type'}; + # name of underlying database module... + + my $dbModuleName = "OpenSLX/MetaDB/$dbType.pm"; + my $dbModule = "OpenSLX::MetaDB::$dbType"; + unless (eval { require $dbModuleName } ) { + if ($! == 2) { die _tr( - 'Please use slxsettings if you want to switch to another db-type.'); + "Unable to load DB-module <%s>\nthat database type is not supported (yet?)\n", + $dbModuleName + ); + } else { + die _tr("Unable to load DB-module <%s> (%s)\n", $dbModuleName, $@); } - - $self->{'db-type'} = $dbType; - $self->{'meta-db'} = $metaDB; - foreach my $tk (keys %{$DbSchema->{tables}}) { - $metaDB->schemaDeclareTable($tk, $DbSchema->{tables}->{$tk}); + } + my $metaDB = $dbModule->new(); + if (!$metaDB->connect($dbParams)) { + warn _tr("Unable to connect to DB-module <%s>\n%s", $dbModuleName, $@); + warn _tr("These DB-modules seem to work ok:"); + foreach my $dbMod ('mysql', 'SQLite') { + my $fullDbModName = "DBD/$dbMod.pm"; + if (eval { require $fullDbModName }) { + vlog(0, "\t$dbMod\n"); + } } + die _tr( + 'Please use slxsettings if you want to switch to another db-type.' + ); + } - _checkAndUpgradeDBSchemaIfNecessary($metaDB); + $self->{'db-type'} = $dbType; + $self->{'meta-db'} = $metaDB; + foreach my $tk (keys %{$DbSchema->{tables}}) { + $metaDB->schemaDeclareTable($tk, $DbSchema->{tables}->{$tk}); } - $self->{connectedCount}++; + _checkAndUpgradeDBSchemaIfNecessary($metaDB); - return; + return 1; } =item C @@ -220,18 +201,15 @@ sub disconnect { my $self = shift; - if ($self->{connectedCount} == 1) { - $self->{'meta-db'}->disconnect(); - } - $self->{connectedCount}--; + $self->{'meta-db'}->disconnect(); - return; + return 1; } =item C -Opens a database transaction - most useful if you want to make sure a couple of changes -apply as a whole or not at all. +Opens a database transaction - most useful if you want to make sure a couple of +changes apply as a whole or not at all. =cut @@ -240,13 +218,13 @@ sub startTransaction my $self = shift; $self->{'meta-db'}->startTransaction(); - return; + return 1; } =item C -Commits a database transaction - so all changes done inside of this transaction will -be applied to the database. +Commits a database transaction - so all changes done inside of this transaction +will be applied to the database. =cut @@ -255,13 +233,13 @@ sub commitTransaction my $self = shift; $self->{'meta-db'}->commitTransaction(); - return; + return 1; } =item C -Revokes a database transaction - so all changes done inside of this transaction will -be undone. +Revokes a database transaction - so all changes done inside of this transaction +will be undone. =cut @@ -270,7 +248,7 @@ sub rollbackTransaction my $self = shift; $self->{'meta-db'}->rollbackTransaction(); - return; + return 1; } =back @@ -2024,7 +2002,7 @@ sub emptyDatabase my @vendorOSIDs = map { $_->{id} } $self->fetchVendorOSByFilter(); $self->removeVendorOS(\@vendorOSIDs); - return; + return 1; } =back @@ -2062,7 +2040,7 @@ sub mergeDefaultAttributesIntoSystem my $defaultClient = $self->fetchClientByFilter({name => '<<>>'}); pushAttributes($system, $defaultClient); - return; + return 1; } =item C @@ -2105,7 +2083,7 @@ sub mergeDefaultAndGroupAttributesIntoClient vlog(3, _tr('merging from default client...')); my $defaultClient = $self->fetchClientByFilter({name => '<<>>'}); mergeAttributes($client, $defaultClient); - return; + return 1; } =item C @@ -2348,7 +2326,7 @@ sub mergeAttributes $target->{$key} = $sourceVal; } } - return; + return 1; } =item C @@ -2385,7 +2363,7 @@ sub pushAttributes $target->{$key} = $sourceVal; } } - return; + return 1; } =item C @@ -2600,7 +2578,7 @@ sub _checkAndUpgradeDBSchemaIfNecessary } else { vlog(1, _tr('DB matches current schema version %s', $currVersion)); } - return; + return 1; } sub _aref @@ -2618,7 +2596,3 @@ sub _unique } 1; - -=back - -=cut diff --git a/config-db/OpenSLX/MetaDB/DBI.pm b/config-db/OpenSLX/MetaDB/DBI.pm index b80f3c0a..bb0a8fdc 100644 --- a/config-db/OpenSLX/MetaDB/DBI.pm +++ b/config-db/OpenSLX/MetaDB/DBI.pm @@ -86,14 +86,15 @@ sub _doSelect vlog(3, _trim($sql)); my $sth = $dbh->prepare($sql) - or - croak _tr(q[Can't prepare SQL-statement <%s> (%s)], $sql, $dbh->errstr); + or croak _tr( + q[Can't prepare SQL-statement <%s> (%s)], $sql, $dbh->errstr + ); $sth->execute() - or - croak _tr(q[Can't execute SQL-statement <%s> (%s)], $sql, $dbh->errstr); - my (@vals, $row); - while ($row = $sth->fetchrow_hashref()) { - + or croak _tr( + q[Can't execute SQL-statement <%s> (%s)], $sql, $dbh->errstr + ); + my @vals; + while (my $row = $sth->fetchrow_hashref()) { if (defined $resultCol) { return $row->{$resultCol} unless wantarray(); push @vals, $row->{$resultCol}; @@ -113,10 +114,11 @@ sub fetchVendorOSByFilter $resultCols = '*' unless (defined $resultCols); my $sql = "SELECT $resultCols FROM vendor_os"; - my $connector; + my ($connector, $quotedVal); foreach my $col (keys %$filter) { $connector = !defined $connector ? 'WHERE' : 'AND'; - $sql .= " $connector $col = '$filter->{$col}'"; + $quotedVal = $self->{dbh}->quote($filter->{$col}); + $sql .= " $connector $col = $quotedVal"; } return $self->_doSelect($sql); } diff --git a/config-db/OpenSLX/MetaDB/SQLite.pm b/config-db/OpenSLX/MetaDB/SQLite.pm index 558aebf9..c0725191 100644 --- a/config-db/OpenSLX/MetaDB/SQLite.pm +++ b/config-db/OpenSLX/MetaDB/SQLite.pm @@ -35,6 +35,27 @@ sub new return bless $self, $class; } +sub databaseExists +{ + my $self = shift; + + my $fullDBPath = $self->_getDBPath() . "/$openslxConfig{'db-name'}"; +print "$fullDBPath\n"; + return -e $fullDBPath; +} + +sub dropDatabase +{ + my $self = shift; + + if ($self->{dbh}) { + die "need to disconnect before you can drop the database!"; + } + + my $fullDBPath = $self->_getDBPath() . "/$openslxConfig{'db-name'}"; + system("rm -rf $fullDBPath") if -e $fullDBPath; +} + sub connect ## no critic (ProhibitBuiltinHomonyms) { my $self = shift; @@ -42,9 +63,7 @@ sub connect ## no critic (ProhibitBuiltinHomonyms) my $dbSpec = $openslxConfig{'db-spec'}; if (!defined $dbSpec) { # build $dbSpec from individual parameters: - my $dbBasepath = "$openslxConfig{'private-path'}/db"; - my $dbDatadir = 'sqlite'; - my $dbPath = "$dbBasepath/$dbDatadir"; + my $dbPath = $self->_getDBPath; system("mkdir -p $dbPath") unless -e $dbPath; $dbSpec = "dbname=$dbPath/$openslxConfig{'db-name'}"; } @@ -102,4 +121,11 @@ sub schemaAddColumns return; } +sub _getDBPath +{ + my $self = shift; + + return "$openslxConfig{'private-path'}/db/sqlite"; +} + 1; -- cgit v1.2.3-55-g7522