protected function getSerialPrimaryKey() { $pk = $this->definition->getPrimaryKey(); if (count($pk) == 1) { if ($this->definition->getType($pk[0])->serial) { return $pk[0]; } } return null; }
public function __construct(Definition $copy = null) { if (isset($copy)) { if ($copy instanceof self) { $this->fields = $copy->fields; $this->keys = $copy->keys; } else { foreach ($copy->getFields() as $field) { $this->fields[$field] = $copy->getType($field); } foreach ($copy->getKeys() as $key) { $this->keys[$key] = ['columns' => $copy->getKey($key), 'unique' => $copy->isUnique($key)]; } } } }
/** * {@inheritdoc} */ public function createTable($table, \Jivoo\Data\Definition $definition) { $sql = 'CREATE TABLE `' . $this->db->tableName($table) . '` ('; $columns = $definition->getFields(); $first = true; foreach ($columns as $column) { $type = $definition->getType($column); if (!$first) { $sql .= ', '; } else { $first = false; } $sql .= $column; $sql .= ' ' . $this->fromDataType($type); } foreach ($definition->getKeys() as $key) { $columns = $definition->getKey($key); $sql .= ', '; if ($key == 'PRIMARY') { $sql .= 'PRIMARY KEY ('; } elseif ($definition->isUnique($key)) { $sql .= 'UNIQUE ('; } else { $sql .= 'INDEX ('; } $sql .= implode(', ', $columns) . ')'; } $sql .= ') CHARACTER SET utf8'; $this->db->execute($sql); }
/** * {@inheritdoc} */ public function withRecord($field, Definition $definition) { $clone = clone $this; foreach ($definition->getFields() as $schemaField) { $alias = $field . '_' . $schemaField; $clone->additionalFields[$alias] = array('alias' => $alias, 'expression' => $field . '.' . $schemaField, 'type' => $definition->getType($schemaField), 'definition' => $definition, 'record' => $field, 'recordField' => $schemaField); } return $clone; }
/** * {@inheritdoc} */ public function createTable($table, \Jivoo\Data\Definition $definition) { $sql = 'CREATE TABLE "' . $this->db->tableName($table) . '" ('; $columns = $definition->getFields(); $first = true; $primaryKey = $definition->getPrimaryKey(); $singlePrimary = count($primaryKey) == 1; foreach ($columns as $column) { $type = $definition->getType($column); if (!$first) { $sql .= ', '; } else { $first = false; } $sql .= $column; $sql .= ' ' . $this->fromDataType($type, $singlePrimary and $primaryKey[0] == $column); } if (!$singlePrimary) { $sql .= ', PRIMARY KEY (' . implode(', ', $definition->getPrimaryKey()) . ')'; } $sql .= ')'; $this->db->execute($sql); foreach ($definition->getKeys() as $key) { if ($key == 'PRIMARY') { continue; } $sql = 'CREATE'; if ($definition->isUnique($key)) { $sql .= ' UNIQUE'; } $sql .= ' INDEX "'; $sql .= $this->db->tableName($table) . '_' . $key; $sql .= '" ON "' . $this->db->tableName($table); $sql .= '" ('; $sql .= implode(', ', $definition->getKey($key)) . ')'; $this->db->execute($sql); } }
/** * {@inheritdoc} */ public function createTable($table, \Jivoo\Data\Definition $definition) { $sql = 'CREATE TABLE ' . $this->db->quoteModel($table) . ' ('; $columns = $definition->getFields(); $first = true; foreach ($columns as $column) { $type = $definition->getType($column); if (!$first) { $sql .= ', '; } else { $first = false; } $sql .= $this->db->quoteField($column); $sql .= ' ' . $this->fromDataType($type); } $pk = $definition->getPrimaryKey(); if (count($pk) > 0) { $sql .= ', CONSTRAINT "' . $this->db->tableName($table) . '_PRIMARY" PRIMARY KEY ('; $pk = array_map(array($this->db, 'quoteField'), $pk); $sql .= implode(', ', $pk) . ')'; } $sql .= ')'; $this->db->execute($sql); foreach ($definition->getKeys() as $key) { if ($key == 'PRIMARY') { continue; } $sql = 'CREATE'; if ($definition->isUnique($key)) { $sql .= ' UNIQUE'; } $sql .= ' INDEX "' . $this->db->tableName($table) . '_' . $key . '"'; $sql .= ' ON ' . $this->db->quoteModel($table); $columns = array_map(array($this->db, 'quoteField'), $definition->getKey($key)); $sql .= ' (' . implode(', ', $columns) . ')'; $this->db->execute($sql); } }