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):
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