SQLAnywhereStatement.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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\SQLAnywhere;
  20. use IteratorAggregate;
  21. use PDO;
  22. use Doctrine\DBAL\Driver\Statement;
  23. /**
  24. * SAP SQL Anywhere implementation of the Statement interface.
  25. *
  26. * @author Steve Müller <st.mueller@dzh-online.de>
  27. * @link www.doctrine-project.org
  28. * @since 2.5
  29. */
  30. class SQLAnywhereStatement implements IteratorAggregate, Statement
  31. {
  32. /**
  33. * @var resource The connection resource.
  34. */
  35. private $conn;
  36. /**
  37. * @var string Name of the default class to instantiate when fetch mode is \PDO::FETCH_CLASS.
  38. */
  39. private $defaultFetchClass = '\stdClass';
  40. /**
  41. * @var string Constructor arguments for the default class to instantiate when fetch mode is \PDO::FETCH_CLASS.
  42. */
  43. private $defaultFetchClassCtorArgs = array();
  44. /**
  45. * @var int Default fetch mode to use.
  46. */
  47. private $defaultFetchMode = PDO::FETCH_BOTH;
  48. /**
  49. * @var resource The result set resource to fetch.
  50. */
  51. private $result;
  52. /**
  53. * @var resource The prepared SQL statement to execute.
  54. */
  55. private $stmt;
  56. /**
  57. * Constructor.
  58. *
  59. * Prepares given statement for given connection.
  60. *
  61. * @param resource $conn The connection resource to use.
  62. * @param string $sql The SQL statement to prepare.
  63. *
  64. * @throws SQLAnywhereException
  65. */
  66. public function __construct($conn, $sql)
  67. {
  68. if ( ! is_resource($conn) || get_resource_type($conn) !== 'SQLAnywhere connection') {
  69. throw new SQLAnywhereException('Invalid SQL Anywhere connection resource: ' . $conn);
  70. }
  71. $this->conn = $conn;
  72. $this->stmt = sasql_prepare($conn, $sql);
  73. if ( ! is_resource($this->stmt) || get_resource_type($this->stmt) !== 'SQLAnywhere statement') {
  74. throw SQLAnywhereException::fromSQLAnywhereError($conn);
  75. }
  76. }
  77. /**
  78. * {@inheritdoc}
  79. *
  80. * @throws SQLAnywhereException
  81. */
  82. public function bindParam($column, &$variable, $type = null, $length = null)
  83. {
  84. switch ($type) {
  85. case PDO::PARAM_INT:
  86. case PDO::PARAM_BOOL:
  87. $type = 'i';
  88. break;
  89. case PDO::PARAM_LOB:
  90. $type = 'b';
  91. break;
  92. case PDO::PARAM_NULL:
  93. case PDO::PARAM_STR:
  94. $type = 's';
  95. break;
  96. default:
  97. throw new SQLAnywhereException('Unknown type: ' . $type);
  98. }
  99. if ( ! sasql_stmt_bind_param_ex($this->stmt, $column - 1, $variable, $type, $variable === null)) {
  100. throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt);
  101. }
  102. return true;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function bindValue($param, $value, $type = null)
  108. {
  109. return $this->bindParam($param, $value, $type);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. *
  114. * @throws SQLAnywhereException
  115. */
  116. public function closeCursor()
  117. {
  118. if (!sasql_stmt_reset($this->stmt)) {
  119. throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt);
  120. }
  121. return true;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function columnCount()
  127. {
  128. return sasql_stmt_field_count($this->stmt);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function errorCode()
  134. {
  135. return sasql_stmt_errno($this->stmt);
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function errorInfo()
  141. {
  142. return sasql_stmt_error($this->stmt);
  143. }
  144. /**
  145. * {@inheritdoc}
  146. *
  147. * @throws SQLAnywhereException
  148. */
  149. public function execute($params = null)
  150. {
  151. if (is_array($params)) {
  152. $hasZeroIndex = array_key_exists(0, $params);
  153. foreach ($params as $key => $val) {
  154. $key = ($hasZeroIndex && is_numeric($key)) ? $key + 1 : $key;
  155. $this->bindValue($key, $val);
  156. }
  157. }
  158. if ( ! sasql_stmt_execute($this->stmt)) {
  159. throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt);
  160. }
  161. $this->result = sasql_stmt_result_metadata($this->stmt);
  162. return true;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. *
  167. * @throws SQLAnywhereException
  168. */
  169. public function fetch($fetchMode = null)
  170. {
  171. if ( ! is_resource($this->result) || get_resource_type($this->result) !== 'SQLAnywhere result') {
  172. return false;
  173. }
  174. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  175. switch ($fetchMode) {
  176. case PDO::FETCH_ASSOC:
  177. return sasql_fetch_assoc($this->result);
  178. case PDO::FETCH_BOTH:
  179. return sasql_fetch_array($this->result, SASQL_BOTH);
  180. case PDO::FETCH_CLASS:
  181. $className = $this->defaultFetchClass;
  182. $ctorArgs = $this->defaultFetchClassCtorArgs;
  183. if (func_num_args() >= 2) {
  184. $args = func_get_args();
  185. $className = $args[1];
  186. $ctorArgs = isset($args[2]) ? $args[2] : array();
  187. }
  188. $result = sasql_fetch_object($this->result);
  189. if ($result instanceof \stdClass) {
  190. $result = $this->castObject($result, $className, $ctorArgs);
  191. }
  192. return $result;
  193. case PDO::FETCH_NUM:
  194. return sasql_fetch_row($this->result);
  195. case PDO::FETCH_OBJ:
  196. return sasql_fetch_object($this->result);
  197. default:
  198. throw new SQLAnywhereException('Fetch mode is not supported: ' . $fetchMode);
  199. }
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function fetchAll($fetchMode = null)
  205. {
  206. $rows = array();
  207. switch ($fetchMode) {
  208. case PDO::FETCH_CLASS:
  209. while ($row = call_user_func_array(array($this, 'fetch'), func_get_args())) {
  210. $rows[] = $row;
  211. }
  212. break;
  213. case PDO::FETCH_COLUMN:
  214. while ($row = $this->fetchColumn()) {
  215. $rows[] = $row;
  216. }
  217. break;
  218. default:
  219. while ($row = $this->fetch($fetchMode)) {
  220. $rows[] = $row;
  221. }
  222. }
  223. return $rows;
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. public function fetchColumn($columnIndex = 0)
  229. {
  230. $row = $this->fetch(PDO::FETCH_NUM);
  231. if (false === $row) {
  232. return false;
  233. }
  234. return isset($row[$columnIndex]) ? $row[$columnIndex] : null;
  235. }
  236. /**
  237. * {@inheritdoc}
  238. */
  239. public function getIterator()
  240. {
  241. return new \ArrayIterator($this->fetchAll());
  242. }
  243. /**
  244. * {@inheritdoc}
  245. */
  246. public function rowCount()
  247. {
  248. return sasql_stmt_affected_rows($this->stmt);
  249. }
  250. /**
  251. * {@inheritdoc}
  252. */
  253. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  254. {
  255. $this->defaultFetchMode = $fetchMode;
  256. $this->defaultFetchClass = $arg2 ? $arg2 : $this->defaultFetchClass;
  257. $this->defaultFetchClassCtorArgs = $arg3 ? (array) $arg3 : $this->defaultFetchClassCtorArgs;
  258. }
  259. /**
  260. * Casts a stdClass object to the given class name mapping its' properties.
  261. *
  262. * @param \stdClass $sourceObject Object to cast from.
  263. * @param string|object $destinationClass Name of the class or class instance to cast to.
  264. * @param array $ctorArgs Arguments to use for constructing the destination class instance.
  265. *
  266. * @return object
  267. *
  268. * @throws SQLAnywhereException
  269. */
  270. private function castObject(\stdClass $sourceObject, $destinationClass, array $ctorArgs = array())
  271. {
  272. if ( ! is_string($destinationClass)) {
  273. if ( ! is_object($destinationClass)) {
  274. throw new SQLAnywhereException(sprintf(
  275. 'Destination class has to be of type string or object, %s given.', gettype($destinationClass)
  276. ));
  277. }
  278. } else {
  279. $destinationClass = new \ReflectionClass($destinationClass);
  280. $destinationClass = $destinationClass->newInstanceArgs($ctorArgs);
  281. }
  282. $sourceReflection = new \ReflectionObject($sourceObject);
  283. $destinationClassReflection = new \ReflectionObject($destinationClass);
  284. foreach ($sourceReflection->getProperties() as $sourceProperty) {
  285. $sourceProperty->setAccessible(true);
  286. $name = $sourceProperty->getName();
  287. $value = $sourceProperty->getValue($sourceObject);
  288. if ($destinationClassReflection->hasProperty($name)) {
  289. $destinationProperty = $destinationClassReflection->getProperty($name);
  290. $destinationProperty->setAccessible(true);
  291. $destinationProperty->setValue($destinationClass, $value);
  292. } else {
  293. $destinationClass->$name = $value;
  294. }
  295. }
  296. return $destinationClass;
  297. }
  298. }