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

Add type to store UUIDs as binary (UUID_BINARY) #1917

Merged
merged 8 commits into from Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 47 additions & 35 deletions .github/workflows/ci.yml
Expand Up @@ -24,33 +24,40 @@ jobs:
php-version: '7.4'
- symfony-version: '6-max'
php-version: '7.4'
env:
DB_NAME: 'propel_tests'
DB_USER: 'propel'
DB_PW: 'propel'
steps:
- name: Install PostgreSQL latest
if: matrix.db-type == 'pgsql' && matrix.php-version != '7.3'
if: matrix.db-type == 'pgsql' && matrix.php-version != '7.4'
uses: CasperWA/postgresql-action@v1.2
with:
postgresql db: 'propel-tests'
postgresql user: 'postgres'
postgresql password: 'postgres'
postgresql db: $DB_NAME
postgresql user: $DB_USER
postgresql password: $DB_PW

- name: Install PostgreSQL min
if: matrix.db-type == 'pgsql' && matrix.php-version == '7.3'
if: matrix.db-type == 'pgsql' && matrix.php-version == '7.4'
uses: CasperWA/postgresql-action@v1.2
with:
postgresql version: 9
postgresql db: 'propel-tests'
postgresql user: 'postgres'
postgresql password: 'postgres'
postgresql db: $DB_NAME
postgresql user: $DB_USER
postgresql password: $DB_PW

- name: Install MariaDb latest
if: matrix.db-type == 'mysql' && matrix.php-version != '7.3'
uses: getong/mariadb-action@v1.1
- name: Install MySQL latest
if: matrix.db-type == 'mysql' && matrix.php-version != '7.4'
uses: mirromutth/mysql-action@v1.1
with:
mysql root password: $DB_PW
dereuromark marked this conversation as resolved.
Show resolved Hide resolved

- name: Install MariaDb min
if: matrix.db-type == 'mysql' && matrix.php-version == '7.3'
if: matrix.db-type == 'mysql' && matrix.php-version == '7.4'
uses: getong/mariadb-action@v1.1
with:
mariadb version: '10.2'
mysql root password: $DB_PW

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -79,34 +86,39 @@ jobs:
- name: Composer install (Symfony version ${{ matrix.symfony-version }})
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Setup Postgresql database for test suite
if: matrix.db-type == 'pgsql'
run: tests/bin/setup.pgsql.sh
- name: Wait for MySQL server to load
if: matrix.db-type == 'mysql'
run: |
bash -c "
for i in {1..10}; do
mysqladmin -h 127.0.0.1 -u root status >/dev/null 2>&1 && exit 0 || sleep 6
echo 'trying again'
done;
echo 'could not establish connection after 10 tries'
exit 1
"
env:
DB_NAME: 'propel-tests'
DB_USER: 'postgres'
DB_PW: 'postgres'
MYSQL_PWD: ${{ env.DB_PW }}

- name: Setup the database for test suite
if: matrix.db-type != 'agnostic' && matrix.db-type != 'pgsql'
run: tests/bin/setup.${{ matrix.db-type }}.sh

- name: Run PostgreSQL tests
if: matrix.db-type == 'pgsql'
shell: 'script -q -e -c "bash {0}"'
- name: Create MySQL Propel user
if: matrix.db-type == 'mysql'
run: |
if [[ ${{ matrix.php-version }} == '7.4' && ${{ matrix.symfony-version == '5-max' }} ]]; then
export CODECOVERAGE=1 && vendor/bin/phpunit -c tests/pgsql.phpunit.xml --verbose --coverage-clover=tests/coverage.xml
else
vendor/bin/phpunit -c tests/pgsql.phpunit.xml
fi
mysql -h 127.0.0.1 -u root -e "
CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PW';
CREATE USER '$DB_USER'@'%' IDENTIFIED BY '$DB_PW';
GRANT ALL PRIVILEGES ON *.* TO '$DB_USER'@'localhost';
GRANT ALL PRIVILEGES ON *.* TO '$DB_USER'@'%';
FLUSH PRIVILEGES;
"
env:
DB_NAME: 'propel-tests'
DB_USER: 'postgres'
DB_PW: 'postgres'
MYSQL_PWD: ${{ env.DB_PW }}

- name: Setup database for test suite
if: matrix.db-type != 'agnostic'
run: tests/bin/setup.${{ matrix.db-type }}.sh

