EGOCMS  24.0
EGOTEC Content-Managament-System
Ego_Sql_innodb.php
gehe zur Dokumentation dieser Datei
1 <?php
5 require_once('base/Ego_Sql_Abstract.php');
6 
16 {
22  function connect($database='', $host='', $user='', $password='')
23  {
24  try {
25  if (func_num_args()==0)
26  {
28  { // Nur die EGOTEC Standardverbindung wird global gespeichert.
30  return;
31  } else {
32  $this->_db = Ego_Sql_innodb::$_staticInnodbHandle = new PDO(
33  'mysql:host='.$GLOBALS['egotec_conf']['db']['host'].
34  ($GLOBALS['egotec_conf']['db']['port']?';port='.$GLOBALS['egotec_conf']['db']['port']:'').
35  ';dbname='.$GLOBALS['egotec_conf']['db']['database'],
36  $GLOBALS['egotec_conf']['db']['user'],
37  $GLOBALS['egotec_conf']['db']['password']);
38  }
39  } else {
40  $this->_db = new PDO('mysql:host='.$host.';dbname='.$database, $user, $password);
41  }
42  } catch (PDOException $e) {
43  $this->onError('connect');
44  throw new Ego_Sql_Exception(
45  "Konnte keine Datenbankverbindung herstellen.\n".$e->getMessage(),
47  );
48  }
49  $this->_db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
50  $this->_db->query('SET NAMES utf8; SET collation_connection=\'utf8_bin\'');
51  }
52 
56  private function _changeType($type)
57  {
58  $type = str_replace('/*bin*/', '', $type, $count);
59 
60  switch ($type)
61  {
62  case 'smalltext':
63  $type = 'TEXT';
64  break;
65  case 'text':
66  $type = 'LONGTEXT';
67  break;
68  case 'tinyint':
69  case 'int':
70  case 'bigint':
71  $type .= ' DEFAULT 0';
72  }
73  if ($count > 0) {
74  $type .= ' character set utf8 collate utf8_bin';
75  }
76  return $type;
77  }
78 
89  function createTable($table, $struct, $drop_flag=true, $notexists_flag=false)
90  {
91  if ($drop_flag) {
92  $this->query('DROP TABLE IF EXISTS '.$table); // Eine evtl. vorhandene Tabelle entfernen.
93  }
94  $query = array();
95  foreach ($struct as $name => $type)
96  {
97  $key = explode(' ', $name);
98  $type = $this->_changeType($type);
99  if (sizeof($key)==1)
100  { // Ein Feld erzeugen.
101  $query[] = $name.' '.$type.' NOT NULL';
102  } else {
103  switch ($key[0])
104  {
105  case 'PRIMARY':
106  $query[] = 'PRIMARY KEY ('.$type.')';
107  break;
108  case 'KEY':
109  $query[] = 'KEY '.$key[1].' ('.$type.')';
110  break;
111  case 'UNIQUE':
112  $query[] = 'UNIQUE KEY '.$key[1].' ('.$type.')';
113  }
114  }
115  }
116  $this->query('CREATE TABLE '.($notexists_flag?'IF NOT EXISTS ':'').$table.' ('.implode(',', $query).
117  ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;');
118  }
119 
132  public function showColumns($table)
133  {
134  if (!$table) return array();
135  $hdl = $this->_db->query('SHOW COLUMNS FROM '.$table);
136  $cols = array();
137  while($record = $hdl->fetch(PDO::FETCH_ASSOC))
138  {
139  $cols[] = array(
140  'field' => $record['Field'],
141  'type' => $record['Type']
142  );
143  }
144  return $cols;
145  }
146 
147  function alterTable($table, $struct)
148  {
149  $query = array();
150  foreach ($struct as $name => $type)
151  {
152  $key = explode(' ', $name);
153  $type = $this->_changeType($type);
154  if (sizeof($key)==1)
155  { // Ein Feld erzeugen.
156  $query[] = 'ADD '.$name.' '.$type.' NOT NULL';
157  } else {
158  switch ($key[0])
159  {
160  case 'PRIMARY':
161  $query[] = 'ADD PRIMARY KEY ('.$type.')';
162  break;
163  case 'KEY':
164  $query[] = 'ADD KEY '.$key[1].' ('.$type.')';
165  break;
166  case 'UNIQUE':
167  $query[] = 'ADD UNIQUE KEY '.$key[1].' ('.$type.')';
168  break;
169  case 'FULLTEXT':
170  $query[] = 'ADD FULLTEXT '.$key[1].' ('.$type.')';
171  break;
172  case 'CHANGE':
173  $query[] = 'CHANGE '.$key[1].' '.$key[2].' '.$type;
174  }
175  }
176  }
177  return $this->query('ALTER TABLE '.$table.' '.implode(',', $query));
178  }
179 
183  function begin()
184  {
185  $this->_db->beginTransaction();
186  $this->_transaction = true;
187  }
188 
192  function commit()
193  {
194  $this->_transaction = false;
195  $this->_db->commit();
196  }
197 
201  function rollback()
202  {
203  if ($this->_transaction) { // Die Transaktion nur zurücksetzen wenn eine gestartet wurde.
204  $this->_db->rollback();
205  $this->_transaction = false;
206  }
207  }
208 
213  function getVersion() {
214  $this->query('SELECT @@version');
215  if ($this->nextRecord() && preg_match('/^[0-9.]+/', $this->Record['@@version'], $matches)) {
216  return $matches[0];
217  }
218  return '';
219  }
220 
236  function tableExists($table) {
237  if (!$table)
238  {
239  return false;
240  }
241  $this->query("SHOW TABLES FROM `".$GLOBALS['egotec_conf']['db']['database']."` LIKE '$table'");
242 
243  return (bool)$this->nextRecord();
244  }
245 
252  function getPrimary($table)
253  {
254  $db = clone $this;
255  $db->select(array(
256  'fields' => 'cols.column_name,cols2.data_type',
257  'table' => 'information_schema.table_constraints cons,information_schema.key_column_usage cols,information_schema.columns cols2',
258  'where' => 'cols.table_name=\''.$table.'\' '.
259  'AND cols.table_schema=\''.$GLOBALS['egotec_conf']['db']['database'].'\''.
260  'AND cons.constraint_type=\'PRIMARY KEY\' '.
261  'AND cols.constraint_name=cons.constraint_name AND cols.table_schema=cons.table_schema AND cols.table_name=cons.table_name '.
262  'AND cols2.table_schema=cons.table_schema AND cols2.table_name=cons.table_name AND cols2.column_name=cols.column_name'
263  ));
264  $primary = array();
265  while ($db->nextRecord())
266  {
267  if (strpos($db->Record['data_type'], 'int')!==false)
268  {
269  $type = 'int';
270  } else {
271  $type = 'string';
272  }
273  $primary[$db->Record['column_name']] = $type;
274  }
275  return $primary;
276  }
277 }
278 ?>
query($query, $appendCurrent=false)
alterTable($table, $struct)
createTable($table, $struct, $drop_flag=true, $notexists_flag=false)
static $_staticInnodbHandle
connect($database='', $host='', $user='', $password='')