Skip to content

Commit a88d42c

Browse files
committed
close pdo cursor after handling results
1 parent e0f23fd commit a88d42c

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed

Diff for: src/Analyzer/QueryPlanAnalyzerMysql.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public function analyze(string $query): QueryPlanResult
5757
$stmt = $this->connection->query($simulatedQuery);
5858

5959
// @phpstan-ignore-next-line
60-
return $this->buildResult($simulatedQuery, $stmt);
60+
$planResult = $this->buildResult($simulatedQuery, $stmt);
61+
$stmt->closeCursor();
62+
63+
return $planResult;
6164
} finally {
6265
$this->connection->rollBack();
6366
}
@@ -67,7 +70,10 @@ public function analyze(string $query): QueryPlanResult
6770
try {
6871
$result = $this->connection->query($simulatedQuery);
6972
if ($result instanceof \mysqli_result) {
70-
return $this->buildResult($simulatedQuery, $result);
73+
$planResult = $this->buildResult($simulatedQuery, $result);
74+
$result->close();
75+
76+
return $planResult;
7177
}
7278
} finally {
7379
$this->connection->rollback();

Diff for: src/DbSchema/SchemaHasherMysql.php

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function hashDb(): string
6868
foreach ($stmt as $row) {
6969
$hash = $row['dbsignature'] ?? '';
7070
}
71+
$stmt->closeCursor();
7172
} finally {
7273
$this->connection->rollBack();
7374
}
@@ -80,6 +81,7 @@ public function hashDb(): string
8081
$row = $result->fetch_assoc();
8182
$hash = $row['dbsignature'] ?? '';
8283
}
84+
$result->close();
8385
} finally {
8486
$this->connection->rollback();
8587
}

Diff for: src/QueryReflection/PdoMysqlQueryReflector.php

+19-13
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ protected function simulateQuery(string $queryString)
8282
++$columnIndex;
8383
}
8484

85+
$stmt->closeCursor();
86+
8587
return $this->cache[$queryString];
8688
}
8789

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

110-
$this->stmt->execute([$tableName]);
111-
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
112-
113-
foreach ($result as $row) {
114-
$extra = $row['EXTRA'];
115-
$columnType = $row['COLUMN_TYPE'];
116-
$columnName = $row['COLUMN_NAME'];
117-
118-
if (str_contains($extra, 'auto_increment')) {
119-
yield $columnName => TypeMapper::FLAG_AUTO_INCREMENT;
120-
}
121-
if (str_contains($columnType, 'unsigned')) {
122-
yield $columnName => TypeMapper::FLAG_UNSIGNED;
112+
try {
113+
$this->stmt->execute([$tableName]);
114+
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
115+
116+
foreach ($result as $row) {
117+
$extra = $row['EXTRA'];
118+
$columnType = $row['COLUMN_TYPE'];
119+
$columnName = $row['COLUMN_NAME'];
120+
121+
if (str_contains($extra, 'auto_increment')) {
122+
yield $columnName => TypeMapper::FLAG_AUTO_INCREMENT;
123+
}
124+
if (str_contains($columnType, 'unsigned')) {
125+
yield $columnName => TypeMapper::FLAG_UNSIGNED;
126+
}
123127
}
128+
} finally {
129+
$this->stmt->closeCursor();
124130
}
125131
}
126132
}

Diff for: src/QueryReflection/PdoPgSqlQueryReflector.php

+21-20
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ protected function simulateQuery(string $queryString)
5656
} catch (PDOException $e) {
5757
return $this->cache[$queryString] = $e;
5858
} finally {
59-
try {
60-
$this->pdo->rollBack();
61-
} catch (PDOException $e) {
62-
// not all drivers may support transactions
63-
throw new \RuntimeException('Failed to rollback transaction', $e->getCode(), $e);
64-
}
59+
$this->pdo->rollBack();
6560
}
6661

6762
$this->cache[$queryString] = [];
@@ -91,6 +86,8 @@ protected function simulateQuery(string $queryString)
9186
++$columnIndex;
9287
}
9388

89+
$stmt->closeCursor();
90+
9491
return $this->cache[$queryString];
9592
}
9693

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

117-
$this->stmt->execute([$tableName]);
118-
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
119-
120-
/** @var array{column_default?: string, column_name: string, is_nullable: string} $row */
121-
foreach ($result as $row) {
122-
$default = $row['column_default'] ?? '';
123-
$columnName = $row['column_name'];
124-
$isNullable = 'YES' === $row['is_nullable'];
125-
126-
if (!$isNullable) {
127-
yield $columnName => PgsqlTypeMapper::FLAG_NOT_NULL;
128-
}
129-
if (str_contains($default, 'nextval')) {
130-
yield $columnName => PgsqlTypeMapper::FLAG_AUTO_INCREMENT;
114+
try {
115+
$this->stmt->execute([$tableName]);
116+
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
117+
118+
/** @var array{column_default?: string, column_name: string, is_nullable: string} $row */
119+
foreach ($result as $row) {
120+
$default = $row['column_default'] ?? '';
121+
$columnName = $row['column_name'];
122+
$isNullable = 'YES' === $row['is_nullable'];
123+
124+
if (!$isNullable) {
125+
yield $columnName => PgsqlTypeMapper::FLAG_NOT_NULL;
126+
}
127+
if (str_contains($default, 'nextval')) {
128+
yield $columnName => PgsqlTypeMapper::FLAG_AUTO_INCREMENT;
129+
}
131130
}
131+
} finally {
132+
$this->stmt->closeCursor();
132133
}
133134
}
134135
}

0 commit comments

Comments
 (0)