From aad639f19a34d675c6e4776e420af9cf0f559bf6 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 22 Jul 2021 16:24:34 +0800 Subject: [PATCH] bugfix --- sqlor/ddl_template_sqlite3.py | 37 ++++++++++++++++++++++++++++++ sqlor/runsql.py | 0 sqlor/sqlite3or.py | 42 +++++++++++++++++------------------ 3 files changed, 58 insertions(+), 21 deletions(-) create mode 100755 sqlor/ddl_template_sqlite3.py mode change 100644 => 100755 sqlor/runsql.py diff --git a/sqlor/ddl_template_sqlite3.py b/sqlor/ddl_template_sqlite3.py new file mode 100755 index 0000000..2102217 --- /dev/null +++ b/sqlor/ddl_template_sqlite3.py @@ -0,0 +1,37 @@ +sqlite_ddl_tmpl = """{% macro typeStr(type,len,dec) %} +{%- if type in ['str', 'char', 'date', 'time', 'datetime', 'timestamp'] -%} +TEXT +{%- elif type in ['long', 'int', 'short', 'longlong' ] -%} +int +{%- elif type in ['float', 'double', 'ddouble'] -%} +real +{%- elif type=='bin' -%} +blob +{%- else -%} +{{type}} +{%- 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 {{summary[0].name}}; +CREATE TABLE {{summary[0].name}} +( +{% for field in fields %} + `{{field.name}}` {{typeStr(field.type,field.length,field.dec)}} {{nullStr(field.nullable)}} {%if field.title -%} -- {{field.title}}{%- endif %}{%- if not loop.last -%},{%- endif -%} +{% endfor %} +{% if summary[0].primary and len(summary[0].primary)>0 %} +{{primary()}} +{% endif %} +) +{% if summary[0].title %} --{{summary[0].title}}{% endif %} +; +{% for v in indexes %} +CREATE {% if v.idxtype=='unique' %}UNIQUE{% endif %} INDEX {{summary[0].name}}_{{v.name}} ON {{summary[0].name}}({{",".join(v.idxfields)}}); +{%- endfor -%} +""" diff --git a/sqlor/runsql.py b/sqlor/runsql.py old mode 100644 new mode 100755 diff --git a/sqlor/sqlite3or.py b/sqlor/sqlite3or.py index 9d96c8f..a57ab4f 100755 --- a/sqlor/sqlite3or.py +++ b/sqlor/sqlite3or.py @@ -3,30 +3,22 @@ from .sor import SQLor class SQLite3or(SQLor): db2modelTypeMapping = { - 'char':'char', - 'nchar':'str', - 'text':'text', - 'ntext':'text', - 'varchar':'str', - 'nvarchar':'str', + 'text':'str', 'blob':'file', + 'int':'long', 'integer':'long', - 'double':'float', - 'date':'date', - 'time':'time', - 'timestamp':'timestamp', - 'number':'long', + 'real':'float', } model2dbTypemapping = { - 'date':'date', - 'time':'time', - 'timestamp':'timestamp', - 'str':'nvarchar', - 'char':'char', + 'date':'text', + 'time':'text', + 'timestamp':'text', + 'str':'text', + 'char':'text', 'short':'int', - 'long':'integer', - 'float':'double', - 'text':'ntext', + 'long':'int', + 'float':'real', + 'text':'text', 'file':'blob', } @classmethod @@ -54,7 +46,7 @@ class SQLite3or(SQLor): return sqlcmd def fieldsSQL(self,tablename): - sqlcmd="""PRAGMA table_info('%s')""" % tablename.lower() + # sqlcmd="""PRAGMA table_info('%s')""" % tablename.lower() return sqlcmd def fields(self,tablename): @@ -78,7 +70,7 @@ class SQLite3or(SQLor): r['title'] = r['name'] ret = [] for r in recs: - r.update({'type':self.db2modelTypeMapping[r['type'].lower()]}) + r.update({'type':self.db2modelTypeMapping.get(r['type'].lower(),'text')}) r.update({'name':r['name'].lower()}) ret.append(r) return ret @@ -98,3 +90,11 @@ class SQLite3or(SQLor): def pkSQL(self,tablename): sqlcmd = "" return sqlcmd + + def indexesSQL(self,tablename=None): + sqlcmd = """select * from sqlite_master +where lower(type) = 'index' + """ + if tablename: + sqlcmd += "and lower(tbl_name)='" + tablename.lower() + "' " + return sqlcmd