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

close pdo cursor after handling results #500

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Analyzer/QueryPlanAnalyzerMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public function analyze(string $query): QueryPlanResult
$stmt = $this->connection->query($simulatedQuery);

// @phpstan-ignore-next-line
return $this->buildResult($simulatedQuery, $stmt);
$planResult = $this->buildResult($simulatedQuery, $stmt);
$stmt->closeCursor();

return $planResult;
} finally {
$this->connection->rollBack();
}
Expand All @@ -67,7 +70,10 @@ public function analyze(string $query): QueryPlanResult
try {
$result = $this->connection->query($simulatedQuery);
if ($result instanceof \mysqli_result) {
return $this->buildResult($simulatedQuery, $result);
$planResult = $this->buildResult($simulatedQuery, $result);
$result->close();

return $planResult;
}
} finally {
$this->connection->rollback();
Expand Down
13 changes: 11 additions & 2 deletions src/DbSchema/SchemaHasherMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ public function hashDb(): string
// for a schema with 3.000 columns we need roughly
// 70.000 group concat max length
$maxConcatQuery = 'SET SESSION group_concat_max_len = 1000000';
$this->connection->query($maxConcatQuery);
$result = $this->connection->query($maxConcatQuery);
if ($result) {
if ($this->connection instanceof PDO) {
$result->close();
} else {
$result->closeCursor();
}
}

$query = '
$query = '
SELECT
MD5(
GROUP_CONCAT(
Expand Down Expand Up @@ -68,6 +75,7 @@ public function hashDb(): string
foreach ($stmt as $row) {
$hash = $row['dbsignature'] ?? '';
}
$stmt->closeCursor();
} finally {
$this->connection->rollBack();
}
Expand All @@ -79,6 +87,7 @@ public function hashDb(): string
if ($result instanceof \mysqli_result) {
$row = $result->fetch_assoc();
$hash = $row['dbsignature'] ?? '';
$result->close();
}
} finally {
$this->connection->rollback();
Expand Down
2 changes: 1 addition & 1 deletion src/QueryReflection/MysqliQueryReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private function simulateQuery(string $queryString)
}

$resultInfo = $result->fetch_fields();
$result->free();
$result->close();

return $this->cache[$queryString] = $resultInfo;
} catch (mysqli_sql_exception $e) {
Expand Down
32 changes: 19 additions & 13 deletions src/QueryReflection/PdoMysqlQueryReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ protected function simulateQuery(string $queryString)
++$columnIndex;
}

$stmt->closeCursor();

return $this->cache[$queryString];
}

Expand All @@ -107,20 +109,24 @@ protected function checkInformationSchema(string $tableName): Iterator
);
}

$this->stmt->execute([$tableName]);
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
$extra = $row['EXTRA'];
$columnType = $row['COLUMN_TYPE'];
$columnName = $row['COLUMN_NAME'];

if (str_contains($extra, 'auto_increment')) {
yield $columnName => TypeMapper::FLAG_AUTO_INCREMENT;
}
if (str_contains($columnType, 'unsigned')) {
yield $columnName => TypeMapper::FLAG_UNSIGNED;
try {
$this->stmt->execute([$tableName]);
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
$extra = $row['EXTRA'];
$columnType = $row['COLUMN_TYPE'];
$columnName = $row['COLUMN_NAME'];

if (str_contains($extra, 'auto_increment')) {
yield $columnName => TypeMapper::FLAG_AUTO_INCREMENT;
}
if (str_contains($columnType, 'unsigned')) {
yield $columnName => TypeMapper::FLAG_UNSIGNED;
}
}
} finally {
$this->stmt->closeCursor();
}
}
}
41 changes: 21 additions & 20 deletions src/QueryReflection/PdoPgSqlQueryReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ protected function simulateQuery(string $queryString) // @phpstan-ignore-line
} catch (PDOException $e) {
return $this->cache[$queryString] = $e;
} finally {
try {
$this->pdo->rollBack();
} catch (PDOException $e) {
// not all drivers may support transactions
throw new \RuntimeException('Failed to rollback transaction', $e->getCode(), $e);
}
$this->pdo->rollBack();
}

$this->cache[$queryString] = [];
Expand Down Expand Up @@ -91,6 +86,8 @@ protected function simulateQuery(string $queryString) // @phpstan-ignore-line
++$columnIndex;
}

$stmt->closeCursor();

return $this->cache[$queryString];
}

Expand All @@ -114,21 +111,25 @@ protected function checkInformationSchema(string $tableName): Iterator
);
}

$this->stmt->execute([$tableName]);
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);

/** @var array{column_default?: string, column_name: string, is_nullable: string} $row */
foreach ($result as $row) {
$default = $row['column_default'] ?? '';
$columnName = $row['column_name'];
$isNullable = 'YES' === $row['is_nullable'];

if (!$isNullable) {
yield $columnName => PgsqlTypeMapper::FLAG_NOT_NULL;
}
if (str_contains($default, 'nextval')) {
yield $columnName => PgsqlTypeMapper::FLAG_AUTO_INCREMENT;
try {
$this->stmt->execute([$tableName]);
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);

/** @var array{column_default?: string, column_name: string, is_nullable: string} $row */
foreach ($result as $row) {
$default = $row['column_default'] ?? '';
$columnName = $row['column_name'];
$isNullable = 'YES' === $row['is_nullable'];

if (!$isNullable) {
yield $columnName => PgsqlTypeMapper::FLAG_NOT_NULL;
}
if (str_contains($default, 'nextval')) {
yield $columnName => PgsqlTypeMapper::FLAG_AUTO_INCREMENT;
}
}
} finally {
$this->stmt->closeCursor();
}
}
}