MySqlGrammar.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. <?php
  2. namespace Illuminate\Database\Schema\Grammars;
  3. use Illuminate\Support\Fluent;
  4. use Illuminate\Database\Connection;
  5. use Illuminate\Database\Schema\Blueprint;
  6. class MySqlGrammar extends Grammar
  7. {
  8. /**
  9. * The possible column modifiers.
  10. *
  11. * @var array
  12. */
  13. protected $modifiers = [
  14. 'Unsigned', 'VirtualAs', 'StoredAs', 'Charset', 'Collate', 'Nullable',
  15. 'Default', 'Increment', 'Comment', 'After', 'First',
  16. ];
  17. /**
  18. * The possible column serials.
  19. *
  20. * @var array
  21. */
  22. protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger'];
  23. /**
  24. * Compile the query to determine the list of tables.
  25. *
  26. * @return string
  27. */
  28. public function compileTableExists()
  29. {
  30. return 'select * from information_schema.tables where table_schema = ? and table_name = ?';
  31. }
  32. /**
  33. * Compile the query to determine the list of columns.
  34. *
  35. * @return string
  36. */
  37. public function compileColumnListing()
  38. {
  39. return 'select column_name as `column_name` from information_schema.columns where table_schema = ? and table_name = ?';
  40. }
  41. /**
  42. * Compile a create table command.
  43. *
  44. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  45. * @param \Illuminate\Support\Fluent $command
  46. * @param \Illuminate\Database\Connection $connection
  47. * @return string
  48. */
  49. public function compileCreate(Blueprint $blueprint, Fluent $command, Connection $connection)
  50. {
  51. $sql = $this->compileCreateTable(
  52. $blueprint, $command, $connection
  53. );
  54. // Once we have the primary SQL, we can add the encoding option to the SQL for
  55. // the table. Then, we can check if a storage engine has been supplied for
  56. // the table. If so, we will add the engine declaration to the SQL query.
  57. $sql = $this->compileCreateEncoding(
  58. $sql, $connection, $blueprint
  59. );
  60. // Finally, we will append the engine configuration onto this SQL statement as
  61. // the final thing we do before returning this finished SQL. Once this gets
  62. // added the query will be ready to execute against the real connections.
  63. return $this->compileCreateEngine(
  64. $sql, $connection, $blueprint
  65. );
  66. }
  67. /**
  68. * Create the main create table clause.
  69. *
  70. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  71. * @param \Illuminate\Support\Fluent $command
  72. * @param \Illuminate\Database\Connection $connection
  73. * @return string
  74. */
  75. protected function compileCreateTable($blueprint, $command, $connection)
  76. {
  77. return sprintf('%s table %s (%s)',
  78. $blueprint->temporary ? 'create temporary' : 'create',
  79. $this->wrapTable($blueprint),
  80. implode(', ', $this->getColumns($blueprint))
  81. );
  82. }
  83. /**
  84. * Append the character set specifications to a command.
  85. *
  86. * @param string $sql
  87. * @param \Illuminate\Database\Connection $connection
  88. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  89. * @return string
  90. */
  91. protected function compileCreateEncoding($sql, Connection $connection, Blueprint $blueprint)
  92. {
  93. // First we will set the character set if one has been set on either the create
  94. // blueprint itself or on the root configuration for the connection that the
  95. // table is being created on. We will add these to the create table query.
  96. if (isset($blueprint->charset)) {
  97. $sql .= ' default character set '.$blueprint->charset;
  98. } elseif (! is_null($charset = $connection->getConfig('charset'))) {
  99. $sql .= ' default character set '.$charset;
  100. }
  101. // Next we will add the collation to the create table statement if one has been
  102. // added to either this create table blueprint or the configuration for this
  103. // connection that the query is targeting. We'll add it to this SQL query.
  104. if (isset($blueprint->collation)) {
  105. $sql .= ' collate '.$blueprint->collation;
  106. } elseif (! is_null($collation = $connection->getConfig('collation'))) {
  107. $sql .= ' collate '.$collation;
  108. }
  109. return $sql;
  110. }
  111. /**
  112. * Append the engine specifications to a command.
  113. *
  114. * @param string $sql
  115. * @param \Illuminate\Database\Connection $connection
  116. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  117. * @return string
  118. */
  119. protected function compileCreateEngine($sql, Connection $connection, Blueprint $blueprint)
  120. {
  121. if (isset($blueprint->engine)) {
  122. return $sql.' engine = '.$blueprint->engine;
  123. } elseif (! is_null($engine = $connection->getConfig('engine'))) {
  124. return $sql.' engine = '.$engine;
  125. }
  126. return $sql;
  127. }
  128. /**
  129. * Compile an add column command.
  130. *
  131. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  132. * @param \Illuminate\Support\Fluent $command
  133. * @return string
  134. */
  135. public function compileAdd(Blueprint $blueprint, Fluent $command)
  136. {
  137. $columns = $this->prefixArray('add', $this->getColumns($blueprint));
  138. return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns);
  139. }
  140. /**
  141. * Compile a primary key command.
  142. *
  143. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  144. * @param \Illuminate\Support\Fluent $command
  145. * @return string
  146. */
  147. public function compilePrimary(Blueprint $blueprint, Fluent $command)
  148. {
  149. $command->name(null);
  150. return $this->compileKey($blueprint, $command, 'primary key');
  151. }
  152. /**
  153. * Compile a unique key command.
  154. *
  155. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  156. * @param \Illuminate\Support\Fluent $command
  157. * @return string
  158. */
  159. public function compileUnique(Blueprint $blueprint, Fluent $command)
  160. {
  161. return $this->compileKey($blueprint, $command, 'unique');
  162. }
  163. /**
  164. * Compile a plain index key command.
  165. *
  166. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  167. * @param \Illuminate\Support\Fluent $command
  168. * @return string
  169. */
  170. public function compileIndex(Blueprint $blueprint, Fluent $command)
  171. {
  172. return $this->compileKey($blueprint, $command, 'index');
  173. }
  174. /**
  175. * Compile a spatial index key command.
  176. *
  177. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  178. * @param \Illuminate\Support\Fluent $command
  179. * @return string
  180. */
  181. public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)
  182. {
  183. return $this->compileKey($blueprint, $command, 'spatial index');
  184. }
  185. /**
  186. * Compile an index creation command.
  187. *
  188. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  189. * @param \Illuminate\Support\Fluent $command
  190. * @param string $type
  191. * @return string
  192. */
  193. protected function compileKey(Blueprint $blueprint, Fluent $command, $type)
  194. {
  195. return sprintf('alter table %s add %s %s%s(%s)',
  196. $this->wrapTable($blueprint),
  197. $type,
  198. $this->wrap($command->index),
  199. $command->algorithm ? ' using '.$command->algorithm : '',
  200. $this->columnize($command->columns)
  201. );
  202. }
  203. /**
  204. * Compile a drop table command.
  205. *
  206. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  207. * @param \Illuminate\Support\Fluent $command
  208. * @return string
  209. */
  210. public function compileDrop(Blueprint $blueprint, Fluent $command)
  211. {
  212. return 'drop table '.$this->wrapTable($blueprint);
  213. }
  214. /**
  215. * Compile a drop table (if exists) command.
  216. *
  217. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  218. * @param \Illuminate\Support\Fluent $command
  219. * @return string
  220. */
  221. public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
  222. {
  223. return 'drop table if exists '.$this->wrapTable($blueprint);
  224. }
  225. /**
  226. * Compile a drop column command.
  227. *
  228. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  229. * @param \Illuminate\Support\Fluent $command
  230. * @return string
  231. */
  232. public function compileDropColumn(Blueprint $blueprint, Fluent $command)
  233. {
  234. $columns = $this->prefixArray('drop', $this->wrapArray($command->columns));
  235. return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns);
  236. }
  237. /**
  238. * Compile a drop primary key command.
  239. *
  240. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  241. * @param \Illuminate\Support\Fluent $command
  242. * @return string
  243. */
  244. public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
  245. {
  246. return 'alter table '.$this->wrapTable($blueprint).' drop primary key';
  247. }
  248. /**
  249. * Compile a drop unique key command.
  250. *
  251. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  252. * @param \Illuminate\Support\Fluent $command
  253. * @return string
  254. */
  255. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  256. {
  257. $index = $this->wrap($command->index);
  258. return "alter table {$this->wrapTable($blueprint)} drop index {$index}";
  259. }
  260. /**
  261. * Compile a drop index command.
  262. *
  263. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  264. * @param \Illuminate\Support\Fluent $command
  265. * @return string
  266. */
  267. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  268. {
  269. $index = $this->wrap($command->index);
  270. return "alter table {$this->wrapTable($blueprint)} drop index {$index}";
  271. }
  272. /**
  273. * Compile a drop spatial index command.
  274. *
  275. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  276. * @param \Illuminate\Support\Fluent $command
  277. * @return string
  278. */
  279. public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command)
  280. {
  281. return $this->compileDropIndex($blueprint, $command);
  282. }
  283. /**
  284. * Compile a drop foreign key command.
  285. *
  286. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  287. * @param \Illuminate\Support\Fluent $command
  288. * @return string
  289. */
  290. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  291. {
  292. $index = $this->wrap($command->index);
  293. return "alter table {$this->wrapTable($blueprint)} drop foreign key {$index}";
  294. }
  295. /**
  296. * Compile a rename table command.
  297. *
  298. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  299. * @param \Illuminate\Support\Fluent $command
  300. * @return string
  301. */
  302. public function compileRename(Blueprint $blueprint, Fluent $command)
  303. {
  304. $from = $this->wrapTable($blueprint);
  305. return "rename table {$from} to ".$this->wrapTable($command->to);
  306. }
  307. /**
  308. * Compile the SQL needed to drop all tables.
  309. *
  310. * @param array $tables
  311. * @return string
  312. */
  313. public function compileDropAllTables($tables)
  314. {
  315. return 'drop table '.implode(',', $this->wrapArray($tables));
  316. }
  317. /**
  318. * Compile the SQL needed to retrieve all table names.
  319. *
  320. * @return string
  321. */
  322. public function compileGetAllTables()
  323. {
  324. return 'SHOW FULL TABLES WHERE table_type = \'BASE TABLE\'';
  325. }
  326. /**
  327. * Compile the command to enable foreign key constraints.
  328. *
  329. * @return string
  330. */
  331. public function compileEnableForeignKeyConstraints()
  332. {
  333. return 'SET FOREIGN_KEY_CHECKS=1;';
  334. }
  335. /**
  336. * Compile the command to disable foreign key constraints.
  337. *
  338. * @return string
  339. */
  340. public function compileDisableForeignKeyConstraints()
  341. {
  342. return 'SET FOREIGN_KEY_CHECKS=0;';
  343. }
  344. /**
  345. * Create the column definition for a char type.
  346. *
  347. * @param \Illuminate\Support\Fluent $column
  348. * @return string
  349. */
  350. protected function typeChar(Fluent $column)
  351. {
  352. return "char({$column->length})";
  353. }
  354. /**
  355. * Create the column definition for a string type.
  356. *
  357. * @param \Illuminate\Support\Fluent $column
  358. * @return string
  359. */
  360. protected function typeString(Fluent $column)
  361. {
  362. return "varchar({$column->length})";
  363. }
  364. /**
  365. * Create the column definition for a text type.
  366. *
  367. * @param \Illuminate\Support\Fluent $column
  368. * @return string
  369. */
  370. protected function typeText(Fluent $column)
  371. {
  372. return 'text';
  373. }
  374. /**
  375. * Create the column definition for a medium text type.
  376. *
  377. * @param \Illuminate\Support\Fluent $column
  378. * @return string
  379. */
  380. protected function typeMediumText(Fluent $column)
  381. {
  382. return 'mediumtext';
  383. }
  384. /**
  385. * Create the column definition for a long text type.
  386. *
  387. * @param \Illuminate\Support\Fluent $column
  388. * @return string
  389. */
  390. protected function typeLongText(Fluent $column)
  391. {
  392. return 'longtext';
  393. }
  394. /**
  395. * Create the column definition for a big integer type.
  396. *
  397. * @param \Illuminate\Support\Fluent $column
  398. * @return string
  399. */
  400. protected function typeBigInteger(Fluent $column)
  401. {
  402. return 'bigint';
  403. }
  404. /**
  405. * Create the column definition for an integer type.
  406. *
  407. * @param \Illuminate\Support\Fluent $column
  408. * @return string
  409. */
  410. protected function typeInteger(Fluent $column)
  411. {
  412. return 'int';
  413. }
  414. /**
  415. * Create the column definition for a medium integer type.
  416. *
  417. * @param \Illuminate\Support\Fluent $column
  418. * @return string
  419. */
  420. protected function typeMediumInteger(Fluent $column)
  421. {
  422. return 'mediumint';
  423. }
  424. /**
  425. * Create the column definition for a tiny integer type.
  426. *
  427. * @param \Illuminate\Support\Fluent $column
  428. * @return string
  429. */
  430. protected function typeTinyInteger(Fluent $column)
  431. {
  432. return 'tinyint';
  433. }
  434. /**
  435. * Create the column definition for a small integer type.
  436. *
  437. * @param \Illuminate\Support\Fluent $column
  438. * @return string
  439. */
  440. protected function typeSmallInteger(Fluent $column)
  441. {
  442. return 'smallint';
  443. }
  444. /**
  445. * Create the column definition for a float type.
  446. *
  447. * @param \Illuminate\Support\Fluent $column
  448. * @return string
  449. */
  450. protected function typeFloat(Fluent $column)
  451. {
  452. return $this->typeDouble($column);
  453. }
  454. /**
  455. * Create the column definition for a double type.
  456. *
  457. * @param \Illuminate\Support\Fluent $column
  458. * @return string
  459. */
  460. protected function typeDouble(Fluent $column)
  461. {
  462. if ($column->total && $column->places) {
  463. return "double({$column->total}, {$column->places})";
  464. }
  465. return 'double';
  466. }
  467. /**
  468. * Create the column definition for a decimal type.
  469. *
  470. * @param \Illuminate\Support\Fluent $column
  471. * @return string
  472. */
  473. protected function typeDecimal(Fluent $column)
  474. {
  475. return "decimal({$column->total}, {$column->places})";
  476. }
  477. /**
  478. * Create the column definition for a boolean type.
  479. *
  480. * @param \Illuminate\Support\Fluent $column
  481. * @return string
  482. */
  483. protected function typeBoolean(Fluent $column)
  484. {
  485. return 'tinyint(1)';
  486. }
  487. /**
  488. * Create the column definition for an enum type.
  489. *
  490. * @param \Illuminate\Support\Fluent $column
  491. * @return string
  492. */
  493. protected function typeEnum(Fluent $column)
  494. {
  495. return "enum('".implode("', '", $column->allowed)."')";
  496. }
  497. /**
  498. * Create the column definition for a json type.
  499. *
  500. * @param \Illuminate\Support\Fluent $column
  501. * @return string
  502. */
  503. protected function typeJson(Fluent $column)
  504. {
  505. return 'json';
  506. }
  507. /**
  508. * Create the column definition for a jsonb type.
  509. *
  510. * @param \Illuminate\Support\Fluent $column
  511. * @return string
  512. */
  513. protected function typeJsonb(Fluent $column)
  514. {
  515. return 'json';
  516. }
  517. /**
  518. * Create the column definition for a date type.
  519. *
  520. * @param \Illuminate\Support\Fluent $column
  521. * @return string
  522. */
  523. protected function typeDate(Fluent $column)
  524. {
  525. return 'date';
  526. }
  527. /**
  528. * Create the column definition for a date-time type.
  529. *
  530. * @param \Illuminate\Support\Fluent $column
  531. * @return string
  532. */
  533. protected function typeDateTime(Fluent $column)
  534. {
  535. return $column->precision ? "datetime($column->precision)" : 'datetime';
  536. }
  537. /**
  538. * Create the column definition for a date-time (with time zone) type.
  539. *
  540. * @param \Illuminate\Support\Fluent $column
  541. * @return string
  542. */
  543. protected function typeDateTimeTz(Fluent $column)
  544. {
  545. return $this->typeDateTime($column);
  546. }
  547. /**
  548. * Create the column definition for a time type.
  549. *
  550. * @param \Illuminate\Support\Fluent $column
  551. * @return string
  552. */
  553. protected function typeTime(Fluent $column)
  554. {
  555. return $column->precision ? "time($column->precision)" : 'time';
  556. }
  557. /**
  558. * Create the column definition for a time (with time zone) type.
  559. *
  560. * @param \Illuminate\Support\Fluent $column
  561. * @return string
  562. */
  563. protected function typeTimeTz(Fluent $column)
  564. {
  565. return $this->typeTime($column);
  566. }
  567. /**
  568. * Create the column definition for a timestamp type.
  569. *
  570. * @param \Illuminate\Support\Fluent $column
  571. * @return string
  572. */
  573. protected function typeTimestamp(Fluent $column)
  574. {
  575. $columnType = $column->precision ? "timestamp($column->precision)" : 'timestamp';
  576. return $column->useCurrent ? "$columnType default CURRENT_TIMESTAMP" : $columnType;
  577. }
  578. /**
  579. * Create the column definition for a timestamp (with time zone) type.
  580. *
  581. * @param \Illuminate\Support\Fluent $column
  582. * @return string
  583. */
  584. protected function typeTimestampTz(Fluent $column)
  585. {
  586. return $this->typeTimestamp($column);
  587. }
  588. /**
  589. * Create the column definition for a year type.
  590. *
  591. * @param \Illuminate\Support\Fluent $column
  592. * @return string
  593. */
  594. protected function typeYear(Fluent $column)
  595. {
  596. return 'year';
  597. }
  598. /**
  599. * Create the column definition for a binary type.
  600. *
  601. * @param \Illuminate\Support\Fluent $column
  602. * @return string
  603. */
  604. protected function typeBinary(Fluent $column)
  605. {
  606. return 'blob';
  607. }
  608. /**
  609. * Create the column definition for a uuid type.
  610. *
  611. * @param \Illuminate\Support\Fluent $column
  612. * @return string
  613. */
  614. protected function typeUuid(Fluent $column)
  615. {
  616. return 'char(36)';
  617. }
  618. /**
  619. * Create the column definition for an IP address type.
  620. *
  621. * @param \Illuminate\Support\Fluent $column
  622. * @return string
  623. */
  624. protected function typeIpAddress(Fluent $column)
  625. {
  626. return 'varchar(45)';
  627. }
  628. /**
  629. * Create the column definition for a MAC address type.
  630. *
  631. * @param \Illuminate\Support\Fluent $column
  632. * @return string
  633. */
  634. protected function typeMacAddress(Fluent $column)
  635. {
  636. return 'varchar(17)';
  637. }
  638. /**
  639. * Create the column definition for a spatial Geometry type.
  640. *
  641. * @param \Illuminate\Support\Fluent $column
  642. * @return string
  643. */
  644. public function typeGeometry(Fluent $column)
  645. {
  646. return 'geometry';
  647. }
  648. /**
  649. * Create the column definition for a spatial Point type.
  650. *
  651. * @param \Illuminate\Support\Fluent $column
  652. * @return string
  653. */
  654. public function typePoint(Fluent $column)
  655. {
  656. return 'point';
  657. }
  658. /**
  659. * Create the column definition for a spatial LineString type.
  660. *
  661. * @param \Illuminate\Support\Fluent $column
  662. * @return string
  663. */
  664. public function typeLineString(Fluent $column)
  665. {
  666. return 'linestring';
  667. }
  668. /**
  669. * Create the column definition for a spatial Polygon type.
  670. *
  671. * @param \Illuminate\Support\Fluent $column
  672. * @return string
  673. */
  674. public function typePolygon(Fluent $column)
  675. {
  676. return 'polygon';
  677. }
  678. /**
  679. * Create the column definition for a spatial GeometryCollection type.
  680. *
  681. * @param \Illuminate\Support\Fluent $column
  682. * @return string
  683. */
  684. public function typeGeometryCollection(Fluent $column)
  685. {
  686. return 'geometrycollection';
  687. }
  688. /**
  689. * Create the column definition for a spatial MultiPoint type.
  690. *
  691. * @param \Illuminate\Support\Fluent $column
  692. * @return string
  693. */
  694. public function typeMultiPoint(Fluent $column)
  695. {
  696. return 'multipoint';
  697. }
  698. /**
  699. * Create the column definition for a spatial MultiLineString type.
  700. *
  701. * @param \Illuminate\Support\Fluent $column
  702. * @return string
  703. */
  704. public function typeMultiLineString(Fluent $column)
  705. {
  706. return 'multilinestring';
  707. }
  708. /**
  709. * Create the column definition for a spatial MultiPolygon type.
  710. *
  711. * @param \Illuminate\Support\Fluent $column
  712. * @return string
  713. */
  714. public function typeMultiPolygon(Fluent $column)
  715. {
  716. return 'multipolygon';
  717. }
  718. /**
  719. * Get the SQL for a generated virtual column modifier.
  720. *
  721. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  722. * @param \Illuminate\Support\Fluent $column
  723. * @return string|null
  724. */
  725. protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column)
  726. {
  727. if (! is_null($column->virtualAs)) {
  728. return " as ({$column->virtualAs})";
  729. }
  730. }
  731. /**
  732. * Get the SQL for a generated stored column modifier.
  733. *
  734. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  735. * @param \Illuminate\Support\Fluent $column
  736. * @return string|null
  737. */
  738. protected function modifyStoredAs(Blueprint $blueprint, Fluent $column)
  739. {
  740. if (! is_null($column->storedAs)) {
  741. return " as ({$column->storedAs}) stored";
  742. }
  743. }
  744. /**
  745. * Get the SQL for an unsigned column modifier.
  746. *
  747. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  748. * @param \Illuminate\Support\Fluent $column
  749. * @return string|null
  750. */
  751. protected function modifyUnsigned(Blueprint $blueprint, Fluent $column)
  752. {
  753. if ($column->unsigned) {
  754. return ' unsigned';
  755. }
  756. }
  757. /**
  758. * Get the SQL for a character set column modifier.
  759. *
  760. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  761. * @param \Illuminate\Support\Fluent $column
  762. * @return string|null
  763. */
  764. protected function modifyCharset(Blueprint $blueprint, Fluent $column)
  765. {
  766. if (! is_null($column->charset)) {
  767. return ' character set '.$column->charset;
  768. }
  769. }
  770. /**
  771. * Get the SQL for a collation column modifier.
  772. *
  773. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  774. * @param \Illuminate\Support\Fluent $column
  775. * @return string|null
  776. */
  777. protected function modifyCollate(Blueprint $blueprint, Fluent $column)
  778. {
  779. if (! is_null($column->collation)) {
  780. return ' collate '.$column->collation;
  781. }
  782. }
  783. /**
  784. * Get the SQL for a nullable column modifier.
  785. *
  786. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  787. * @param \Illuminate\Support\Fluent $column
  788. * @return string|null
  789. */
  790. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  791. {
  792. if (is_null($column->virtualAs) && is_null($column->storedAs)) {
  793. return $column->nullable ? ' null' : ' not null';
  794. }
  795. }
  796. /**
  797. * Get the SQL for a default column modifier.
  798. *
  799. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  800. * @param \Illuminate\Support\Fluent $column
  801. * @return string|null
  802. */
  803. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  804. {
  805. if (! is_null($column->default)) {
  806. return ' default '.$this->getDefaultValue($column->default);
  807. }
  808. }
  809. /**
  810. * Get the SQL for an auto-increment column modifier.
  811. *
  812. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  813. * @param \Illuminate\Support\Fluent $column
  814. * @return string|null
  815. */
  816. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  817. {
  818. if (in_array($column->type, $this->serials) && $column->autoIncrement) {
  819. return ' auto_increment primary key';
  820. }
  821. }
  822. /**
  823. * Get the SQL for a "first" column modifier.
  824. *
  825. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  826. * @param \Illuminate\Support\Fluent $column
  827. * @return string|null
  828. */
  829. protected function modifyFirst(Blueprint $blueprint, Fluent $column)
  830. {
  831. if (! is_null($column->first)) {
  832. return ' first';
  833. }
  834. }
  835. /**
  836. * Get the SQL for an "after" column modifier.
  837. *
  838. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  839. * @param \Illuminate\Support\Fluent $column
  840. * @return string|null
  841. */
  842. protected function modifyAfter(Blueprint $blueprint, Fluent $column)
  843. {
  844. if (! is_null($column->after)) {
  845. return ' after '.$this->wrap($column->after);
  846. }
  847. }
  848. /**
  849. * Get the SQL for a "comment" column modifier.
  850. *
  851. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  852. * @param \Illuminate\Support\Fluent $column
  853. * @return string|null
  854. */
  855. protected function modifyComment(Blueprint $blueprint, Fluent $column)
  856. {
  857. if (! is_null($column->comment)) {
  858. return " comment '".addslashes($column->comment)."'";
  859. }
  860. }
  861. /**
  862. * Wrap a single string in keyword identifiers.
  863. *
  864. * @param string $value
  865. * @return string
  866. */
  867. protected function wrapValue($value)
  868. {
  869. if ($value !== '*') {
  870. return '`'.str_replace('`', '``', $value).'`';
  871. }
  872. return $value;
  873. }
  874. }