Example #1
0
    /**
     * Fetch constraint information from the database
     *
     * Invoked by the constructor, the default implementation queries
     * information_schema.table_constraints and information_schema.key_column_usage.
     * To make this functional, subclasses must set
     * \Nada\Database\AbstractDatabase::$_tableSchema.
     *
     * Only primary keys are recognized for now. All other constraints are
     * ignored.
     *
     * @todo Recognize other constraint types
     */
    protected function _fetchConstraints()
    {
        $constraints = $this->_database->query(<<<EOT
            SELECT kcu.column_name
            FROM information_schema.key_column_usage kcu
            JOIN information_schema.table_constraints tc
            USING (table_schema, table_name, constraint_schema, constraint_name)
            WHERE tc.constraint_type = 'PRIMARY KEY'
            AND tc.table_schema = ?
            AND LOWER(tc.table_name) = ?
            ORDER BY kcu.ordinal_position
EOT
, array($this->_database->getTableSchema(), $this->_name));
        foreach ($constraints as $constraint) {
            $this->_primaryKey[] = $this->_columns[strtolower($constraint['column_name'])];
        }
    }