XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
dbmanager.php
Go to the documentation of this file.
1 <?php
2 // ------------------------------------------------------------------------ //
3 // XOOPS - PHP Content Management System //
4 // Copyright (c) 2000 XOOPS.org //
5 // <http://www.xoops.org/> //
6 // ------------------------------------------------------------------------ //
7 // This program is free software; you can redistribute it and/or modify //
8 // it under the terms of the GNU General Public License as published by //
9 // the Free Software Foundation; either version 2 of the License, or //
10 // (at your option) any later version. //
11 // //
12 // You may not change or alter any portion of this comment or credits //
13 // of supporting developers from this source code or any supporting //
14 // source code which is considered copyrighted (c) material of the //
15 // original comment or credit authors. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details. //
21 // //
22 // You should have received a copy of the GNU General Public License //
23 // along with this program; if not, write to the Free Software //
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
25 // ------------------------------------------------------------------------ //
26 include_once XOOPS_ROOT_PATH . '/class/logger/xoopslogger.php';
27 include_once XOOPS_ROOT_PATH . '/class/database/databasefactory.php';
28 include_once XOOPS_ROOT_PATH . '/class/database/' . XOOPS_DB_TYPE . 'database.php';
29 include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
30 
39 {
40 
41  var $s_tables = array();
42  var $f_tables = array();
43  var $db;
44 
45  function db_manager()
46  {
48  $this->db->setPrefix(XOOPS_DB_PREFIX);
49  $this->db->setLogger(XoopsLogger::getInstance());
50  }
51 
52  function isConnectable()
53  {
54  return ($this->db->connect(false) != false) ? true : false;
55  }
56 
57  function dbExists()
58  {
59  return ($this->db->connect() != false) ? true : false;
60  }
61 
62  function createDB()
63  {
64  $this->db->connect(false);
65 
66  $result = $this->db->query("CREATE DATABASE " . XOOPS_DB_NAME);
67 
68  return ($result != false) ? true : false;
69  }
70 
71  function queryFromFile($sql_file_path)
72  {
73  $tables = array();
74 
75  if (!file_exists($sql_file_path)) {
76  return false;
77  }
78  $sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
79  SqlUtility::splitMySqlFile($pieces, $sql_query);
80  $this->db->connect();
81  foreach ($pieces as $piece) {
82  $piece = trim($piece);
83  // [0] contains the prefixed query
84  // [4] contains unprefixed table name
85  $prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix());
86  if ($prefixed_query != false ) {
87  $table = $this->db->prefix($prefixed_query[4]);
88  if ($prefixed_query[1] == 'CREATE TABLE') {
89  if ($this->db->query($prefixed_query[0]) != false) {
90  if (!isset($this->s_tables['create'][$table])) {
91  $this->s_tables['create'][$table] = 1;
92  }
93  } else {
94  if (!isset($this->f_tables['create'][$table])) {
95  $this->f_tables['create'][$table] = 1;
96  }
97  }
98  } else if ($prefixed_query[1] == 'INSERT INTO') {
99  if ($this->db->query($prefixed_query[0]) != false) {
100  if (!isset($this->s_tables['insert'][$table])) {
101  $this->s_tables['insert'][$table] = 1;
102  } else {
103  $this->s_tables['insert'][$table]++;
104  }
105  } else {
106  if (!isset($this->f_tables['insert'][$table])) {
107  $this->f_tables['insert'][$table] = 1;
108  } else {
109  $this->f_tables['insert'][$table]++;
110  }
111  }
112  } else if ($prefixed_query[1] == 'ALTER TABLE') {
113  if ($this->db->query($prefixed_query[0]) != false) {
114  if (!isset($this->s_tables['alter'][$table])) {
115  $this->s_tables['alter'][$table] = 1;
116  }
117  } else {
118  if (!isset($this->s_tables['alter'][$table])) {
119  $this->f_tables['alter'][$table] = 1;
120  }
121  }
122  } else if ($prefixed_query[1] == 'DROP TABLE') {
123  if ($this->db->query('DROP TABLE '.$table) != false) {
124  if (!isset($this->s_tables['drop'][$table])) {
125  $this->s_tables['drop'][$table] = 1;
126  }
127  } else {
128  if (!isset($this->s_tables['drop'][$table])) {
129  $this->f_tables['drop'][$table] = 1;
130  }
131  }
132  }
133  }
134  }
135  return true;
136  }
137 
138  var $successStrings = array(
139  'create' => TABLE_CREATED,
140  'insert' => ROWS_INSERTED,
141  'alter' => TABLE_ALTERED,
142  'drop' => TABLE_DROPPED,
143  );
144  var $failureStrings = array(
145  'create' => TABLE_NOT_CREATED,
146  'insert' => ROWS_FAILED,
147  'alter' => TABLE_NOT_ALTERED,
148  'drop' => TABLE_NOT_DROPPED,
149  );
150 
151 
152  function report()
153  {
154  $commands = array( 'create', 'insert', 'alter', 'drop' );
155  $content = '<ul class="log">';
156  foreach ($commands as $cmd) {
157  if (!@empty($this->s_tables[$cmd])) {
158  foreach ($this->s_tables[$cmd] as $key => $val) {
159  $content .= '<li class="success">';
160  $content .= ($cmd != 'insert') ? sprintf($this->successStrings[$cmd], $key) : sprintf($this->successStrings[$cmd], $val, $key);
161  $content .= "</li>\n";
162  }
163  }
164  }
165  foreach ($commands as $cmd) {
166  if (!@empty( $this->f_tables[$cmd])) {
167  foreach ($this->f_tables[$cmd] as $key => $val) {
168  $content .= '<li class="failure">';
169  $content .= ($cmd != 'insert') ? sprintf($this->failureStrings[$cmd], $key) : sprintf($this->failureStrings[$cmd], $val, $key);
170  $content .= "</li>\n";
171  }
172  }
173  }
174  $content .= '</ul>';
175  return $content;
176  }
177 
178  function query($sql)
179  {
180  $this->db->connect();
181  return $this->db->query($sql);
182  }
183 
184  function prefix($table)
185  {
186  $this->db->connect();
187  return $this->db->prefix($table);
188  }
189 
190  function fetchArray($ret)
191  {
192  $this->db->connect();
193  return $this->db->fetchArray($ret);
194  }
195 
196  function insert($table, $query)
197  {
198  $this->db->connect();
199  $table = $this->db->prefix($table);
200  $query = 'INSERT INTO ' . $table . ' '. $query;
201  if (!$this->db->queryF($query)) {
202  if (!isset($this->f_tables['insert'][$table])) {
203  $this->f_tables['insert'][$table] = 1;
204  } else {
205  $this->f_tables['insert'][$table]++;
206  }
207  return false;
208  } else {
209  if (!isset($this->s_tables['insert'][$table])) {
210  $this->s_tables['insert'][$table] = 1;
211  } else {
212  $this->s_tables['insert'][$table]++;
213  }
214  return $this->db->getInsertId();
215  }
216  }
217 
218  function isError()
219  {
220  return (isset($this->f_tables)) ? true : false;
221  }
222 
224  {
225  $deleted = array();
226  $this->db->connect();
227  foreach ($tables as $key => $val) {
228  if (!$this->db->query("DROP TABLE " . $this->db->prefix($key))) {
229  $deleted[] = $ct;
230  }
231  }
232  return $deleted;
233  }
234 
235  function tableExists($table)
236  {
237  $table = trim($table);
238  $ret = false;
239  if ($table != '') {
240  $this->db->connect();
241  $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix($table);
242  $ret = (false != $this->db->query($sql)) ? true : false;
243  }
244  return $ret;
245  }
246 }
247 
248 ?>