/** * Cast default value based on column type. Required to prevent conflicts when not nullable * column added to existed table with data in. * * @param AbstractColumn $column * @return bool|float|int|mixed|string */ private function castDefault(AbstractColumn $column) { if ($column->abstractType() == 'timestamp' || $column->abstractType() == 'datetime') { $driver = $this->tableSchema->driver(); return $driver::DEFAULT_DATETIME; } if ($column->abstractType() == 'enum') { //We can use first enum value as default return $column->getEnumValues()[0]; } if ($column->abstractType() == 'json') { return '{}'; } switch ($column->phpType()) { case 'int': return 0; break; case 'float': return 0.0; break; case 'bool': return false; break; } return ''; }
/** * Index sql creation syntax. * * @param bool $includeTable Include table ON statement (not required for inline index * creation). * @return string */ public function sqlStatement($includeTable = true) { $statement = []; $statement[] = $this->type . ($this->type == self::UNIQUE ? ' INDEX' : ''); $statement[] = $this->getName(true); if ($includeTable) { $statement[] = 'ON ' . $this->table->getName(true); } $statement[] = '(' . join(', ', array_map([$this->table->driver(), 'identifier'], $this->columns)) . ')'; return join(' ', $statement); }
/** * Foreign key creation syntax. * * @return string */ public function sqlStatement() { $statement = []; $statement[] = 'CONSTRAINT'; $statement[] = $this->getName(true); $statement[] = 'FOREIGN KEY'; $statement[] = '(' . $this->table->driver()->identifier($this->column) . ')'; $statement[] = 'REFERENCES ' . $this->table->driver()->identifier($this->foreignTable); $statement[] = '(' . $this->table->driver()->identifier($this->foreignKey) . ')'; $statement[] = "ON DELETE {$this->deleteRule}"; $statement[] = "ON UPDATE {$this->updateRule}"; return join(" ", $statement); }
/** * Must return driver specific default value. * * @return string */ protected function prepareDefault() { if (($defaultValue = $this->getDefaultValue()) === null) { return 'NULL'; } if ($defaultValue instanceof SQLFragmentInterface) { return $defaultValue->sqlStatement(); } if ($this->phpType() == 'bool') { return $defaultValue ? 'TRUE' : 'FALSE'; } if ($this->phpType() == 'float') { return sprintf('%F', $defaultValue); } if ($this->phpType() == 'int') { return $defaultValue; } return $this->table->driver()->getPDO()->quote($defaultValue); }