ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.pdoDB.php
Go to the documentation of this file.
1 <?php
2 
3 class pdoDB
4 {
5 
9  protected $pdo;
10  protected static $staticPbo;
14  protected $type_to_mysql_type = array(
15  'text' => 'VARCHAR',
16  'integer' => 'INT',
17  'float' => 'DOUBLE',
18  'date' => 'DATE',
19  'time' => 'TIME',
20  'datetime' => 'TIMESTAMP',
21  'clob' => 'LONGTEXT',
22 
23  );
24 
25 
26  public function __construct()
27  {
28  $this->pdo = new PDO('mysql:host=localhost;dbname=test_db;charset=utf8', 'travis', '');
29  $this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
30  $attr = PDO::MYSQL_ATTR_USE_BUFFERED_QUERY;
31  }
32 
33 
39  public function nextId($table_name)
40  {
41  if ($this->tableExists($table_name . '_seq')) {
42  $table_seq = $table_name . '_seq';
43  $stmt = $this->pdo->prepare("SELECT * FROM $table_seq");
44  $stmt->execute();
45  $rows = $stmt->fetch(PDO::FETCH_ASSOC);
46  $stmt->closeCursor();
47 
48  return count($rows) ? 0 : $rows['seq'];
49  } else {
50  // return $this->pdo->lastInsertId($table_name) + 1;
51  return 0;
52  }
53  }
54 
55 
62  public function createTable($table_name, $fields)
63  {
64  $fields_query = $this->createTableFields($fields);
65  $query = "CREATE TABLE $table_name ($fields_query);";
66  $this->pdo->exec($query);
67  }
68 
69 
75  protected function createTableFields($fields)
76  {
77  $query = "";
78  foreach ($fields as $name => $field) {
79  $type = $this->type_to_mysql_type[$field['type']];
80  $length = $field['length'];
81  $primary = isset($field['is_primary']) && $field['is_primary'] ? "PRIMARY KEY" : "";
82  $notnull = isset($field['is_notnull']) && $field['is_notnull'] ? "NOT NULL" : "";
83  $sequence = isset($field['sequence']) && $field['sequence'] ? "AUTO_INCREMENT" : "";
84  $query .= "$name $type ($length) $sequence $primary $notnull,";
85  }
86 
87  return substr($query, 0, -1);
88  }
89 
90 
95  public function addPrimaryKey($table_name, $primary_keys)
96  {
97  $keys = implode($primary_keys);
98  $this->pdo->exec("ALTER TABLE $table_name ADD PRIMARY KEY ($keys)");
99  }
100 
101 
105  public function createSequence($table_name)
106  {
107  //TODO
108  }
109 
110 
116  public function tableExists($table_name)
117  {
118  $result = $this->pdo->prepare("SHOW TABLES LIKE :table_name");
119  $result->execute(array( ':table_name' => $table_name ));
120  $return = $result->rowCount();
121  $result->closeCursor();
122 
123  return $return > 0;
124  }
125 
126 
133  public function tableColumnExists($table_name, $column_name)
134  {
135  $statement = $this->pdo->query("SHOW COLUMNS FROM $table_name WHERE Field = '$column_name'");
136  $statement != null ? $statement->closeCursor() : "";
137 
138  return $statement != null && $statement->rowCount() != 0;
139  }
140 
141 
147  public function addTableColumn($table_name, $column_name, $attributes)
148  {
149  $col = array( $column_name => $attributes );
150  $col_str = $this->createTableFields($col);
151  $this->pdo->exec("ALTER TABLE $$table_name ADD $$col_str");
152  }
153 
154 
158  public function dropTable($table_name)
159  {
160  $this->pdo->exec("DROP TABLE $table_name");
161  }
162 
163 
169  public function query($query)
170  {
171  $res = $this->pdo->query($query);
172  $err = $this->pdo->errorInfo();
173 
174  return $res;
175  }
176 
177 
183  public function fetchAll($query_result)
184  {
185  return $query_result->fetchAll($query_result);
186  }
187 
188 
192  public function dropSequence($table_name)
193  {
194  $table_seq = $table_name . "_seq";
195  if ($this->tableExists($table_seq)) {
196  $this->pdo->exec("DROP TABLE $table_seq");
197  }
198  }
199 
200 
205  public function dropTableColumn($table_name, $column_name)
206  {
207  $this->pdo->exec("ALTER TABLE $$table_name DROP COLUMN $column_name");
208  }
209 
210 
216  public function renameTableColumn($table_name, $column_old_name, $column_new_name)
217  {
218  $this->pdo->exec("alter table $table_name change $column_old_name $column_new_name");
219  }
220 
221 
226  public function insert($table_name, $values)
227  {
228  $real = array();
229  foreach ($values as $val) {
230  $real[] = $this->quote($val[1], $val[0]);
231  }
232  $values = implode(",", $real);
233  $this->pdo->exec("INSERT INTO $table_name VALUES ($values)");
234  }
235 
236 
242  public function fetchObject($query_result)
243  {
244  $res = $query_result->fetchObject();
245  if ($res == null) {
246  $query_result->closeCursor();
247 
248  return null;
249  }
250 
251  return $res;
252  }
253 
254 
260  public function update($table_name, $values, $where)
261  {
262  $query = "UPDATE $table_name SET ";
263  foreach ($values as $key => $val) {
264  $qval = $this->quote($val[1], $val[0]);
265  $query .= "$key=$qval,";
266  }
267  $query = substr($query, 0, -1) . " WHERE ";
268  foreach ($where as $key => $val) {
269  $qval = $this->quote($val[1], $val[0]);
270  $query .= "$key=$qval,";
271  }
272  $query = substr($query, 0, -1);
273  $this->pdo->exec($query);
274  }
275 
276 
280  public function manipulate($query)
281  {
282  $this->pdo->exec($query);
283  }
284 
285 
291  public function fetchAssoc($query_result)
292  {
293  $res = $query_result->fetch(PDO::FETCH_ASSOC);
294  if ($res == null) {
295  $query_result->closeCursor();
296 
297  return null;
298  }
299 
300  return $res;
301  }
302 
303 
309  public function numRows($query_result)
310  {
311  return $query_result->rowCount();
312  }
313 
314 
321  public function quote($value, $type)
322  {
323  //TODO TYPE SENSITIVE.
324  return $this->pdo->quote($value);
325  }
326 
327 
334  public function addIndex($table_name, $index_name)
335  {
336  return null;
337  }
338 }
dropTable($table_name)
dropSequence($table_name)
$result
$stmt
$type
$type_to_mysql_type
Definition: class.pdoDB.php:14
addTableColumn($table_name, $column_name, $attributes)
addIndex($table_name, $index_name)
tableExists($table_name)
createTable($table_name, $fields)
experimental....
Definition: class.pdoDB.php:62
fetchAll($query_result)
$keys
insert($table_name, $values)
createSequence($table_name)
fetchObject($query_result)
manipulate($query)
fetchAssoc($query_result)
foreach($_POST as $key=> $value) $res
$values
tableColumnExists($table_name, $column_name)
query($query)
__construct()
Definition: class.pdoDB.php:26
$query
renameTableColumn($table_name, $column_old_name, $column_new_name)
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
numRows($query_result)
$rows
Definition: xhr_table.php:10
createTableFields($fields)
Definition: class.pdoDB.php:75
static $staticPbo
Definition: class.pdoDB.php:10
update($table_name, $values, $where)
dropTableColumn($table_name, $column_name)
quote($value, $type)
nextId($table_name)
Definition: class.pdoDB.php:39
$key
Definition: croninfo.php:18
addPrimaryKey($table_name, $primary_keys)
Definition: class.pdoDB.php:95