Skip to content

Commit

Permalink
Added postgres support and changes to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphschindler committed Aug 7, 2012
1 parent d527db8 commit d0b4f89
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 13 deletions.
2 changes: 1 addition & 1 deletion example-01.php
Expand Up @@ -9,7 +9,7 @@
* Sqlite via PDO
*/

/* @var $adapter Zend\Db\Adapter */
/* @var $adapter Zend\Db\Adapter\Adapter */
$adapter = include ((file_exists('bootstrap.php')) ? 'bootstrap.php' : 'bootstrap.dist.php');
refresh_data($adapter);

Expand Down
4 changes: 4 additions & 0 deletions example-07.php
Expand Up @@ -12,6 +12,10 @@

$id = $artistTable->getLastInsertValue();

if ($id == null && $adapter->getPlatform()->getName() == 'PostgreSQL') {
$id = $adapter->getDriver()->getConnection()->getLastGeneratedValue('artist_id_seq');
}

assert_example_works($result === 1, true);

$artistTable = new Zend\Db\TableGateway\TableGateway('artist', $adapter);
Expand Down
20 changes: 12 additions & 8 deletions example-10.php
Expand Up @@ -8,7 +8,6 @@
// get the table names
$tableNames = $metadata->getTableNames();


foreach ($tableNames as $tableName) {
echo 'In Table ' . $tableName . PHP_EOL;

Expand All @@ -24,19 +23,24 @@

echo PHP_EOL;
echo ' With constraints: ' . PHP_EOL;

foreach ($metadata->getConstraints($tableName) as $constraint) {
/** @var $constraint Zend\Db\Metadata\Object\ConstraintObject */
echo ' ' . $constraint->getName()
. ' -> ' . $constraint->getType()
. PHP_EOL;
foreach ($constraint->getKeys() as $key) {
echo ' column: ' . $constraint->getTableName() . '.'
. $key->getColumnName();
if ($constraint->isForeignKey()) {
echo ' => ' . $key->getReferencedTableName()
. '.' . $key->getReferencedColumnName();
if (!$constraint->hasColumns()) {
continue;
}
echo ' column: ' . implode(', ', $constraint->getColumns());
if ($constraint->isForeignKey()) {
$fkCols = array();
foreach ($constraint->getReferencedColumns() as $refColumn) {
$fkCols[] = $constraint->getReferencedTableName() . '.' . $refColumn;
}
echo PHP_EOL;
echo ' => ' . implode(', ', $fkCols);
}
echo PHP_EOL;

}

Expand Down
2 changes: 1 addition & 1 deletion example-12.php
Expand Up @@ -16,7 +16,7 @@
$rowset = $artistTable->select(array('id' => 2));

// make sure all rows come back and RowGateway
$rowset->setRowObjectPrototype(new Zend\Db\RowGateway\RowGateway('id', 'artist', $adapter));
$rowset->setArrayObjectPrototype(new Zend\Db\RowGateway\RowGateway('id', 'artist', $adapter));

$row = $rowset->current();

Expand Down
2 changes: 1 addition & 1 deletion example-15.php
Expand Up @@ -18,7 +18,7 @@
$select->prepareStatement($adapter, $statement);

$resultSet = new ResultSet();
$resultSet->setDataSource($statement->execute());
$resultSet->initialize($statement->execute());

$albums = array();
foreach ($resultSet as $row) {
Expand Down
4 changes: 2 additions & 2 deletions example-16.php
Expand Up @@ -12,7 +12,7 @@
$select->from('artist')
->columns(array()) // no columns from main table
->join('album', 'artist.id = album.artist_id', array('title', 'release_date'))
->order(array('release_date', 'title'))
//->order(array('release_date', 'title'))
->limit(2)->offset(0)
->where->like('artist.name', '%Brit%');

Expand All @@ -27,7 +27,7 @@
foreach (array(0, 2, 4) as $offset) {

$container->offsetSet('offset', $offset);
$resultSet->setDataSource($statement->execute());
$resultSet->initialize($statement->execute());

$output = '';
foreach ($resultSet->toArray() as $row) {
Expand Down
12 changes: 12 additions & 0 deletions example-18.php
@@ -0,0 +1,12 @@
<?php

/** @var $adapter Zend\Db\Adapter\Adapter */
$adapter = include ((file_exists('bootstrap.php')) ? 'bootstrap.php' : 'bootstrap.dist.php');
refresh_data($adapter);

use Zend\Db\TableGateway\TableGateway,
Zend\Db\TableGateway\Feature\FeatureSet,
Zend\Db\TableGateway\Feature\MetadataFeature;

$artistTable = new TableGateway('artist', $adapter, new MetadataFeature);
var_dump($artistTable->getColumns());
5 changes: 5 additions & 0 deletions includes/functions.php
@@ -1,5 +1,10 @@
<?php

function example_autoloader($class) {
if (strpos($class, 'Zend\\') === 0) {
include (defined('ZF2_PATH') ? ZF2_PATH : __DIR__ . '/../../') . 'library/' . str_replace('\\', '/', $class) . '.php';
}
}

function assert_example_works($expression, $continue_if_true = false) {
if ($expression) {
Expand Down
74 changes: 74 additions & 0 deletions setup/vendor/postgresql.php
@@ -0,0 +1,74 @@
<?php

$common = include 'common.php';

$data = array_merge($common, array(
'schema_up' => array(
'CREATE TABLE IF NOT EXISTS "artist" (
"id" SERIAL PRIMARY KEY,
"name" VARCHAR(255) NOT NULL,
"history" text
);',
'CREATE TABLE IF NOT EXISTS "album" (
"id" SERIAL PRIMARY KEY,
"artist_id" INTEGER NOT NULL,
"title" VARCHAR(255) NOT NULL,
"release_date" DATE NOT NULL,
FOREIGN KEY ("artist_id") REFERENCES "artist"("id") ON DELETE CASCADE ON UPDATE CASCADE
);',
'CREATE TABLE IF NOT EXISTS "genre" (
"id" SERIAL PRIMARY KEY,
"parent_id" INTEGER DEFAULT NULL,
"name" VARCHAR(255) NOT NULL,
UNIQUE ("name"),
FOREIGN KEY ("parent_id") REFERENCES "genre" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION
);',
'CREATE TABLE IF NOT EXISTS "artist_genre" (
"artist_id" INTEGER NOT NULL,
"genre_id" INTEGER NOT NULL,
"added_on" DATE NOT NULL,
PRIMARY KEY ("artist_id","genre_id"),
FOREIGN KEY ("artist_id") REFERENCES "artist" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY ("genre_id") REFERENCES "genre" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);',
'CREATE TABLE IF NOT EXISTS "track" (
"id" SERIAL PRIMARY KEY,
"artist_id" INTEGER DEFAULT NULL,
"album_id" INTEGER DEFAULT NULL,
"number" INTEGER DEFAULT NULL,
"title" VARCHAR(255) NOT NULL,
"length" INTEGER NOT NULL,
FOREIGN KEY ("artist_id") REFERENCES "artist" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY ("album_id") REFERENCES "album" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);'
),
'schema_down' => array(
'DROP TABLE IF EXISTS "track"',
'DROP TABLE IF EXISTS "artist_genre"',
'DROP TABLE IF EXISTS "genre"',
'DROP TABLE IF EXISTS "album"',
'DROP TABLE IF EXISTS "artist"'
),
'data_down' => array(
'DELETE FROM "track"',
'DELETE FROM "artist_genre"',
'DELETE FROM "genre"',
'DELETE FROM "album"',
'DELETE FROM "artist"',
),

));

$data['data_up'] = array(
'artist' => $data['data_up']['artist'],
"SELECT setval('artist_id_seq', 6)",
'album' => $data['data_up']['album'],
"SELECT setval('album_id_seq', 25)",
'genre' => $data['data_up']['genre'],
"SELECT setval('genre_id_seq', 4)",
'artist_genre' => $data['data_up']['artist_genre'],
'track' => $data['data_up']['track'],
"SELECT setval('track_id_seq', 1)",
);

return $data;
20 changes: 20 additions & 0 deletions whiteboard.php
@@ -0,0 +1,20 @@
<?php

/** @var $adapter Zend\Db\Adapter\Adapter */
$adapter = include ((file_exists('bootstrap.php')) ? 'bootstrap.php' : 'bootstrap.dist.php');
refresh_data($adapter);


$sql = new Zend\Db\Sql\Sql($adapter);
$select = $sql->select('album');

$select->columns(array(
'aid' => new Zend\Db\Sql\Expression('DISTINCT artist_id') //, 'artist_id', array(Zend\Db\Sql\Expression::TYPE_IDENTIFIER))
));

$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute();
foreach ($result as $row) {
var_dump($row);
}

0 comments on commit d0b4f89

Please sign in to comment.