From ec35ee964af21f8a948c55002874660938dadf7a Mon Sep 17 00:00:00 2001 From: haobibo Date: Thu, 11 Sep 2025 14:12:53 +0800 Subject: [PATCH] fix duckdb --- src/README.md | 29 +++++++++++++++++++++-------- src/aloha/db/duckdb.py | 28 ++++++++++++++++------------ 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/README.md b/src/README.md index 511972c..050c39a 100644 --- a/src/README.md +++ b/src/README.md @@ -1,26 +1,39 @@ # Aloha! +## What is it? + +`aloha` is a versatile Python utility package for building microservices. + [![License](https://img.shields.io/github/license/QPod/aloha)](https://github.com/QPod/aloha/blob/main/LICENSE) [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/QPod/aloha-python/pip.yml?branch=main)](https://github.com/QPod/aloha-python/actions) -[![Join the Gitter Chat](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/QPod/) -[![PyPI version](https://img.shields.io/pypi/v/aloha)](https://pypi.python.org/pypi/aloha/) -[![PyPI Downloads](https://img.shields.io/pypi/dm/aloha)](https://pepy.tech/badge/aloha/) [![Code Activity](https://img.shields.io/github/commit-activity/m/QPod/aloha)](https://github.com/QPod/aloha/pulse) [![Recent Code Update](https://img.shields.io/github/last-commit/QPod/docker-images.svg)](https://github.com/QPod/aloha/stargazers) -Please generously STAR★ our project or donate to us! [![GitHub Starts](https://img.shields.io/github/stars/QPod/aloha.svg?label=Stars&style=social)](https://github.com/QPod/aloha/stargazers) +[![PyPI version](https://img.shields.io/pypi/v/aloha)](https://pypi.python.org/pypi/aloha/) +[![PyPI Downloads](https://img.shields.io/pypi/dm/aloha)](https://pepy.tech/badge/aloha/) + +--- + +Please generously STAR★ our project or donate to us! +[![GitHub Starts](https://img.shields.io/github/stars/QPod/aloha.svg?label=Stars&style=social)](https://github.com/QPod/aloha/stargazers) [![Donate-PayPal](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://paypal.me/haobibo) [![Donate-AliPay](https://img.shields.io/badge/Donate-Alipay-blue.svg)](https://raw.githubusercontent.com/wiki/haobibo/resources/img/Donate-AliPay.png) [![Donate-WeChat](https://img.shields.io/badge/Donate-WeChat-green.svg)](https://raw.githubusercontent.com/wiki/haobibo/resources/img/Donate-WeChat.png) -## What is it? - -`aloha` is a versatile Python utility package for building microservices. +- For questions, try DeepWiki: [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/QPod/aloha-python) -[📚 Document & 中文文档](https://aloha-python.readthedocs.io/) +- To contribute or talk to a human: [![Open an Issue on GitHub](https://img.shields.io/github/issues/QPod/aloha-python)](https://github.com/QPod/aloha-python/issues) [![Join the Discord Chat](https://img.shields.io/badge/Discuss_on-Discord-green)](https://discord.gg/kHUzgQxgbJ) ## Getting started +Refer to[📚 Document & 中文文档](https://aloha-python.readthedocs.io/) for detailed introduction. + ```shell pip install aloha[all] ``` + +And then: +```python +from aloha.logger import LOG +from aloha.settings import SETTINGS as S +``` diff --git a/src/aloha/db/duckdb.py b/src/aloha/db/duckdb.py index 9fada3d..384506c 100644 --- a/src/aloha/db/duckdb.py +++ b/src/aloha/db/duckdb.py @@ -25,9 +25,10 @@ def __init__(self, db_config, **kwargs): 'schema': db_config.get('schema', 'main'), 'read_only': bool(db_config.get('read_only', False)), 'config': db_config.get('config', {}), + 'auto_commit': db_config.get('auto_commit', True), } - if not self._config['path'] or self._config['path'] == ':memory:': # in-memroy mode + if not self._config['path'] or self._config['path'] == ':memory:': # in-memory mode self._config['path'] = ':memory:' if self._config['read_only']: # in-memory mode cannot be read-only @@ -39,14 +40,14 @@ def __init__(self, db_config, **kwargs): try: str_connection = f"duckdb:///{self._config['path']}" - self.conn = create_engine( + self.engine = create_engine( str_connection, connect_args={ 'read_only': self._config['read_only'], 'config': self._config['config'] }, **kwargs - ).connect() + ) self._initialize_schema() msg = f"DuckDB connected: {self._config['path']} [schema={self._config['schema']}, read_only={self._config['read_only']}]" @@ -89,7 +90,7 @@ def _initialize_schema(self): try: if self._config['read_only']: - result = self.conn.execute( + result = self.engine.connext().execute( text("SELECT schema_name FROM information_schema.schemata WHERE schema_name = :schema"), {'schema': self._config['schema']} ) @@ -98,21 +99,24 @@ def _initialize_schema(self): f"Schema '{self._config['schema']}' does not exist and read_only=True" ) else: - self.conn.execute(text(f"CREATE SCHEMA IF NOT EXISTS {self._config['schema']}")) + self.engine.connect().execute(text(f"CREATE SCHEMA IF NOT EXISTS {self._config['schema']}")) - self.conn.execute(text(f"SET schema '{self._config['schema']}'")) + self.engine.connect().execute(text(f"SET schema '{self._config['schema']}'")) except Exception as e: raise RuntimeError(f'Failed to initialize schema: {e}') @property def connection(self): - return self.conn + return self.engine - def execute_query(self, sql, auto_commit: bool = True, *args, **kwargs): - cur = self.conn.execute(text(sql), *args, **kwargs) - if auto_commit: - self.conn.commit() - return cur + conn = connection + + def execute_query(self, sql, *args, **kwargs): + with self.engine.connect() as conn: + cur = conn.execute(text(sql), *args, *kwargs) + if self._config.get('auto_commit', True): + conn.commit() + return cur @property def connection_str(self) -> str: