DataSet.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @author Ironpilot
  4. *
  5. *
  6. */
  7. class Staple_Query_DataSet implements ArrayAccess, Iterator, Countable
  8. {
  9. private $data = array();
  10. private $literal = array();
  11. public function __construct(array $data = NULL)
  12. {
  13. if(isset($data))
  14. {
  15. $this->setData($data);
  16. }
  17. }
  18. /* (non-PHPdoc)
  19. * @see ArrayAccess::offsetExists()
  20. */
  21. public function offsetExists($offset)
  22. {
  23. return array_key_exists($offset, $this->data);
  24. }
  25. /* (non-PHPdoc)
  26. * @see ArrayAccess::offsetGet()
  27. */
  28. public function offsetGet($offset)
  29. {
  30. if(array_key_exists($offset, $this->data))
  31. {
  32. return $this->data[$offset];
  33. }
  34. else
  35. {
  36. return NULL;
  37. }
  38. }
  39. /* (non-PHPdoc)
  40. * @see ArrayAccess::offsetSet()
  41. */
  42. public function offsetSet($offset, $value)
  43. {
  44. $this->data[$offset] = $value;
  45. $this->literal[$offset] = false;
  46. }
  47. /* (non-PHPdoc)
  48. * @see ArrayAccess::offsetUnset()
  49. */
  50. public function offsetUnset($offset)
  51. {
  52. unset($this->data[$offset]);
  53. unset($this->literal[$offset]);
  54. }
  55. /* (non-PHPdoc)
  56. * @see Iterator::current()
  57. */
  58. public function current()
  59. {
  60. return current($this->data);
  61. }
  62. /* (non-PHPdoc)
  63. * @see Iterator::key()
  64. */
  65. public function key()
  66. {
  67. return key($this->data);
  68. }
  69. /* (non-PHPdoc)
  70. * @see Iterator::next()
  71. */
  72. public function next()
  73. {
  74. return next($this->data);
  75. }
  76. /* (non-PHPdoc)
  77. * @see Iterator::rewind()
  78. */
  79. public function rewind()
  80. {
  81. return reset($this->data);
  82. }
  83. /* (non-PHPdoc)
  84. * @see Iterator::valid()
  85. */
  86. public function valid()
  87. {
  88. if(key($this->data) === NULL)
  89. {
  90. return false;
  91. }
  92. else
  93. {
  94. return true;
  95. }
  96. }
  97. /* (non-PHPdoc)
  98. * @see Countable::count()
  99. */
  100. public function count()
  101. {
  102. return count($this->data);
  103. }
  104. /**
  105. * Adds a data pair (key and value) to the dataset. Will overwrite the value of any existing duplicate key.
  106. * @param string $key
  107. * @param mixed $value
  108. */
  109. public function addDataPair($key,$value)
  110. {
  111. $this->data[$key] = $value;
  112. return $this;
  113. }
  114. /**
  115. * Appends the contents of the submitted array to the dataset.
  116. * Note: numeric keys will be renumbered if duplicated, and string keys will be overwritten
  117. * if duplicated.
  118. *
  119. * @param array $data
  120. */
  121. public function addData(array $data)
  122. {
  123. foreach ($data as $key=>$value)
  124. {
  125. $this->literal[$key] = false;
  126. }
  127. $this->data = array_merge($this->data, $data);
  128. return $this;
  129. }
  130. /**
  131. * Clears any current data and uses the supplied data array.
  132. *
  133. * @param array $data
  134. */
  135. public function setData(array $data)
  136. {
  137. foreach ($data as $key=>$value)
  138. {
  139. $this->literal[$key] = false;
  140. }
  141. $this->data = $data;
  142. return $this;
  143. }
  144. public function getData()
  145. {
  146. return $this->data;
  147. }
  148. public function getColumns()
  149. {
  150. return array_keys($this->data);
  151. }
  152. public function getLiteralFor($column)
  153. {
  154. if(array_key_exists($column, $this->literal))
  155. {
  156. return $this->literal[$column];
  157. }
  158. else
  159. {
  160. return NULL;
  161. }
  162. }
  163. public function addLiteralColumn($column, $data)
  164. {
  165. $this->data[$column] = $data;
  166. $this->literal[$column] = true;
  167. return $this;
  168. }
  169. public function addLiteralData(array $data)
  170. {
  171. foreach ($data as $key=>$value)
  172. {
  173. $this->literal[$key] = true;
  174. }
  175. $this->data = array_merge($this->data, $data);
  176. return $this;
  177. }
  178. public function getInsertString()
  179. {
  180. $stmt = '('.implode(',',$this->getColumns()).') ';
  181. $stmt .= "\nVALUES (";
  182. $colcount = 0;
  183. foreach ($this->data as $name=>$col)
  184. {
  185. if($colcount > 0)
  186. {
  187. $stmt .= ',';
  188. }
  189. if($this->literal[$name] === true)
  190. {
  191. $stmt .= $col;
  192. }
  193. else
  194. {
  195. $stmt .= Staple_Query::convertTypes($col);
  196. }
  197. $colcount++;
  198. }
  199. $stmt .= ") ";
  200. return $stmt;
  201. }
  202. public function getInsertMultipleString()
  203. {
  204. $stmt = '(';
  205. $colcount = 0;
  206. foreach ($this->data as $name=>$col)
  207. {
  208. if($colcount > 0)
  209. {
  210. $stmt .= ',';
  211. }
  212. if($this->literal[$name] === true)
  213. {
  214. $stmt .= $col;
  215. }
  216. else
  217. {
  218. $stmt .= Staple_Query::convertTypes($col);
  219. }
  220. $colcount++;
  221. }
  222. $stmt .= ")";
  223. return $stmt;
  224. }
  225. public function getUpdateString()
  226. {
  227. $stmt = '';
  228. $colcount = 0;
  229. foreach ($this->data as $name=>$col)
  230. {
  231. if($colcount > 0)
  232. {
  233. $stmt .= ',';
  234. }
  235. $stmt .= ' '.$name.'=';
  236. if($this->literal[$name] === true)
  237. {
  238. $stmt .= $col;
  239. }
  240. else
  241. {
  242. $stmt .= Staple_Query::convertTypes($col);
  243. }
  244. $colcount++;
  245. }
  246. return $stmt;
  247. }
  248. }
  249. ?>