MySqlGrammar.php 28 KB

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