Bouncy Melons 0.2
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  $re = $this->getPDO()->query($sql);
28  if($re === false) {
29  list($errno, $errint, $msg) = $this->getPDO()->errorInfo();
30  throw new EInsertFail($msg, $errint);
31  }
32  if(!empty($items['id'])) {
33  return $items['id'];
34  }
35  $id = $this->getPDO()->lastInsertId();
36  if($id == 0) {
37  throw new EInsertFail();
38  }
39  return $id;
40  }
41 
42  public function createViewAs($view, $sql) {
43  $this->exec("CREATE OR REPLACE SQL SECURITY INVOKER VIEW `".$view."` AS ".$sql);
44  }
45 
46  public function createTemptableAs($name, $sql) {
47  $this->exec("CREATE TEMPORARY TABLE `".$name."` AS ".$sql);
48  }
49 
50  public function truncate($table) {
51  $this->exec("TRUNCATE `".$table."`");
52  }
53 }
truncate($table)
Definition: MysqlDriver.php:50
createTemptableAs($name, $sql)
Definition: MysqlDriver.php:46
insertOrUpdate($table, $items)
Definition: MysqlDriver.php:19
createViewAs($view, $sql)
Definition: MysqlDriver.php:42
getTableFields($table)
Definition: MysqlDriver.php:7