MySqlGrammar.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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', 'Srid',
  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 a rename index command.
  309. *
  310. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  311. * @param \Illuminate\Support\Fluent $command
  312. * @return string
  313. */
  314. public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
  315. {
  316. return sprintf('alter table %s rename index %s to %s',
  317. $this->wrapTable($blueprint),
  318. $this->wrap($command->from),
  319. $this->wrap($command->to)
  320. );
  321. }
  322. /**
  323. * Compile the SQL needed to drop all tables.
  324. *
  325. * @param array $tables
  326. * @return string
  327. */
  328. public function compileDropAllTables($tables)
  329. {
  330. return 'drop table '.implode(',', $this->wrapArray($tables));
  331. }
  332. /**
  333. * Compile the SQL needed to retrieve all table names.
  334. *
  335. * @return string
  336. */
  337. public function compileGetAllTables()
  338. {
  339. return 'SHOW FULL TABLES WHERE table_type = \'BASE TABLE\'';
  340. }
  341. /**
  342. * Compile the command to enable foreign key constraints.
  343. *
  344. * @return string
  345. */
  346. public function compileEnableForeignKeyConstraints()
  347. {
  348. return 'SET FOREIGN_KEY_CHECKS=1;';
  349. }
  350. /**
  351. * Compile the command to disable foreign key constraints.
  352. *
  353. * @return string
  354. */
  355. public function compileDisableForeignKeyConstraints()
  356. {
  357. return 'SET FOREIGN_KEY_CHECKS=0;';
  358. }
  359. /**
  360. * Create the column definition for a char type.
  361. *
  362. * @param \Illuminate\Support\Fluent $column
  363. * @return string
  364. */
  365. protected function typeChar(Fluent $column)
  366. {
  367. return "char({$column->length})";
  368. }
  369. /**
  370. * Create the column definition for a string type.
  371. *
  372. * @param \Illuminate\Support\Fluent $column
  373. * @return string
  374. */
  375. protected function typeString(Fluent $column)
  376. {
  377. return "varchar({$column->length})";
  378. }
  379. /**
  380. * Create the column definition for a text type.
  381. *
  382. * @param \Illuminate\Support\Fluent $column
  383. * @return string
  384. */
  385. protected function typeText(Fluent $column)
  386. {
  387. return 'text';
  388. }
  389. /**
  390. * Create the column definition for a medium text type.
  391. *
  392. * @param \Illuminate\Support\Fluent $column
  393. * @return string
  394. */
  395. protected function typeMediumText(Fluent $column)
  396. {
  397. return 'mediumtext';
  398. }
  399. /**
  400. * Create the column definition for a long text type.
  401. *
  402. * @param \Illuminate\Support\Fluent $column
  403. * @return string
  404. */
  405. protected function typeLongText(Fluent $column)
  406. {
  407. return 'longtext';
  408. }
  409. /**
  410. * Create the column definition for a big integer type.
  411. *
  412. * @param \Illuminate\Support\Fluent $column
  413. * @return string
  414. */
  415. protected function typeBigInteger(Fluent $column)
  416. {
  417. return 'bigint';
  418. }
  419. /**
  420. * Create the column definition for an integer type.
  421. *
  422. * @param \Illuminate\Support\Fluent $column
  423. * @return string
  424. */
  425. protected function typeInteger(Fluent $column)
  426. {
  427. return 'int';
  428. }
  429. /**
  430. * Create the column definition for a medium integer type.
  431. *
  432. * @param \Illuminate\Support\Fluent $column
  433. * @return string
  434. */
  435. protected function typeMediumInteger(Fluent $column)
  436. {
  437. return 'mediumint';
  438. }
  439. /**
  440. * Create the column definition for a tiny integer type.
  441. *
  442. * @param \Illuminate\Support\Fluent $column
  443. * @return string
  444. */
  445. protected function typeTinyInteger(Fluent $column)
  446. {
  447. return 'tinyint';
  448. }
  449. /**
  450. * Create the column definition for a small integer type.
  451. *
  452. * @param \Illuminate\Support\Fluent $column
  453. * @return string
  454. */
  455. protected function typeSmallInteger(Fluent $column)
  456. {
  457. return 'smallint';
  458. }
  459. /**
  460. * Create the column definition for a float type.
  461. *
  462. * @param \Illuminate\Support\Fluent $column
  463. * @return string
  464. */
  465. protected function typeFloat(Fluent $column)
  466. {
  467. return $this->typeDouble($column);
  468. }
  469. /**
  470. * Create the column definition for a double type.
  471. *
  472. * @param \Illuminate\Support\Fluent $column
  473. * @return string
  474. */
  475. protected function typeDouble(Fluent $column)
  476. {
  477. if ($column->total && $column->places) {
  478. return "double({$column->total}, {$column->places})";
  479. }
  480. return 'double';
  481. }
  482. /**
  483. * Create the column definition for a decimal type.
  484. *
  485. * @param \Illuminate\Support\Fluent $column
  486. * @return string
  487. */
  488. protected function typeDecimal(Fluent $column)
  489. {
  490. return "decimal({$column->total}, {$column->places})";
  491. }
  492. /**
  493. * Create the column definition for a boolean type.
  494. *
  495. * @param \Illuminate\Support\Fluent $column
  496. * @return string
  497. */
  498. protected function typeBoolean(Fluent $column)
  499. {
  500. return 'tinyint(1)';
  501. }
  502. /**
  503. * Create the column definition for an enumeration type.
  504. *
  505. * @param \Illuminate\Support\Fluent $column
  506. * @return string
  507. */
  508. protected function typeEnum(Fluent $column)
  509. {
  510. return sprintf('enum(%s)', $this->quoteString($column->allowed));
  511. }
  512. /**
  513. * Create the column definition for a json type.
  514. *
  515. * @param \Illuminate\Support\Fluent $column
  516. * @return string
  517. */
  518. protected function typeJson(Fluent $column)
  519. {
  520. return 'json';
  521. }
  522. /**
  523. * Create the column definition for a jsonb type.
  524. *
  525. * @param \Illuminate\Support\Fluent $column
  526. * @return string
  527. */
  528. protected function typeJsonb(Fluent $column)
  529. {
  530. return 'json';
  531. }
  532. /**
  533. * Create the column definition for a date type.
  534. *
  535. * @param \Illuminate\Support\Fluent $column
  536. * @return string
  537. */
  538. protected function typeDate(Fluent $column)
  539. {
  540. return 'date';
  541. }
  542. /**
  543. * Create the column definition for a date-time type.
  544. *
  545. * @param \Illuminate\Support\Fluent $column
  546. * @return string
  547. */
  548. protected function typeDateTime(Fluent $column)
  549. {
  550. return $column->precision ? "datetime($column->precision)" : 'datetime';
  551. }
  552. /**
  553. * Create the column definition for a date-time (with time zone) type.
  554. *
  555. * @param \Illuminate\Support\Fluent $column
  556. * @return string
  557. */
  558. protected function typeDateTimeTz(Fluent $column)
  559. {
  560. return $this->typeDateTime($column);
  561. }
  562. /**
  563. * Create the column definition for a time type.
  564. *
  565. * @param \Illuminate\Support\Fluent $column
  566. * @return string
  567. */
  568. protected function typeTime(Fluent $column)
  569. {
  570. return $column->precision ? "time($column->precision)" : 'time';
  571. }
  572. /**
  573. * Create the column definition for a time (with time zone) type.
  574. *
  575. * @param \Illuminate\Support\Fluent $column
  576. * @return string
  577. */
  578. protected function typeTimeTz(Fluent $column)
  579. {
  580. return $this->typeTime($column);
  581. }
  582. /**
  583. * Create the column definition for a timestamp type.
  584. *
  585. * @param \Illuminate\Support\Fluent $column
  586. * @return string
  587. */
  588. protected function typeTimestamp(Fluent $column)
  589. {
  590. $columnType = $column->precision ? "timestamp($column->precision)" : 'timestamp';
  591. return $column->useCurrent ? "$columnType default CURRENT_TIMESTAMP" : $columnType;
  592. }
  593. /**
  594. * Create the column definition for a timestamp (with time zone) type.
  595. *
  596. * @param \Illuminate\Support\Fluent $column
  597. * @return string
  598. */
  599. protected function typeTimestampTz(Fluent $column)
  600. {
  601. return $this->typeTimestamp($column);
  602. }
  603. /**
  604. * Create the column definition for a year type.
  605. *
  606. * @param \Illuminate\Support\Fluent $column
  607. * @return string
  608. */
  609. protected function typeYear(Fluent $column)
  610. {
  611. return 'year';
  612. }
  613. /**
  614. * Create the column definition for a binary type.
  615. *
  616. * @param \Illuminate\Support\Fluent $column
  617. * @return string
  618. */
  619. protected function typeBinary(Fluent $column)
  620. {
  621. return 'blob';
  622. }
  623. /**
  624. * Create the column definition for a uuid type.
  625. *
  626. * @param \Illuminate\Support\Fluent $column
  627. * @return string
  628. */
  629. protected function typeUuid(Fluent $column)
  630. {
  631. return 'char(36)';
  632. }
  633. /**
  634. * Create the column definition for an IP address type.
  635. *
  636. * @param \Illuminate\Support\Fluent $column
  637. * @return string
  638. */
  639. protected function typeIpAddress(Fluent $column)
  640. {
  641. return 'varchar(45)';
  642. }
  643. /**
  644. * Create the column definition for a MAC address type.
  645. *
  646. * @param \Illuminate\Support\Fluent $column
  647. * @return string
  648. */
  649. protected function typeMacAddress(Fluent $column)
  650. {
  651. return 'varchar(17)';
  652. }
  653. /**
  654. * Create the column definition for a spatial Geometry type.
  655. *
  656. * @param \Illuminate\Support\Fluent $column
  657. * @return string
  658. */
  659. public function typeGeometry(Fluent $column)
  660. {
  661. return 'geometry';
  662. }
  663. /**
  664. * Create the column definition for a spatial Point type.
  665. *
  666. * @param \Illuminate\Support\Fluent $column
  667. * @return string
  668. */
  669. public function typePoint(Fluent $column)
  670. {
  671. return 'point';
  672. }
  673. /**
  674. * Create the column definition for a spatial LineString type.
  675. *
  676. * @param \Illuminate\Support\Fluent $column
  677. * @return string
  678. */
  679. public function typeLineString(Fluent $column)
  680. {
  681. return 'linestring';
  682. }
  683. /**
  684. * Create the column definition for a spatial Polygon type.
  685. *
  686. * @param \Illuminate\Support\Fluent $column
  687. * @return string
  688. */
  689. public function typePolygon(Fluent $column)
  690. {
  691. return 'polygon';
  692. }
  693. /**
  694. * Create the column definition for a spatial GeometryCollection type.
  695. *
  696. * @param \Illuminate\Support\Fluent $column
  697. * @return string
  698. */
  699. public function typeGeometryCollection(Fluent $column)
  700. {
  701. return 'geometrycollection';
  702. }
  703. /**
  704. * Create the column definition for a spatial MultiPoint type.
  705. *
  706. * @param \Illuminate\Support\Fluent $column
  707. * @return string
  708. */
  709. public function typeMultiPoint(Fluent $column)
  710. {
  711. return 'multipoint';
  712. }
  713. /**
  714. * Create the column definition for a spatial MultiLineString type.
  715. *
  716. * @param \Illuminate\Support\Fluent $column
  717. * @return string
  718. */
  719. public function typeMultiLineString(Fluent $column)
  720. {
  721. return 'multilinestring';
  722. }
  723. /**
  724. * Create the column definition for a spatial MultiPolygon type.
  725. *
  726. * @param \Illuminate\Support\Fluent $column
  727. * @return string
  728. */
  729. public function typeMultiPolygon(Fluent $column)
  730. {
  731. return 'multipolygon';
  732. }
  733. /**
  734. * Get the SQL for a generated virtual column modifier.
  735. *
  736. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  737. * @param \Illuminate\Support\Fluent $column
  738. * @return string|null
  739. */
  740. protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column)
  741. {
  742. if (! is_null($column->virtualAs)) {
  743. return " as ({$column->virtualAs})";
  744. }
  745. }
  746. /**
  747. * Get the SQL for a generated stored column modifier.
  748. *
  749. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  750. * @param \Illuminate\Support\Fluent $column
  751. * @return string|null
  752. */
  753. protected function modifyStoredAs(Blueprint $blueprint, Fluent $column)
  754. {
  755. if (! is_null($column->storedAs)) {
  756. return " as ({$column->storedAs}) stored";
  757. }
  758. }
  759. /**
  760. * Get the SQL for an unsigned column modifier.
  761. *
  762. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  763. * @param \Illuminate\Support\Fluent $column
  764. * @return string|null
  765. */
  766. protected function modifyUnsigned(Blueprint $blueprint, Fluent $column)
  767. {
  768. if ($column->unsigned) {
  769. return ' unsigned';
  770. }
  771. }
  772. /**
  773. * Get the SQL for a character set column modifier.
  774. *
  775. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  776. * @param \Illuminate\Support\Fluent $column
  777. * @return string|null
  778. */
  779. protected function modifyCharset(Blueprint $blueprint, Fluent $column)
  780. {
  781. if (! is_null($column->charset)) {
  782. return ' character set '.$column->charset;
  783. }
  784. }
  785. /**
  786. * Get the SQL for a collation column modifier.
  787. *
  788. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  789. * @param \Illuminate\Support\Fluent $column
  790. * @return string|null
  791. */
  792. protected function modifyCollate(Blueprint $blueprint, Fluent $column)
  793. {
  794. if (! is_null($column->collation)) {
  795. return " collate '{$column->collation}'";
  796. }
  797. }
  798. /**
  799. * Get the SQL for a nullable column modifier.
  800. *
  801. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  802. * @param \Illuminate\Support\Fluent $column
  803. * @return string|null
  804. */
  805. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  806. {
  807. if (is_null($column->virtualAs) && is_null($column->storedAs)) {
  808. return $column->nullable ? ' null' : ' not null';
  809. }
  810. }
  811. /**
  812. * Get the SQL for a default column modifier.
  813. *
  814. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  815. * @param \Illuminate\Support\Fluent $column
  816. * @return string|null
  817. */
  818. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  819. {
  820. if (! is_null($column->default)) {
  821. return ' default '.$this->getDefaultValue($column->default);
  822. }
  823. }
  824. /**
  825. * Get the SQL for an auto-increment column modifier.
  826. *
  827. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  828. * @param \Illuminate\Support\Fluent $column
  829. * @return string|null
  830. */
  831. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  832. {
  833. if (in_array($column->type, $this->serials) && $column->autoIncrement) {
  834. return ' auto_increment primary key';
  835. }
  836. }
  837. /**
  838. * Get the SQL for a "first" column modifier.
  839. *
  840. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  841. * @param \Illuminate\Support\Fluent $column
  842. * @return string|null
  843. */
  844. protected function modifyFirst(Blueprint $blueprint, Fluent $column)
  845. {
  846. if (! is_null($column->first)) {
  847. return ' first';
  848. }
  849. }
  850. /**
  851. * Get the SQL for an "after" column modifier.
  852. *
  853. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  854. * @param \Illuminate\Support\Fluent $column
  855. * @return string|null
  856. */
  857. protected function modifyAfter(Blueprint $blueprint, Fluent $column)
  858. {
  859. if (! is_null($column->after)) {
  860. return ' after '.$this->wrap($column->after);
  861. }
  862. }
  863. /**
  864. * Get the SQL for a "comment" column modifier.
  865. *
  866. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  867. * @param \Illuminate\Support\Fluent $column
  868. * @return string|null
  869. */
  870. protected function modifyComment(Blueprint $blueprint, Fluent $column)
  871. {
  872. if (! is_null($column->comment)) {
  873. return " comment '".addslashes($column->comment)."'";
  874. }
  875. }
  876. /**
  877. * Get the SQL for a SRID column modifier.
  878. *
  879. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  880. * @param \Illuminate\Support\Fluent $column
  881. * @return string|null
  882. */
  883. protected function modifySrid(Blueprint $blueprint, Fluent $column)
  884. {
  885. if (! is_null($column->srid) && is_int($column->srid) && $column->srid > 0) {
  886. return ' srid '.$column->srid;
  887. }
  888. }
  889. /**
  890. * Wrap a single string in keyword identifiers.
  891. *
  892. * @param string $value
  893. * @return string
  894. */
  895. protected function wrapValue($value)
  896. {
  897. if ($value !== '*') {
  898. return '`'.str_replace('`', '``', $value).'`';
  899. }
  900. return $value;
  901. }
  902. }