- name: Run ${{ matrix.db-type }} tests
if: matrix.db-type != 'pgsql'
- name: Run database tests
if: matrix.db-type != 'agnostic'
shell: 'script -q -e -c "bash {0}"'
run: |
if [[ ${{ matrix.php-version }} == '7.4' && ${{ matrix.symfony-version == '5-max' }} ]]; then
Expand Down
25 changes: 25 additions & 0 deletions src/Propel/Generator/Builder/Om/AbstractOMBuilder.php
Expand Up @@ -18,6 +18,7 @@
use Propel\Generator\Model\CrossForeignKeys;
use Propel\Generator\Model\ForeignKey;
use Propel\Generator\Model\Table;
use Propel\Generator\Model\VendorInfo;

/**
* Baseclass for OM-building classes.
Expand Down Expand Up @@ -1189,4 +1190,28 @@ abstract protected function addClassBody(string &$script): void;
* @return void
*/
abstract protected function addClassClose(string &$script): void;

/**
* Returns the vendor info from the table for the configured platform.
*
* @return \Propel\Generator\Model\VendorInfo
*/
protected function getVendorInfo(): VendorInfo
{
$dbVendorId = $this->getPlatform()->getDatabaseType();

return $this->getTable()->getVendorInfoForType($dbVendorId);
}

/**
* @psalm-return 'true'|'false'
*
* @see \Propel\Generator\Model\VendorInfo::getUuidSwapFlagLiteral()
*
* @return string
*/
protected function getUuidSwapFlagLiteral(): string
{
return $this->getVendorInfo()->getUuidSwapFlagLiteral();
}
}
64 changes: 56 additions & 8 deletions src/Propel/Generator/Builder/Om/ObjectBuilder.php
Expand Up @@ -323,6 +323,12 @@ protected function addClassBody(string &$script): void
}

$table = $this->getTable();

$additionalModelClasses = $table->getAdditionalModelClassImports();
if ($additionalModelClasses) {
$this->declareClasses(...$additionalModelClasses);
}

