5 require_once(
'base/Ego_Sql_Abstract.php');
22 function connect($database=
'', $host=
'', $user=
'', $password=
'')
25 if (func_num_args()==0)
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']);
40 $this->_db =
new PDO(
'mysql:host='.$host.
';dbname='.$database, $user, $password);
42 }
catch (PDOException $e) {
45 "Konnte keine Datenbankverbindung herstellen.\n".$e->getMessage(),
49 $this->_db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,
true);
50 $this->_db->query(
'SET NAMES utf8; SET collation_connection=\'utf8_bin\'');
56 private function _changeType($type)
58 $type = str_replace(
'/*bin*/',
'', $type, $count);
71 $type .=
' DEFAULT 0';
74 $type .=
' character set utf8 collate utf8_bin';
89 function createTable($table, $struct, $drop_flag=
true, $notexists_flag=
false)
92 $this->
query(
'DROP TABLE IF EXISTS '.$table);
95 foreach ($struct as $name => $type)
97 $key = explode(
' ', $name);
98 $type = $this->_changeType($type);
101 $query[] = $name.
' '.$type.
' NOT NULL';
106 $query[] =
'PRIMARY KEY ('.$type.
')';
109 $query[] =
'KEY '.$key[1].
' ('.$type.
')';
112 $query[] =
'UNIQUE KEY '.$key[1].
' ('.$type.
')';
116 $this->
query(
'CREATE TABLE '.($notexists_flag?
'IF NOT EXISTS ':
'').$table.
' ('.implode(
',', $query).
117 ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;');
134 if (!$table)
return array();
135 $hdl = $this->_db->query(
'SHOW COLUMNS FROM '.$table);
137 while($record = $hdl->fetch(PDO::FETCH_ASSOC))
140 'field' => $record[
'Field'],
141 'type' => $record[
'Type']
150 foreach ($struct as $name => $type)
152 $key = explode(
' ', $name);
153 $type = $this->_changeType($type);
156 $query[] =
'ADD '.$name.
' '.$type.
' NOT NULL';
161 $query[] =
'ADD PRIMARY KEY ('.$type.
')';
164 $query[] =
'ADD KEY '.$key[1].
' ('.$type.
')';
167 $query[] =
'ADD UNIQUE KEY '.$key[1].
' ('.$type.
')';
170 $query[] =
'ADD FULLTEXT '.$key[1].
' ('.$type.
')';
173 $query[] =
'CHANGE '.$key[1].
' '.$key[2].
' '.$type;
177 return $this->
query(
'ALTER TABLE '.$table.
' '.implode(
',', $query));
185 $this->_db->beginTransaction();
186 $this->_transaction =
true;
194 $this->_transaction =
false;
195 $this->_db->commit();
203 if ($this->_transaction) {
204 $this->_db->rollback();
205 $this->_transaction =
false;
214 $this->
query(
'SELECT @@version');
215 if ($this->
nextRecord() && preg_match(
'/^[0-9.]+/', $this->Record[
'@@version'], $matches)) {
241 $this->
query(
"SHOW TABLES FROM `".$GLOBALS[
'egotec_conf'][
'db'][
'database'].
"` LIKE '$table'");
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'
265 while ($db->nextRecord())
267 if (strpos($db->Record[
'data_type'],
'int')!==
false)
273 $primary[$db->Record[
'column_name']] = $type;
query($query, $appendCurrent=false)
alterTable($table, $struct)
createTable($table, $struct, $drop_flag=true, $notexists_flag=false)
static $_staticInnodbHandle
connect($database='', $host='', $user='', $password='')