PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php

https://bitbucket.org/trujka/phparcade
PHP | 438 lines | 228 code | 43 blank | 167 comment | 13 complexity | 79cdbdabab541140a07efe2687ae867e MD5 | raw file
Possible License(s): LGPL-2.1, MIT, CC-BY-3.0
  1. <?php
  2. /**
  3. * Phinx
  4. *
  5. * (The MIT license)
  6. * Copyright (c) 2014 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 Phinx\Db\Adapter;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. use Symfony\Component\Console\Output\NullOutput;
  32. use Phinx\Db\Table;
  33. use Phinx\Migration\MigrationInterface;
  34. /**
  35. * Phinx PDO Adapter.
  36. *
  37. * @author Rob Morgan <robbym@gmail.com>
  38. */
  39. abstract class PdoAdapter implements AdapterInterface
  40. {
  41. /**
  42. * @var array
  43. */
  44. protected $options;
  45. /**
  46. * @var OutputInterface
  47. */
  48. protected $output;
  49. /**
  50. * @var string
  51. */
  52. protected $schemaTableName = 'phinxlog';
  53. /**
  54. * @var \PDO
  55. */
  56. protected $connection;
  57. /**
  58. * @var float
  59. */
  60. protected $commandStartTime;
  61. /**
  62. * Class Constructor.
  63. *
  64. * @param array $options Options
  65. * @param OutputInterface $output Output Interface
  66. * @return void
  67. */
  68. public function __construct(array $options, OutputInterface $output = null)
  69. {
  70. $this->setOptions($options);
  71. if (null !== $output) {
  72. $this->setOutput($output);
  73. }
  74. }
  75. /**
  76. * Sets the adapter options.
  77. *
  78. * @param array $options Options
  79. * return AdapterInterface
  80. */
  81. public function setOptions(array $options)
  82. {
  83. $this->options = $options;
  84. if (isset($options['default_migration_table'])) {
  85. $this->setSchemaTableName($options['default_migration_table']);
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Gets the adapter options.
  91. *
  92. * @return array
  93. */
  94. public function getOptions()
  95. {
  96. return $this->options;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function setOutput(OutputInterface $output)
  102. {
  103. $this->output = $output;
  104. return $this;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getOutput()
  110. {
  111. if (null == $this->output) {
  112. $output = new NullOutput();
  113. $this->setOutput($output);
  114. }
  115. return $this->output;
  116. }
  117. /**
  118. * Sets the schema table name.
  119. *
  120. * @param string $schemaTableName Schema Table Name
  121. * @return void
  122. */
  123. public function setSchemaTableName($schemaTableName)
  124. {
  125. $this->schemaTableName = $schemaTableName;
  126. return $this;
  127. }
  128. /**
  129. * Gets the schema table name.
  130. *
  131. * @return string
  132. */
  133. public function getSchemaTableName()
  134. {
  135. return $this->schemaTableName;
  136. }
  137. /**
  138. * Sets the database connection.
  139. *
  140. * @param \PDO $connection Connection
  141. * @return AdapterInterface
  142. */
  143. public function setConnection(\PDO $connection)
  144. {
  145. $this->connection = $connection;
  146. return $this;
  147. }
  148. /**
  149. * Gets the database connection
  150. *
  151. * @return \PDO
  152. */
  153. public function getConnection()
  154. {
  155. if (null === $this->connection) {
  156. $this->connect();
  157. }
  158. return $this->connection;
  159. }
  160. /**
  161. * Sets the command start time
  162. *
  163. * @param int $time
  164. * @return AdapterInterface
  165. */
  166. public function setCommandStartTime($time)
  167. {
  168. $this->commandStartTime = $time;
  169. return $this;
  170. }
  171. /**
  172. * Gets the command start time
  173. *
  174. * @return int
  175. */
  176. public function getCommandStartTime()
  177. {
  178. return $this->commandStartTime;
  179. }
  180. /**
  181. * Start timing a command.
  182. *
  183. * @return void
  184. */
  185. public function startCommandTimer()
  186. {
  187. $this->setCommandStartTime(microtime(true));
  188. }
  189. /**
  190. * Stop timing the current command and write the elapsed time to the
  191. * output.
  192. *
  193. * @return void
  194. */
  195. public function endCommandTimer()
  196. {
  197. $end = microtime(true);
  198. if (OutputInterface::VERBOSITY_VERBOSE === $this->getOutput()->getVerbosity()) {
  199. $this->getOutput()->writeln(' -> ' . sprintf('%.4fs', $end - $this->getCommandStartTime()));
  200. }
  201. }
  202. /**
  203. * Write a Phinx command to the output.
  204. *
  205. * @param string $command Command Name
  206. * @param array $args Command Args
  207. * @return void
  208. */
  209. public function writeCommand($command, $args = array())
  210. {
  211. if (OutputInterface::VERBOSITY_VERBOSE === $this->getOutput()->getVerbosity()) {
  212. if (count($args)) {
  213. $outArr = array();
  214. foreach ($args as $arg) {
  215. if (is_array($arg)) {
  216. $arg = array_map(function ($value) {
  217. return '\'' . $value . '\'';
  218. }, $arg);
  219. $outArr[] = '[' . implode(', ', $arg) . ']';
  220. continue;
  221. }
  222. $outArr[] = '\'' . $arg . '\'';
  223. }
  224. return $this->getOutput()->writeln(' -- ' . $command . '(' . implode(', ', $outArr) . ')');
  225. }
  226. $this->getOutput()->writeln(' -- ' . $command);
  227. }
  228. }
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function connect()
  233. {
  234. }
  235. /**
  236. * {@inheritdoc}
  237. */
  238. public function disconnect()
  239. {
  240. }
  241. /**
  242. * {@inheritdoc}
  243. */
  244. public function execute($sql)
  245. {
  246. return $this->getConnection()->exec($sql);
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. public function query($sql)
  252. {
  253. return $this->getConnection()->query($sql);
  254. }
  255. /**
  256. * {@inheritdoc}
  257. */
  258. public function fetchRow($sql)
  259. {
  260. $result = $this->query($sql);
  261. return $result->fetch();
  262. }
  263. /**
  264. * {@inheritdoc}
  265. */
  266. public function fetchAll($sql)
  267. {
  268. $rows = array();
  269. $result = $this->query($sql);
  270. while ($row = $result->fetch()) {
  271. $rows[] = $row;
  272. }
  273. return $rows;
  274. }
  275. /**
  276. * {@inheritdoc}
  277. */
  278. public function getVersions()
  279. {
  280. $versions = array();
  281. $rows = $this->fetchAll(sprintf('SELECT * FROM %s ORDER BY version ASC', $this->getSchemaTableName()));
  282. return array_map(
  283. function ($v) {
  284. return $v['version'];
  285. },
  286. $rows
  287. );
  288. }
  289. /**
  290. * {@inheritdoc}
  291. */
  292. public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime)
  293. {
  294. if (strtolower($direction) == MigrationInterface::UP) {
  295. // up
  296. $sql = sprintf(
  297. 'INSERT INTO %s ('
  298. . 'version, start_time, end_time'
  299. . ') VALUES ('
  300. . '"%s",'
  301. . '"%s",'
  302. . '"%s"'
  303. . ');',
  304. $this->getSchemaTableName(),
  305. $migration->getVersion(),
  306. $startTime,
  307. $endTime
  308. );
  309. $this->query($sql);
  310. } else {
  311. // down
  312. $sql = sprintf(
  313. "DELETE FROM %s WHERE version = '%s'",
  314. $this->getSchemaTableName(),
  315. $migration->getVersion()
  316. );
  317. $this->query($sql);
  318. }
  319. return $this;
  320. }
  321. /**
  322. * Describes a database table.
  323. *
  324. * @todo MySQL Specific so move to MysqlAdapter.
  325. * @return array
  326. */
  327. public function describeTable($tableName)
  328. {
  329. $options = $this->getOptions();
  330. // mysql specific
  331. $sql = sprintf(
  332. 'SELECT *'
  333. . ' FROM information_schema.tables'
  334. . ' WHERE table_schema = "%s"'
  335. . ' AND table_name = "%s"',
  336. $options['name'],
  337. $tableName
  338. );
  339. return $this->fetchRow($sql);
  340. }
  341. /**
  342. * {@inheritdoc}
  343. */
  344. public function hasSchemaTable()
  345. {
  346. return $this->hasTable($this->getSchemaTableName());
  347. }
  348. /**
  349. * {@inheritdoc}
  350. */
  351. public function createSchemaTable()
  352. {
  353. try {
  354. $options = array(
  355. 'id' => false
  356. );
  357. $table = new \Phinx\Db\Table($this->getSchemaTableName(), $options, $this);
  358. $table->addColumn('version', 'biginteger', array('limit' => 14))
  359. ->addColumn('start_time', 'timestamp')
  360. ->addColumn('end_time', 'timestamp')
  361. ->save();
  362. } catch (\Exception $exception) {
  363. throw new \InvalidArgumentException('There was a problem creating the schema table: ' . $exception->getMessage());
  364. }
  365. }
  366. /**
  367. * {@inheritdoc}
  368. */
  369. public function getAdapterType()
  370. {
  371. $options = $this->getOptions();
  372. return $options['adapter'];
  373. }
  374. /**
  375. * {@inheritdoc}
  376. */
  377. public function getColumnTypes()
  378. {
  379. return array(
  380. 'primary_key',
  381. 'string',
  382. 'text',
  383. 'integer',
  384. 'biginteger',
  385. 'float',
  386. 'decimal',
  387. 'datetime',
  388. 'timestamp',
  389. 'time',
  390. 'date',
  391. 'binary',
  392. 'boolean'
  393. );
  394. }
  395. }