feat: implement database schema discovery and management services with frontend integration
This commit is contained in:
@@ -302,4 +302,41 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
|
||||
|
||||
return $this->query($sql, [$database]);
|
||||
}
|
||||
|
||||
public function batchUpdate(string $table, array $changes): bool
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
|
||||
// Find primary key
|
||||
$schema = $this->getTableSchema($table);
|
||||
$primaryKey = 'id'; // default
|
||||
foreach ($schema as $col) {
|
||||
if (($col->Key ?? '') === 'PRI') {
|
||||
$primaryKey = $col->Field;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$connection->beginTransaction();
|
||||
try {
|
||||
foreach ($changes as $change) {
|
||||
if (!isset($change[$primaryKey])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = $change[$primaryKey];
|
||||
$updateData = $change;
|
||||
unset($updateData[$primaryKey]);
|
||||
|
||||
if (empty($updateData)) continue;
|
||||
|
||||
$connection->table($table)->where($primaryKey, $id)->update($updateData);
|
||||
}
|
||||
$connection->commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$connection->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,4 +156,12 @@ class DatabaseService
|
||||
{
|
||||
return $this->getDriver()->getTablesMetadata($database);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform batch update on a table.
|
||||
*/
|
||||
public function batchUpdate(string $table, array $changes): bool
|
||||
{
|
||||
return $this->getDriver()->batchUpdate($table, $changes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user