PageRenderTime 33ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php

https://gitlab.com/mario.uriarte/doctrine2.5-tutorial
PHP | 344 lines | 190 code | 54 blank | 100 comment | 22 complexity | 5ad65cd9a9d16ca997f8eddb27f4a25d MD5 | raw file
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Driver\IBMDB2;
  20. use Doctrine\DBAL\Driver\Statement;
  21. class DB2Statement implements \IteratorAggregate, Statement
  22. {
  23. /**
  24. * @var resource
  25. */
  26. private $_stmt = null;
  27. /**
  28. * @var array
  29. */
  30. private $_bindParam = array();
  31. /**
  32. * @var string Name of the default class to instantiate when fetch mode is \PDO::FETCH_CLASS.
  33. */
  34. private $defaultFetchClass = '\stdClass';
  35. /**
  36. * @var string Constructor arguments for the default class to instantiate when fetch mode is \PDO::FETCH_CLASS.
  37. */
  38. private $defaultFetchClassCtorArgs = array();
  39. /**
  40. * @var integer
  41. */
  42. private $_defaultFetchMode = \PDO::FETCH_BOTH;
  43. /**
  44. * DB2_BINARY, DB2_CHAR, DB2_DOUBLE, or DB2_LONG
  45. *
  46. * @var array
  47. */
  48. static private $_typeMap = array(
  49. \PDO::PARAM_INT => DB2_LONG,
  50. \PDO::PARAM_STR => DB2_CHAR,
  51. );
  52. /**
  53. * @param resource $stmt
  54. */
  55. public function __construct($stmt)
  56. {
  57. $this->_stmt = $stmt;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function bindValue($param, $value, $type = null)
  63. {
  64. return $this->bindParam($param, $value, $type);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function bindParam($column, &$variable, $type = null, $length = null)
  70. {
  71. $this->_bindParam[$column] =& $variable;
  72. if ($type && isset(self::$_typeMap[$type])) {
  73. $type = self::$_typeMap[$type];
  74. } else {
  75. $type = DB2_CHAR;
  76. }
  77. if (!db2_bind_param($this->_stmt, $column, "variable", DB2_PARAM_IN, $type)) {
  78. throw new DB2Exception(db2_stmt_errormsg());
  79. }
  80. return true;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function closeCursor()
  86. {
  87. if ( ! $this->_stmt) {
  88. return false;
  89. }
  90. $this->_bindParam = array();
  91. db2_free_result($this->_stmt);
  92. $ret = db2_free_stmt($this->_stmt);
  93. $this->_stmt = false;
  94. return $ret;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function columnCount()
  100. {
  101. if ( ! $this->_stmt) {
  102. return false;
  103. }
  104. return db2_num_fields($this->_stmt);
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function errorCode()
  110. {
  111. return db2_stmt_error();
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function errorInfo()
  117. {
  118. return array(
  119. 0 => db2_stmt_errormsg(),
  120. 1 => db2_stmt_error(),
  121. );
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function execute($params = null)
  127. {
  128. if ( ! $this->_stmt) {
  129. return false;
  130. }
  131. /*$retval = true;
  132. if ($params !== null) {
  133. $retval = @db2_execute($this->_stmt, $params);
  134. } else {
  135. $retval = @db2_execute($this->_stmt);
  136. }*/
  137. if ($params === null) {
  138. ksort($this->_bindParam);
  139. $params = array_values($this->_bindParam);
  140. }
  141. $retval = @db2_execute($this->_stmt, $params);
  142. if ($retval === false) {
  143. throw new DB2Exception(db2_stmt_errormsg());
  144. }
  145. return $retval;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  151. {
  152. $this->_defaultFetchMode = $fetchMode;
  153. $this->defaultFetchClass = $arg2 ? $arg2 : $this->defaultFetchClass;
  154. $this->defaultFetchClassCtorArgs = $arg3 ? (array) $arg3 : $this->defaultFetchClassCtorArgs;
  155. return true;
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function getIterator()
  161. {
  162. $data = $this->fetchAll();
  163. return new \ArrayIterator($data);
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function fetch($fetchMode = null)
  169. {
  170. $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
  171. switch ($fetchMode) {
  172. case \PDO::FETCH_BOTH:
  173. return db2_fetch_both($this->_stmt);
  174. case \PDO::FETCH_ASSOC:
  175. return db2_fetch_assoc($this->_stmt);
  176. case \PDO::FETCH_CLASS:
  177. $className = $this->defaultFetchClass;
  178. $ctorArgs = $this->defaultFetchClassCtorArgs;
  179. if (func_num_args() >= 2) {
  180. $args = func_get_args();
  181. $className = $args[1];
  182. $ctorArgs = isset($args[2]) ? $args[2] : array();
  183. }
  184. $result = db2_fetch_object($this->_stmt);
  185. if ($result instanceof \stdClass) {
  186. $result = $this->castObject($result, $className, $ctorArgs);
  187. }
  188. return $result;
  189. case \PDO::FETCH_NUM:
  190. return db2_fetch_array($this->_stmt);
  191. case \PDO::FETCH_OBJ:
  192. return db2_fetch_object($this->_stmt);
  193. default:
  194. throw new DB2Exception("Given Fetch-Style " . $fetchMode . " is not supported.");
  195. }
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function fetchAll($fetchMode = null)
  201. {
  202. $rows = array();
  203. switch ($fetchMode) {
  204. case \PDO::FETCH_CLASS:
  205. while ($row = call_user_func_array(array($this, 'fetch'), func_get_args())) {
  206. $rows[] = $row;
  207. }
  208. break;
  209. case \PDO::FETCH_COLUMN:
  210. while ($row = $this->fetchColumn()) {
  211. $rows[] = $row;
  212. }
  213. break;
  214. default:
  215. while ($row = $this->fetch($fetchMode)) {
  216. $rows[] = $row;
  217. }
  218. }
  219. return $rows;
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. public function fetchColumn($columnIndex = 0)
  225. {
  226. $row = $this->fetch(\PDO::FETCH_NUM);
  227. if (false === $row) {
  228. return false;
  229. }
  230. return isset($row[$columnIndex]) ? $row[$columnIndex] : null;
  231. }
  232. /**
  233. * {@inheritdoc}
  234. */
  235. public function rowCount()
  236. {
  237. return (@db2_num_rows($this->_stmt))?:0;
  238. }
  239. /**
  240. * Casts a stdClass object to the given class name mapping its' properties.
  241. *
  242. * @param \stdClass $sourceObject Object to cast from.
  243. * @param string|object $destinationClass Name of the class or class instance to cast to.
  244. * @param array $ctorArgs Arguments to use for constructing the destination class instance.
  245. *
  246. * @return object
  247. *
  248. * @throws DB2Exception
  249. */
  250. private function castObject(\stdClass $sourceObject, $destinationClass, array $ctorArgs = array())
  251. {
  252. if ( ! is_string($destinationClass)) {
  253. if ( ! is_object($destinationClass)) {
  254. throw new DB2Exception(sprintf(
  255. 'Destination class has to be of type string or object, %s given.', gettype($destinationClass)
  256. ));
  257. }
  258. } else {
  259. $destinationClass = new \ReflectionClass($destinationClass);
  260. $destinationClass = $destinationClass->newInstanceArgs($ctorArgs);
  261. }
  262. $sourceReflection = new \ReflectionObject($sourceObject);
  263. $destinationClassReflection = new \ReflectionObject($destinationClass);
  264. /** @var \ReflectionProperty[] $destinationProperties */
  265. $destinationProperties = array_change_key_case($destinationClassReflection->getProperties(), \CASE_LOWER);
  266. foreach ($sourceReflection->getProperties() as $sourceProperty) {
  267. $sourceProperty->setAccessible(true);
  268. $name = $sourceProperty->getName();
  269. $value = $sourceProperty->getValue($sourceObject);
  270. // Try to find a case-matching property.
  271. if ($destinationClassReflection->hasProperty($name)) {
  272. $destinationProperty = $destinationClassReflection->getProperty($name);
  273. $destinationProperty->setAccessible(true);
  274. $destinationProperty->setValue($destinationClass, $value);
  275. continue;
  276. }
  277. $name = strtolower($name);
  278. // Try to find a property without matching case.
  279. // Fallback for the driver returning either all uppercase or all lowercase column names.
  280. if (isset($destinationProperties[$name])) {
  281. $destinationProperty = $destinationProperties[$name];
  282. $destinationProperty->setAccessible(true);
  283. $destinationProperty->setValue($destinationClass, $value);
  284. continue;
  285. }
  286. $destinationClass->$name = $value;
  287. }
  288. return $destinationClass;
  289. }
  290. }