From 6c5aa03b031e84be58bd5be84414cdf4fd77e69d Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 14 Oct 2025 17:43:30 +0800 Subject: [PATCH] bugfix --- sqlor/ddl_template_duckdb.py | 62 ++++++++++++++++++++++++++++++++++++ sqlor/duckdbor.py | 7 +--- 2 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 sqlor/ddl_template_duckdb.py diff --git a/sqlor/ddl_template_duckdb.py b/sqlor/ddl_template_duckdb.py new file mode 100644 index 0000000..ee59e0f --- /dev/null +++ b/sqlor/ddl_template_duckdb.py @@ -0,0 +1,62 @@ +duckdb_ddl_tmpl = """{% macro typeStr(type,len,dec) %} +{%- if type in ['str', 'char'] -%} +VARCHAR({{len}}) +{%- elif type in ['short', 'int'] -%} +INTEGER +{%- elif type == 'long' -%} +BIGINT +{%- elif type in ['float', 'double', 'ddouble'] -%} +DOUBLE +{%- elif type == 'date' -%} +DATE +{%- elif type == 'time' -%} +TIME +{%- elif type in ['datetime', 'timestamp'] -%} +TIMESTAMP +{%- elif type == 'text' -%} +VARCHAR +{%- elif type == 'bin' -%} +BLOB +{%- else -%} +{{type | upper}} +{%- endif %} +{%- endmacro %} + +{%- macro defaultValue(defaultv) %} +{%- if defaultv %} DEFAULT '{{defaultv}}'{%- endif -%} +{%- endmacro %} + +{% macro nullStr(nullable) %} +{%- if nullable=='no' -%} +NOT NULL +{%- endif -%} +{% endmacro %} + +{% macro primary() %} +, PRIMARY KEY({{ ','.join(summary[0].primary) }}) +{% endmacro %} + +-- Drop table if exists +DROP TABLE IF EXISTS {{summary[0].name}}; + +-- Create table +CREATE TABLE {{summary[0].name}} ( +{% for field in fields %} + "{{field.name}}" {{typeStr(field.type,field.length,field.dec)}} {{nullStr(field.nullable)}} {{defaultValue(field.default)}}{% if not loop.last %},{% endif %} +{% endfor %} +{% if summary[0].primary and len(summary[0].primary)>0 %} +{{primary()}} +{% endif %} +); + +-- Table comment (DuckDB 不支持 COMMENT 语法) +{% if summary[0].title %} +-- COMMENT: {{summary[0].title}} +{% endif %} + +-- DuckDB 当前版本不支持 CREATE INDEX 语法,以下保留作兼容注释 +{% for v in indexes %} +-- INDEX {{summary[0].name}}_{{v.name}} ON {{summary[0].name}}({{ ",".join(v.idxfields) }}); +{% endfor %} +""" + diff --git a/sqlor/duckdbor.py b/sqlor/duckdbor.py index 41a09f4..0672f03 100644 --- a/sqlor/duckdbor.py +++ b/sqlor/duckdbor.py @@ -2,12 +2,7 @@ from appPublic.argsConvert import ArgsConvert, ConditionConvert from .sor import SQLor from .const import ROWS - -duckdb_ddl_tmpl = { - # 可以按需扩展 - 'create_table': "CREATE TABLE {table} ({fields})", - 'drop_table': "DROP TABLE IF EXISTS {table}", -} +from .ddl_template_duckdb import duckdb_ddl_tmpl class DuckDBor(SQLor): """