Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom platform to build custom query #815

Closed
fzerorubigd opened this issue Jan 13, 2014 · 0 comments · Fixed by #816
Closed

Custom platform to build custom query #815

fzerorubigd opened this issue Jan 13, 2014 · 0 comments · Fixed by #816

Comments

@fzerorubigd
Copy link
Contributor

I want to have a custom platform for pgsql, mostly for create index on LOWER(key)
For this I create a new platform (Just to fix my problem, not a very generic code):

namespace {

    /**
     * Class CustomPgsqlPlatform
     */
    class CustomPgsqlPlatform extends PgsqlPlatform
    {

        /**
         * Add a table to list
         *
         * @param Table $table table to add
         *
         * @return string
         */
        public function getAddTableDDL(Table $table)
        {
            $ret = '';
            $ret .= $this->getUseSchemaDDL($table);
            $ret .= $this->getAddSequenceDDL($table);

            $lines = array();

            foreach ($table->getColumns() as $column) {
                $lines[] = $this->getColumnDDL($column);
            }

            if ($table->hasPrimaryKey()) {
                $lines[] = $this->getPrimaryKeyDDL($table);
            }

            $sep = ",
    ";
            $pattern = "
CREATE TABLE %s
(
    %s
);
";
            $ret .= sprintf(
                $pattern,
                $this->quoteIdentifier($table->getName()),
                implode($sep, $lines)
            );

            if ($table->hasDescription()) {
                $pattern = "
COMMENT ON TABLE %s IS %s;
";
                $ret .= sprintf(
                    $pattern,
                    $this->quoteIdentifier($table->getName()),
                    $this->quote($table->getDescription())
                );
            }

            $ret .= $this->getAddColumnsComments($table);
            $ret .= $this->getResetSchemaDDL($table);


            foreach ($table->getUnices() as $unique) {
                $ret .= $this->getUniqueDDL($table, $unique);
            }


            return $ret;
        }

        /**
         * Add a unique field
         *
         * @param Table  $table  table to add
         * @param Unique $unique unique field to add
         *
         * @return string
         */
        public function getUniqueDDL(Table $table, Unique $unique)
        {
            $list = array();
            foreach ($unique->getColumns() as $column) {
                if (!$column instanceof Column) {
                    $column = $table->getColumn($column);
                }
                if (strtoupper($column->getType()) == 'VARCHAR') {
                    $list[] = "LOWER(" . $this->quoteIdentifier($column->getName()) . ")";
                } else {
                    $list[] = $this->quoteIdentifier($column->getName());
                }

            }

            return sprintf(
                'CREATE UNIQUE INDEX %s ON %s (%s);',
                $this->quoteIdentifier($unique->getName()),
                $this->quoteIdentifier($table->getName()),
                implode(",", $list)
            );
        }
    }
}

The configs are ok, and php load this class, but for building the sql files, phing use the old platform file
here : https://github.com/propelorm/Propel/blob/master/generator/lib/config/GeneratorConfig.php#L167

So the first time, one instance is created from my class, but in the real sql-task, it use the propel version of this behavior.

I don't know is this a bug or its normal, and if its normal, how I can use my custom platform for building query for insert-sql task?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant