PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

https://gitlab.com/daniruizcamacho/pfcascensores
PHP | 577 lines | 220 code | 66 blank | 291 comment | 11 complexity | 0d15c73119b3694694da770dabda2be4 MD5 | raw file
  1. <?php namespace Illuminate\Database\Schema\Grammars;
  2. use Illuminate\Support\Fluent;
  3. use Illuminate\Database\Connection;
  4. use Illuminate\Database\Schema\Blueprint;
  5. class MySqlGrammar extends Grammar {
  6. /**
  7. * The keyword identifier wrapper format.
  8. *
  9. * @var string
  10. */
  11. protected $wrapper = '`%s`';
  12. /**
  13. * The possible column modifiers.
  14. *
  15. * @var array
  16. */
  17. protected $modifiers = array('Unsigned', 'Nullable', 'Default', 'Increment', 'After');
  18. /**
  19. * The possible column serials
  20. *
  21. * @var array
  22. */
  23. protected $serials = array('bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger');
  24. /**
  25. * Compile the query to determine the list of tables.
  26. *
  27. * @return string
  28. */
  29. public function compileTableExists()
  30. {
  31. return 'select * from information_schema.tables where table_schema = ? and table_name = ?';
  32. }
  33. /**
  34. * Compile the query to determine the list of columns.
  35. *
  36. * @param string $table
  37. * @return string
  38. */
  39. public function compileColumnExists()
  40. {
  41. return "select column_name from information_schema.columns where table_schema = ? and table_name = ?";
  42. }
  43. /**
  44. * Compile a create table command.
  45. *
  46. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  47. * @param \Illuminate\Support\Fluent $command
  48. * @param \Illuminate\Database\Connection $connection
  49. * @return string
  50. */
  51. public function compileCreate(Blueprint $blueprint, Fluent $command, Connection $connection)
  52. {
  53. $columns = implode(', ', $this->getColumns($blueprint));
  54. $sql = 'create table '.$this->wrapTable($blueprint)." ($columns)";
  55. // Once we have the primary SQL, we can add the encoding option to the SQL for
  56. // the table. Then, we can check if a storage engine has been supplied for
  57. // the table. If so, we will add the engine declaration to the SQL query.
  58. $sql = $this->compileCreateEncoding($sql, $connection);
  59. if (isset($blueprint->engine))
  60. {
  61. $sql .= ' engine = '.$blueprint->engine;
  62. }
  63. return $sql;
  64. }
  65. /**
  66. * Append the character set specifications to a command.
  67. *
  68. * @param string $sql
  69. * @param \Illuminate\Database\Connection $connection
  70. * @return string
  71. */
  72. protected function compileCreateEncoding($sql, Connection $connection)
  73. {
  74. if ( ! is_null($charset = $connection->getConfig('charset')))
  75. {
  76. $sql .= ' default character set '.$charset;
  77. }
  78. if ( ! is_null($collation = $connection->getConfig('collation')))
  79. {
  80. $sql .= ' collate '.$collation;
  81. }
  82. return $sql;
  83. }
  84. /**
  85. * Compile a create table command.
  86. *
  87. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  88. * @param \Illuminate\Support\Fluent $command
  89. * @return string
  90. */
  91. public function compileAdd(Blueprint $blueprint, Fluent $command)
  92. {
  93. $table = $this->wrapTable($blueprint);
  94. $columns = $this->prefixArray('add', $this->getColumns($blueprint));
  95. return 'alter table '.$table.' '.implode(', ', $columns);
  96. }
  97. /**
  98. * Compile a primary key command.
  99. *
  100. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  101. * @param \Illuminate\Support\Fluent $command
  102. * @return string
  103. */
  104. public function compilePrimary(Blueprint $blueprint, Fluent $command)
  105. {
  106. $command->name(null);
  107. return $this->compileKey($blueprint, $command, 'primary key');
  108. }
  109. /**
  110. * Compile a unique key command.
  111. *
  112. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  113. * @param \Illuminate\Support\Fluent $command
  114. * @return string
  115. */
  116. public function compileUnique(Blueprint $blueprint, Fluent $command)
  117. {
  118. return $this->compileKey($blueprint, $command, 'unique');
  119. }
  120. /**
  121. * Compile a plain index key command.
  122. *
  123. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  124. * @param \Illuminate\Support\Fluent $command
  125. * @return string
  126. */
  127. public function compileIndex(Blueprint $blueprint, Fluent $command)
  128. {
  129. return $this->compileKey($blueprint, $command, 'index');
  130. }
  131. /**
  132. * Compile an index creation command.
  133. *
  134. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  135. * @param \Illuminate\Support\Fluent $command
  136. * @param string $type
  137. * @return string
  138. */
  139. protected function compileKey(Blueprint $blueprint, Fluent $command, $type)
  140. {
  141. $columns = $this->columnize($command->columns);
  142. $table = $this->wrapTable($blueprint);
  143. return "alter table {$table} add {$type} {$command->index}($columns)";
  144. }
  145. /**
  146. * Compile a drop table command.
  147. *
  148. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  149. * @param \Illuminate\Support\Fluent $command
  150. * @return string
  151. */
  152. public function compileDrop(Blueprint $blueprint, Fluent $command)
  153. {
  154. return 'drop table '.$this->wrapTable($blueprint);
  155. }
  156. /**
  157. * Compile a drop table (if exists) command.
  158. *
  159. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  160. * @param \Illuminate\Support\Fluent $command
  161. * @return string
  162. */
  163. public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
  164. {
  165. return 'drop table if exists '.$this->wrapTable($blueprint);
  166. }
  167. /**
  168. * Compile a drop column command.
  169. *
  170. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  171. * @param \Illuminate\Support\Fluent $command
  172. * @return string
  173. */
  174. public function compileDropColumn(Blueprint $blueprint, Fluent $command)
  175. {
  176. $columns = $this->prefixArray('drop', $this->wrapArray($command->columns));
  177. $table = $this->wrapTable($blueprint);
  178. return 'alter table '.$table.' '.implode(', ', $columns);
  179. }
  180. /**
  181. * Compile a drop primary key command.
  182. *
  183. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  184. * @param \Illuminate\Support\Fluent $command
  185. * @return string
  186. */
  187. public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
  188. {
  189. return 'alter table '.$this->wrapTable($blueprint).' drop primary key';
  190. }
  191. /**
  192. * Compile a drop unique key command.
  193. *
  194. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  195. * @param \Illuminate\Support\Fluent $command
  196. * @return string
  197. */
  198. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  199. {
  200. $table = $this->wrapTable($blueprint);
  201. return "alter table {$table} drop index {$command->index}";
  202. }
  203. /**
  204. * Compile a drop index command.
  205. *
  206. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  207. * @param \Illuminate\Support\Fluent $command
  208. * @return string
  209. */
  210. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  211. {
  212. $table = $this->wrapTable($blueprint);
  213. return "alter table {$table} drop index {$command->index}";
  214. }
  215. /**
  216. * Compile a drop foreign key command.
  217. *
  218. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  219. * @param \Illuminate\Support\Fluent $command
  220. * @return string
  221. */
  222. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  223. {
  224. $table = $this->wrapTable($blueprint);
  225. return "alter table {$table} drop foreign key {$command->index}";
  226. }
  227. /**
  228. * Compile a rename table command.
  229. *
  230. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  231. * @param \Illuminate\Support\Fluent $command
  232. * @return string
  233. */
  234. public function compileRename(Blueprint $blueprint, Fluent $command)
  235. {
  236. $from = $this->wrapTable($blueprint);
  237. return "rename table {$from} to ".$this->wrapTable($command->to);
  238. }
  239. /**
  240. * Create the column definition for a char type.
  241. *
  242. * @param \Illuminate\Support\Fluent $column
  243. * @return string
  244. */
  245. protected function typeChar(Fluent $column)
  246. {
  247. return "char({$column->length})";
  248. }
  249. /**
  250. * Create the column definition for a string type.
  251. *
  252. * @param \Illuminate\Support\Fluent $column
  253. * @return string
  254. */
  255. protected function typeString(Fluent $column)
  256. {
  257. return "varchar({$column->length})";
  258. }
  259. /**
  260. * Create the column definition for a text type.
  261. *
  262. * @param \Illuminate\Support\Fluent $column
  263. * @return string
  264. */
  265. protected function typeText(Fluent $column)
  266. {
  267. return 'text';
  268. }
  269. /**
  270. * Create the column definition for a medium text type.
  271. *
  272. * @param \Illuminate\Support\Fluent $column
  273. * @return string
  274. */
  275. protected function typeMediumText(Fluent $column)
  276. {
  277. return 'mediumtext';
  278. }
  279. /**
  280. * Create the column definition for a long text type.
  281. *
  282. * @param \Illuminate\Support\Fluent $column
  283. * @return string
  284. */
  285. protected function typeLongText(Fluent $column)
  286. {
  287. return 'longtext';
  288. }
  289. /**
  290. * Create the column definition for a big integer type.
  291. *
  292. * @param \Illuminate\Support\Fluent $column
  293. * @return string
  294. */
  295. protected function typeBigInteger(Fluent $column)
  296. {
  297. return 'bigint';
  298. }
  299. /**
  300. * Create the column definition for a integer type.
  301. *
  302. * @param \Illuminate\Support\Fluent $column
  303. * @return string
  304. */
  305. protected function typeInteger(Fluent $column)
  306. {
  307. return 'int';
  308. }
  309. /**
  310. * Create the column definition for a medium integer type.
  311. *
  312. * @param \Illuminate\Support\Fluent $column
  313. * @return string
  314. */
  315. protected function typeMediumInteger(Fluent $column)
  316. {
  317. return 'mediumint';
  318. }
  319. /**
  320. * Create the column definition for a tiny integer type.
  321. *
  322. * @param \Illuminate\Support\Fluent $column
  323. * @return string
  324. */
  325. protected function typeTinyInteger(Fluent $column)
  326. {
  327. return 'tinyint';
  328. }
  329. /**
  330. * Create the column definition for a small integer type.
  331. *
  332. * @param \Illuminate\Support\Fluent $column
  333. * @return string
  334. */
  335. protected function typeSmallInteger(Fluent $column)
  336. {
  337. return 'smallint';
  338. }
  339. /**
  340. * Create the column definition for a float type.
  341. *
  342. * @param \Illuminate\Support\Fluent $column
  343. * @return string
  344. */
  345. protected function typeFloat(Fluent $column)
  346. {
  347. return "float({$column->total}, {$column->places})";
  348. }
  349. /**
  350. * Create the column definition for a double type.
  351. *
  352. * @param \Illuminate\Support\Fluent $column
  353. * @return string
  354. */
  355. protected function typeDouble(Fluent $column)
  356. {
  357. if ($column->total && $column->places)
  358. {
  359. return "double({$column->total}, {$column->places})";
  360. }
  361. else
  362. {
  363. return 'double';
  364. }
  365. }
  366. /**
  367. * Create the column definition for a decimal type.
  368. *
  369. * @param \Illuminate\Support\Fluent $column
  370. * @return string
  371. */
  372. protected function typeDecimal(Fluent $column)
  373. {
  374. return "decimal({$column->total}, {$column->places})";
  375. }
  376. /**
  377. * Create the column definition for a boolean type.
  378. *
  379. * @param \Illuminate\Support\Fluent $column
  380. * @return string
  381. */
  382. protected function typeBoolean(Fluent $column)
  383. {
  384. return 'tinyint(1)';
  385. }
  386. /**
  387. * Create the column definition for an enum type.
  388. *
  389. * @param \Illuminate\Support\Fluent $column
  390. * @return string
  391. */
  392. protected function typeEnum(Fluent $column)
  393. {
  394. return "enum('".implode("', '", $column->allowed)."')";
  395. }
  396. /**
  397. * Create the column definition for a date type.
  398. *
  399. * @param \Illuminate\Support\Fluent $column
  400. * @return string
  401. */
  402. protected function typeDate(Fluent $column)
  403. {
  404. return 'date';
  405. }
  406. /**
  407. * Create the column definition for a date-time type.
  408. *
  409. * @param \Illuminate\Support\Fluent $column
  410. * @return string
  411. */
  412. protected function typeDateTime(Fluent $column)
  413. {
  414. return 'datetime';
  415. }
  416. /**
  417. * Create the column definition for a time type.
  418. *
  419. * @param \Illuminate\Support\Fluent $column
  420. * @return string
  421. */
  422. protected function typeTime(Fluent $column)
  423. {
  424. return 'time';
  425. }
  426. /**
  427. * Create the column definition for a timestamp type.
  428. *
  429. * @param \Illuminate\Support\Fluent $column
  430. * @return string
  431. */
  432. protected function typeTimestamp(Fluent $column)
  433. {
  434. if ( ! $column->nullable) return 'timestamp default 0';
  435. return 'timestamp';
  436. }
  437. /**
  438. * Create the column definition for a binary type.
  439. *
  440. * @param \Illuminate\Support\Fluent $column
  441. * @return string
  442. */
  443. protected function typeBinary(Fluent $column)
  444. {
  445. return 'blob';
  446. }
  447. /**
  448. * Get the SQL for an unsigned column modifier.
  449. *
  450. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  451. * @param \Illuminate\Support\Fluent $column
  452. * @return string|null
  453. */
  454. protected function modifyUnsigned(Blueprint $blueprint, Fluent $column)
  455. {
  456. if ($column->unsigned) return ' unsigned';
  457. }
  458. /**
  459. * Get the SQL for a nullable column modifier.
  460. *
  461. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  462. * @param \Illuminate\Support\Fluent $column
  463. * @return string|null
  464. */
  465. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  466. {
  467. return $column->nullable ? ' null' : ' not null';
  468. }
  469. /**
  470. * Get the SQL for a default column modifier.
  471. *
  472. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  473. * @param \Illuminate\Support\Fluent $column
  474. * @return string|null
  475. */
  476. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  477. {
  478. if ( ! is_null($column->default))
  479. {
  480. return " default ".$this->getDefaultValue($column->default);
  481. }
  482. }
  483. /**
  484. * Get the SQL for an auto-increment column modifier.
  485. *
  486. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  487. * @param \Illuminate\Support\Fluent $column
  488. * @return string|null
  489. */
  490. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  491. {
  492. if (in_array($column->type, $this->serials) && $column->autoIncrement)
  493. {
  494. return ' auto_increment primary key';
  495. }
  496. }
  497. /**
  498. * Get the SQL for an "after" column modifier.
  499. *
  500. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  501. * @param \Illuminate\Support\Fluent $column
  502. * @return string|null
  503. */
  504. protected function modifyAfter(Blueprint $blueprint, Fluent $column)
  505. {
  506. if ( ! is_null($column->after))
  507. {
  508. return ' after '.$this->wrap($column->after);
  509. }
  510. }
  511. }