wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.DBUtil.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.DBUtil.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/model/mapper/class.RDBMapper.php");
20 require_once(BASE."wcmf/lib/util/class.Log.php");
21 require_once(BASE."wcmf/lib/util/class.InifileParser.php");
22 
23 /**
24  * @class DBUtil
25  * @ingroup Util
26  * @brief DBUtil provides database helper functions.
27  *
28  * @author ingo herwig <ingo@wemove.com>
29  */
30 class DBUtil
31 {
32  /**
33  * Execute a sql script. Execution is done inside a transaction, which is rolled back in case of failure.
34  * @param file The filename of the sql script
35  * @param initSection The name of the configuration section that defines the database connection
36  * @return True/False wether execution succeeded or not.
37  */
38  function executeScript($file, $initSection)
39  {
40  if (file_exists($file))
41  {
42  Log::info('Executing SQL script '.$file.' ...', __CLASS__);
43 
44  // find init params
45  $parser = &InifileParser::getInstance();
46  $initParams = null;
47  if (($initParams = $parser->getSection($initSection)) === false)
48  {
49  WCMFException::throwEx("No '".$initSection."' section given in configfile.", __FILE__, __LINE__);
50  return false;
51  }
52  // connect to the database using a RDBMapper instance
53  $mapper = new RDBMapper($initParams);
54  $connection = $mapper->getConnection();
55 
56  Log::debug('Starting transaction ...', __CLASS__);
57  $connection->beginTransaction();
58 
59  $exception = null;
60  $fh = fopen($file, 'r');
61  if ($fh)
62  {
63  while (!feof($fh))
64  {
65  $command = fgets($fh, 8192);
66  if (strlen(trim($command)) > 0)
67  {
68  Log::debug('Executing command: '.$command, __CLASS__);
69  try {
70  $connection->query($command);
71  }
72  catch(PDOException $ex) {
73  $exception = $ex;
74  break;
75  }
76  }
77  }
78  fclose($fh);
79  }
80  if ($exception == null)
81  {
82  Log::debug('Execution succeeded, committing ...', __CLASS__);
83  $connection->commit();
84  }
85  else
86  {
87  Log::error('Execution failed. Reason'.$exception->getMessage(), __CLASS__);
88  Log::debug('Rolling back ...', __CLASS__);
89  $connection->rollBack();
90  }
91  Log::debug('Finished SQL script '.$file.'.', __CLASS__);
92  }
93  else
94  {
95  Log::error('SQL script '.$file.' not found.', __CLASS__);
96  }
97  }
98 
99  /**
100  * Duplicate a database on the same server (same user). This works only for MySQL databases.
101  * @param srcName The name of the source database
102  * @param destName The name of the source database
103  * @param server The name of the database server
104  * @param user The user name
105  * @param password The password
106  */
107  public static function copyDatabase($srcName, $destName, $server, $user, $password)
108  {
109  if($srcName && $destName && $server && $user)
110  {
111  DBUtil::createDatabase($destName, $server, $user, $password);
112 
113  // setup connection
114  $dbConnect = mysql_connect($server, $user, $password);
115  if (!$dbConnect) {
116  WCMFException::throwEx("Couldn't connect to MySql: ".mysql_error(), __FILE__, __LINE__);
117  }
118  // get table list from source database
119  $sqlStatement = "SHOW TABLES FROM ".$srcName;
120  $tables = mysql_query($sqlStatement, $dbConnect);
121  if ($tables)
122  {
123  while($row = mysql_fetch_row($tables))
124  {
125  // create new table
126  $sqlStatement = "CREATE TABLE ".$destName.".".$row[0]." LIKE ".$srcName.".".$row[0];
127  Log::debug($sqlStatement, __CLASS__);
128  $result = mysql_query($sqlStatement, $dbConnect);
129  if (!$result) {
130  WCMFException::throwEx("Couldn't create table: ".mysql_error(), __FILE__, __LINE__);
131  }
132  mysql_free_result($result);
133 
134  // insert data
135  $sqlStatement = "INSERT INTO ".$destName.".".$row[0]." SELECT * FROM ".$srcName.".".$row[0];
136  Log::debug($sqlStatement, __CLASS__);
137  $result = mysql_query($sqlStatement, $dbConnect);
138  if (!$result) {
139  WCMFException::throwEx("Couldn't copy data: ".mysql_error(), __FILE__, __LINE__);
140  }
141  mysql_free_result($result);
142  }
143  mysql_free_result($tables);
144  mysql_close($dbConnect);
145  }
146  else {
147  WCMFException::throwEx("Couldn't select tables: ".mysql_error(), __FILE__, __LINE__);
148  }
149  }
150  }
151  /**
152  * Crate a database on the server. This works only for MySQL databases.
153  * @param name The name of the source database
154  * @param server The name of the database server
155  * @param user The user name
156  * @param password The password
157  */
158  public static function createDatabase($name, $server, $user, $password)
159  {
160  $created = false;
161  if($name && $server && $user)
162  {
163  // setup connection
164  $dbConnect = mysql_connect($server, $user, $password);
165  if (!$dbConnect) {
166  WCMFException::throwEx("Couldn't connect to MySql: ".mysql_error(), __FILE__, __LINE__);
167  }
168  // create database
169  $sqlStatement = "CREATE DATABASE IF NOT EXISTS ".$name;
170  $result = mysql_query($sqlStatement, $dbConnect);
171  if ($result) {
172  $created = true;
173  }
174  mysql_free_result($result);
175  mysql_close($dbConnect);
176  if (!$created) {
177  WCMFException::throwEx("Couldn't create database:".mysql_error(), __FILE__, __LINE__);
178  }
179  }
180  }
181 }
182 ?>
executeScript($file, $initSection)
RDBMapper maps objects of one type to a relational database schema. It defines a persistence mechanis...
error($message, $category)
Definition: class.Log.php:69
debug($message, $category)
Definition: class.Log.php:39
info($message, $category)
Definition: class.Log.php:49
throwEx($message, $file='', $line='')
static createDatabase($name, $server, $user, $password)
DBUtil provides database helper functions.
static copyDatabase($srcName, $destName, $server, $user, $password)