DDC-639: Schema info included in output SQL #789

Open
opened 2026-01-22 12:50:31 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 15, 2010).

Jira issue originally created by user ss10sb:

Schema information isn't included into the generated SQL from ORM. I think I found a way to make it show up if it is included in the entity, if I missed the 'real' way to generate it, feel free to disregard this (and let me know ;)).

/****
* Gets the (possibly quoted) schema name of this class for safe use
* in an SQL statement.
* 
* @param AbstractPlatform $platform
* @return string
*/
public function getQuotedSchemaName($platform)
{
        //abstract out the dot separator? I think all major rdbms use it
        return isset($this->table['schema']) ?
                        isset($this->table['quoted']) ?
                        $platform->quoteIdentifier($this->table['schema']) . '.' :
                        $this->table['schema'] . '.' :
                    null;
}
/****
* Walks down a FromClause AST node, thereby generating the appropriate SQL.
*
* @return string The SQL.
*/
public function walkFromClause($fromClause)
{
    ...
<ins>    $sql .= $class->getQuotedSchemaName($this->*platform) . $class->getQuotedTableName($this->*platform) . ' '
              . $this->getSqlTableAlias($class->table['name'], $dqlAlias);
-    $sql .= $class->getQuotedTableName($this->_platform) . ' '
              . $this->getSqlTableAlias($class->table['name'], $dqlAlias);
    ...
}
/****
* Walks down a JoinVariableDeclaration AST node and creates the corresponding SQL.
*
* @param JoinVariableDeclaration $joinVarDecl
* @return string The SQL.
*/
public function walkJoinVariableDeclaration($joinVarDecl)
{
        $join = $joinVarDecl->join;
        $joinType = $join->joinType;

        if ($joinType == AST\Join::JOIN*TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE*LEFTOUTER) {
            $sql = ' LEFT JOIN ';
        } else {
            $sql = ' INNER JOIN ';
        }

        $joinAssocPathExpr = $join->joinAssociationPathExpression;
        $joinedDqlAlias = $join->aliasIdentificationVariable;
        $relation = $this->_queryComponents[$joinedDqlAlias]['relation'];
        $targetClass = $this->_em->getClassMetadata($relation->targetEntityName);
        $sourceClass = $this->_em->getClassMetadata($relation->sourceEntityName);
</ins>       $targetSchemaName = $targetClass->getQuotedSchemaName($this->_platform);
        $targetTableName = $targetClass->getQuotedTableName($this->_platform);
        $targetTableAlias = $this->getSqlTableAlias($targetClass->table['name'], $joinedDqlAlias);
        $sourceTableAlias = $this->getSqlTableAlias($sourceClass->table['name'], $joinAssocPathExpr->identificationVariable);

    ...

        if ($assoc->isOneToOne()) {
<ins>          $sql .= $targetSchemaName . $targetTableName . ' ' . $targetTableAlias . ' ON ';
-          $sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ';
            $first = true;

            ...

        } else if ($assoc->isManyToMany()) {
            // Join relation table
            $joinTable = $assoc->joinTable;
</ins>           $joinTableSchema = $assoc->getQuotedSchemaName($this->_platform);
            $joinTableAlias = $this->getSqlTableAlias($joinTable['name'], $joinedDqlAlias);
<ins>           $sql .= $joinTableSchema . $assoc->getQuotedJoinTableName($this->_platform) . ' ' . $joinTableAlias . ' ON ';
-           $sql .= $assoc->getQuotedJoinTableName($this->_platform) . ' ' . $joinTableAlias . ' ON ';

            ...

            // Join target table
            $sql .= ($joinType == AST\Join::JOIN*TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE*LEFTOUTER)
                ? ' LEFT JOIN ' : ' INNER JOIN ';
</ins>           $sql .= $targetSchemaName . $targetTableName . ' ' . $targetTableAlias . ' ON ';
-           $sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ';

...

}
Originally created by @doctrinebot on GitHub (Jun 15, 2010). Jira issue originally created by user ss10sb: Schema information isn't included into the generated SQL from ORM. I think I found a way to make it show up if it is included in the entity, if I missed the 'real' way to generate it, feel free to disregard this (and let me know ;)). ``` /**** * Gets the (possibly quoted) schema name of this class for safe use * in an SQL statement. * * @param AbstractPlatform $platform * @return string */ public function getQuotedSchemaName($platform) { //abstract out the dot separator? I think all major rdbms use it return isset($this->table['schema']) ? isset($this->table['quoted']) ? $platform->quoteIdentifier($this->table['schema']) . '.' : $this->table['schema'] . '.' : null; } ``` ``` /**** * Walks down a FromClause AST node, thereby generating the appropriate SQL. * * @return string The SQL. */ public function walkFromClause($fromClause) { ... <ins> $sql .= $class->getQuotedSchemaName($this->*platform) . $class->getQuotedTableName($this->*platform) . ' ' . $this->getSqlTableAlias($class->table['name'], $dqlAlias); - $sql .= $class->getQuotedTableName($this->_platform) . ' ' . $this->getSqlTableAlias($class->table['name'], $dqlAlias); ... } /**** * Walks down a JoinVariableDeclaration AST node and creates the corresponding SQL. * * @param JoinVariableDeclaration $joinVarDecl * @return string The SQL. */ public function walkJoinVariableDeclaration($joinVarDecl) { $join = $joinVarDecl->join; $joinType = $join->joinType; if ($joinType == AST\Join::JOIN*TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE*LEFTOUTER) { $sql = ' LEFT JOIN '; } else { $sql = ' INNER JOIN '; } $joinAssocPathExpr = $join->joinAssociationPathExpression; $joinedDqlAlias = $join->aliasIdentificationVariable; $relation = $this->_queryComponents[$joinedDqlAlias]['relation']; $targetClass = $this->_em->getClassMetadata($relation->targetEntityName); $sourceClass = $this->_em->getClassMetadata($relation->sourceEntityName); </ins> $targetSchemaName = $targetClass->getQuotedSchemaName($this->_platform); $targetTableName = $targetClass->getQuotedTableName($this->_platform); $targetTableAlias = $this->getSqlTableAlias($targetClass->table['name'], $joinedDqlAlias); $sourceTableAlias = $this->getSqlTableAlias($sourceClass->table['name'], $joinAssocPathExpr->identificationVariable); ... if ($assoc->isOneToOne()) { <ins> $sql .= $targetSchemaName . $targetTableName . ' ' . $targetTableAlias . ' ON '; - $sql .= $targetTableName . ' ' . $targetTableAlias . ' ON '; $first = true; ... } else if ($assoc->isManyToMany()) { // Join relation table $joinTable = $assoc->joinTable; </ins> $joinTableSchema = $assoc->getQuotedSchemaName($this->_platform); $joinTableAlias = $this->getSqlTableAlias($joinTable['name'], $joinedDqlAlias); <ins> $sql .= $joinTableSchema . $assoc->getQuotedJoinTableName($this->_platform) . ' ' . $joinTableAlias . ' ON '; - $sql .= $assoc->getQuotedJoinTableName($this->_platform) . ' ' . $joinTableAlias . ' ON '; ... // Join target table $sql .= ($joinType == AST\Join::JOIN*TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE*LEFTOUTER) ? ' LEFT JOIN ' : ' INNER JOIN '; </ins> $sql .= $targetSchemaName . $targetTableName . ' ' . $targetTableAlias . ' ON '; - $sql .= $targetTableName . ' ' . $targetTableAlias . ' ON '; ... } ```
admin added the New Feature label 2026-01-22 12:50:31 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#789