Skip to content

Commit 73ddbe6

Browse files
chanwutkGithub Actions Bot
and
Github Actions Bot
authored
Ck/fix duckdb (#71)
* fix database sql * style: [CI] format --------- Co-authored-by: Github Actions Bot <[email protected]>
1 parent 3669a86 commit 73ddbe6

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

spatialyze/database.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import TYPE_CHECKING, Callable, NamedTuple
44

55
import duckdb
6-
import pandas as pd
76
import shapely.geometry
87

98
from .data_types.camera_key import CameraKey
@@ -103,10 +102,10 @@ class Database:
103102

104103
def __init__(self, connection: "duckdb.DuckDBPyConnection"):
105104
self.connection = connection
105+
self.connection.install_extension("spatial")
106+
self.connection.load_extension("spatial")
106107
self.cursor = self.connection.cursor()
107108

108-
self.cursor.execute("INSTALL spatial;")
109-
self.cursor.execute("LOAD spatial;")
110109
self.cursor.commit()
111110
self.connection.commit()
112111

@@ -287,7 +286,8 @@ def insert_camera(self, camera: list[CameraConfig]):
287286
cursor = self.connection.cursor()
288287
cursor.executemany(
289288
"INSERT INTO Camera VALUES "
290-
"(?, ?, ?, ?, ST_GeomFromWKB(?), ?, ?, ST_GeomFromWKB(?), ?, ?, ?, ?)",
289+
"(?, ?, ?, ?, ST_GeomFromWKB(?), ?,"
290+
" ?, ST_GeomFromWKB(?), ?, ?, ?, ?)",
291291
map(_config, camera),
292292
)
293293

@@ -333,12 +333,9 @@ def predicate(self, predicate: "PredicateNode", temporal: bool = True):
333333
for frame_number, camera_id, filename, *item_ids in self.execute(sql_str)
334334
]
335335

336-
def sql(self, query: str) -> pd.DataFrame:
337-
results, cursor = self.execute_and_cursor(query)
338-
description = cursor.description
339-
assert description is not None
340-
cursor.close()
341-
return pd.DataFrame(results, columns=[d.name for d in description])
336+
def sql(self, query: str) -> duckdb.DuckDBPyRelation:
337+
with self.connection.cursor() as cursor:
338+
return cursor.sql(query)
342339

343340

344341
def _join_table(temporal: bool):

0 commit comments

Comments
 (0)