Skip to content
This repository
Browse code

Prepare Dibi storage

  • Loading branch information...
commit f79b5b6f1bf817b696176da152e5adff907add84 1 parent d4819c6
Jakub Vrána authored
4  NotORM/Row.php
@@ -6,7 +6,7 @@ class NotORM_Row extends NotORM_Abstract implements IteratorAggregate, ArrayAcce
6 6
 	private $modified = array();
7 7
 	protected $row, $result;
8 8
 	
9  
-	protected function __construct(array $row, NotORM_Result $result) {
  9
+	protected function __construct($row, NotORM_Result $result) {
10 10
 		$this->row = $row;
11 11
 		$this->result = $result;
12 12
 	}
@@ -107,7 +107,7 @@ function getIterator() {
107 107
 	*/
108 108
 	function offsetExists($key) {
109 109
 		$this->access($key);
110  
-		return array_key_exists($key, $this->row);
  110
+		return array_key_exists($key, (array) $this->row);
111 111
 	}
112 112
 	
113 113
 	/** Get value of column
70  NotORM/Storage.php
@@ -262,3 +262,73 @@ function delete($table, array $where, array $parameters = array(), $order = "",
262 262
 	}
263 263
 	
264 264
 }
  265
+
  266
+
  267
+
  268
+/** Storage using DibiConnection
  269
+*/
  270
+class NotORM_Storage_Dibi implements NotORM_Storage {
  271
+	protected $connection;
  272
+	
  273
+	function __construct(DibiConnection $connection) {
  274
+		$this->connection = $connection;
  275
+	}
  276
+	
  277
+	function query($query, array $parameters = array()) {
  278
+		array_unshift($parameters, $query);
  279
+		return $this->connection->query($parameters);
  280
+	}
  281
+	
  282
+	protected function whereString(array $args, array $where = array(), $group = "", $having = "", array $order = array(), $limit = null, $offset = null) {
  283
+		if ($where) {
  284
+			$args[0] .= " WHERE %and";
  285
+			$args[] = $where;
  286
+		}
  287
+		$return = $this->connection->translate($args);
  288
+		if ($group) {
  289
+			$return .= " GROUP BY $group";
  290
+		}
  291
+		if ($having) {
  292
+			$return .= " HAVING $having";
  293
+		}
  294
+		if ($order) {
  295
+			$return .= " ORDER BY " . implode(", ", $order);
  296
+		}
  297
+		if (isset($limit)) {
  298
+			$this->connection->getDriver()->applyLimit($return, $limit, $offset);
  299
+		}
  300
+		return $return;
  301
+	}
  302
+	
  303
+	function select($columns, $table, array $where = array(), $group = "", $having = "", array $order = array(), $limit = null, $offset = null, $lock = null) {
  304
+		$return = $this->whereString(array("SELECT $columns FROM $table"), $where, $group, $having, $order, $limit, $offset);
  305
+		if (isset($lock)) {
  306
+			$return .= ($lock ? " FOR UPDATE" : " LOCK IN SHARE MODE");
  307
+		}
  308
+		return $return;
  309
+	}
  310
+	
  311
+	function insert($table, $data) {
  312
+		if (!$this->connection->query("INSERT INTO $table", $data)) {
  313
+			return false;
  314
+		}
  315
+		return $this->connection->getInsertId();
  316
+	}
  317
+	
  318
+	function update($table, array $data, array $where, array $parameters = array(), $order = "", $limit = null, $offset = null) {
  319
+		$query = $this->whereString(array("UPDATE $table SET", $data), $where, "", "", $order, $limit, $offset);
  320
+		if (!$this->query($query, $parameters)) {
  321
+			return false;
  322
+		}
  323
+		return $this->connection->getAffectedRows();
  324
+	}
  325
+	
  326
+	function delete($table, array $where, array $parameters = array(), $order = "", $limit = null, $offset = null) {
  327
+		$query = $this->whereString(array("DELETE FROM $table"), $where, "", "", $order, $limit, $offset);
  328
+		if (!$this->query($query, $parameters)) {
  329
+			return false;
  330
+		}
  331
+		return $this->connection->getAffectedRows();
  332
+	}
  333
+	
  334
+}

0 notes on commit f79b5b6

Please sign in to comment.
Something went wrong with that request. Please try again.