public function testGetConstraintKeys() { $refProp = new \ReflectionProperty($this->abstractSourceMock, 'data'); $refProp->setAccessible(true); // internal data $data = array('constraint_references' => array('foo_schema' => array(array('constraint_name' => 'bam_constraint', 'update_rule' => 'UP', 'delete_rule' => 'DOWN', 'referenced_table_name' => 'another_table', 'referenced_column_name' => 'another_column'))), 'constraint_keys' => array('foo_schema' => array(array('table_name' => 'bar_table', 'constraint_name' => 'bam_constraint', 'column_name' => 'a', 'ordinal_position' => 1)))); $refProp->setValue($this->abstractSourceMock, $data); $constraints = $this->abstractSourceMock->getConstraintKeys('bam_constraint', 'bar_table', 'foo_schema'); $this->assertCount(1, $constraints); /** * @var \Zend\Db\Metadata\Object\ConstraintKeyObject $constraintKeyObj */ $constraintKeyObj = $constraints[0]; $this->assertInstanceOf('Zend\\Db\\Metadata\\Object\\ConstraintKeyObject', $constraintKeyObj); // check value object is mapped correctly $this->assertEquals('a', $constraintKeyObj->getColumnName()); $this->assertEquals(1, $constraintKeyObj->getOrdinalPosition()); $this->assertEquals('another_table', $constraintKeyObj->getReferencedTableName()); $this->assertEquals('another_column', $constraintKeyObj->getReferencedColumnName()); $this->assertEquals('UP', $constraintKeyObj->getForeignKeyUpdateRule()); $this->assertEquals('DOWN', $constraintKeyObj->getForeignKeyDeleteRule()); }
protected function loadConstraintReferences($table, $schema) { parent::loadConstraintReferences($table, $schema); $p = $this->adapter->getPlatform(); $isColumns = array(array('RC', 'TABLE_NAME'), array('RC', 'CONSTRAINT_NAME'), array('RC', 'UPDATE_RULE'), array('RC', 'DELETE_RULE'), array('KCU', 'REFERENCED_TABLE_SCHEMA'), array('KCU', 'REFERENCED_TABLE_NAME'), array('KCU', 'REFERENCED_COLUMN_NAME')); array_walk($isColumns, function (&$c) use($p) { $c = $p->quoteIdentifierChain($c); }); $sql = 'SELECT ' . implode(', ', $isColumns) . 'FROM ' . $p->quoteIdentifierChain(array('INFORMATION_SCHEMA', 'TABLES')) . 'T' . ' INNER JOIN ' . $p->quoteIdentifierChain(array('INFORMATION_SCHEMA', 'REFERENTIAL_CONSTRAINTS')) . 'RC' . ' ON ' . $p->quoteIdentifierChain(array('T', 'TABLE_SCHEMA')) . ' = ' . $p->quoteIdentifierChain(array('RC', 'CONSTRAINT_SCHEMA')) . ' AND ' . $p->quoteIdentifierChain(array('T', 'TABLE_NAME')) . ' = ' . $p->quoteIdentifierChain(array('RC', 'TABLE_NAME')) . ' INNER JOIN ' . $p->quoteIdentifierChain(array('INFORMATION_SCHEMA', 'KEY_COLUMN_USAGE')) . 'KCU' . ' ON ' . $p->quoteIdentifierChain(array('RC', 'CONSTRAINT_SCHEMA')) . ' = ' . $p->quoteIdentifierChain(array('KCU', 'TABLE_SCHEMA')) . ' AND ' . $p->quoteIdentifierChain(array('RC', 'TABLE_NAME')) . ' = ' . $p->quoteIdentifierChain(array('KCU', 'TABLE_NAME')) . ' AND ' . $p->quoteIdentifierChain(array('RC', 'CONSTRAINT_NAME')) . ' = ' . $p->quoteIdentifierChain(array('KCU', 'CONSTRAINT_NAME')) . 'WHERE ' . $p->quoteIdentifierChain(array('T', 'TABLE_TYPE')) . ' IN (\'BASE TABLE\', \'VIEW\')'; if ($schema != self::DEFAULT_SCHEMA) { $sql .= ' AND ' . $p->quoteIdentifierChain(array('T', 'TABLE_SCHEMA')) . ' = ' . $p->quoteTrustedValue($schema); } else { $sql .= ' AND ' . $p->quoteIdentifierChain(array('T', 'TABLE_SCHEMA')) . ' != \'INFORMATION_SCHEMA\''; } $results = $this->adapter->query($sql, Adapter::QUERY_MODE_EXECUTE); $data = array(); foreach ($results->toArray() as $row) { $data[] = array_change_key_case($row, CASE_LOWER); } $this->data['constraint_references'][$schema] = $data; }