Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RedshiftAdapter #496

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ protected function __construct($info)
}
else
$host = "unix_socket=$info->host";
// PDO driver redshift == pgsql
if ($info->protocol == 'redshift') {
$info->protocol = 'pgsql';
}

$this->connection = new PDO("$info->protocol:$host;dbname=$info->db", $info->user, $info->pass, static::$PDO_OPTIONS);
} catch (PDOException $e) {
Expand Down
6 changes: 3 additions & 3 deletions lib/adapters/PgsqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* Adapter for Postgres (not completed yet)
*
*
* @package ActiveRecord
*/
class PgsqlAdapter extends Connection
Expand Down Expand Up @@ -47,7 +47,7 @@ public function query_column_info($table)
WHERE c.oid = pg_index.indrelid
AND a.attnum = ANY (pg_index.indkey)
AND pg_index.indisprimary = 't'
) IS NOT NULL AS pk,
) IS NOT NULL AS pk,
REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE((SELECT pg_attrdef.adsrc
FROM pg_attrdef
WHERE c.oid = pg_attrdef.adrelid
Expand Down Expand Up @@ -75,7 +75,7 @@ public function create_column(&$column)
$c->inflected_name = Inflector::instance()->variablize($column['field']);
$c->name = $column['field'];
$c->nullable = ($column['not_nullable'] ? false : true);
$c->pk = ($column['pk'] ? true : false);
$c->pk = ($column['pk'] == 't');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about this change? I don't know postgres, so I can't be the judge of this..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$c->auto_increment = false;

if (substr($column['type'],0,9) == 'timestamp')
Expand Down
56 changes: 56 additions & 0 deletions lib/adapters/RedshiftAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* @package ActiveRecord
*/
namespace ActiveRecord;

require_once __DIR__ . '/PgsqlAdapter.php';

/**
* Adapter for Redshift postgres version < 8.0.2 (not completed yet)
*
* @package ActiveRecord
*/
class RedshiftAdapter extends PgsqlAdapter
{
public function supports_sequences()
{
return false;
}

public function query_column_info($table)
{
$sql = <<<SQL
SELECT
a.attname AS field,
a.attlen,
REPLACE(pg_catalog.format_type(a.atttypid, a.atttypmod), 'character varying', 'varchar') AS type,
a.attnotnull AS not_nullable,
(SELECT 't'
FROM pg_index
WHERE c.oid = pg_index.indrelid
AND a.attnum = ANY (
string_to_array(
textin(
int2vectorout(pg_index.indkey)
), ''
)
)
AND pg_index.indisprimary = 't'
) IS NOT NULL AS pk,
REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE((SELECT pg_attrdef.adsrc
FROM pg_attrdef
WHERE c.oid = pg_attrdef.adrelid
AND pg_attrdef.adnum=a.attnum
),'::[a-z_ ]+',''),'''$',''),'^''','') AS default
FROM pg_attribute a, pg_class c, pg_type t
WHERE c.relname = ?
AND a.attnum > 0
AND a.attrelid = c.oid
AND a.atttypid = t.oid
ORDER BY a.attnum
SQL;
$values = array($table);
return $this->query($sql,$values);
}
}