PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cakephp/migrations/src/CakeAdapter.php

https://gitlab.com/vannh/portal_training
PHP | 586 lines | 210 code | 52 blank | 324 comment | 0 complexity | f5b0ff6145fbd43b366d9ce941255cfd MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. * @link http://cakephp.org CakePHP(tm) Project
  10. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  11. */
  12. namespace Migrations;
  13. use Cake\Database\Connection;
  14. use Phinx\Db\Adapter\AdapterInterface;
  15. use Phinx\Db\Table;
  16. use Phinx\Db\Table\Column;
  17. use Phinx\Db\Table\ForeignKey;
  18. use Phinx\Db\Table\Index;
  19. use Phinx\Migration\MigrationInterface;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. /**
  22. * Decorates an AdapterInterface in order to proxy some method to the actual
  23. * connection object.
  24. */
  25. class CakeAdapter implements AdapterInterface
  26. {
  27. /**
  28. * Decorated adapter
  29. *
  30. * @var \Phinx\Db\Adapter\AdapterInterface
  31. */
  32. protected $adapter;
  33. /**
  34. * Database connection
  35. *
  36. * @var \Cake\Database\Connection
  37. */
  38. protected $connection;
  39. /**
  40. * Constructor
  41. *
  42. * @param \Phinx\Db\Adapter\AdapterInterface $adapter The original adapter to decorate.
  43. * @param \Cake\Database\Connection $connection The connection to actually use.
  44. */
  45. public function __construct(AdapterInterface $adapter, Connection $connection)
  46. {
  47. $this->adapter = $adapter;
  48. $this->connection = $connection;
  49. $pdo = $adapter->getConnection();
  50. $connection->driver()->connection($pdo);
  51. }
  52. /**
  53. * Get all migrated version numbers.
  54. *
  55. * @return array
  56. */
  57. public function getVersions()
  58. {
  59. return $this->adapter->getVersions();
  60. }
  61. /**
  62. * Set adapter configuration options.
  63. *
  64. * @param array $options
  65. * @return AdapterInterface
  66. */
  67. public function setOptions(array $options)
  68. {
  69. return $this->adapter->setOptions($options);
  70. }
  71. /**
  72. * Get all adapter options.
  73. *
  74. * @return array
  75. */
  76. public function getOptions()
  77. {
  78. return $this->adapter->getOptions();
  79. }
  80. /**
  81. * Check if an option has been set.
  82. *
  83. * @param string $name
  84. * @return boolean
  85. */
  86. public function hasOption($name)
  87. {
  88. return $this->adapter->hasOption($name);
  89. }
  90. /**
  91. * Get a single adapter option, or null if the option does not exist.
  92. *
  93. * @param string $name
  94. * @return mixed
  95. */
  96. public function getOption($name)
  97. {
  98. return $this->adapter->getOption($name);
  99. }
  100. /**
  101. * Sets the console output.
  102. *
  103. * @param OutputInterface $output Output
  104. * @return AdapterInterface
  105. */
  106. public function setOutput(OutputInterface $output)
  107. {
  108. return $this->adapter->setOutput($output);
  109. }
  110. /**
  111. * Gets the console output.
  112. *
  113. * @return OutputInterface
  114. */
  115. public function getOutput()
  116. {
  117. return $this->adapter->getOutput();
  118. }
  119. /**
  120. * Records a migration being run.
  121. *
  122. * @param MigrationInterface $migration Migration
  123. * @param string $direction Direction
  124. * @param int $startTime Start Time
  125. * @param int $endTime End Time
  126. * @return AdapterInterface
  127. */
  128. public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime)
  129. {
  130. return $this->adapter->migrated($migration, $direction, $startTime, $endTime);
  131. }
  132. /**
  133. * Does the schema table exist?
  134. *
  135. * @deprecated use hasTable instead.
  136. * @return boolean
  137. */
  138. public function hasSchemaTable()
  139. {
  140. return $this->adapter->hasSchemaTable();
  141. }
  142. /**
  143. * Creates the schema table.
  144. *
  145. * @return void
  146. */
  147. public function createSchemaTable()
  148. {
  149. return $this->adapter->createSchemaTable();
  150. }
  151. /**
  152. * Returns the adapter type.
  153. *
  154. * @return string
  155. */
  156. public function getAdapterType()
  157. {
  158. return $this->adapter->getAdapterType();
  159. }
  160. /**
  161. * Initializes the database connection.
  162. *
  163. * @throws \RuntimeException When the requested database driver is not installed.
  164. * @return void
  165. */
  166. public function connect()
  167. {
  168. return $this->adapter->connect();
  169. }
  170. /**
  171. * Closes the database connection.
  172. *
  173. * @return void
  174. */
  175. public function disconnect()
  176. {
  177. return $this->adapter->disconnect();
  178. }
  179. /**
  180. * Does the adapter support transactions?
  181. *
  182. * @return boolean
  183. */
  184. public function hasTransactions()
  185. {
  186. return $this->adapter->hasTransactions();
  187. }
  188. /**
  189. * Begin a transaction.
  190. *
  191. * @return void
  192. */
  193. public function beginTransaction()
  194. {
  195. $this->connection->begin();
  196. }
  197. /**
  198. * Commit a transaction.
  199. *
  200. * @return void
  201. */
  202. public function commitTransaction()
  203. {
  204. $this->connection->commit();
  205. }
  206. /**
  207. * Rollback a transaction.
  208. *
  209. * @return void
  210. */
  211. public function rollbackTransaction()
  212. {
  213. $this->connection->rollback();
  214. }
  215. /**
  216. * Executes a SQL statement and returns the number of affected rows.
  217. *
  218. * @param string $sql SQL
  219. * @return int
  220. */
  221. public function execute($sql)
  222. {
  223. return $this->adapter->execute($sql);
  224. }
  225. /**
  226. * Executes a SQL statement and returns the result as an array.
  227. *
  228. * @param string $sql SQL
  229. * @return array
  230. */
  231. public function query($sql)
  232. {
  233. return $this->adapter->query($sql);
  234. }
  235. /**
  236. * Executes a query and returns only one row as an array.
  237. *
  238. * @param string $sql SQL
  239. * @return array
  240. */
  241. public function fetchRow($sql)
  242. {
  243. return $this->adapter->fetchRow($sql);
  244. }
  245. /**
  246. * Executes a query and returns an array of rows.
  247. *
  248. * @param string $sql SQL
  249. * @return array
  250. */
  251. public function fetchAll($sql)
  252. {
  253. return $this->adapter->fetchAll($sql);
  254. }
  255. /**
  256. * Inserts data into the table
  257. *
  258. * @param Table $table where to insert data
  259. * @param array $columns column names
  260. * @param $data
  261. */
  262. public function insert(Table $table, $columns, $data)
  263. {
  264. return $this->adapter->insert($table, $columns, $data);
  265. }
  266. /**
  267. * Quotes a table name for use in a query.
  268. *
  269. * @param string $tableName Table Name
  270. * @return string
  271. */
  272. public function quoteTableName($tableName)
  273. {
  274. return $this->adapter->quoteTableName($tableName);
  275. }
  276. /**
  277. * Quotes a column name for use in a query.
  278. *
  279. * @param string $columnName Table Name
  280. * @return string
  281. */
  282. public function quoteColumnName($columnName)
  283. {
  284. return $this->adapter->quoteColumnName($columnName);
  285. }
  286. /**
  287. * Checks to see if a table exists.
  288. *
  289. * @param string $tableName Table Name
  290. * @return boolean
  291. */
  292. public function hasTable($tableName)
  293. {
  294. return $this->adapter->hasTable($tableName);
  295. }
  296. /**
  297. * Creates the specified database table.
  298. *
  299. * @param Table $table Table
  300. * @return void
  301. */
  302. public function createTable(Table $table)
  303. {
  304. return $this->adapter->createTable($table);
  305. }
  306. /**
  307. * Renames the specified database table.
  308. *
  309. * @param string $tableName Table Name
  310. * @param string $newName New Name
  311. * @return void
  312. */
  313. public function renameTable($tableName, $newName)
  314. {
  315. return $this->adapter->renameTable($tableName, $newName);
  316. }
  317. /**
  318. * Drops the specified database table.
  319. *
  320. * @param string $tableName Table Name
  321. * @return void
  322. */
  323. public function dropTable($tableName)
  324. {
  325. return $this->adapter->dropTable($tableName);
  326. }
  327. /**
  328. * Returns table columns
  329. *
  330. * @param string $tableName Table Name
  331. * @return Column[]
  332. */
  333. public function getColumns($tableName)
  334. {
  335. return $this->adapter->getColumns($tableName);
  336. }
  337. /**
  338. * Checks to see if a column exists.
  339. *
  340. * @param string $tableName Table Name
  341. * @param string $columnName Column Name
  342. * @return boolean
  343. */
  344. public function hasColumn($tableName, $columnName)
  345. {
  346. return $this->adapter->hasColumn($tableName, $columnName);
  347. }
  348. /**
  349. * Adds the specified column to a database table.
  350. *
  351. * @param Table $table Table
  352. * @param Column $column Column
  353. * @return void
  354. */
  355. public function addColumn(Table $table, Column $column)
  356. {
  357. return $this->adapter->addColumn($table, $column);
  358. }
  359. /**
  360. * Renames the specified column.
  361. *
  362. * @param string $tableName Table Name
  363. * @param string $columnName Column Name
  364. * @param string $newColumnName New Column Name
  365. * @return void
  366. */
  367. public function renameColumn($tableName, $columnName, $newColumnName)
  368. {
  369. return $this->adapter->renameColumn($tableName, $columnName, $newColumnName);
  370. }
  371. /**
  372. * Change a table column type.
  373. *
  374. * @param string $tableName Table Name
  375. * @param string $columnName Column Name
  376. * @param Column $newColumn New Column
  377. * @return Table
  378. */
  379. public function changeColumn($tableName, $columnName, Column $newColumn)
  380. {
  381. return $this->adapter->changeColumn($tableName, $columnName, $newColumn);
  382. }
  383. /**
  384. * Drops the specified column.
  385. *
  386. * @param string $tableName Table Name
  387. * @param string $columnName Column Name
  388. * @return void
  389. */
  390. public function dropColumn($tableName, $columnName)
  391. {
  392. return $this->adapter->dropColumn($tableName, $columnName);
  393. }
  394. /**
  395. * Checks to see if an index exists.
  396. *
  397. * @param string $tableName Table Name
  398. * @param mixed $columns Column(s)
  399. * @return boolean
  400. */
  401. public function hasIndex($tableName, $columns)
  402. {
  403. return $this->adapter->hasIndex($tableName, $columns);
  404. }
  405. /**
  406. * Adds the specified index to a database table.
  407. *
  408. * @param Table $table Table
  409. * @param Index $index Index
  410. * @return void
  411. */
  412. public function addIndex(Table $table, Index $index)
  413. {
  414. return $this->adapter->addIndex($table, $index);
  415. }
  416. /**
  417. * Drops the specified index from a database table.
  418. *
  419. * @param string $tableName
  420. * @param mixed $columns Column(s)
  421. * @return void
  422. */
  423. public function dropIndex($tableName, $columns)
  424. {
  425. return $this->adapter->dropIndex($tableName, $columns);
  426. }
  427. /**
  428. * Drops the index specified by name from a database table.
  429. *
  430. * @param string $tableName
  431. * @param string $indexName
  432. * @return void
  433. */
  434. public function dropIndexByName($tableName, $indexName)
  435. {
  436. return $this->adapter->dropIndexByName($tableName, $indexName);
  437. }
  438. /**
  439. * Checks to see if a foreign key exists.
  440. *
  441. * @param string $tableName
  442. * @param string[] $columns Column(s)
  443. * @param string $constraint Constraint name
  444. * @return boolean
  445. */
  446. public function hasForeignKey($tableName, $columns, $constraint = null)
  447. {
  448. return $this->adapter->hasForeignKey($tableName, $columns, $constraint);
  449. }
  450. /**
  451. * Adds the specified foreign key to a database table.
  452. *
  453. * @param Table $table
  454. * @param ForeignKey $foreignKey
  455. * @return void
  456. */
  457. public function addForeignKey(Table $table, ForeignKey $foreignKey)
  458. {
  459. return $this->adapter->addForeignKey($table, $foreignKey);
  460. }
  461. /**
  462. * Drops the specified foreign key from a database table.
  463. *
  464. * @param string $tableName
  465. * @param string[] $columns Column(s)
  466. * @param string $constraint Constraint name
  467. * @return void
  468. */
  469. public function dropForeignKey($tableName, $columns, $constraint = null)
  470. {
  471. return $this->adapter->dropForeignKey($tableName, $columns, $constraint);
  472. }
  473. /**
  474. * Returns an array of the supported Phinx column types.
  475. *
  476. * @return array
  477. */
  478. public function getColumnTypes()
  479. {
  480. return $this->adapter->getColumnTypes();
  481. }
  482. /**
  483. * Checks that the given column is of a supported type.
  484. *
  485. * @param Column $column
  486. * @return boolean
  487. */
  488. public function isValidColumnType(Column $column)
  489. {
  490. return $this->adapter->isValidColumnType($column);
  491. }
  492. /**
  493. * Converts the Phinx logical type to the adapter's SQL type.
  494. *
  495. * @param string $type
  496. * @param integer $limit
  497. * @return string
  498. */
  499. public function getSqlType($type, $limit = null)
  500. {
  501. return $this->adapter->getSqlType($type, $limit);
  502. }
  503. /**
  504. * Creates a new database.
  505. *
  506. * @param string $name Database Name
  507. * @param array $options Options
  508. * @return void
  509. */
  510. public function createDatabase($name, $options = array())
  511. {
  512. return $this->adapter->createDatabase($name, $options);
  513. }
  514. /**
  515. * Checks to see if a database exists.
  516. *
  517. * @param string $name Database Name
  518. * @return boolean
  519. */
  520. public function hasDatabase($name)
  521. {
  522. return $this->adapter->hasDatabase($name);
  523. }
  524. /**
  525. * Drops the specified database.
  526. *
  527. * @param string $name Database Name
  528. * @return void
  529. */
  530. public function dropDatabase($name)
  531. {
  532. return $this->adapter->dropDatabase($name);
  533. }
  534. }