Page MenuHomePhabricator

No OneTemporary

diff --git a/db/drivers/mysqlPDOTable.php b/db/drivers/mysqlPDOTable.php
index 9e29d00..d035cfb 100644
--- a/db/drivers/mysqlPDOTable.php
+++ b/db/drivers/mysqlPDOTable.php
@@ -1,73 +1,66 @@
<?php namespace spitfire\storage\database\drivers;
use Exception;
/**
* Represents table specific properties and methods for the MySQLPDO Driver.
*
* @deprecated since version 0.1-dev 20170807
*/
-class MysqlPDOTable extends stdSQLTable
+class MysqlPDOTable extends sql\SQLTable
{
public function repair() {
$table = $this;
$stt = "DESCRIBE $table";
$fields = $table->getFields();
//Fetch the DB Fields and create on error.
try {
$query = $this->getDb()->execute($stt, Array(), false);
}
catch(Exception $e) {
return $this->create();
}
//Loop through the exiting fields
while (false != ($f = $query->fetch())) {
try {
$field = $this->getField($f['Field']);
unset($fields[$field->getName()]);
}
catch(Exception $e) {/*Ignore*/}
}
foreach($fields as $field) $field->add();
}
public function create() {
$table = $this;
$definitions = $table->columnDefinitions();
- $foreignkeys = $table->foreignKeyDefinitions();
- $pk = $table->getPrimaryKey();
-
- foreach($pk as &$f) { $f = '`' . $f->getName() . '`'; }
-
- if (!empty($foreignkeys)) $definitions = array_merge ($definitions, $foreignkeys);
-
- if (!empty($pk)) $definitions[] = 'PRIMARY KEY(' . implode(', ', $pk) . ')';
+ $indexes = $table->getLayout()->getIndexes();
#Strip empty definitions from the list
- $clean = array_filter($definitions);
+ $clean = array_filter(array_merge($definitions, $indexes->toArray()));
$stt = sprintf('CREATE TABLE %s (%s) ENGINE=InnoDB CHARACTER SET=utf8',
$table,
implode(', ', $clean)
);
return $table->getDb()->execute($stt);
}
public function destroy() {
$this->getDb()->execute('DROP TABLE ' . $this->getTable());
}
/**
* Returns the name of a table as DB Object reference (with quotes).
*
* @return string The name of the table escaped and ready for use inside
* of a query.
*/
public function __toString() {
return strval($this->getLayout());
}
}
\ No newline at end of file
diff --git a/db/drivers/stdSQLTable.php b/db/drivers/stdSQLTable.php
deleted file mode 100644
index 8b1c8a2..0000000
--- a/db/drivers/stdSQLTable.php
+++ /dev/null
@@ -1,69 +0,0 @@
-<?php
-
-namespace spitfire\storage\database\drivers;
-
-use spitfire\storage\database\Table;
-use Reference;
-
-abstract class stdSQLTable extends Table
-{
-
- /**
- * Creates the column definitions for each column
- *
- * @return mixed
- */
- protected function columnDefinitions() {
- $fields = $this->getFields();
- foreach ($fields as $name => $f) {
- $fields[$name] = '`'. $name . '` ' . $f->columnDefinition();
- }
- return $fields;
- }
-
- /**
- * Creates a list of definitions for CONSTRAINTS defined by the references
- * this table's model makes to other models.
- *
- * @return array
- */
- protected function foreignKeyDefinitions() {
-
- $ret = Array();
- $refs = $this->schema->getFields();
-
- foreach ($refs as $name => $ref) {
- if (!$ref instanceof Reference) unset($refs[$name]);
- }
-
- if (empty($refs)) return Array();
-
- foreach ($refs as $ref) {
- //Check the integrity of the remote table
- if ($ref->getTarget() !== $this->schema) {
- $this->getDb()->table($ref->getTarget())->getTable()->repair();
- }
-
- #Get the fields the model references from $ref
- $fields = $ref->getPhysical();
- foreach ($fields as &$field) $field = $field->getName();
- unset($field);
- #Get the table that represents $ref
- $referencedtable = $ref->getTarget()->getTable();
- $primary = $referencedtable->getPrimaryKey();
- foreach ($primary as &$field) $field = $field->getName();
- unset($field);
- //Prepare the statement
- $refstt = sprintf('FOREIGN KEY %s (%s) REFERENCES %s(%s) ON DELETE CASCADE ON UPDATE CASCADE',
- 'fk_' . rand(), #Constraint name. Temporary fix, constraints should have proper names
- implode(', ', $fields),
- $referencedtable,
- implode(', ', $primary)
- );
-
- $ret[] = $refstt;
- }
-
- return $ret;
- }
-}
diff --git a/storage/database/drivers/sql/SQLTable.php b/storage/database/drivers/sql/SQLTable.php
new file mode 100644
index 0000000..d17d29c
--- /dev/null
+++ b/storage/database/drivers/sql/SQLTable.php
@@ -0,0 +1,20 @@
+<?php namespace spitfire\storage\database\drivers\sql;
+
+use spitfire\storage\database\Table;
+
+abstract class SQLTable extends Table
+{
+
+ /**
+ * Creates the column definitions for each column
+ *
+ * @return mixed
+ */
+ protected function columnDefinitions() {
+ $fields = $this->getFields();
+ foreach ($fields as $name => $f) {
+ $fields[$name] = '`'. $name . '` ' . $f->columnDefinition();
+ }
+ return $fields;
+ }
+}

File Metadata

Mime Type
text/x-diff
Expires
Apr 13 2021, 8:30 PM (9 w, 43 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1921
Default Alt Text
(4 KB)

Event Timeline