Skip to content

Commit

Permalink
Oracle additions
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphschindler committed Sep 25, 2012
1 parent d9be786 commit 9d4b553
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 11 deletions.
1 change: 0 additions & 1 deletion Zend_Db-2.0.0dev.phar

This file was deleted.

Binary file removed Zend_Db-2.0.0dev20120403.phar
Binary file not shown.
2 changes: 1 addition & 1 deletion example-01.php 100644 → 100755
Expand Up @@ -16,7 +16,7 @@
// create completely portable SQL by hand
$sql = 'SELECT * FROM '
. $adapter->platform->quoteIdentifier('artist')
. ' WHERE id = ' . $adapter->driver->formatParameterName('id');
. ' WHERE ' . $adapter->platform->quoteIdentifier('id') . ' = ' . $adapter->driver->formatParameterName('id');

/* @var $statement \Zend\Db\Adapter\Driver\StatementInterface */
$statement = $adapter->query($sql);
Expand Down
2 changes: 1 addition & 1 deletion example-02.php 100644 → 100755
Expand Up @@ -33,7 +33,7 @@
/* @var $statement Zend\Db\Adapter\Driver\StatementInterface */
$statement = $adapter->query('SELECT * FROM '
. $qi('artist')
. ' WHERE id = ' . $fp('id'));
. ' WHERE ' . $qi('id') . ' = ' . $fp('id'));

/* @var $results Zend\Db\ResultSet\ResultSet */
$results = $statement->execute(array('id' => $id));
Expand Down
30 changes: 22 additions & 8 deletions includes/functions.php 100644 → 100755
Expand Up @@ -2,7 +2,7 @@

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

Expand All @@ -21,11 +21,12 @@ function assert_example_works($expression, $continue_if_true = false) {
}

