"Fossies" - the Fresh Open Source Software Archive

Member "serendipity/bundled-libs/zendframework/zend-db/src/Metadata/Source/AbstractSource.php" (16 Oct 2019, 15812 Bytes) of package /linux/www/serendipity-2.3.2.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractSource.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * Zend Framework (http://framework.zend.com/)
    4  *
    5  * @link      http://github.com/zendframework/zf2 for the canonical source repository
    6  * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
    7  * @license   http://framework.zend.com/license/new-bsd New BSD License
    8  */
    9 
   10 namespace Zend\Db\Metadata\Source;
   11 
   12 use Zend\Db\Adapter\Adapter;
   13 use Zend\Db\Metadata\MetadataInterface;
   14 use Zend\Db\Metadata\Object\TableObject;
   15 use Zend\Db\Metadata\Object\ViewObject;
   16 use Zend\Db\Metadata\Object\ColumnObject;
   17 use Zend\Db\Metadata\Object\ConstraintObject;
   18 use Zend\Db\Metadata\Object\ConstraintKeyObject;
   19 use Zend\Db\Metadata\Object\TriggerObject;
   20 
   21 abstract class AbstractSource implements MetadataInterface
   22 {
   23     const DEFAULT_SCHEMA = '__DEFAULT_SCHEMA__';
   24 
   25     /**
   26      *
   27      * @var Adapter
   28      */
   29     protected $adapter = null;
   30 
   31     /**
   32      *
   33      * @var string
   34      */
   35     protected $defaultSchema = null;
   36 
   37     /**
   38      *
   39      * @var array
   40      */
   41     protected $data = [];
   42 
   43     /**
   44      * Constructor
   45      *
   46      * @param Adapter $adapter
   47      */
   48     public function __construct(Adapter $adapter)
   49     {
   50         $this->adapter = $adapter;
   51         $this->defaultSchema = ($adapter->getCurrentSchema()) ?: self::DEFAULT_SCHEMA;
   52     }
   53 
   54     /**
   55      * Get schemas
   56      *
   57      */
   58     public function getSchemas()
   59     {
   60         $this->loadSchemaData();
   61 
   62         return $this->data['schemas'];
   63     }
   64 
   65     /**
   66      * {@inheritdoc}
   67      */
   68     public function getTableNames($schema = null, $includeViews = false)
   69     {
   70         if ($schema === null) {
   71             $schema = $this->defaultSchema;
   72         }
   73 
   74         $this->loadTableNameData($schema);
   75 
   76         if ($includeViews) {
   77             return array_keys($this->data['table_names'][$schema]);
   78         }
   79 
   80         $tableNames = [];
   81         foreach ($this->data['table_names'][$schema] as $tableName => $data) {
   82             if ('BASE TABLE' == $data['table_type']) {
   83                 $tableNames[] = $tableName;
   84             }
   85         }
   86         return $tableNames;
   87     }
   88 
   89     /**
   90      * {@inheritdoc}
   91      */
   92     public function getTables($schema = null, $includeViews = false)
   93     {
   94         if ($schema === null) {
   95             $schema = $this->defaultSchema;
   96         }
   97 
   98         $tables = [];
   99         foreach ($this->getTableNames($schema, $includeViews) as $tableName) {
  100             $tables[] = $this->getTable($tableName, $schema);
  101         }
  102         return $tables;
  103     }
  104 
  105     /**
  106      * {@inheritdoc}
  107      */
  108     public function getTable($tableName, $schema = null)
  109     {
  110         if ($schema === null) {
  111             $schema = $this->defaultSchema;
  112         }
  113 
  114         $this->loadTableNameData($schema);
  115 
  116         if (! isset($this->data['table_names'][$schema][$tableName])) {
  117             throw new \Exception('Table "' . $tableName . '" does not exist');
  118         }
  119 
  120         $data = $this->data['table_names'][$schema][$tableName];
  121         switch ($data['table_type']) {
  122             case 'BASE TABLE':
  123                 $table = new TableObject($tableName);
  124                 break;
  125             case 'VIEW':
  126                 $table = new ViewObject($tableName);
  127                 $table->setViewDefinition($data['view_definition']);
  128                 $table->setCheckOption($data['check_option']);
  129                 $table->setIsUpdatable($data['is_updatable']);
  130                 break;
  131             default:
  132                 throw new \Exception(
  133                     'Table "' . $tableName . '" is of an unsupported type "' . $data['table_type'] . '"'
  134                 );
  135         }
  136         $table->setColumns($this->getColumns($tableName, $schema));
  137         $table->setConstraints($this->getConstraints($tableName, $schema));
  138         return $table;
  139     }
  140 
  141     /**
  142      * {@inheritdoc}
  143      */
  144     public function getViewNames($schema = null)
  145     {
  146         if ($schema === null) {
  147             $schema = $this->defaultSchema;
  148         }
  149 
  150         $this->loadTableNameData($schema);
  151 
  152         $viewNames = [];
  153         foreach ($this->data['table_names'][$schema] as $tableName => $data) {
  154             if ('VIEW' == $data['table_type']) {
  155                 $viewNames[] = $tableName;
  156             }
  157         }
  158         return $viewNames;
  159     }
  160 
  161     /**
  162      * {@inheritdoc}
  163      */
  164     public function getViews($schema = null)
  165     {
  166         if ($schema === null) {
  167             $schema = $this->defaultSchema;
  168         }
  169 
  170         $views = [];
  171         foreach ($this->getViewNames($schema) as $tableName) {
  172             $views[] = $this->getTable($tableName, $schema);
  173         }
  174         return $views;
  175     }
  176 
  177     /**
  178      * {@inheritdoc}
  179      */
  180     public function getView($viewName, $schema = null)
  181     {
  182         if ($schema === null) {
  183             $schema = $this->defaultSchema;
  184         }
  185 
  186         $this->loadTableNameData($schema);
  187 
  188         $tableNames = $this->data['table_names'][$schema];
  189         if (isset($tableNames[$viewName]) && 'VIEW' == $tableNames[$viewName]['table_type']) {
  190             return $this->getTable($viewName, $schema);
  191         }
  192         throw new \Exception('View "' . $viewName . '" does not exist');
  193     }
  194 
  195     /**
  196      * {@inheritdoc}
  197      */
  198     public function getColumnNames($table, $schema = null)
  199     {
  200         if ($schema === null) {
  201             $schema = $this->defaultSchema;
  202         }
  203 
  204         $this->loadColumnData($table, $schema);
  205 
  206         if (! isset($this->data['columns'][$schema][$table])) {
  207             throw new \Exception('"' . $table . '" does not exist');
  208         }
  209 
  210         return array_keys($this->data['columns'][$schema][$table]);
  211     }
  212 
  213     /**
  214      * {@inheritdoc}
  215      */
  216     public function getColumns($table, $schema = null)
  217     {
  218         if ($schema === null) {
  219             $schema = $this->defaultSchema;
  220         }
  221 
  222         $this->loadColumnData($table, $schema);
  223 
  224         $columns = [];
  225         foreach ($this->getColumnNames($table, $schema) as $columnName) {
  226             $columns[] = $this->getColumn($columnName, $table, $schema);
  227         }
  228         return $columns;
  229     }
  230 
  231     /**
  232      * {@inheritdoc}
  233      */
  234     public function getColumn($columnName, $table, $schema = null)
  235     {
  236         if ($schema === null) {
  237             $schema = $this->defaultSchema;
  238         }
  239 
  240         $this->loadColumnData($table, $schema);
  241 
  242         if (! isset($this->data['columns'][$schema][$table][$columnName])) {
  243             throw new \Exception('A column by that name was not found.');
  244         }
  245 
  246         $info = $this->data['columns'][$schema][$table][$columnName];
  247 
  248         $column = new ColumnObject($columnName, $table, $schema);
  249         $props = [
  250             'ordinal_position', 'column_default', 'is_nullable',
  251             'data_type', 'character_maximum_length', 'character_octet_length',
  252             'numeric_precision', 'numeric_scale', 'numeric_unsigned',
  253             'erratas'
  254         ];
  255         foreach ($props as $prop) {
  256             if (isset($info[$prop])) {
  257                 $column->{'set' . str_replace('_', '', $prop)}($info[$prop]);
  258             }
  259         }
  260 
  261         $column->setOrdinalPosition($info['ordinal_position']);
  262         $column->setColumnDefault($info['column_default']);
  263         $column->setIsNullable($info['is_nullable']);
  264         $column->setDataType($info['data_type']);
  265         $column->setCharacterMaximumLength($info['character_maximum_length']);
  266         $column->setCharacterOctetLength($info['character_octet_length']);
  267         $column->setNumericPrecision($info['numeric_precision']);
  268         $column->setNumericScale($info['numeric_scale']);
  269         $column->setNumericUnsigned($info['numeric_unsigned']);
  270         $column->setErratas($info['erratas']);
  271 
  272         return $column;
  273     }
  274 
  275     /**
  276      * {@inheritdoc}
  277      */
  278     public function getConstraints($table, $schema = null)
  279     {
  280         if ($schema === null) {
  281             $schema = $this->defaultSchema;
  282         }
  283 
  284         $this->loadConstraintData($table, $schema);
  285 
  286         $constraints = [];
  287         foreach (array_keys($this->data['constraints'][$schema][$table]) as $constraintName) {
  288             $constraints[] = $this->getConstraint($constraintName, $table, $schema);
  289         }
  290 
  291         return $constraints;
  292     }
  293 
  294     /**
  295      * {@inheritdoc}
  296      */
  297     public function getConstraint($constraintName, $table, $schema = null)
  298     {
  299         if ($schema === null) {
  300             $schema = $this->defaultSchema;
  301         }
  302 
  303         $this->loadConstraintData($table, $schema);
  304 
  305         if (! isset($this->data['constraints'][$schema][$table][$constraintName])) {
  306             throw new \Exception('Cannot find a constraint by that name in this table');
  307         }
  308 
  309         $info = $this->data['constraints'][$schema][$table][$constraintName];
  310         $constraint = new ConstraintObject($constraintName, $table, $schema);
  311 
  312         foreach ([
  313             'constraint_type'         => 'setType',
  314             'match_option'            => 'setMatchOption',
  315             'update_rule'             => 'setUpdateRule',
  316             'delete_rule'             => 'setDeleteRule',
  317             'columns'                 => 'setColumns',
  318             'referenced_table_schema' => 'setReferencedTableSchema',
  319             'referenced_table_name'   => 'setReferencedTableName',
  320             'referenced_columns'      => 'setReferencedColumns',
  321             'check_clause'            => 'setCheckClause',
  322         ] as $key => $setMethod) {
  323             if (isset($info[$key])) {
  324                 $constraint->{$setMethod}($info[$key]);
  325             }
  326         }
  327 
  328         return $constraint;
  329     }
  330 
  331     /**
  332      * {@inheritdoc}
  333      */
  334     public function getConstraintKeys($constraint, $table, $schema = null)
  335     {
  336         if ($schema === null) {
  337             $schema = $this->defaultSchema;
  338         }
  339 
  340         $this->loadConstraintReferences($table, $schema);
  341 
  342         // organize references first
  343         $references = [];
  344         foreach ($this->data['constraint_references'][$schema] as $refKeyInfo) {
  345             if ($refKeyInfo['constraint_name'] == $constraint) {
  346                 $references[$refKeyInfo['constraint_name']] = $refKeyInfo;
  347             }
  348         }
  349 
  350         $this->loadConstraintDataKeys($schema);
  351 
  352         $keys = [];
  353         foreach ($this->data['constraint_keys'][$schema] as $constraintKeyInfo) {
  354             if ($constraintKeyInfo['table_name'] == $table && $constraintKeyInfo['constraint_name'] === $constraint) {
  355                 $keys[] = $key = new ConstraintKeyObject($constraintKeyInfo['column_name']);
  356                 $key->setOrdinalPosition($constraintKeyInfo['ordinal_position']);
  357                 if (isset($references[$constraint])) {
  358                     //$key->setReferencedTableSchema($constraintKeyInfo['referenced_table_schema']);
  359                     $key->setForeignKeyUpdateRule($references[$constraint]['update_rule']);
  360                     $key->setForeignKeyDeleteRule($references[$constraint]['delete_rule']);
  361                     //$key->setReferencedTableSchema($references[$constraint]['referenced_table_schema']);
  362                     $key->setReferencedTableName($references[$constraint]['referenced_table_name']);
  363                     $key->setReferencedColumnName($references[$constraint]['referenced_column_name']);
  364                 }
  365             }
  366         }
  367 
  368         return $keys;
  369     }
  370 
  371     /**
  372      * {@inheritdoc}
  373      */
  374     public function getTriggerNames($schema = null)
  375     {
  376         if ($schema === null) {
  377             $schema = $this->defaultSchema;
  378         }
  379 
  380         $this->loadTriggerData($schema);
  381 
  382         return array_keys($this->data['triggers'][$schema]);
  383     }
  384 
  385     /**
  386      * {@inheritdoc}
  387      */
  388     public function getTriggers($schema = null)
  389     {
  390         if ($schema === null) {
  391             $schema = $this->defaultSchema;
  392         }
  393 
  394         $triggers = [];
  395         foreach ($this->getTriggerNames($schema) as $triggerName) {
  396             $triggers[] = $this->getTrigger($triggerName, $schema);
  397         }
  398         return $triggers;
  399     }
  400 
  401     /**
  402      * {@inheritdoc}
  403      */
  404     public function getTrigger($triggerName, $schema = null)
  405     {
  406         if ($schema === null) {
  407             $schema = $this->defaultSchema;
  408         }
  409 
  410         $this->loadTriggerData($schema);
  411 
  412         if (! isset($this->data['triggers'][$schema][$triggerName])) {
  413             throw new \Exception('Trigger "' . $triggerName . '" does not exist');
  414         }
  415 
  416         $info = $this->data['triggers'][$schema][$triggerName];
  417 
  418         $trigger = new TriggerObject();
  419 
  420         $trigger->setName($triggerName);
  421         $trigger->setEventManipulation($info['event_manipulation']);
  422         $trigger->setEventObjectCatalog($info['event_object_catalog']);
  423         $trigger->setEventObjectSchema($info['event_object_schema']);
  424         $trigger->setEventObjectTable($info['event_object_table']);
  425         $trigger->setActionOrder($info['action_order']);
  426         $trigger->setActionCondition($info['action_condition']);
  427         $trigger->setActionStatement($info['action_statement']);
  428         $trigger->setActionOrientation($info['action_orientation']);
  429         $trigger->setActionTiming($info['action_timing']);
  430         $trigger->setActionReferenceOldTable($info['action_reference_old_table']);
  431         $trigger->setActionReferenceNewTable($info['action_reference_new_table']);
  432         $trigger->setActionReferenceOldRow($info['action_reference_old_row']);
  433         $trigger->setActionReferenceNewRow($info['action_reference_new_row']);
  434         $trigger->setCreated($info['created']);
  435 
  436         return $trigger;
  437     }
  438 
  439     /**
  440      * Prepare data hierarchy
  441      *
  442      * @param string $type
  443      * @param string $key ...
  444      */
  445     protected function prepareDataHierarchy($type)
  446     {
  447         $data = &$this->data;
  448         foreach (func_get_args() as $key) {
  449             if (! isset($data[$key])) {
  450                 $data[$key] = [];
  451             }
  452             $data = &$data[$key];
  453         }
  454     }
  455 
  456     /**
  457      * Load schema data
  458      */
  459     protected function loadSchemaData()
  460     {
  461     }
  462 
  463     /**
  464      * Load table name data
  465      *
  466      * @param string $schema
  467      */
  468     protected function loadTableNameData($schema)
  469     {
  470         if (isset($this->data['table_names'][$schema])) {
  471             return;
  472         }
  473 
  474         $this->prepareDataHierarchy('table_names', $schema);
  475     }
  476 
  477     /**
  478      * Load column data
  479      *
  480      * @param string $table
  481      * @param string $schema
  482      */
  483     protected function loadColumnData($table, $schema)
  484     {
  485         if (isset($this->data['columns'][$schema][$table])) {
  486             return;
  487         }
  488 
  489         $this->prepareDataHierarchy('columns', $schema, $table);
  490     }
  491 
  492     /**
  493      * Load constraint data
  494      *
  495      * @param string $table
  496      * @param string $schema
  497      */
  498     protected function loadConstraintData($table, $schema)
  499     {
  500         if (isset($this->data['constraints'][$schema])) {
  501             return;
  502         }
  503 
  504         $this->prepareDataHierarchy('constraints', $schema);
  505     }
  506 
  507     /**
  508      * Load constraint data keys
  509      *
  510      * @param string $schema
  511      */
  512     protected function loadConstraintDataKeys($schema)
  513     {
  514         if (isset($this->data['constraint_keys'][$schema])) {
  515             return;
  516         }
  517 
  518         $this->prepareDataHierarchy('constraint_keys', $schema);
  519     }
  520 
  521     /**
  522      * Load constraint references
  523      *
  524      * @param string $table
  525      * @param string $schema
  526      */
  527     protected function loadConstraintReferences($table, $schema)
  528     {
  529         if (isset($this->data['constraint_references'][$schema])) {
  530             return;
  531         }
  532 
  533         $this->prepareDataHierarchy('constraint_references', $schema);
  534     }
  535 
  536     /**
  537      * Load trigger data
  538      *
  539      * @param string $schema
  540      */
  541     protected function loadTriggerData($schema)
  542     {
  543         if (isset($this->data['triggers'][$schema])) {
  544             return;
  545         }
  546 
  547         $this->prepareDataHierarchy('triggers', $schema);
  548     }
  549 }