Skip to content

Commit 3733f98

Browse files
committed
fix: PDOStatement remove conn
1 parent 631b3ce commit 3733f98

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

flake.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
php
3333
turso-cli
3434
] ++ lib.optionals stdenv.isDarwin [ iconv ];
35+
36+
shellHook = ''
37+
export TURSO_URL=$(turso db show --url php-test)
38+
export TURSO_AUTH_TOKEN=$(turso db tokens create -e 1d php-test)
39+
'';
3540
};
3641
});
3742
}

src/PDO.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public function prepare(string $query, array $options = []): PDOStatement|false
8787
return new PDOStatement(
8888
$prepare,
8989
$query,
90-
$conn,
9190
);
9291
}
9392

@@ -101,6 +100,7 @@ public function errorInfo(): array
101100
throw new \Exception('Not implemented');
102101
}
103102

103+
#[\ReturnTypeWillChange]
104104
public function lastInsertId(?string $name = null): string|int|false
105105
{
106106
return $this->conn->lastInsertId();

src/PDOStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class PDOStatement extends \PDOStatement
1111
private ?int $affectedRows = null;
1212
private ?int $mode = PDO::FETCH_BOTH;
1313

14-
public function __construct(private Statement $statement, string $query, private Connection $conn)
14+
public function __construct(private Statement $statement, string $query)
1515
{
1616
$this->query = $query;
1717
}

tests/Test.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ public function testEmbeddedReplica(): void
122122
}
123123
}
124124

125+
public function testRemoteReplica(): void
126+
{
127+
$db = new Database(
128+
url: getenv('TURSO_URL'),
129+
authToken: getenv('TURSO_AUTH_TOKEN'),
130+
);
131+
$conn = $db->connect();
132+
133+
$conn->execute('drop table if exists test');
134+
$conn->execute('create table test (i integer, r real, t text)');
135+
136+
$max = 20;
137+
138+
foreach (range(0, $max - 1) as $i) {
139+
$stmt = $conn->prepare('insert into test values (:a, :b, :c)');
140+
$stmt->bind([
141+
":a" => $i,
142+
":b" => exp($i / 10),
143+
":c" => strval(exp($i / 10))
144+
]);
145+
$stmt->execute();
146+
}
147+
148+
foreach ($conn->query('select * from test') as $i => $row) {
149+
$this->assertSame($row->i, $i);
150+
# $this->assertSame($row->r, exp($i / 10));
151+
$this->assertSame($row->t, strval(exp($i / 10)));
152+
}
153+
}
154+
125155
public function testBlob(): void
126156
{
127157
$db = new Database();
@@ -195,8 +225,31 @@ public function testPDORemoteFetchObj(): void
195225

196226
foreach ($rows as $i => $row) {
197227
$this->assertSame($row->i, $i);
198-
$this->assertSame($row->r, exp($i / 10));
199228
$this->assertSame($row->t, strval(exp($i / 10)));
200229
}
201230
}
231+
232+
public function testPDOTransaction(): void
233+
{
234+
$pdo = new \Libsql\PDO();
235+
236+
$tx = $pdo->beginTransaction();
237+
238+
$pdo->exec('create table test(i integer)');
239+
240+
$pdo->exec('insert into test values (0)');
241+
$pdo->exec('insert into test values (1)');
242+
$pdo->exec('insert into test values (2)');
243+
244+
$statement = $pdo->prepare('select * from test');
245+
$statement->execute();
246+
$statement->setFetchMode(\Libsql\PDO::FETCH_OBJ);
247+
$rows = $statement->fetchAll();
248+
249+
foreach ($rows as $i => $row) {
250+
$this->assertSame($row->i, $i);
251+
}
252+
253+
$tx = $pdo->commit();
254+
}
202255
}

0 commit comments

Comments
 (0)