if (!$table->isAlias()) {
$this->addConstants($script);
$this->addAttributes($script);
Expand Down Expand Up @@ -1709,6 +1715,13 @@ protected function addLazyLoaderBody(string &$script, Column $column): void
} elseif ($column->isPhpObjectType()) {
$script .= "
\$this->$clo = (\$firstColumn !== null) ? new " . $column->getPhpType() . '($firstColumn) : null;';
} elseif ($column->getType() === PropelTypes::UUID_BINARY) {
$uuidSwapFlag = $this->getUuidSwapFlagLiteral();
$script .= "
if (is_resource(\$firstColumn)) {
\$firstColumn = stream_get_contents(\$firstColumn);
}
\$this->$clo = (\$firstColumn) ? UuidConverter::binToUuid(\$firstColumn, $uuidSwapFlag) : null;";
} else {
$script .= "
\$this->$clo = \$firstColumn;";
Expand Down Expand Up @@ -2716,6 +2729,13 @@ protected function addHydrateBody(string &$script): void
}
$script .= "
\$this->$clo = (null !== \$col) ? PropelDateTime::newInstance(\$col, null, '$dateTimeClass') : null;";
} elseif ($col->isUuidBinaryType()) {
$uuidSwapFlag = $this->getUuidSwapFlagLiteral();
$script .= "
if (is_resource(\$col)) {
\$col = stream_get_contents(\$col);
}
\$this->$clo = (\$col) ? UuidConverter::binToUuid(\$col, $uuidSwapFlag) : null;";
} elseif ($col->isPhpPrimitiveType()) {
$script .= "
\$this->$clo = (null !== \$col) ? (" . $col->getPhpType() . ') $col : null;';
Expand Down Expand Up @@ -2745,8 +2765,8 @@ protected function addHydrateBody(string &$script): void

if ($this->getBuildProperty('generator.objectModel.addSaveMethod')) {
$script .= "
\$this->resetModified();
";

\$this->resetModified();";
}

$script .= "
Expand Down Expand Up @@ -2950,10 +2970,11 @@ protected function addBuildCriteriaBody(string &$script): void
\$criteria = new Criteria(" . $this->getTableMapClass() . "::DATABASE_NAME);
";
foreach ($this->getTable()->getColumns() as $col) {
$clo = $col->getLowercasedName();
$accessValueStatement = $this->getAccessValueStatement($col);
$columnConstant = $this->getColumnConstant($col);
$script .= "
if (\$this->isColumnModified(" . $this->getColumnConstant($col) . ")) {
\$criteria->add(" . $this->getColumnConstant($col) . ", \$this->$clo);
if (\$this->isColumnModified($columnConstant)) {
\$criteria->add($columnConstant, $accessValueStatement);
}";
}
}
Expand Down Expand Up @@ -6599,12 +6620,15 @@ protected function addDoInsertBodyRaw(): string
\$stmt = \$con->prepare(\$sql);
foreach (\$modifiedColumns as \$identifier => \$columnName) {
switch (\$columnName) {";

$tab = ' ';
foreach ($table->getColumns() as $column) {
$columnNameCase = var_export($this->quoteIdentifier($column->getName()), true);
$accessValueStatement = $this->getAccessValueStatement($column);
$bindValueStatement = $platform->getColumnBindingPHP($column, '$identifier', $accessValueStatement, $tab);
$script .= "
case $columnNameCase:";
$script .= $platform->getColumnBindingPHP($column, '$identifier', '$this->' . $column->getLowercasedName(), ' ');
$script .= "
case $columnNameCase:$bindValueStatement

break;";
}
$script .= "
Expand Down Expand Up @@ -6645,6 +6669,30 @@ protected function addDoInsertBodyRaw(): string
return $script;
}

/**
* Get the statement how a column value is accessed in the script.
*
* Note that this is not necessarily just the getter. If the value is
* stored on the model in an encoded format, the statement returned by
* this method includes the statement to decode the value.
*
* @param \Propel\Generator\Model\Column $column
*
* @return string
*/
protected function getAccessValueStatement(Column $column): string
{
$columnName = $column->getLowercasedName();

if ($column->isUuidBinaryType()) {
$uuidSwapFlag = $this->getUuidSwapFlagLiteral();

return "(\$this->$columnName) ? UuidConverter::uuidToBin(\$this->$columnName, $uuidSwapFlag) : null";
}

return "\$this->$columnName";
}

/**
* get the doUpdate() method code
*
Expand Down
8 changes: 8 additions & 0 deletions src/Propel/Generator/Builder/Om/QueryBuilder.php
Expand Up @@ -174,6 +174,10 @@ protected function addClassBody(string &$script): void
);
$this->declareClassFromBuilder($this->getStubQueryBuilder(), 'Child');
$this->declareClassFromBuilder($this->getTableMapBuilder());
$additionalModelClasses = $table->getAdditionalModelClassImports();
if ($additionalModelClasses) {
$this->declareClasses(...$additionalModelClasses);
}

// apply behaviors
$this->applyBehaviorModifier('queryAttributes', $script, ' ');
Expand Down Expand Up @@ -1147,6 +1151,10 @@ public function filterBy$colPhpName(\$$variableName = null, ?string \$comparison
if (is_string(\$$variableName)) {
\$$variableName = in_array(strtolower(\$$variableName), array('false', 'off', '-', 'no', 'n', '0', '')) ? false : true;
}";
} elseif ($col->isUuidBinaryType()) {
$uuidSwapFlag = $this->getUuidSwapFlagLiteral();
$script .= "
\$$variableName = UuidConverter::uuidToBinRecursive(\$$variableName, $uuidSwapFlag);";
}
$script .= "

Expand Down
36 changes: 36 additions & 0 deletions src/Propel/Generator/Model/Column.php
Expand Up @@ -1243,6 +1243,18 @@ public function getType(): string
return $this->getDomain()->getType();
}

/**
* Returns the SQL type as a string.
*
* @see Domain::getSqlType()
*
* @return string
*/
public function getSqlType(): string
{
return $this->getDomain()->getSqlType();
}

/**
* Returns the column PDO type integer for this column's mapping type.
*
Expand Down Expand Up @@ -1333,6 +1345,16 @@ public function isUuidType(): bool
return PropelTypes::isUuidType($this->getType());
}

/**
* Returns whether this column is a uuid bin type.
*
* @return bool
*/
public function isUuidBinaryType(): bool
{
return $this->getType() === PropelTypes::UUID_BINARY;
}

/**
* Returns whether the column is an array column.
*
Expand Down Expand Up @@ -1713,4 +1735,18 @@ public static function generatePhpSingularName(string $phpName): string
{
return rtrim($phpName, 's');
}

/**
* Checks if xml attributes from schema.xml matches expected content declaration.
*
* @param string $content
*
* @return bool
*/
public function isContent(string $content): bool
{
$contentAttribute = $this->getAttribute('content');

return $contentAttribute && strtoupper($contentAttribute) === strtoupper($contentAttribute);
}
}