示例#1
0
 /**
  * Get constraint keys
  *
  * @param  string $constraint
  * @param  string $table
  * @param  string $schema
  * @return array
  */
 public function getConstraintKeys($constraint, $table, $schema = null)
 {
     if ($schema === null) {
         $schema = $this->defaultSchema;
     }
     $this->loadConstraintData($table, $schema);
     // organize references first
     $references = array();
     foreach ($this->data['constraint_references'][$schema] as $refKeyInfo) {
         if ($refKeyInfo['constraint_name'] == $constraint) {
             $references[$refKeyInfo['constraint_name']] = $refKeyInfo;
         }
     }
     $keys = array();
     foreach ($this->data['constraint_keys'][$schema] as $constraintKeyInfo) {
         if ($constraintKeyInfo['table_name'] == $table && $constraintKeyInfo['constraint_name'] === $constraint) {
             $keys[] = $key = new Object\ConstraintKeyObject($constraintKeyInfo['column_name']);
             $key->setOrdinalPosition($constraintKeyInfo['ordinal_position']);
             if (isset($references[$constraint])) {
                 //$key->setReferencedTableSchema($constraintKeyInfo['referenced_table_schema']);
                 $key->setForeignKeyUpdateRule($references[$constraint]['update_rule']);
                 $key->setForeignKeyDeleteRule($references[$constraint]['delete_rule']);
                 //$key->setReferencedTableSchema($references[$constraint]['referenced_table_schema']);
                 $key->setReferencedTableName($references[$constraint]['referenced_table_name']);
                 $key->setReferencedColumnName($references[$constraint]['referenced_column_name']);
             }
         }
     }
     return $keys;
 }
示例#2
0
    /**
     * Get constraint keys
     * 
     * @param  string $constraint
     * @param  string $table
     * @param  string $schema
     * @param  string $database
     * @return Object\ConstraintKeyObject 
     */
    public function getConstraintKeys($constraint, $table, $schema = null)
    {
        if ($this->constraintData == null) {
            $this->loadConstraintData();
        }

        $found = false;
        foreach ($this->constraintData as $tableName => $constraints) {
            foreach ($constraints as $constraintData) {
                if ($tableName == $table && $constraintData['name'] == $constraint) {
                    $found = $constraintData;
                    break 2;
                }
            }
        }

        if (!$found) {
            throw new \Exception('invalid constraint, or constraint not found');
        }

        $keys = array();
        foreach ($found['keys'] as $keyData) {
            $keys[] = $key = new Object\ConstraintKeyObject($keyData['column']);
            if ($found['type'] == 'FOREIGN KEY') {
                $key->setReferencedTableName($keyData['referenced_table']);
                $key->setReferencedColumnName($keyData['referenced_column']);
                $key->setForeignKeyUpdateRule($keyData['update_rule']);
                $key->setForeignKeyDeleteRule($keyData['delete_rule']);
            }
        }

        return $keys;
    }