Bouncy Melons 0.1
declarative style orm
BouncyMelons.php
Go to the documentation of this file.
1 <?php
2 namespace bm;
3 
4 class Exception extends \Exception {}
5 class EConnectionFailed extends Exception {}
6 class EItemNotFound extends Exception {}
8 class EInsertFail extends Exception {}
9 class EExec extends Exception {}
10 class ESelect extends Exception {}
11 class ESelectOne extends Exception {}
13 class EViewModelNotFound extends Exception {}
14 
18 class BouncyMelons {
19 
20  private $pdo;
21  private $vms = [];
22  private $cache;
23 
24  public function __construct() {
25  $this->cache = new Cache();
26  }
27 
31  public function getCache() {
32  return $this->cache;
33  }
34 
35 
42  public function setPDO(\PDO $pdo) {
43  $this->pdo = $pdo;
44  }
45 
50  public function getPDO() {
51  if(empty($this->pdo)) {
52  throw new EConnectionNotEstablished("use BouncyMelons::connect() or BouncyMelons::setPDO()");
53  }
54  return $this->pdo;
55  }
56 
63  public function connect($dsn, $user, $password, $options = [\PDO::ATTR_PERSISTENT => true]) {
64  try {
65  $this->setPDO(new \PDO($dsn, $user, $password, $options));
66  if(!$this->getDriver() instanceof SqliteDriver) {
67  $this->exec('set names utf8'); // sqlite des not support set names
68  }
69  } catch(\PDOException $e) {
70  throw new EConnectionFailed($e->getMessage());
71  }
72  }
73 
77  public function park($vms) {
78  foreach($vms as $vm) {
79  $this->parkOne($vm);
80  }
81  }
82 
86  public function parkOne($vm) {
87  $this->vms[] = $vm;
88  $vm->setBm($this);
89  }
90 
95  public function getVmBySlug($slug) {
96  foreach($this->vms as $vm) {
97  $class = get_class($vm);
98  if($class::getSlug()==$slug) {
99  return $vm;
100  }
101  }
102  throw new EViewModelNotFound("No view model found for slug '".$slug."'");
103  }
104 
110  public function tableExist($name) {
111  return false !== $this->getPDO()->query("SELECT 1 FROM `".$name."`");
112  }
113 
117  public function getDriver() {
118  $driver = $this->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME);
119  if($driver == 'sqlite') {
120  return new SqliteDriver($this->getPDO());
121  }
122  return new MysqlDriver($this->getPDO());
123  }
124 
130  public function getTableFields($table) {
131  return $this->getDriver()->getTableFields($table);
132  }
133 
134  public function getCreatePrimaryId() {
135  $driver = $this->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME);
136  if($driver == 'sqlite') {
137  return "`id` INTEGER PRIMARY KEY ASC";
138  }
139  return "`id` int(11) auto_increment primary key";
140  }
141 
142  public function getCreateDefaultCharset() {
143  $driver = $this->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME);
144  if($driver == 'sqlite') {
145  return "";
146  }
147  return "DEFAULT CHARSET UTF8";
148  }
149 
153  public function dropConnection() {
154  $this->pdo = null;
155  }
156 
157 
161  public function exec($query) {
162  return $this->getDriver()->exec($query);
163  }
164 
171  public function query($sql) {
172  return $this->getDriver()->query($sql);
173  }
174 
181  public function queryOne($sql) {
182  return $this->getDriver()->queryOne($sql);
183  }
184 
190  public function queryParam($sql) {
191  return $this->getDriver()->queryParam($sql);
192  }
193 
202  public function select($table, $where, $limit = null, $orderby = null, $groupBy = null) {
203  $wr = '';
204  if(!empty($where)) {
205  $wr = "WHERE ".$where;
206  }
207  $ob = '';
208  if(!empty($orderby)) {
209  $ob = "ORDER BY ".$orderby;
210  }
211  $lim = '';
212  if(!empty($limit)) {
213  $lim = "LIMIT ".$limit;
214  }
215  $gb = '';
216  if(!empty($groupBy)) {
217  $gb = "GROUP BY ".$groupBy;
218  }
219 // var_dump("SELECT * FROM `".$table."` ".$wr." ".$gb." ".$ob." ".$lim);
220  return $this->query("SELECT * FROM `".$table."` ".$wr." ".$gb." ".$ob." ".$lim);
221  }
222 
229  public function selectOne($table, $where) {
230  return $this->queryOne("SELECT * FROM `".$table."` WHERE ".$where." LIMIT 1");
231  }
232 
238  public function insert($table, $items) {
239  $sqlItems = [];
240  foreach($items as $key=>$value) {
241  $sqlItems[] = " `".$key."` = '".$value."'";
242  }
243  $sql = "INSERT INTO `".$table."` SET
244  ".implode(",",$sqlItems)."
245  ";
246  $this->getPDO()->query($sql);
247  $id = $this->getPDO()->lastInsertId();
248  if($id == 0) {
249  throw new EInsertFail();
250  }
251  return $id;
252  }
253 
259  public function insertOrUpdate($table, $items) {
260  return $this->getDriver()->insertOrUpdate($table, $items);
261  }
262 
267  public function remove($table, $id) {
268 // var_dump(['removing',$table,$id]);
269  $this->exec("DELETE FROM `".$table."` WHERE `id` = '".(int)$id."'");
270  }
271 
276  public function truncate($table) {
277  $this->getDriver()->truncate($table);
278  }
279 
280 
285  public function createViewAs($view, $sql) {
286  $this->getDriver()->createViewAs($view, $sql);
287  }
288 
293  public function createTemptableAs($name, $sql) {
294  $this->getDriver()->createTemptableAs($name, $sql);
295  }
296 
297 
298 }
select($table, $where, $limit=null, $orderby=null, $groupBy=null)
createTemptableAs($name, $sql)
createViewAs($view, $sql)
connect($dsn, $user, $password, $options=[\PDO::ATTR_PERSISTENT=> true])
insert($table, $items)
insertOrUpdate($table, $items)
selectOne($table, $where)
getTableFields($table)
setPDO(\PDO $pdo)