20 use Laminas\Db\Adapter\Adapter;
29 private static function createConnection($connectionParams) {
31 if (isset($connectionParams[
'dbType']) && isset($connectionParams[
'dbHostName']) &&
32 isset($connectionParams[
'dbUserName']) && isset($connectionParams[
'dbPassword']) &&
33 isset($connectionParams[
'dbName'])) {
36 $charSet = isset($connectionParams[
'dbCharSet']) ? $connectionParams[
'dbCharSet'] :
'utf8';
37 $dbType = strtolower($connectionParams[
'dbType']);
40 $pdoParams = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
44 $pdoParams[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] =
true;
45 $pdoParams[PDO::MYSQL_ATTR_INIT_COMMAND] =
"SET NAMES ".$charSet;
48 if (strtolower($connectionParams[
'dbName']) ==
':memory:') {
49 $pdoParams[PDO::ATTR_PERSISTENT] =
true;
52 $connectionParams[
'dbName'] =
FileUtil::realpath(WCMF_BASE.$connectionParams[
'dbName']);
58 'host' => $connectionParams[
'dbHostName'],
59 'username' => $connectionParams[
'dbUserName'],
60 'password' => $connectionParams[
'dbPassword'],
61 'database' => $connectionParams[
'dbName'],
62 'driver' =>
'Pdo_'.ucfirst($connectionParams[
'dbType']),
63 'driver_options' => $pdoParams
65 if (!empty($connectionParams[
'dbPort'])) {
66 $params[
'port'] = $connectionParams[
'dbPort'];
68 $adapter =
new Adapter($params);
69 $conn = $adapter->getDriver()->getConnection()->getResource();
72 catch(\Exception $ex) {
74 $connectionParams[
'dbName'].
" failed: ".$ex->getMessage());
90 if (file_exists($file)) {
91 $logger->info(
'Executing SQL script '.$file.
' ...');
95 if (($connectionParams = $config->getSection($initSection)) ===
false) {
99 $conn = self::createConnection($connectionParams);
101 $logger->debug(
'Starting transaction ...');
102 $conn->beginTransaction();
105 $fh = fopen($file,
'r');
108 $command = fgets($fh, 8192);
109 if (strlen(trim($command)) > 0) {
110 $logger->debug(
'Executing command: '.preg_replace(
'/[\n]+$/',
'', $command));
112 $conn->query($command);
114 catch(\Exception $ex) {
122 if ($exception ==
null) {
123 $logger->debug(
'Execution succeeded, committing ...');
127 $logger->error(
'Execution failed. Reason'.$exception->getMessage());
128 $logger->debug(
'Rolling back ...');
131 $logger->debug(
'Finished SQL script '.$file.
'.');
134 $logger->error(
'SQL script '.$file.
' not found.');
146 public static function copyDatabase($srcName, $destName, $server, $user, $password) {
148 if ($srcName && $destName && $server && $user) {
154 $conn =
new PDO(
"mysql:host=$server", $user, $password);
156 catch(\Exception $ex) {
160 $conn->beginTransaction();
163 foreach ($conn->query(
"SHOW TABLES FROM $srcName") as $row) {
165 $sqlStmt =
"CREATE TABLE $destName.$row[0] LIKE $srcName.$row[0]";
166 $logger->debug($sqlStmt);
167 $result = $conn->query($sqlStmt);
173 $sqlStmt =
"INSERT INTO $destName.$row[0] SELECT * FROM $srcName.$row[0]";
174 $logger->debug($sqlStmt);
175 $result = $conn->query($sqlStmt);
181 }
catch (\Exception $ex) {
196 if($name && $server && $user) {
200 $conn =
new PDO(
"mysql:host=$server", $user, $password);
202 catch(\Exception $ex) {
206 $sqlStmt =
"CREATE DATABASE IF NOT EXISTS $name";
207 $result = $conn->query($sqlStmt);
static executeScript($file, $initSection)
Execute a sql script.
PersistenceException signals an exception in the persistence service.
IllegalArgumentException signals an exception in method arguments.
ConfigurationException signals an exception in the configuration.
static copyDatabase($srcName, $destName, $server, $user, $password)
Duplicate a database on the same server (same user).
FileUtil provides basic support for file functionality like HTTP file upload.
static getLogger($name)
Get the logger with the given name.
static getInstance($name, $dynamicConfiguration=[])
static createDatabase($name, $server, $user, $password)
Crate a database on the server.
DBUtil provides database helper functions.
LogManager is used to retrieve Logger instances.
static realpath($path)
Realpath function that also works for non existing paths code from http://www.php....
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...