Skip to content

Commit a67ca20

Browse files
authored
Merge pull request #181 from Planetbiru/feature/version-3.21.3
Fix boolean on database dump
2 parents b615084 + 8ab5a69 commit a67ca20

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/Generator/PicoDatabaseDump.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ public function dumpDataFromSchema($entity, $databaseType, $batchSize = 100)
171171
{
172172
// Check if the target database is PostgreSQL
173173
$isPgSql = $databaseType == PicoDatabaseType::DATABASE_TYPE_PGSQL || $databaseType == PicoDatabaseType::DATABASE_TYPE_POSTGRESQL;
174+
175+
// Check if the target database is SQLite
176+
$isSqlite = $databaseType == PicoDatabaseType::DATABASE_TYPE_SQLITE;
177+
178+
// Check if the target database is SQL Server
179+
$isSqlServer = $databaseType == PicoDatabaseType::DATABASE_TYPE_SQLSERVER;
174180

175181
$tableName = $entity['name'];
176182

@@ -208,7 +214,7 @@ public function dumpDataFromSchema($entity, $databaseType, $batchSize = 100)
208214

209215
$rows = array();
210216
foreach ($filteredBatch as $data) {
211-
$rows[] = "(" . implode(", ", $this->fixData($data, $columnInfo, $isPgSql)) . ")";
217+
$rows[] = "(" . implode(", ", $this->fixData($data, $columnInfo, $isPgSql, $isSqlite, $isSqlServer)) . ")";
212218
}
213219

214220
$allSql .= implode("\r\n", $sqlInsert)
@@ -250,9 +256,11 @@ private function prepareColumnInfo($entity)
250256
* @param array $data Associative array of column => value.
251257
* @param array $columnInfo Metadata for each column.
252258
* @param bool $isPgSql Whether the target database is PostgreSQL.
259+
* @param bool $isSqlite Whether the target database is SQLite.
260+
* @param bool $isSqlServer Whether the target database is SQL Server.
253261
* @return array The formatted data array.
254262
*/
255-
public function fixData($data, $columnInfo, $isPgSql)
263+
public function fixData($data, $columnInfo, $isPgSql, $isSqlite, $isSqlServer)
256264
{
257265
foreach ($data as $key => $value) {
258266
if ($value === null) {
@@ -261,9 +269,23 @@ public function fixData($data, $columnInfo, $isPgSql)
261269
} else if (isset($columnInfo[$key]) && in_array($columnInfo[$key]->normalizedType, ['integer', 'float'])) {
262270
// Keep numeric values as they are (no quotes)
263271
$data[$key] = $value;
264-
} else if (isset($columnInfo[$key]) && in_array($columnInfo[$key]->normalizedType, ['boolean', 'bool']) && $isPgSql) {
265-
// Handle PostgreSQL boolean conversion
266-
$data[$key] = $this->toBoolean($value) ? 'true' : 'false';
272+
} else if (isset($columnInfo[$key]) && in_array($columnInfo[$key]->normalizedType, ['boolean', 'bool'])) {
273+
// Handle boolean values
274+
if($isPgSql)
275+
{
276+
// Force to boolean
277+
$data[$key] = $this->toBoolean($value) ? 'true' : 'false';
278+
}
279+
else if($isSqlite || $isSqlServer)
280+
{
281+
// Force to integer
282+
$data[$key] = $this->toBoolean($value) ? '1' : '0';
283+
}
284+
else
285+
{
286+
// MySQL and MariaDB
287+
$data[$key] = $this->toBoolean($value) ? 'true' : 'false';
288+
}
267289
} else {
268290
// Treat as string: escape single quotes and wrap in quotes
269291
$data[$key] = "'" . str_replace("'", "''", $value) . "'";

0 commit comments

Comments
 (0)