PdoAdapter.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * Phinx
  4. *
  5. * (The MIT license)
  6. * Copyright (c) 2015 Rob Morgan
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated * documentation files (the "Software"), to
  10. * deal in the Software without restriction, including without limitation the
  11. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. *
  26. * @package Phinx
  27. * @subpackage Phinx\Db\Adapter
  28. */
  29. namespace Schema\Db\Adapter;
  30. use Schema\Db\Table;
  31. use Schema\Db\Table\Column;
  32. /**
  33. * Phinx PDO Adapter.
  34. *
  35. * @author Rob Morgan <robbym@gmail.com>
  36. */
  37. abstract class PdoAdapter implements AdapterInterface
  38. {
  39. /**
  40. * @var \PDO
  41. */
  42. protected $connection;
  43. /**
  44. * @var float
  45. */
  46. protected $commandStartTime;
  47. /**
  48. * Class Constructor.
  49. *
  50. * @param array $options Options
  51. * @param InputInterface $input Input Interface
  52. * @param OutputInterface $output Output Interface
  53. */
  54. public function __construct()
  55. {
  56. }
  57. /**
  58. * Sets the database connection.
  59. *
  60. * @param \PDO $connection Connection
  61. * @return AdapterInterface
  62. */
  63. public function setConnection(\PDO $connection)
  64. {
  65. $this->connection = $connection;
  66. return $this;
  67. }
  68. /**
  69. * Gets the database connection
  70. *
  71. * @return \PDO
  72. */
  73. public function getConnection()
  74. {
  75. if (null === $this->connection) {
  76. $this->connect();
  77. }
  78. return $this->connection;
  79. }
  80. /**
  81. * Gets the table prefix
  82. *
  83. * @return \PDO
  84. */
  85. public function getTablePrefix()
  86. {
  87. if(isset($this->connection->prefix)){
  88. return $this->connection->prefix;
  89. }
  90. return '';
  91. }
  92. /**
  93. * Sets the command start time
  94. *
  95. * @param int $time
  96. * @return AdapterInterface
  97. */
  98. public function setCommandStartTime($time)
  99. {
  100. $this->commandStartTime = $time;
  101. return $this;
  102. }
  103. /**
  104. * Gets the command start time
  105. *
  106. * @return int
  107. */
  108. public function getCommandStartTime()
  109. {
  110. return $this->commandStartTime;
  111. }
  112. /**
  113. * Start timing a command.
  114. *
  115. * @return void
  116. */
  117. public function startCommandTimer()
  118. {
  119. $this->setCommandStartTime(microtime(true));
  120. }
  121. /**
  122. * Stop timing the current command and write the elapsed time to the
  123. * output.
  124. *
  125. * @return void
  126. */
  127. public function endCommandTimer()
  128. {
  129. return;
  130. $end = microtime(true);
  131. if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) {
  132. $this->getOutput()->writeln(' -> ' . sprintf('%.4fs', $end - $this->getCommandStartTime()));
  133. }
  134. }
  135. /**
  136. * Write a Phinx command to the output.
  137. *
  138. * @param string $command Command Name
  139. * @param array $args Command Args
  140. * @return void
  141. */
  142. public function writeCommand($command, $args = array())
  143. {
  144. return '';
  145. if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) {
  146. if (count($args)) {
  147. $outArr = array();
  148. foreach ($args as $arg) {
  149. if (is_array($arg)) {
  150. $arg = array_map(function ($value) {
  151. return '\'' . $value . '\'';
  152. }, $arg);
  153. $outArr[] = '[' . implode(', ', $arg) . ']';
  154. continue;
  155. }
  156. $outArr[] = '\'' . $arg . '\'';
  157. }
  158. $this->getOutput()->writeln(' -- ' . $command . '(' . implode(', ', $outArr) . ')');
  159. return;
  160. }
  161. $this->getOutput()->writeln(' -- ' . $command);
  162. }
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function connect()
  168. {
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function disconnect()
  174. {
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function execute($sql)
  180. {
  181. // $sql;
  182. return $this->getConnection()->exec($sql);
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function query($sql)
  188. {
  189. return $this->getConnection()->query($sql);
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function fetchRow($sql)
  195. {
  196. $result = $this->query($sql);
  197. return $result->fetch();
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function fetchAll($sql)
  203. {
  204. $rows = array();
  205. $result = $this->query($sql);
  206. while ($row = $result->fetch()) {
  207. $rows[] = $row;
  208. }
  209. return $rows;
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function insert(Table $table, $row)
  215. {
  216. $this->startCommandTimer();
  217. //$this->writeCommand('insert', array($table->getName()));
  218. $sql = sprintf(
  219. "INSERT INTO %s ",
  220. $this->quoteTableName($table->getName())
  221. );
  222. $columns = array_keys($row);
  223. $sql .= "(". implode(', ', array_map(array($this, 'quoteColumnName'), $columns)) . ")";
  224. $sql .= " VALUES (" . implode(', ', array_fill(0, count($columns), '?')) . ")";
  225. $stmt = $this->getConnection()->prepare($sql);
  226. $stmt->execute(array_values($row));
  227. $this->endCommandTimer();
  228. }
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function getAdapterType()
  233. {
  234. return $this->getOption('adapter');
  235. }
  236. /**
  237. * {@inheritdoc}
  238. */
  239. public function getColumnTypes()
  240. {
  241. return array(
  242. 'string',
  243. 'char',
  244. 'text',
  245. 'integer',
  246. 'biginteger',
  247. 'float',
  248. 'decimal',
  249. 'datetime',
  250. 'timestamp',
  251. 'time',
  252. 'date',
  253. 'blob',
  254. 'binary',
  255. 'varbinary',
  256. 'boolean',
  257. 'uuid',
  258. // Geospatial data types
  259. 'geometry',
  260. 'point',
  261. 'linestring',
  262. 'polygon',
  263. );
  264. }
  265. /**
  266. * {@inheritdoc}
  267. */
  268. public function isValidColumnType(Column $column) {
  269. return in_array($column->getType(), $this->getColumnTypes());
  270. }
  271. /**
  272. * Cast a value to a boolean appropriate for the adapter.
  273. *
  274. * @param mixed $value The value to be cast
  275. *
  276. * @return mixed
  277. */
  278. public function castToBool($value)
  279. {
  280. return (bool) $value ? 1 : 0;
  281. }
  282. }