From 8ab5a69956dfbc876c2ea1aef1570d3273fc1169 Mon Sep 17 00:00:00 2001 From: "Kamshory, MT" Date: Wed, 14 Jan 2026 14:50:35 +0700 Subject: [PATCH] Fix boolean on database dump --- src/Generator/PicoDatabaseDump.php | 32 +++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Generator/PicoDatabaseDump.php b/src/Generator/PicoDatabaseDump.php index 9987072..3df87d3 100644 --- a/src/Generator/PicoDatabaseDump.php +++ b/src/Generator/PicoDatabaseDump.php @@ -171,6 +171,12 @@ public function dumpDataFromSchema($entity, $databaseType, $batchSize = 100) { // Check if the target database is PostgreSQL $isPgSql = $databaseType == PicoDatabaseType::DATABASE_TYPE_PGSQL || $databaseType == PicoDatabaseType::DATABASE_TYPE_POSTGRESQL; + + // Check if the target database is SQLite + $isSqlite = $databaseType == PicoDatabaseType::DATABASE_TYPE_SQLITE; + + // Check if the target database is SQL Server + $isSqlServer = $databaseType == PicoDatabaseType::DATABASE_TYPE_SQLSERVER; $tableName = $entity['name']; @@ -208,7 +214,7 @@ public function dumpDataFromSchema($entity, $databaseType, $batchSize = 100) $rows = array(); foreach ($filteredBatch as $data) { - $rows[] = "(" . implode(", ", $this->fixData($data, $columnInfo, $isPgSql)) . ")"; + $rows[] = "(" . implode(", ", $this->fixData($data, $columnInfo, $isPgSql, $isSqlite, $isSqlServer)) . ")"; } $allSql .= implode("\r\n", $sqlInsert) @@ -250,9 +256,11 @@ private function prepareColumnInfo($entity) * @param array $data Associative array of column => value. * @param array $columnInfo Metadata for each column. * @param bool $isPgSql Whether the target database is PostgreSQL. + * @param bool $isSqlite Whether the target database is SQLite. + * @param bool $isSqlServer Whether the target database is SQL Server. * @return array The formatted data array. */ - public function fixData($data, $columnInfo, $isPgSql) + public function fixData($data, $columnInfo, $isPgSql, $isSqlite, $isSqlServer) { foreach ($data as $key => $value) { if ($value === null) { @@ -261,9 +269,23 @@ public function fixData($data, $columnInfo, $isPgSql) } else if (isset($columnInfo[$key]) && in_array($columnInfo[$key]->normalizedType, ['integer', 'float'])) { // Keep numeric values as they are (no quotes) $data[$key] = $value; - } else if (isset($columnInfo[$key]) && in_array($columnInfo[$key]->normalizedType, ['boolean', 'bool']) && $isPgSql) { - // Handle PostgreSQL boolean conversion - $data[$key] = $this->toBoolean($value) ? 'true' : 'false'; + } else if (isset($columnInfo[$key]) && in_array($columnInfo[$key]->normalizedType, ['boolean', 'bool'])) { + // Handle boolean values + if($isPgSql) + { + // Force to boolean + $data[$key] = $this->toBoolean($value) ? 'true' : 'false'; + } + else if($isSqlite || $isSqlServer) + { + // Force to integer + $data[$key] = $this->toBoolean($value) ? '1' : '0'; + } + else + { + // MySQL and MariaDB + $data[$key] = $this->toBoolean($value) ? 'true' : 'false'; + } } else { // Treat as string: escape single quotes and wrap in quotes $data[$key] = "'" . str_replace("'", "''", $value) . "'";