/**
* @param Zend\Db\Adapter $adapter
* @param Zend\Db\Adapter\Adapter $adapter
*/
function refresh_data($adapter) {
$platform = $adapter->getPlatform()->getName();
$vendorData = include __DIR__ . '/../setup/vendor/' . strtolower($platform) . '.php';
$platform = $adapter->getPlatform();
$platformName = $platform->getName();
$vendorData = include __DIR__ . '/../setup/vendor/' . strtolower($platformName) . '.php';

try {
foreach ($vendorData['data_down'] as $downSql) {
Expand Down Expand Up @@ -53,20 +54,33 @@ function refresh_data($adapter) {

$keys = array_keys($rowData);
$values = array_values($rowData);
array_walk(
$keys,
function (&$key) use ($platform) {
$key = $platform->quoteIdentifier($key);
}
);
array_walk(
$values,
function (&$value) {
$value = ($value == null) ? 'NULL' : '\'' . $value . '\'';
function (&$value) use ($platform) {
switch (gettype($value)) {
case 'NULL':
$value = 'NULL';
break;
case 'string':
$value = $platform->quoteValue($value);
break;
}
}
);

$insertSql = 'INSERT INTO ' . $tableName
$insertSql = 'INSERT INTO ' . $platform->quoteIdentifier($tableName)
. ' (' . implode(', ', $keys) . ') '
. ' VALUES (' .
implode(', ',
$values
)
. ');';
. ')';

$adapter->query(
$insertSql,
Expand Down
Empty file modified setup/up.php 100644 → 100755
Empty file.
100 changes: 100 additions & 0 deletions setup/vendor/oracle.php
@@ -0,0 +1,100 @@
<?php

$common = include 'common.php';

$data = array_merge($common, array(
'schema_up' => array(
'CREATE TABLE "artist" (
"id" NUMBER PRIMARY KEY,
"name" VARCHAR(255) NOT NULL,
"history" CLOB
)',
'CREATE TABLE "album" (
"id" NUMBER PRIMARY KEY,
"artist_id" NUMBER NOT NULL,
"title" VARCHAR(255) NOT NULL,
"release_date" DATE NOT NULL,
CONSTRAINT "album_artist_fk" FOREIGN KEY ("artist_id") REFERENCES "artist" ("id")
)',
'CREATE TABLE "genre" (
"id" NUMBER PRIMARY KEY,
"parent_id" NUMBER DEFAULT NULL,
"name" VARCHAR(255) NOT NULL,
UNIQUE ("name"),
CONSTRAINT "genre_genre_fk" FOREIGN KEY ("parent_id") REFERENCES "genre" ("id")
)',
'CREATE TABLE "artist_genre" (
"artist_id" NUMBER NOT NULL,
"genre_id" NUMBER NOT NULL,
"added_on" DATE NOT NULL,
PRIMARY KEY ("artist_id","genre_id"),
CONSTRAINT "artist_genre_artist_fk" FOREIGN KEY ("artist_id") REFERENCES "artist" ("id"),
CONSTRAINT "artist_genre_genre_fk" FOREIGN KEY ("genre_id") REFERENCES "genre" ("id")
)',
'CREATE TABLE "track" (
"id" NUMBER PRIMARY KEY,
"artist_id" NUMBER DEFAULT NULL,
"album_id" NUMBER DEFAULT NULL,
"number" NUMBER DEFAULT NULL,
"title" VARCHAR(255) NOT NULL,
"length" NUMBER NOT NULL,
CONSTRAINT "track_artist_fk" FOREIGN KEY ("artist_id") REFERENCES "artist" ("id"),
CONSTRAINT "track_album_fk" FOREIGN KEY ("album_id") REFERENCES "album" ("id")
)',
'CREATE SEQUENCE "artist_id_seq" START WITH 1 INCREMENT BY 1 NOMAXVALUE',
'CREATE TRIGGER "artist_autoincrement_trig"' . "\n" . 'BEFORE INSERT ON "artist"' . "\n" . 'FOR EACH ROW WHEN (NEW."id" IS NULL)' . "\n" . 'BEGIN SELECT "artist_id_seq".NEXTVAL INTO :NEW."id" FROM DUAL;' . "\n" . 'END;',
'CREATE SEQUENCE "album_id_seq" START WITH 1 INCREMENT BY 1 NOMAXVALUE',
'CREATE TRIGGER "album_autoincrement_trig"' . "\n" . 'BEFORE INSERT ON "album"' . "\n" . 'FOR EACH ROW WHEN (NEW."id" IS NULL)' . "\n" . 'BEGIN SELECT "album_id_seq".NEXTVAL INTO :NEW."id" FROM DUAL;' . "\n" . 'END;',
'CREATE SEQUENCE "genre_id_seq" START WITH 1 INCREMENT BY 1 NOMAXVALUE',
'CREATE TRIGGER "genre_autoincrement_trig"' . "\n" . 'BEFORE INSERT ON "genre"' . "\n" . 'FOR EACH ROW WHEN (NEW."id" IS NULL)' . "\n" . 'BEGIN SELECT "genre_id_seq".NEXTVAL INTO :NEW."id" FROM DUAL;' . "\n" . 'END;',
'CREATE SEQUENCE "track_id_seq" START WITH 1 INCREMENT BY 1 NOMAXVALUE',
'CREATE TRIGGER "track_autoincrement_trig"' . "\n" . 'BEFORE INSERT ON "track"' . "\n" . 'FOR EACH ROW WHEN (NEW."id" IS NULL)' . "\n" . 'BEGIN SELECT "track_id_seq".NEXTVAL INTO :NEW."id" FROM DUAL;' . "\n" . 'END;',
),
'schema_down' => array(
'BEGIN EXECUTE IMMEDIATE \'DROP TABLE "track"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP TABLE "artist_genre"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP TABLE "genre"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP TABLE "album"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP TABLE "artist"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP SEQUENCE "artist_id_seq"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -2289 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP TRIGGER "artist_autoincrement_trig"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -4080 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP SEQUENCE "album_id_seq"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -2289 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP TRIGGER "album_autoincrement_trig"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -4080 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP SEQUENCE "genre_id_seq"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -2289 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP TRIGGER "genre_autoincrement_trig"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -4080 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP SEQUENCE "track_id_seq"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -2289 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DROP TRIGGER "track_autoincrement_trig"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -4080 THEN RAISE; END IF; END;',
),
'data_down' => array(
'BEGIN EXECUTE IMMEDIATE \'DELETE FROM "track"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DELETE FROM "artist_genre"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DELETE FROM "genre"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DELETE FROM "album"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END;',
'BEGIN EXECUTE IMMEDIATE \'DELETE FROM "artist"\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END;',
),

));

foreach ($data['data_up'] as $tName => $tData) {
foreach ($tData as $tRowIndex => $tRow) {
foreach ($tRow as $cName => $cValue) {
if (strpos($cName, '_date') !== false || strpos($cName, '_on') !== false) {
$data['data_up'][$tName][$tRowIndex][$cName] = date('d-M-y', strtotime($cValue));
}
}
}
}

//$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;

0 comments on commit 9d4b553

Please sign in to comment.