Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added metadata for oracle #5032

Closed
wants to merge 1 commit into from

5 participants

@tux-rampage

This commit adds a metadata source for Oracle as requested in #3956

This is missing the code for resolving triggers at the moment, but it's better than having no metadata source at all.
I've not been able to implement a Test case, yet.

It would be great if someone could tackle the missing items due to the lack of time from my side.

@samsonasik samsonasik commented on the diff
library/Zend/Db/Metadata/Source/OracleMetadata.php
((69 lines not shown))
+ 'character_octet_length' => null,
+ 'numeric_precision' => $row['DATA_PRECISION'],
+ 'numeric_scale' => $row['DATA_SCALE'],
+ 'numeric_unsigned' => false,
+ 'erratas' => array(),
+ );
+ }
+
+ $this->data['columns'][$schema][$table] = $columns;
+ return $this;
+ }
+
+ /**
+ * Constraint type
+ *
+ * @param string $type

double space after @param to consistency with #2419 and #2422

Sorry, but this isn't listed in the coding standards: http://framework.zend.com/wiki/display/ZFDEV2/Coding+Standards
Or did I miss it?

[edit: corrected url]

yes, but it just for consistency with >6000 files other.

I'm sorry, but there are more important things than an additional whitespace after an annotation tag I guess. The time of the people that are comitting is valuable and most of them don't get paid for their effort. I'd rather like to see their resources spend in improving the frameworks code instead of changing unimportant stuff which is not even worth to be mentioned in the coding standards ...
No offense, but this looks a bit pedantic to me.

@Ocramius Collaborator
Ocramius added a note

@tux-rampage we're all on the same boat. Making the framework better and trying to respect any possible aesthetic betterment is perfectly ok.

@samsonasik is ALSO putting his own free unpaid time into this. You are free to skip the CS comments if you think so, but please don't tell reviewers to not review, since they also put a lot of effort in it ;)

@samsonasik
Sorry, but I have to agree with @tux-rampage. This is not defined in the coding standards.
And if I look at the Zend\Db\Sql\Ddl classes (only one example), it is not fair to make these hard requirements.

Please do not get me wrong, I want the same: a better framework and a good documentation (incl. DocBlocks). But the additional whitespace is not in the coding standards.

@Ocramius I don't complain about the review, which is highly encouraged and thanks for doing so. But complaining about a missing white space, which is not even required by CS, is imho a bit too much.

@tux-rampage @froschdesign it's up to you to agree with me or not. I just make a suggestion for consistencies. It not be a problem for me if it merged without my change suggestion :).

@samsonasik Thanks for clarification. It sounded a lot like a must have requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tux-rampage

@ralphschindler

Could we merge this into the development tree?
What test cases should a Unit test for this metadata source cover? Imho this would be more an integration instead of an unit test.

@ralphschindler
Collaborator

Yep, I've been working through the DB pull requests, and this one is on my list. I should be able to get to the Oracle specific ones today.

@tux-rampage

cool, thanks. :+1:

@ralphschindler ralphschindler referenced this pull request from a commit
@ralphschindler ralphschindler Merging #5032
Merge branch 'zf3956' of git://github.com/tux-rampage/zf2 into tux-rampage-zf3956

* 'zf3956' of git://github.com/tux-rampage/zf2:
  Added metadata for oracle See issue #3956
74fdaf3
@ralphschindler ralphschindler referenced this pull request from a commit
@ralphschindler ralphschindler Merging #5032
Merge branch 'tux-rampage-zf3956' into develop

* tux-rampage-zf3956:
  Added metadata for oracle See issue #3956
b52bef4
@ralphschindler ralphschindler self-assigned this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 27, 2013
  1. @tux-rampage

    Added metadata for oracle

    tux-rampage authored
    See issue #3956
This page is out of date. Refresh to see the latest.
View
2  library/Zend/Db/Metadata/Metadata.php
@@ -53,6 +53,8 @@ protected function createSourceFromAdapter(Adapter $adapter)
return new Source\SqliteMetadata($adapter);
case 'PostgreSQL':
return new Source\PostgresqlMetadata($adapter);
+ case 'Oracle':
+ return new Source\OracleMetadata($adapter);
}
throw new \Exception('cannot create source from adapter');
View
256 library/Zend/Db/Metadata/Source/OracleMetadata.php
@@ -0,0 +1,256 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace Zend\Db\Metadata\Source;
+
+use Zend\Db\Adapter\Adapter;
+
+/**
+ * Metadata source for Oracle
+ */
+class OracleMetadata extends AbstractSource
+{
+ /**
+ * @var array
+ */
+ protected $constraintTypeMap = array(
+ 'C' => 'CHECK',
+ 'P' => 'PRIMARY KEY',
+ 'R' => 'FOREIGN_KEY'
+ );
+
+ /**
+ * {@inheritdoc}
+ * @see \Zend\Db\Metadata\Source\AbstractSource::loadColumnData()
+ */
+ protected function loadColumnData($table, $schema)
+ {
+ if (isset($this->data['columns'][$schema][$table])) {
+ return;
+ }
+
+ $isColumns = array(
+ 'COLUMN_ID',
+ 'COLUMN_NAME',
+ 'DATA_DEFAULT',
+ 'NULLABLE',
+ 'DATA_TYPE',
+ 'DATA_LENGTH',
+ 'DATA_PRECISION',
+ 'DATA_SCALE'
+ );
+
+ $this->prepareDataHierarchy('columns', $schema, $table);
+ $parameters = array(
+ ':ownername' => $schema,
+ ':tablename' => $table
+ );
+
+ $sql = 'SELECT ' . implode(', ', $isColumns)
+ . ' FROM all_tab_columns'
+ . ' WHERE owner = :ownername AND table_name = :tablename';
+
+ $result = $this->adapter->query($sql)->execute($parameters);
+ $columns = array();
+
+ foreach ($result as $row) {
+ $columns[$row['COLUMN_NAME']] = array(
+ 'ordinal_position' => $row['COLUMN_ID'],
+ 'column_default' => $row['DATA_DEFAULT'],
+ 'is_nullable' => ('Y' == $row['NULLABLE']),
+ 'data_type' => $row['DATA_TYPE'],
+ 'character_maximum_length' => $row['DATA_LENGTH'],
+ 'character_octet_length' => null,
+ 'numeric_precision' => $row['DATA_PRECISION'],
+ 'numeric_scale' => $row['DATA_SCALE'],
+ 'numeric_unsigned' => false,
+ 'erratas' => array(),
+ );
+ }
+
+ $this->data['columns'][$schema][$table] = $columns;
+ return $this;
+ }
+
+ /**
+ * Constraint type
+ *
+ * @param string $type

double space after @param to consistency with #2419 and #2422

Sorry, but this isn't listed in the coding standards: http://framework.zend.com/wiki/display/ZFDEV2/Coding+Standards
Or did I miss it?

[edit: corrected url]

yes, but it just for consistency with >6000 files other.

I'm sorry, but there are more important things than an additional whitespace after an annotation tag I guess. The time of the people that are comitting is valuable and most of them don't get paid for their effort. I'd rather like to see their resources spend in improving the frameworks code instead of changing unimportant stuff which is not even worth to be mentioned in the coding standards ...
No offense, but this looks a bit pedantic to me.

@Ocramius Collaborator
Ocramius added a note

@tux-rampage we're all on the same boat. Making the framework better and trying to respect any possible aesthetic betterment is perfectly ok.

@samsonasik is ALSO putting his own free unpaid time into this. You are free to skip the CS comments if you think so, but please don't tell reviewers to not review, since they also put a lot of effort in it ;)

@samsonasik
Sorry, but I have to agree with @tux-rampage. This is not defined in the coding standards.
And if I look at the Zend\Db\Sql\Ddl classes (only one example), it is not fair to make these hard requirements.

Please do not get me wrong, I want the same: a better framework and a good documentation (incl. DocBlocks). But the additional whitespace is not in the coding standards.

@Ocramius I don't complain about the review, which is highly encouraged and thanks for doing so. But complaining about a missing white space, which is not even required by CS, is imho a bit too much.

@tux-rampage @froschdesign it's up to you to agree with me or not. I just make a suggestion for consistencies. It not be a problem for me if it merged without my change suggestion :).

@samsonasik Thanks for clarification. It sounded a lot like a must have requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * @return string
+ */
+ protected function getConstraintType($type)
+ {
+ if (isset($this->constraintTypeMap[$type])) {
+ return $this->constraintTypeMap[$type];
+ }
+
+ return $type;
+ }
+
+ /**
+ * {@inheritdoc}
+ * @see \Zend\Db\Metadata\Source\AbstractSource::loadConstraintData()
+ */
+ protected function loadConstraintData($table, $schema)
+ {
+ if (isset($this->data['constraints'][$schema][$table])) {
+ return;
+ }
+
+ $this->prepareDataHierarchy('constraints', $schema, $table);
+ $sql = '
+ SELECT
+ ac.owner,
+ ac.constraint_name,
+ ac.constraint_type,
+ ac.search_condition check_clause,
+ ac.table_name,
+ ac.delete_rule,
+ cc1.column_name,
+ cc2.table_name as ref_table,
+ cc2.column_name as ref_column,
+ cc2.owner as ref_owner
+ FROM all_constraints ac
+ INNER JOIN all_cons_columns cc1
+ ON cc1.constraint_name = ac.constraint_name
+ LEFT JOIN all_cons_columns cc2
+ ON cc2.constraint_name = ac.r_constraint_name
+ AND cc2.position = cc1.position
+
+ WHERE
+ ac.owner = :schema AND ac.table_name = :table
+
+ ORDER BY ac.constraint_name;
+ ';
+
+ $parameters = array(
+ ':schema' => $schema,
+ ':table' => $table
+ );
+
+ $results = $this->adapter->query($sql)->execute($parameters);
+ $isFK = false;
+ $name = null;
+ $constraints = array();
+
+ foreach ($results as $row) {
+ if ($row['CONSTRAINT_NAME'] !== $name) {
+ $name = $row['CONSTRAINT_NAME'];
+ $constraints[$name] = array(
+ 'constraint_name' => $name,
+ 'constraint_type' => $this->getConstraintType($row['CONSTRAINT_TYPE']),
+ 'table_name' => $row['TABLE_NAME'],
+ );
+
+ if ('C' == $row['CONSTRAINT_TYPE']) {
+ $constraints[$name]['CHECK_CLAUSE'] = $row['CHECK_CLAUSE'];
+ continue;
+ }
+
+ $constraints[$name]['columns'] = array();
+
+ $isFK = ('R' == $row['CONSTRAINT_TYPE']);
+ if ($isFK) {
+ $constraints[$name]['referenced_table_schema'] = $row['REF_OWNER'];
+ $constraints[$name]['referenced_table_name'] = $row['REF_TABLE'];
+ $constraints[$name]['referenced_columns'] = array();
+ $constraints[$name]['match_option'] = 'NONE';
+ $constraints[$name]['update_rule'] = null;
+ $constraints[$name]['delete_rule'] = $row['DELETE_RULE'];
+ }
+ }
+
+ $constraints[$name]['columns'][] = $row['COLUMN_NAME'];
+ if ($isFK) {
+ $constraints[$name]['referenced_columns'][] = $row['REF_COLUMN'];
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ * @see \Zend\Db\Metadata\Source\AbstractSource::loadSchemaData()
+ */
+ protected function loadSchemaData()
+ {
+ if (isset($this->data['schemas'])) {
+ return;
+ }
+
+ $this->prepareDataHierarchy('schemas');
+ $sql = 'SELECT USERNAME FROM ALL_USERS';
+ $results = $this->adapter->query($sql, Adapter::QUERY_MODE_EXECUTE);
+
+ $schemas = array();
+ foreach ($results->toArray() as $row) {
+ $schemas[] = $row['USERNAME'];
+ }
+
+ $this->data['schemas'] = $schemas;
+ }
+
+ /**
+ * {@inheritdoc}
+ * @see \Zend\Db\Metadata\Source\AbstractSource::loadTableNameData()
+ */
+ protected function loadTableNameData($schema)
+ {
+ if (isset($this->data['table_names'][$schema])) {
+ return $this;
+ }
+
+ $this->prepareDataHierarchy('table_names', $schema);
+ $tables = array();
+
+ // Tables
+ $bind = array(':OWNER' => strtoupper($schema));
+ $result = $this->adapter->query('SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER=:OWNER')->execute($bind);
+
+ foreach ($result as $row) {
+ $tables[$row['TABLE_NAME']] = array(
+ 'table_type' => 'BASE TABLE',
+ 'view_definition' => null,
+ 'check_option' => null,
+ 'is_updatable' => false,
+ );
+ }
+
+ // Views
+ $result = $this->adapter->query('SELECT VIEW_NAME, TEXT FROM ALL_VIEWS WHERE OWNER=:OWNER', $bind);
+ foreach ($result as $row) {
+ $tables[$row['VIEW_NAME']] = array(
+ 'table_type' => 'VIEW',
+ 'view_definition' => null,
+ 'check_option' => 'NONE',
+ 'is_updatable' => false,
+ );
+ }
+
+ $this->data['table_names'][$schema] = $tables;
+ return $this;
+ }
+
+ /**
+ * FIXME: load trigger data
+ *
+ * {@inheritdoc}
+ *
+ * @see \Zend\Db\Metadata\Source\AbstractSource::loadTriggerData()
+ */
+ protected function loadTriggerData($schema)
+ {
+ if (isset($this->data['triggers'][$schema])) {
+ return;
+ }
+
+ $this->prepareDataHierarchy('triggers', $schema);
+ }
+}
Something went wrong with that request. Please try again.