Bouncy Melons 0.1
declarative style orm
MysqlDriver.php
Go to the documentation of this file.
1 <?php
2 
3 namespace bm;
4 
5 class MysqlDriver extends Driver implements IDriver {
6 
7  public function getTableFields($table) {
8  $list = $this->pdo->query("SHOW FIELDS FROM `".$table."`")->fetchAll(\PDO::FETCH_ASSOC);
9  $re = [];
10  foreach($list as $item) {
11  $re[] = [
12  'field' => $item['Field'],
13  'type' => $item['Type'],
14  ];
15  }
16  return $re;
17  }
18 
19  public function insertOrUpdate($table, $items) {
20  $sqlItems = [];
21  foreach($items as $key=>$value) {
22  $sqlItems[] = " `".$key."` = '".$value."'";
23  }
24  $sql = "INSERT INTO `".$table."` SET
25  ".implode(",",$sqlItems)."
26  ON DUPLICATE KEY UPDATE ".implode(",",$sqlItems)." ";
27 // var_dump(str_replace("\n", " ", $sql));
28  $re = $this->getPDO()->query($sql);
29  if($re === false) {
30  list($errno, $errint, $msg) = $this->getPDO()->errorInfo();
31  throw new EInsertFail($msg, $errint);
32  }
33  if(!empty($items['id'])) {
34  return $items['id'];
35  }
36  $id = $this->getPDO()->lastInsertId();
37  if($id == 0) {
38  throw new EInsertFail();
39  }
40  return $id;
41  }
42 
43  public function createViewAs($view, $sql) {
44  $this->exec("CREATE OR REPLACE SQL SECURITY INVOKER VIEW `".$view."` AS ".$sql);
45  }
46 
47  public function createTemptableAs($name, $sql) {
48  $this->exec("CREATE TEMPORARY TABLE `".$name."` AS ".$sql);
49  }
50 
51  public function truncate($table) {
52  $this->exec("TRUNCATE `".$table."`");
53  }
54 }
truncate($table)
Definition: MysqlDriver.php:51
createTemptableAs($name, $sql)
Definition: MysqlDriver.php:47
insertOrUpdate($table, $items)
Definition: MysqlDriver.php:19
createViewAs($view, $sql)
Definition: MysqlDriver.php:43
getTableFields($table)
Definition: MysqlDriver.php:7