summaryrefslogtreecommitdiff
path: root/lib/pgsqlschema.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pgsqlschema.php')
-rw-r--r--lib/pgsqlschema.php39
1 files changed, 32 insertions, 7 deletions
diff --git a/lib/pgsqlschema.php b/lib/pgsqlschema.php
index 91bc09667..afb498f4a 100644
--- a/lib/pgsqlschema.php
+++ b/lib/pgsqlschema.php
@@ -1,3 +1,4 @@
+
<?php
/**
* StatusNet, the distributed open-source microblogging tool
@@ -61,7 +62,8 @@ class PgsqlSchema extends Schema
public function getTableDef($name)
{
- $res = $this->conn->query("select *, column_default as default, is_nullable as Null, udt_name as Type, column_name AS Field from INFORMATION_SCHEMA.COLUMNS where table_name = '$name'");
+ $res = $this->conn->query("SELECT *, column_default as default, is_nullable as Null,
+ udt_name as Type, column_name AS Field from INFORMATION_SCHEMA.COLUMNS where table_name = '$name'");
if (PEAR::isError($res)) {
throw new Exception($res->getMessage());
@@ -72,6 +74,9 @@ class PgsqlSchema extends Schema
$td->name = $name;
$td->columns = array();
+ if ($res->numRows() == 0 ) {
+ throw new Exception('no such table'); //pretend to be the msyql error. yeah, this sucks.
+ }
$row = array();
while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
@@ -166,12 +171,10 @@ class PgsqlSchema extends Schema
}
if (count($primary) > 0) { // it really should be...
- $sql .= ",\nconstraint primary key (" . implode(',', $primary) . ")";
+ $sql .= ",\n primary key (" . implode(',', $primary) . ")";
}
- foreach ($uniques as $u) {
- $sql .= ",\nunique index {$name}_{$u}_idx ($u)";
- }
+
foreach ($indices as $i) {
$sql .= ",\nindex {$name}_{$i}_idx ($i)";
@@ -179,6 +182,10 @@ class PgsqlSchema extends Schema
$sql .= "); ";
+
+ foreach ($uniques as $u) {
+ $sql .= "\n CREATE index {$name}_{$u}_idx ON {$name} ($u); ";
+ }
$res = $this->conn->query($sql);
if (PEAR::isError($res)) {
@@ -210,6 +217,22 @@ class PgsqlSchema extends Schema
}
/**
+ * Translate the (mostly) mysql-ish column types into somethings more standard
+ * @param string column type
+ *
+ * @return string postgres happy column type
+ */
+ private function _columnTypeTranslation($type) {
+ $map = array(
+ 'datetime' => 'timestamp'
+ );
+ if(!empty($map[$type])) {
+ return $map[$type];
+ }
+ return $type;
+ }
+
+ /**
* Adds an index to a table.
*
* If no name is provided, a name will be made up based
@@ -359,6 +382,7 @@ class PgsqlSchema extends Schema
try {
$td = $this->getTableDef($tableName);
+
} catch (Exception $e) {
if (preg_match('/no such table/', $e->getMessage())) {
return $this->createTable($tableName, $columns);
@@ -477,11 +501,12 @@ class PgsqlSchema extends Schema
private function _columnSql($cd)
{
$sql = "{$cd->name} ";
+ $type = $this->_columnTypeTranslation($cd->type);
if (!empty($cd->size)) {
- $sql .= "{$cd->type}({$cd->size}) ";
+ $sql .= "{$type}({$cd->size}) ";
} else {
- $sql .= "{$cd->type} ";
+ $sql .= "{$type} ";
}
if (!empty($cd->default)) {