Skip to content

Commit

Permalink
close pdo cursor after handling results
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jan 18, 2023
1 parent e0f23fd commit a88d42c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
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
2 changes: 2 additions & 0 deletions src/DbSchema/SchemaHasherMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function hashDb(): string
foreach ($stmt as $row) {
$hash = $row['dbsignature'] ?? '';
}
$stmt->closeCursor();
} finally {
$this->connection->rollBack();
}
Expand All @@ -80,6 +81,7 @@ public function hashDb(): string
$row = $result->fetch_assoc();
$hash = $row['dbsignature'] ?? '';
}
$result->close();
} finally {
$this->connection->rollback();
}
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)
} 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)
++$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();
}
}
}

0 comments on commit a88d42c

Please sign in to comment.