Bouncy Melons 0.2
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  const DEFAULT_ITEMS_PER_PAGE = 50;
21 
22  private $pdo;
23  private $vms = [];
24  private $cache;
25  private $itemsPerPage = self::DEFAULT_ITEMS_PER_PAGE;
26 
27  public function __construct() {
28  $this->cache = new Cache();
29  }
30 
34  public function getCache() {
35  return $this->cache;
36  }
37 
38 
45  public function setPDO(\PDO $pdo) {
46  $this->pdo = $pdo;
47  }
48 
53  public function getPDO() {
54  if(empty($this->pdo)) {
55  throw new EConnectionNotEstablished("use BouncyMelons::connect() or BouncyMelons::setPDO()");
56  }
57  return $this->pdo;
58  }
59 
66  public function connect($dsn, $user, $password, $options = [\PDO::ATTR_PERSISTENT => true]) {
67  try {
68  $this->setPDO(new \PDO($dsn, $user, $password, $options));
69  if(!$this->getDriver() instanceof SqliteDriver) {
70  $this->exec('set names utf8'); // sqlite des not support set names
71  }
72  } catch(\PDOException $e) {
73  throw new EConnectionFailed($e->getMessage());
74  }
75  }
76 
80  public function park($vms) {
81  foreach($vms as $vm) {
82  $this->parkOne($vm);
83  }
84  }
85 
89  public function parkOne($vm) {
90  $this->vms[] = $vm;
91  $vm->setBm($this);
92  }
93 
98  public function getVmBySlug($slug) {
99  foreach($this->vms as $vm) {
100  $class = get_class($vm);
101  if($class::getSlug()==$slug) {
102  return $vm;
103  }
104  }
105  throw new EViewModelNotFound("No view model found for slug '".$slug."'");
106  }
107 
113  public function tableExist($name) {
114  return false !== $this->getPDO()->query("SELECT 1 FROM `".$name."`");
115  }
116 
120  public function getDriver() {
121  $driver = $this->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME);
122  if($driver == 'sqlite') {
123  return new SqliteDriver($this->getPDO());
124  }
125  return new MysqlDriver($this->getPDO());
126  }
127 
133  public function getTableFields($table) {
134  return $this->getDriver()->getTableFields($table);
135  }
136 
137  public function getCreatePrimaryId() {
138  $driver = $this->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME);
139  if($driver == 'sqlite') {
140  return "`id` INTEGER PRIMARY KEY ASC";
141  }
142  return "`id` int(11) auto_increment primary key";
143  }
144 
145  public function getCreateDefaultCharset() {
146  $driver = $this->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME);
147  if($driver == 'sqlite') {
148  return "";
149  }
150  return "DEFAULT CHARSET UTF8";
151  }
152 
156  public function dropConnection() {
157  $this->pdo = null;
158  }
159 
160 
164  public function exec($query) {
165  return $this->getDriver()->exec($query);
166  }
167 
174  public function query($sql) {
175  return $this->getDriver()->query($sql);
176  }
177 
184  public function queryOne($sql) {
185  return $this->getDriver()->queryOne($sql);
186  }
187 
193  public function queryParam($sql) {
194  return $this->getDriver()->queryParam($sql);
195  }
196 
205  public function select($table, $fields, $where, $limit = null, $orderby = null, $groupBy = null) {
206  $wr = '';
207  if(!empty($where)) {
208  $wr = "WHERE ".$where;
209  }
210  $ob = '';
211  if(!empty($orderby)) {
212  $ob = "ORDER BY ".$orderby;
213  }
214  $lim = '';
215  if(!empty($limit)) {
216  $lim = "LIMIT ".$limit;
217  }
218  $gb = '';
219  if(!empty($groupBy)) {
220  $gb = "GROUP BY ".$groupBy;
221  }
222  return $this->query("SELECT `".implode("`,`",$fields)."` FROM `".$table."` ".$wr." ".$gb." ".$ob." ".$lim);
223  }
224 
231  public function selectOne($table, $where) {
232  return $this->queryOne("SELECT * FROM `".$table."` WHERE ".$where." LIMIT 1");
233  }
234 
240  public function insert($table, $items) {
241  $sqlItems = [];
242  foreach($items as $key=>$value) {
243  $sqlItems[] = " `".$key."` = '".$value."'";
244  }
245  $sql = "INSERT INTO `".$table."` SET
246  ".implode(",",$sqlItems)."
247  ";
248  $this->getPDO()->query($sql);
249  $id = $this->getPDO()->lastInsertId();
250  if($id == 0) {
251  throw new EInsertFail();
252  }
253  return $id;
254  }
255 
261  public function insertOrUpdate($table, $items) {
262  return $this->getDriver()->insertOrUpdate($table, $items);
263  }
264 
269  public function remove($table, $id) {
270  $this->exec("DELETE FROM `".$table."` WHERE `id` = '".(int)$id."'");
271  }
272 
277  public function truncate($table) {
278  $this->getDriver()->truncate($table);
279  }
280 
281 
286  public function createViewAs($view, $sql) {
287  $this->getDriver()->createViewAs($view, $sql);
288  }
289 
294  public function createTemptableAs($name, $sql) {
295  $this->getDriver()->createTemptableAs($name, $sql);
296  }
297 
302  public function setItemsPerPage($itemsPerPage) {
303  $this->itemsPerPage = $itemsPerPage;
304  }
305 
309  public function getItemsPerPage() {
310  return $this->itemsPerPage;
311  }
312 
313 
314 }
createTemptableAs($name, $sql)
createViewAs($view, $sql)
select($table, $fields, $where, $limit=null, $orderby=null, $groupBy=null)
setItemsPerPage($itemsPerPage)
connect($dsn, $user, $password, $options=[\PDO::ATTR_PERSISTENT=> true])
insert($table, $items)
insertOrUpdate($table, $items)
selectOne($table, $where)
getTableFields($table)
setPDO(\PDO $pdo)