19 'datetime' =>
'TIMESTAMP',
26 $this->pdo =
new PDO(
'mysql:host=localhost;dbname=test_db;charset=utf8',
'travis',
'');
27 $this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,
true);
28 $attr = PDO::MYSQL_ATTR_USE_BUFFERED_QUERY;
37 public function nextId($table_name) {
39 $table_seq = $table_name .
'_seq';
40 $stmt = $this->pdo->prepare(
"SELECT * FROM $table_seq");
42 $rows = $stmt->fetch(PDO::FETCH_ASSOC);
45 return count($rows) ? 0 : $rows[
'seq'];
61 $query =
"CREATE TABLE $table_name ($fields_query);";
73 foreach ($fields as $name => $field) {
74 $type = $this->type_to_mysql_type[$field[
'type']];
75 $length = $field[
'length'];
76 $primary = isset($field[
'is_primary']) && $field[
'is_primary'] ?
"PRIMARY KEY" :
"";
77 $notnull = isset($field[
'is_notnull']) && $field[
'is_notnull'] ?
"NOT NULL" :
"";
78 $sequence = isset($field[
'sequence']) && $field[
'sequence'] ?
"AUTO_INCREMENT" :
"";
79 $query .=
"$name $type ($length) $sequence $primary $notnull,";
82 return substr(
$query, 0, - 1);
91 $keys = implode($primary_keys);
92 $this->pdo->exec(
"ALTER TABLE $table_name ADD PRIMARY KEY ($keys)");
110 $result = $this->pdo->prepare(
"SHOW TABLES LIKE :table_name");
111 $result->execute(array(
':table_name' => $table_name ));
126 $statement = $this->pdo->query(
"SHOW COLUMNS FROM $table_name WHERE Field = '$column_name'");
127 $statement != NULL ? $statement->closeCursor() :
"";
129 return $statement != NULL && $statement->rowCount() != 0;
139 $col = array( $column_name => $attributes );
141 $this->pdo->exec(
"ALTER TABLE $$table_name ADD $$col_str");
149 $this->pdo->exec(
"DROP TABLE $table_name");
160 $err = $this->pdo->errorInfo();
172 return $query_result->fetchAll($query_result);
180 $table_seq = $table_name .
"_seq";
182 $this->pdo->exec(
"DROP TABLE $table_seq");
192 $this->pdo->exec(
"ALTER TABLE $$table_name DROP COLUMN $column_name");
202 $this->pdo->exec(
"alter table $table_name change $column_old_name $column_new_name");
210 public function insert($table_name, $values) {
212 foreach ($values as $val) {
213 $real[] = $this->
quote($val[1], $val[0]);
215 $values = implode(
",", $real);
216 $this->pdo->exec(
"INSERT INTO $table_name VALUES ($values)");
226 $res = $query_result->fetchObject();
228 $query_result->closeCursor();
242 public function update($table_name, $values, $where) {
243 $query =
"UPDATE $table_name SET ";
244 foreach ($values as $key => $val) {
245 $qval = $this->
quote($val[1], $val[0]);
249 foreach ($where as $key => $val) {
250 $qval = $this->
quote($val[1], $val[0]);
272 $res = $query_result->fetch(PDO::FETCH_ASSOC);
274 $query_result->closeCursor();
289 return $query_result->rowCount();
299 public function quote($value, $type) {
301 return $this->pdo->quote($value);
311 public function addIndex($table_name, $index_name) {