This commit is contained in:
yumoqing 2021-07-22 16:24:34 +08:00
parent 5a60c34cf8
commit aad639f19a
3 changed files with 58 additions and 21 deletions

37
sqlor/ddl_template_sqlite3.py Executable file
View File

@ -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 -%}
"""

0
sqlor/runsql.py Normal file → Executable file
View File

View File

@ -3,30 +3,22 @@ from .sor import SQLor
class SQLite3or(SQLor): class SQLite3or(SQLor):
db2modelTypeMapping = { db2modelTypeMapping = {
'char':'char', 'text':'str',
'nchar':'str',
'text':'text',
'ntext':'text',
'varchar':'str',
'nvarchar':'str',
'blob':'file', 'blob':'file',
'int':'long',
'integer':'long', 'integer':'long',
'double':'float', 'real':'float',
'date':'date',
'time':'time',
'timestamp':'timestamp',
'number':'long',
} }
model2dbTypemapping = { model2dbTypemapping = {
'date':'date', 'date':'text',
'time':'time', 'time':'text',
'timestamp':'timestamp', 'timestamp':'text',
'str':'nvarchar', 'str':'text',
'char':'char', 'char':'text',
'short':'int', 'short':'int',
'long':'integer', 'long':'int',
'float':'double', 'float':'real',
'text':'ntext', 'text':'text',
'file':'blob', 'file':'blob',
} }
@classmethod @classmethod
@ -54,7 +46,7 @@ class SQLite3or(SQLor):
return sqlcmd return sqlcmd
def fieldsSQL(self,tablename): def fieldsSQL(self,tablename):
sqlcmd="""PRAGMA table_info('%s')""" % tablename.lower() # sqlcmd="""PRAGMA table_info('%s')""" % tablename.lower()
return sqlcmd return sqlcmd
def fields(self,tablename): def fields(self,tablename):
@ -78,7 +70,7 @@ class SQLite3or(SQLor):
r['title'] = r['name'] r['title'] = r['name']
ret = [] ret = []
for r in recs: 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()}) r.update({'name':r['name'].lower()})
ret.append(r) ret.append(r)
return ret return ret
@ -98,3 +90,11 @@ class SQLite3or(SQLor):
def pkSQL(self,tablename): def pkSQL(self,tablename):
sqlcmd = "" sqlcmd = ""
return 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