-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDuckDB.php
68 lines (51 loc) · 1.9 KB
/
DuckDB.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
class DuckDB
{
private FFI $ffi;
private FFI\CData $db;
private FFI\CData $conn;
public function __construct(string $databasePath, string $libraryPath, string $headersPath)
{
$this->ffi = FFI::cdef(file_get_contents($headersPath), $libraryPath);
$this->db = $this->ffi->new('duckdb_database');
$this->conn = $this->ffi->new('duckdb_connection');
$error = $this->ffi->duckdb_open($databasePath, FFI::addr($this->db));
if ($error) {
throw new Exception('error open: ' . $databasePath);
}
$error = $this->ffi->duckdb_connect($this->db, FFI::addr($this->conn));
if ($error) {
throw new Exception('error connect');
}
}
public function __destruct()
{
$this->ffi->duckdb_disconnect(FFI::addr($this->conn));
$this->ffi->duckdb_close(FFI::addr($this->db));
FFI::free($this->conn);
FFI::free($this->db);
unset($this->ffi);
}
public function query(string $query): array
{
$result = $this->ffi->new('duckdb_result');
$error = $this->ffi->duckdb_query($this->conn, $query, FFI::addr($result));
if ($error) {
$message = FFI::string($result->error_message);
$this->ffi->duckdb_destroy_result(FFI::addr($result));
throw new Exception($message);
}
$data = [];
$columns = [];
for ($col = 0; $col < $result->column_count; $col++) {
$columns[] = FFI::string($result->columns[$col]->name);
for ($row = 0; $row < $result->row_count; $row++) {
$value = $this->ffi->duckdb_value_varchar(FFI::addr($result), $col, $row);
$data[$row][$columns[$col]] = FFI::string($value);
FFI::free($value);
}
}
$this->ffi->duckdb_destroy_result(FFI::addr($result));
return $data;
}
}