diff --git a/setup.py b/setup.py index a5ba5e5..8c3d18f 100755 --- a/setup.py +++ b/setup.py @@ -1,42 +1,42 @@ -# -*- coding: utf-8 -*- - - -from distutils.core import setup -from setuptools import setup, find_packages - -# usage: -# python setup.py bdist_wininst generate a window executable file -# python setup.py bdist_egg generate a egg file -# Release information about eway - -version = "0.0.1" -description = "sqlor" -author = "yumoqing" -email = "yumoqing@gmail.com" - -packages=find_packages() -package_data = {} - -setup( - name="sqlor", - version=version, - - # uncomment the following lines if you fill them out in release.py - description=description, - author=author, - author_email=email, - - install_requires=[ - ], - packages=packages, - package_data=package_data, - keywords = [ - ], - classifiers = [ - 'Development Status :: 1 - Alpha', - 'Operating System :: OS Independent', - 'Programming Language :: Python3.5', - 'Topic :: SQL execute :: Libraries :: Python Modules', - ], - platforms= 'any' -) +# -*- coding: utf-8 -*- + + +from distutils.core import setup +from setuptools import setup, find_packages + +# usage: +# python setup.py bdist_wininst generate a window executable file +# python setup.py bdist_egg generate a egg file +# Release information about eway + +version = "0.0.1" +description = "sqlor" +author = "yumoqing" +email = "yumoqing@gmail.com" + +packages=find_packages() +package_data = {} + +setup( + name="sqlor", + version=version, + + # uncomment the following lines if you fill them out in release.py + description=description, + author=author, + author_email=email, + + install_requires=[ + ], + packages=packages, + package_data=package_data, + keywords = [ + ], + classifiers = [ + 'Development Status :: 1 - Alpha', + 'Operating System :: OS Independent', + 'Programming Language :: Python3.5', + 'Topic :: SQL execute :: Libraries :: Python Modules', + ], + platforms= 'any' +) diff --git a/sqlor/dbpools.py b/sqlor/dbpools.py index 10d3002..f0f51b7 100644 --- a/sqlor/dbpools.py +++ b/sqlor/dbpools.py @@ -196,9 +196,11 @@ class DBPools: sor = await self.getSqlor(dbname) try: ret = await func(sor,dbname,*args,**kw) + await sor.conn.commit() return ret except Exception as e: print('error',sor) + sor.conn.rollback() raise e finally: await self.freeSqlor(sor) @@ -209,19 +211,21 @@ class DBPools: @wraps(func) async def wrap_func(dbname,NS,callback=None,**kw): sor = await self.getSqlor(dbname) + ret = None try: desc = func(dbname,NS,callback=callback,**kw) ret = await sor.runSQL(desc,NS,callback,**kw) + await sor.conn.commit() if NS.get('dummy'): return NS['dummy'] else: return [] except Exception as e: print('error:',e) + await sor.conn.rollback() raise e finally: await self.freeSqlor(sor) - return ret return wrap_func def runSQLPaging(self,func): diff --git a/sqlor/ddl_template_mysql.py b/sqlor/ddl_template_mysql.py index 5c9b039..ecde8ca 100644 --- a/sqlor/ddl_template_mysql.py +++ b/sqlor/ddl_template_mysql.py @@ -1,55 +1,55 @@ -mysql_ddl_tmpl = """{% macro typeStr(type,len,dec) %} -{%- if type=='str' -%} -VARCHAR({{len}}) -{%- elif type=='char' -%} -CHAR({{len}}) -{%- elif type=='long' or type=='int' or type=='short' -%} -int -{%- elif type=='long' -%} -bigint -{%- elif type=='float' or type=='double' or type=='ddouble' -%} -double({{len}},{{dec}}) -{%- elif type=='date' -%} -date -{%- elif type=='time' -%} -time -{%- elif type=='datetime' -%} -datetime -{%- elif type=='timestamp' -%} -TIMESTAMP -{%- elif type=='text' -%} -longtext -{%- elif type=='bin' -%} -longblob -{%- else -%} -{{type}} -{%- endif %} -{%- endmacro %} -{% macro nullStr(nullable) %} -{%- if nullable=='no' -%} -NOT NULL -{%- endif -%} -{% endmacro %} -{% macro primary() %} -{% if len(','.join(summary[0].primary))>0 %} -,primary key({{','.join(summary[0].primary)}}) -{% endif %} -{% endmacro %} -drop table {{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 -%} comment '{{field.title}}'{%- endif %}{%- if not loop.last -%},{%- endif -%} -{% endfor %} -{{primary()}} -) -engine=innodb -default charset=utf8 -{% if summary[0].title %}comment '{{summary[0].title}}'{% endif %} -; -{% for v in validation %} -{% if v.oper=='idx' %} -CREATE {% if v.value.idxtype=='unique' %}UNIQUE{% endif %} INDEX {{summary[0].name}}_{{v.name}} ON {{summary[0].name}}({{",".join(v.value.fields)}}); -{% endif %} -{%- endfor -%} +mysql_ddl_tmpl = """{% macro typeStr(type,len,dec) %} +{%- if type=='str' -%} +VARCHAR({{len}}) +{%- elif type=='char' -%} +CHAR({{len}}) +{%- elif type=='long' or type=='int' or type=='short' -%} +int +{%- elif type=='long' -%} +bigint +{%- elif type=='float' or type=='double' or type=='ddouble' -%} +double({{len}},{{dec}}) +{%- elif type=='date' -%} +date +{%- elif type=='time' -%} +time +{%- elif type=='datetime' -%} +datetime +{%- elif type=='timestamp' -%} +TIMESTAMP +{%- elif type=='text' -%} +longtext +{%- elif type=='bin' -%} +longblob +{%- else -%} +{{type}} +{%- endif %} +{%- endmacro %} +{% macro nullStr(nullable) %} +{%- if nullable=='no' -%} +NOT NULL +{%- endif -%} +{% endmacro %} +{% macro primary() %} +{% if len(','.join(summary[0].primary))>0 %} +,primary key({{','.join(summary[0].primary)}}) +{% endif %} +{% endmacro %} +drop table {{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 -%} comment '{{field.title}}'{%- endif %}{%- if not loop.last -%},{%- endif -%} +{% endfor %} +{{primary()}} +) +engine=innodb +default charset=utf8 +{% if summary[0].title %}comment '{{summary[0].title}}'{% endif %} +; +{% for v in validation %} +{% if v.oper=='idx' %} +CREATE {% if v.value.idxtype=='unique' %}UNIQUE{% endif %} INDEX {{summary[0].name}}_{{v.name}} ON {{summary[0].name}}({{",".join(v.value.fields)}}); +{% endif %} +{%- endfor -%} """ \ No newline at end of file diff --git a/sqlor/ddl_template_oracle.py b/sqlor/ddl_template_oracle.py index ff39372..cdc6991 100644 --- a/sqlor/ddl_template_oracle.py +++ b/sqlor/ddl_template_oracle.py @@ -1,49 +1,49 @@ -oracle_ddl_tmpl = """{% macro typeStr(type,len,dec) %} -{%- if type=='str' -%} -VARCHAR2({{len}}) -{%- elif type=='char' -%} -CHAR({{len}}) -{%- elif type=='long' or type=='int' or type=='short' -%} -NUMBER -{%- elif type=='float' or type=='double' or type=='ddouble' -%} -NUMBER({{len}},{{dec}}) -{%- elif type=='date' or type=='time' -%} -DATE -{%- elif type=='timestamp' -%} -TIMESTAMP -{%- elif type=='text' -%} -CLOB -{%- elif type=='bin' -%} -BLOB -{%- else -%} -{{type}} -{%- endif %} -{%- endmacro %} -{% macro nullStr(nullable) %} -{%- if nullable=='no' -%} -NOT NULL -{%- endif -%} -{% endmacro %} -{% macro primary() %} -{% if len(','.join(summary[0].primary))>0 %} -,primary key({{','.join(summary[0].primary)}}) -{% endif %} -{% endmacro %} -drop table {{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 not loop.last -%},{%- endif -%} -{% endfor %} -{{primary()}} -); -{% for v in validation %} -{% if v.oper=='idx' %} -CREATE {% if v.value.idxtype=='unique' %}UNIQUE{% endif %} INDEX {{summary[0].name}}_{{v.name}} ON {{summary[0].name}}({{",".join(v.value.fields)}}); -{% endif %} -{%- endfor -%} -COMMENT ON TABLE {{summary[0].name}} IS '{{summary[0].title}}'; -{% for field in fields %} -COMMENT ON COLUMN {{summary[0].name}}.{{field.name}} is '{{field.title}}'; -{% endfor %} +oracle_ddl_tmpl = """{% macro typeStr(type,len,dec) %} +{%- if type=='str' -%} +VARCHAR2({{len}}) +{%- elif type=='char' -%} +CHAR({{len}}) +{%- elif type=='long' or type=='int' or type=='short' -%} +NUMBER +{%- elif type=='float' or type=='double' or type=='ddouble' -%} +NUMBER({{len}},{{dec}}) +{%- elif type=='date' or type=='time' -%} +DATE +{%- elif type=='timestamp' -%} +TIMESTAMP +{%- elif type=='text' -%} +CLOB +{%- elif type=='bin' -%} +BLOB +{%- else -%} +{{type}} +{%- endif %} +{%- endmacro %} +{% macro nullStr(nullable) %} +{%- if nullable=='no' -%} +NOT NULL +{%- endif -%} +{% endmacro %} +{% macro primary() %} +{% if len(','.join(summary[0].primary))>0 %} +,primary key({{','.join(summary[0].primary)}}) +{% endif %} +{% endmacro %} +drop table {{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 not loop.last -%},{%- endif -%} +{% endfor %} +{{primary()}} +); +{% for v in validation %} +{% if v.oper=='idx' %} +CREATE {% if v.value.idxtype=='unique' %}UNIQUE{% endif %} INDEX {{summary[0].name}}_{{v.name}} ON {{summary[0].name}}({{",".join(v.value.fields)}}); +{% endif %} +{%- endfor -%} +COMMENT ON TABLE {{summary[0].name}} IS '{{summary[0].title}}'; +{% for field in fields %} +COMMENT ON COLUMN {{summary[0].name}}.{{field.name}} is '{{field.title}}'; +{% endfor %} """ \ No newline at end of file diff --git a/sqlor/ddl_template_postgresql.py b/sqlor/ddl_template_postgresql.py index 6b17592..407dde6 100644 --- a/sqlor/ddl_template_postgresql.py +++ b/sqlor/ddl_template_postgresql.py @@ -1,45 +1,45 @@ -postgresql_ddl_tmpl = """{% macro typeStr(type,len,dec) %} -{%- if type=='str' -%} -VARCHAR({{len}}) -{%- elif type=='char' -%} -CHAR({{len}}) -{%- elif type=='long' or type=='int' or type=='short' -%} -INTEGER -{%- elif type=='float' or type=='double' or type=='ddouble' -%} -NUMERIC({{len}},{{dec}}) -{%- elif type=='date' -%} -DATE -{%- elif type=='time' -%} -TIME -{%- elif type=='timestamp' -%} -TIMESTAMP -{%- 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)}}, -{% endfor %} -{{primary()}} -); -{% for v in validation %} -{% if v.oper=='idx' %} -CREATE {% if v.value.idxtype=='unique' %}UNIQUE{% endif %} INDEX {{summary[0].name}}_{{v.name}} ON {{summary[0].name}}({{",".join(v.value.fields)}}); -{% endif %} -{%- endfor -%} -COMMENT ON TABLE {{summary[0].name}} IS '{{summary[0].title.decode('utf8')}}'; -{% for field in fields %} -COMMENT ON COLUMN {{summary[0].name}}.{{field.name}} is '{{field.title.decode('utf8')}}'; -{% endfor %} +postgresql_ddl_tmpl = """{% macro typeStr(type,len,dec) %} +{%- if type=='str' -%} +VARCHAR({{len}}) +{%- elif type=='char' -%} +CHAR({{len}}) +{%- elif type=='long' or type=='int' or type=='short' -%} +INTEGER +{%- elif type=='float' or type=='double' or type=='ddouble' -%} +NUMERIC({{len}},{{dec}}) +{%- elif type=='date' -%} +DATE +{%- elif type=='time' -%} +TIME +{%- elif type=='timestamp' -%} +TIMESTAMP +{%- 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)}}, +{% endfor %} +{{primary()}} +); +{% for v in validation %} +{% if v.oper=='idx' %} +CREATE {% if v.value.idxtype=='unique' %}UNIQUE{% endif %} INDEX {{summary[0].name}}_{{v.name}} ON {{summary[0].name}}({{",".join(v.value.fields)}}); +{% endif %} +{%- endfor -%} +COMMENT ON TABLE {{summary[0].name}} IS '{{summary[0].title.decode('utf8')}}'; +{% for field in fields %} +COMMENT ON COLUMN {{summary[0].name}}.{{field.name}} is '{{field.title.decode('utf8')}}'; +{% endfor %} """ \ No newline at end of file diff --git a/sqlor/ddl_template_sqlserver.py b/sqlor/ddl_template_sqlserver.py index 49e73c7..9861f8f 100644 --- a/sqlor/ddl_template_sqlserver.py +++ b/sqlor/ddl_template_sqlserver.py @@ -1,51 +1,51 @@ -sqlserver_ddl_tmpl = """{% macro typeStr(type,len,dec) %} -{%- if type=='str' -%} -NVARCHAR({{len}}) -{%- elif type=='char' -%} -CHAR({{len}}) -{%- elif type=='long' or type=='int' or type=='short' -%} -NUMERIC -{%- elif type=='float' or type=='double' or type=='ddouble' -%} -numeric({{len}},{{dec}}) -{%- elif type=='date' or type=='time' -%} -DATE -{%- elif type=='timestamp' -%} -TIMESTAMP -{%- elif type=='text' -%} -NVARCHAR(MAX) -{%- elif type=='bin' -%} -IMAGE -{%- else -%} -{{type}} -{%- endif %} -{%- endmacro %} -{% macro nullStr(nullable) %} -{%- if nullable=='no' -%} -NOT NULL -{%- endif -%} -{% endmacro %} - -{% macro primary() %} -{% if len(','.join(summary[0].primary))>0 %} -,primary key({{','.join(summary[0].primary)}}) -{% endif %} -{% endmacro %} - -drop table dbo.{{summary[0].name}}; -CREATE TABLE dbo.{{summary[0].name}} -( -{% for field in fields %} - {{field.name}} {{typeStr(field.type,field.length,field.dec)}} {{nullStr(field.nullable)}}{%- if not loop.last -%},{%- endif -%} -{% endfor %} -{{primary()}} -) -{% for v in validation %} -{% if v.oper=='idx' %} -CREATE {% if v.value.idxtype=='unique' %}UNIQUE{% endif %} INDEX {{summary[0].name}}_{{v.name}} ON {{summary[0].name}}({{",".join(v.value.fields)}}); -{% endif %} -{%- endfor -%} -EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'{{summary[0].title}}' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'{{summary[0].name}}' -{% for field in fields %} -EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'{{field.title}}' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'{{summary[0].name}}', @level2type=N'COLUMN',@level2name=N'{{field.name}}' -{% endfor %} +sqlserver_ddl_tmpl = """{% macro typeStr(type,len,dec) %} +{%- if type=='str' -%} +NVARCHAR({{len}}) +{%- elif type=='char' -%} +CHAR({{len}}) +{%- elif type=='long' or type=='int' or type=='short' -%} +NUMERIC +{%- elif type=='float' or type=='double' or type=='ddouble' -%} +numeric({{len}},{{dec}}) +{%- elif type=='date' or type=='time' -%} +DATE +{%- elif type=='timestamp' -%} +TIMESTAMP +{%- elif type=='text' -%} +NVARCHAR(MAX) +{%- elif type=='bin' -%} +IMAGE +{%- else -%} +{{type}} +{%- endif %} +{%- endmacro %} +{% macro nullStr(nullable) %} +{%- if nullable=='no' -%} +NOT NULL +{%- endif -%} +{% endmacro %} + +{% macro primary() %} +{% if len(','.join(summary[0].primary))>0 %} +,primary key({{','.join(summary[0].primary)}}) +{% endif %} +{% endmacro %} + +drop table dbo.{{summary[0].name}}; +CREATE TABLE dbo.{{summary[0].name}} +( +{% for field in fields %} + {{field.name}} {{typeStr(field.type,field.length,field.dec)}} {{nullStr(field.nullable)}}{%- if not loop.last -%},{%- endif -%} +{% endfor %} +{{primary()}} +) +{% for v in validation %} +{% if v.oper=='idx' %} +CREATE {% if v.value.idxtype=='unique' %}UNIQUE{% endif %} INDEX {{summary[0].name}}_{{v.name}} ON {{summary[0].name}}({{",".join(v.value.fields)}}); +{% endif %} +{%- endfor -%} +EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'{{summary[0].title}}' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'{{summary[0].name}}' +{% for field in fields %} +EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'{{field.title}}' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'{{summary[0].name}}', @level2type=N'COLUMN',@level2name=N'{{field.name}}' +{% endfor %} """ \ No newline at end of file diff --git a/sqlor/mssqlor.py b/sqlor/mssqlor.py index b527a1a..93bcfe0 100644 --- a/sqlor/mssqlor.py +++ b/sqlor/mssqlor.py @@ -1,175 +1,175 @@ -# -*- coding:utf8 -*- -from .sor import SQLor -from .ddl_template_sqlserver import sqlserver_ddl_tmpl - -class MsSqlor(SQLor): - ddl_template = sqlserver_ddl_tmpl - db2modelTypeMapping = { - 'bit':'short', - 'tinyint':'short', - 'date':'date', - 'bigint':'long', - 'smallint':'short', - 'int':'long', - 'decimal':'float', - 'numeric':'float', - 'smallmoney':'float', - 'money':'float', - 'real':'float', - 'float':'float', - 'datetime':'date', - 'timestamp':'timestamp', - 'uniqueidentifier':'timestamp', - 'char':'char', - 'varchar':'str', - 'text':'text', - 'nchar':'str', - 'nvarchar':'str', - 'ntext':'text', - 'binary':'str', - 'varbinary':'str', - 'image':'file', - } - model2dbTypemapping = { - 'date':'datetime', - 'time':'date', - 'timestamp':'timestamp', - 'str':'nvarchar', - 'char':'char', - 'short':'int', - 'long':'numeric', - 'float':'numeric', - 'text':'ntext', - 'file':'image', - } - @classmethod - def isMe(self,name): - return name=='pymssql' - - def grammar(self): - return { - 'select':select_stmt, - } - - def placeHolder(self,varname): - if varname=='__mainsql__' : - return '' - return '%s' - - def dataConvert(self,dataList): - if type(dataList) == type({}): - d = [ i for i in dataList.values()] - else: - d = [ i['value'] for i in dataList] - return tuple(d) - - def pagingSQLmodel(self): - return u"""select * -from ( - select row_number() over(order by $[sort]$ $[order]$) as _row_id,page_s.* - from (%s) page_s - ) A -where _row_id >= $[from_line]$ and _row_id < $[end_line]$""" - - def tablesSQL(self): - sqlcmd = u"""select - lower(d.name) as name, - lower(cast(Isnull(f.VALUE,d.name) as nvarchar )) title - from sysobjects d - left join sys.extended_properties f on d.id = f.major_id and f.minor_id = 0 - where d.xtype = 'U'""" - return sqlcmd - - def fieldsSQL(self,tablename=None): - sqlcmd=u"""SELECT name = lower(a.name) - ,type = b.name - ,length = Columnproperty(a.id,a.name,'PRECISION') - ,dec = Isnull(Columnproperty(a.id,a.name,'Scale'),null) - ,nullable = CASE - WHEN a.isnullable = 1 THEN 'yes' - ELSE 'no' - END - ,title = lower(cast(Isnull(g.[value],a.name) as nvarchar) ) - ,table_name = lower(d.name) - FROM syscolumns a - LEFT JOIN systypes b - ON a.xusertype = b.xusertype - INNER JOIN sysobjects d - ON (a.id = d.id) - AND (d.xtype = 'U') - AND (d.name <> 'dtproperties') - INNER JOIN sys.all_objects c - ON d.id=c.object_id - AND schema_name(schema_id)='dbo' - LEFT JOIN sys.extended_properties g - ON (a.id = g.major_id) - AND (a.colid = g.minor_id) - LEFT JOIN sys.extended_properties f - ON (d.id = f.major_id) - AND (f.minor_id = 0)""" - if tablename is not None: - sqlcmd = sqlcmd + """ where lower(d.name)='%s' - ORDER BY a.id,a.colorder""" % tablename.lower() - else: - sqlcmd = sqlcmd + """ ORDER BY a.id,a.colorder""" - return sqlcmd - - def fkSQL(self,tablename=None): - sqlcmd = u"""select - MainCol.name AS field -- [主表列名] - ,oSub.name AS fk_table -- [子表名称], - ,SubCol.name AS fk_field -- [子表列名], -from - sys.foreign_keys fk - JOIN sys.all_objects oSub - ON (fk.parent_object_id = oSub.object_id) - JOIN sys.all_objects oMain - ON (fk.referenced_object_id = oMain.object_id) - JOIN sys.foreign_key_columns fkCols - ON (fk.object_id = fkCols.constraint_object_id) - JOIN sys.columns SubCol - ON (oSub.object_id = SubCol.object_id - AND fkCols.parent_column_id = SubCol.column_id) - JOIN sys.columns MainCol - ON (oMain.object_id = MainCol.object_id - AND fkCols.referenced_column_id = MainCol.column_id)""" - if tablename is not None: - sqlcmd = sqlcmd + """ where lower(oMain.name) = '%s'""" % tablename.lower() - - return sqlcmd - - def pkSQL(self,tablename=None): - sqlcmd = u"""select - lower(a.table_name) as table_name, - lower(b.column_name) as field_name - from information_schema.table_constraints a - inner join information_schema.constraint_column_usage b - on a.constraint_name = b.constraint_name - where a.constraint_type = 'PRIMARY KEY'""" - if tablename is not None: - sqlcmd = sqlcmd + """ and lower(a.table_name) = '%s'""" % tablename.lower() - return sqlcmd - - def indexesSQL(self,tablename=None): - sqlcmd = """SELECT -index_name=lower(IDX.Name), -index_type=case when KC.type_desc is null then 'primary' WHEN IDX.is_unique=1 THEN 'unique' ELSE 'ununique' END, -table_name=lower(O.Name), -column_name=lower(C.Name) -FROM sys.indexes IDX -INNER JOIN sys.index_columns IDXC -ON IDX.[object_id]=IDXC.[object_id] -AND IDX.index_id=IDXC.index_id -LEFT JOIN sys.key_constraints KC -ON IDX.[object_id]=KC.[parent_object_id] -AND IDX.index_id=KC.unique_index_id -INNER JOIN sys.objects O -ON O.[object_id]=IDX.[object_id] -INNER JOIN sys.columns C -ON O.[object_id]=C.[object_id] -AND O.type='U' -AND O.is_ms_shipped=0 -AND IDXC.Column_id=C.Column_id""" - if tablename is not None: - sqlcmd = sqlcmd + """ where lower(O.name)='%s'""" % tablename.lower() - return sqlcmd +# -*- coding:utf8 -*- +from .sor import SQLor +from .ddl_template_sqlserver import sqlserver_ddl_tmpl + +class MsSqlor(SQLor): + ddl_template = sqlserver_ddl_tmpl + db2modelTypeMapping = { + 'bit':'short', + 'tinyint':'short', + 'date':'date', + 'bigint':'long', + 'smallint':'short', + 'int':'long', + 'decimal':'float', + 'numeric':'float', + 'smallmoney':'float', + 'money':'float', + 'real':'float', + 'float':'float', + 'datetime':'date', + 'timestamp':'timestamp', + 'uniqueidentifier':'timestamp', + 'char':'char', + 'varchar':'str', + 'text':'text', + 'nchar':'str', + 'nvarchar':'str', + 'ntext':'text', + 'binary':'str', + 'varbinary':'str', + 'image':'file', + } + model2dbTypemapping = { + 'date':'datetime', + 'time':'date', + 'timestamp':'timestamp', + 'str':'nvarchar', + 'char':'char', + 'short':'int', + 'long':'numeric', + 'float':'numeric', + 'text':'ntext', + 'file':'image', + } + @classmethod + def isMe(self,name): + return name=='pymssql' + + def grammar(self): + return { + 'select':select_stmt, + } + + def placeHolder(self,varname): + if varname=='__mainsql__' : + return '' + return '%s' + + def dataConvert(self,dataList): + if type(dataList) == type({}): + d = [ i for i in dataList.values()] + else: + d = [ i['value'] for i in dataList] + return tuple(d) + + def pagingSQLmodel(self): + return u"""select * +from ( + select row_number() over(order by $[sort]$ $[order]$) as _row_id,page_s.* + from (%s) page_s + ) A +where _row_id >= $[from_line]$ and _row_id < $[end_line]$""" + + def tablesSQL(self): + sqlcmd = u"""select + lower(d.name) as name, + lower(cast(Isnull(f.VALUE,d.name) as nvarchar )) title + from sysobjects d + left join sys.extended_properties f on d.id = f.major_id and f.minor_id = 0 + where d.xtype = 'U'""" + return sqlcmd + + def fieldsSQL(self,tablename=None): + sqlcmd=u"""SELECT name = lower(a.name) + ,type = b.name + ,length = Columnproperty(a.id,a.name,'PRECISION') + ,dec = Isnull(Columnproperty(a.id,a.name,'Scale'),null) + ,nullable = CASE + WHEN a.isnullable = 1 THEN 'yes' + ELSE 'no' + END + ,title = lower(cast(Isnull(g.[value],a.name) as nvarchar) ) + ,table_name = lower(d.name) + FROM syscolumns a + LEFT JOIN systypes b + ON a.xusertype = b.xusertype + INNER JOIN sysobjects d + ON (a.id = d.id) + AND (d.xtype = 'U') + AND (d.name <> 'dtproperties') + INNER JOIN sys.all_objects c + ON d.id=c.object_id + AND schema_name(schema_id)='dbo' + LEFT JOIN sys.extended_properties g + ON (a.id = g.major_id) + AND (a.colid = g.minor_id) + LEFT JOIN sys.extended_properties f + ON (d.id = f.major_id) + AND (f.minor_id = 0)""" + if tablename is not None: + sqlcmd = sqlcmd + """ where lower(d.name)='%s' + ORDER BY a.id,a.colorder""" % tablename.lower() + else: + sqlcmd = sqlcmd + """ ORDER BY a.id,a.colorder""" + return sqlcmd + + def fkSQL(self,tablename=None): + sqlcmd = u"""select + MainCol.name AS field -- [主表列名] + ,oSub.name AS fk_table -- [子表名称], + ,SubCol.name AS fk_field -- [子表列名], +from + sys.foreign_keys fk + JOIN sys.all_objects oSub + ON (fk.parent_object_id = oSub.object_id) + JOIN sys.all_objects oMain + ON (fk.referenced_object_id = oMain.object_id) + JOIN sys.foreign_key_columns fkCols + ON (fk.object_id = fkCols.constraint_object_id) + JOIN sys.columns SubCol + ON (oSub.object_id = SubCol.object_id + AND fkCols.parent_column_id = SubCol.column_id) + JOIN sys.columns MainCol + ON (oMain.object_id = MainCol.object_id + AND fkCols.referenced_column_id = MainCol.column_id)""" + if tablename is not None: + sqlcmd = sqlcmd + """ where lower(oMain.name) = '%s'""" % tablename.lower() + + return sqlcmd + + def pkSQL(self,tablename=None): + sqlcmd = u"""select + lower(a.table_name) as table_name, + lower(b.column_name) as field_name + from information_schema.table_constraints a + inner join information_schema.constraint_column_usage b + on a.constraint_name = b.constraint_name + where a.constraint_type = 'PRIMARY KEY'""" + if tablename is not None: + sqlcmd = sqlcmd + """ and lower(a.table_name) = '%s'""" % tablename.lower() + return sqlcmd + + def indexesSQL(self,tablename=None): + sqlcmd = """SELECT +index_name=lower(IDX.Name), +index_type=case when KC.type_desc is null then 'primary' WHEN IDX.is_unique=1 THEN 'unique' ELSE 'ununique' END, +table_name=lower(O.Name), +column_name=lower(C.Name) +FROM sys.indexes IDX +INNER JOIN sys.index_columns IDXC +ON IDX.[object_id]=IDXC.[object_id] +AND IDX.index_id=IDXC.index_id +LEFT JOIN sys.key_constraints KC +ON IDX.[object_id]=KC.[parent_object_id] +AND IDX.index_id=KC.unique_index_id +INNER JOIN sys.objects O +ON O.[object_id]=IDX.[object_id] +INNER JOIN sys.columns C +ON O.[object_id]=C.[object_id] +AND O.type='U' +AND O.is_ms_shipped=0 +AND IDXC.Column_id=C.Column_id""" + if tablename is not None: + sqlcmd = sqlcmd + """ where lower(O.name)='%s'""" % tablename.lower() + return sqlcmd diff --git a/sqlor/mysqlor.py b/sqlor/mysqlor.py index e5df373..95bc357 100644 --- a/sqlor/mysqlor.py +++ b/sqlor/mysqlor.py @@ -1,179 +1,179 @@ -# -*- coding:utf8 -*- -from mysql import connector -from appPublic.argsConvert import ArgsConvert,ConditionConvert +# -*- coding:utf8 -*- +from mysql import connector +from appPublic.argsConvert import ArgsConvert,ConditionConvert -from .sor import SQLor -from .ddl_template_mysql import mysql_ddl_tmpl -class MySqlor(SQLor): - ddl_template = mysql_ddl_tmpl - db2modelTypeMapping = { - 'tinyint':'short', - 'smallint':'short', - 'mediumint':'long', - 'int':'long', - 'bigint':'long', - 'decimal':'float', - 'double':'float', - 'float':'float', - 'char':'char', - 'varchar':'str', - 'tinyblob':'text', - 'tinytext':'text', - 'mediumblob':'text', - 'mediumtext':'text', - 'blob':'text', - 'text':'text', - 'mediumblob':'text', - 'mediumtext':'text', - 'longblob':'bin', - 'longtext':'text', - 'barbinary':'text', - 'binary':'text', - 'date':'date', - 'time':'time', - 'datetime':'datetime', - 'timestamp':'datestamp', - 'year':'short', - } - model2dbTypemapping = { - 'date':'date', - 'time':'date', - 'timestamp':'timestamp', - 'str':'varchar', - 'char':'char', - 'short':'int', - 'long':'bigint', - 'float':'double', - 'text':'longtext', - 'bin':'longblob', - 'file':'longblob', - } - @classmethod - def isMe(self,name): - return name=='mysql.connector' - - def grammar(self): - return { - 'select':select_stmt, - } - - def _opendb(self): - self.conn = connector.connect(**self.dbdesc['kwargs']) - - def placeHolder(self,varname): - if varname=='__mainsql__' : - return '' - return '%s' - - def dataConvert(self,dataList): - if type(dataList) == type({}): - d = [ i for i in dataList.values()] - else: - d = [ i['value'] for i in dataList] - return tuple(d) - - def pagingSQL(self,sql,paging,NS): - """ - default it not support paging - """ - page = int(NS.get(paging['pagename'],1)) - rows = int(NS.get(paging['rowsname'],10)) - sort = NS.get(paging.get('sortname','sort'),None) - order = NS.get(paging.get('ordername','asc'),'asc') - if not sort: - return sql - if page < 1: - page = 1 - from_line = (page - 1) * rows - end_line = page * rows + 1 - psql = self.pagingSQLmodel() - ns={ - 'from_line':from_line, - 'end_line':end_line, - 'rows':rows, - 'sort':sort, - 'order':order, - } - ac = ArgsConvert('$[',']$') - psql = ac.convert(psql,ns) - retSQL=psql % sql - return retSQL - - def pagingSQLmodel(self): - return u"""select * from (%s) A order by $[sort]$ $[order]$ -limit $[from_line]$,$[rows]$""" - - def tablesSQL(self): - sqlcmd = """SELECT lower(TABLE_NAME) as name, lower(TABLE_COMMENT) as title FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%s'""" % self.dbdesc.get('dbname','unknown') - return sqlcmd - - def fieldsSQL(self,tablename=None): - sqlcmd=""" - select - lower(column_name) as name, - data_type as type, - case when character_maximum_length is null then NUMERIC_PRECISION - else character_maximum_length end - as length, - NUMERIC_SCALE as dec1, - lower(is_nullable) as nullable, - column_comment as title, - lower(table_name) as table_name - from information_schema.columns where lower(TABLE_SCHEMA) = '%s' """ % self.dbdesc.get('dbname','unknown').lower() - if tablename is not None: - sqlcmd = sqlcmd + """and lower(table_name)='%s';""" % tablename.lower() - return sqlcmd - - def fkSQL(self,tablename=None): - sqlcmd = """SELECT C.TABLE_SCHEMA 拥有者, - C.REFERENCED_TABLE_NAME 父表名称 , - C.REFERENCED_COLUMN_NAME 父表字段 , - C.TABLE_NAME 子表名称, - C.COLUMN_NAME 子表字段, - C.CONSTRAINT_NAME 约束名, - T.TABLE_COMMENT 表注释, - R.UPDATE_RULE 约束更新规则, - R.DELETE_RULE 约束删除规则 - FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE C - JOIN INFORMATION_SCHEMA. TABLES T - ON T.TABLE_NAME = C.TABLE_NAME - JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS R - ON R.TABLE_NAME = C.TABLE_NAME - AND R.CONSTRAINT_NAME = C.CONSTRAINT_NAME - AND R.REFERENCED_TABLE_NAME = C.REFERENCED_TABLE_NAME - WHERE C.REFERENCED_TABLE_NAME IS NOT NULL ; - and C.TABLE_SCHEMA = '%s' -""" % self.dbdesc.get('dbname','unknown').lower() - if tablename is not None: - sqlcmd = sqlcmd + " and C.REFERENCED_TABLE_NAME = '%s'" % tablename.lower() - return sqlcmd - - def pkSQL(self,tablename=None): - sqlcmd = """SELECT - lower(c.table_name) as table_name, - lower(c.COLUMN_NAME) as field_name -FROM - INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t, - INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c -WHERE - t.CONSTRAINT_TYPE = 'PRIMARY KEY' - AND t.TABLE_SCHEMA = '%s' - AND t.TABLE_NAME = c.TABLE_NAME -""" % self.dbdesc.get('dbname','unknown').lower() - if tablename is not None: - sqlcmd = sqlcmd + " AND c.TABLE_NAME = '%s'" % tablename.lower() - return sqlcmd - - def indexesSQL(self,tablename=None): - sqlcmd = """SELECT DISTINCT - lower(index_name) as index_name, - lower(index_type) as index_type, - lower(table_name) as table_name, - lower(column_name) as column_name -FROM - information_schema.statistics -WHERE - table_schema = '%s'""" % self.dbdesc.get('dbname','unknown') - if tablename is not None: - sqlcmd = sqlcmd + """ AND table_name = '%s'""" % tablename.lower() - return sqlcmd +from .sor import SQLor +from .ddl_template_mysql import mysql_ddl_tmpl +class MySqlor(SQLor): + ddl_template = mysql_ddl_tmpl + db2modelTypeMapping = { + 'tinyint':'short', + 'smallint':'short', + 'mediumint':'long', + 'int':'long', + 'bigint':'long', + 'decimal':'float', + 'double':'float', + 'float':'float', + 'char':'char', + 'varchar':'str', + 'tinyblob':'text', + 'tinytext':'text', + 'mediumblob':'text', + 'mediumtext':'text', + 'blob':'text', + 'text':'text', + 'mediumblob':'text', + 'mediumtext':'text', + 'longblob':'bin', + 'longtext':'text', + 'barbinary':'text', + 'binary':'text', + 'date':'date', + 'time':'time', + 'datetime':'datetime', + 'timestamp':'datestamp', + 'year':'short', + } + model2dbTypemapping = { + 'date':'date', + 'time':'date', + 'timestamp':'timestamp', + 'str':'varchar', + 'char':'char', + 'short':'int', + 'long':'bigint', + 'float':'double', + 'text':'longtext', + 'bin':'longblob', + 'file':'longblob', + } + @classmethod + def isMe(self,name): + return name=='mysql.connector' + + def grammar(self): + return { + 'select':select_stmt, + } + + def _opendb(self): + self.conn = connector.connect(**self.dbdesc['kwargs']) + + def placeHolder(self,varname): + if varname=='__mainsql__' : + return '' + return '%s' + + def dataConvert(self,dataList): + if type(dataList) == type({}): + d = [ i for i in dataList.values()] + else: + d = [ i['value'] for i in dataList] + return tuple(d) + + def pagingSQL(self,sql,paging,NS): + """ + default it not support paging + """ + page = int(NS.get(paging['pagename'],1)) + rows = int(NS.get(paging['rowsname'],10)) + sort = NS.get(paging.get('sortname','sort'),None) + order = NS.get(paging.get('ordername','asc'),'asc') + if not sort: + return sql + if page < 1: + page = 1 + from_line = (page - 1) * rows + end_line = page * rows + 1 + psql = self.pagingSQLmodel() + ns={ + 'from_line':from_line, + 'end_line':end_line, + 'rows':rows, + 'sort':sort, + 'order':order, + } + ac = ArgsConvert('$[',']$') + psql = ac.convert(psql,ns) + retSQL=psql % sql + return retSQL + + def pagingSQLmodel(self): + return u"""select * from (%s) A order by $[sort]$ $[order]$ +limit $[from_line]$,$[rows]$""" + + def tablesSQL(self): + sqlcmd = """SELECT lower(TABLE_NAME) as name, lower(TABLE_COMMENT) as title FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%s'""" % self.dbdesc.get('dbname','unknown') + return sqlcmd + + def fieldsSQL(self,tablename=None): + sqlcmd=""" + select + lower(column_name) as name, + data_type as type, + case when character_maximum_length is null then NUMERIC_PRECISION + else character_maximum_length end + as length, + NUMERIC_SCALE as dec1, + lower(is_nullable) as nullable, + column_comment as title, + lower(table_name) as table_name + from information_schema.columns where lower(TABLE_SCHEMA) = '%s' """ % self.dbdesc.get('dbname','unknown').lower() + if tablename is not None: + sqlcmd = sqlcmd + """and lower(table_name)='%s';""" % tablename.lower() + return sqlcmd + + def fkSQL(self,tablename=None): + sqlcmd = """SELECT C.TABLE_SCHEMA 拥有者, + C.REFERENCED_TABLE_NAME 父表名称 , + C.REFERENCED_COLUMN_NAME 父表字段 , + C.TABLE_NAME 子表名称, + C.COLUMN_NAME 子表字段, + C.CONSTRAINT_NAME 约束名, + T.TABLE_COMMENT 表注释, + R.UPDATE_RULE 约束更新规则, + R.DELETE_RULE 约束删除规则 + FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE C + JOIN INFORMATION_SCHEMA. TABLES T + ON T.TABLE_NAME = C.TABLE_NAME + JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS R + ON R.TABLE_NAME = C.TABLE_NAME + AND R.CONSTRAINT_NAME = C.CONSTRAINT_NAME + AND R.REFERENCED_TABLE_NAME = C.REFERENCED_TABLE_NAME + WHERE C.REFERENCED_TABLE_NAME IS NOT NULL ; + and C.TABLE_SCHEMA = '%s' +""" % self.dbdesc.get('dbname','unknown').lower() + if tablename is not None: + sqlcmd = sqlcmd + " and C.REFERENCED_TABLE_NAME = '%s'" % tablename.lower() + return sqlcmd + + def pkSQL(self,tablename=None): + sqlcmd = """SELECT + lower(c.table_name) as table_name, + lower(c.COLUMN_NAME) as field_name +FROM + INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t, + INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c +WHERE + t.CONSTRAINT_TYPE = 'PRIMARY KEY' + AND t.TABLE_SCHEMA = '%s' + AND t.TABLE_NAME = c.TABLE_NAME +""" % self.dbdesc.get('dbname','unknown').lower() + if tablename is not None: + sqlcmd = sqlcmd + " AND c.TABLE_NAME = '%s'" % tablename.lower() + return sqlcmd + + def indexesSQL(self,tablename=None): + sqlcmd = """SELECT DISTINCT + lower(index_name) as index_name, + lower(index_type) as index_type, + lower(table_name) as table_name, + lower(column_name) as column_name +FROM + information_schema.statistics +WHERE + table_schema = '%s'""" % self.dbdesc.get('dbname','unknown') + if tablename is not None: + sqlcmd = sqlcmd + """ AND table_name = '%s'""" % tablename.lower() + return sqlcmd diff --git a/sqlor/oracleor.py b/sqlor/oracleor.py index 75ca325..8fa4328 100644 --- a/sqlor/oracleor.py +++ b/sqlor/oracleor.py @@ -1,131 +1,131 @@ -from .sor import SQLor -from .ddl_template_oracle import oracle_ddl_tmpl -class Oracleor(SQLor): - ddl_template = oracle_ddl_tmpl - db2modelTypeMapping = { - 'char':'char', - 'nchar':'str', - 'varchar':'str', - 'varchar2':'str', - 'nvarchar2':'str', - 'number':'long', - 'integer':'long', - 'binary_float':'float', - 'binary_double':'float', - 'float':'float', - 'timestamp':'timestamp', - 'timestamp with time zone':'timestamp', - 'timestamp with local time zone':'timestamp', - 'interval year to moth':'date', - 'interval day to second':'timestamp', - 'clob':'text', - 'nclob':'text', - 'blob':'file', - 'bfile':'file', - 'date':'date', - } - model2dbTypemapping = { - 'date':'date', - 'time':'date', - 'timestamp':'date', - 'str':'varchar2', - 'char':'char', - 'short':'number', - 'long':'number', - 'float':'number', - 'text':'nclob', - 'file':'blob', - } - @classmethod - def isMe(self,name): - return name=='cx_Oracle' - - def grammar(self): - return { - 'select':select_stmt, - } - - def placeHolder(self,varname): - if varname=='__mainsql__' : - return '' - return ':%s' % varname - - def dataConvert(self,dataList): - if type(dataList) == type({}): - return dataList - d = {} - [ d.update({i['name']:i['value']}) for i in dataList ] - return d - - def pagingSQLmodel(self): - return u"""select * -from ( - select page_s.*,rownum row_id - from (%s) page_s - order by $[sort]$ $[order]$ - ) -where row_id >=$[from_line]$ and row_id < $[end_line]$""" - - def tablesSQL(self): - sqlcmd = """select -lower(table_name) as name, -lower(decode(comments,null,table_name,comments)) as title -from USER_TAB_COMMENTS where table_type = 'TABLE'""" - return sqlcmd - - def fieldsSQL(self,tablename=None): - sqlcmd="""select lower(utc.COLUMN_NAME) name - ,utc.DATA_TYPE type - ,utc.DATA_LENGTH length - ,utc.data_scale dec - ,case when utc.nullable = 'Y' then 'yes' else 'no' end nullable - ,lower(nvl(ucc.comments,utc.COLUMN_NAME)) title - ,lower(utc.table_name) as table_name - from user_tab_cols utc left join USER_COL_COMMENTS ucc on utc.table_name = ucc.table_name and utc.COLUMN_NAME = ucc.COLUMN_NAME""" - if tablename is not None: - sqlcmd = sqlcmd + """ where lower(utc.table_name) = '%s'""" % tablename.lower() - return sqlcmd - - def fkSQL(self,tablename=None): - tablename = tablename.lower() - sqlcmd = """select - distinct(ucc.column_name) as field,rela.table_name as fk_table,rela.column_name as fk_field -from - user_constraints uc,user_cons_columns ucc, - ( - select t2.table_name,t2.column_name,t1.r_constraint_name - from user_constraints t1,user_cons_columns t2 - where t1.r_constraint_name=t2.constraint_name -) rela -where - uc.constraint_name=ucc.constraint_name - and uc.r_constraint_name=rela.r_constraint_name""" - if tablename is not None: - sqlcmd = sqlcmd + """ and lower(uc.table_name)='%s'""" % tablename.lower() - return sqlcmd - - def pkSQL(self,tablename=None): - sqlcmd = """ -select - lower(col.table_name) table_name, - lower(col.column_name) as field_name -from - user_constraints con,user_cons_columns col -where - con.constraint_name=col.constraint_name and con.constraint_type='P'""" - if tablename is not None: - sqlcmd = sqlcmd + """ and lower(col.table_name)='%s'""" % tablename.lower() - return sqlcmd - - def indexesSQL(self,tablename=None): - sqlcmd = """select - lower(a.index_name) index_name, - lower(a.UNIQUENESS) index_type, - lower(a.table_name) table_name, - lower(b.column_name) column_name -from user_indexes a, user_ind_columns b -where a.index_name = b.index_name""" - if tablename is not None: - sqlcmd += """ and lower(a.table_name) = lower('%s')""" % tablename.lower() - return sqlcmd - +from .sor import SQLor +from .ddl_template_oracle import oracle_ddl_tmpl +class Oracleor(SQLor): + ddl_template = oracle_ddl_tmpl + db2modelTypeMapping = { + 'char':'char', + 'nchar':'str', + 'varchar':'str', + 'varchar2':'str', + 'nvarchar2':'str', + 'number':'long', + 'integer':'long', + 'binary_float':'float', + 'binary_double':'float', + 'float':'float', + 'timestamp':'timestamp', + 'timestamp with time zone':'timestamp', + 'timestamp with local time zone':'timestamp', + 'interval year to moth':'date', + 'interval day to second':'timestamp', + 'clob':'text', + 'nclob':'text', + 'blob':'file', + 'bfile':'file', + 'date':'date', + } + model2dbTypemapping = { + 'date':'date', + 'time':'date', + 'timestamp':'date', + 'str':'varchar2', + 'char':'char', + 'short':'number', + 'long':'number', + 'float':'number', + 'text':'nclob', + 'file':'blob', + } + @classmethod + def isMe(self,name): + return name=='cx_Oracle' + + def grammar(self): + return { + 'select':select_stmt, + } + + def placeHolder(self,varname): + if varname=='__mainsql__' : + return '' + return ':%s' % varname + + def dataConvert(self,dataList): + if type(dataList) == type({}): + return dataList + d = {} + [ d.update({i['name']:i['value']}) for i in dataList ] + return d + + def pagingSQLmodel(self): + return u"""select * +from ( + select page_s.*,rownum row_id + from (%s) page_s + order by $[sort]$ $[order]$ + ) +where row_id >=$[from_line]$ and row_id < $[end_line]$""" + + def tablesSQL(self): + sqlcmd = """select +lower(table_name) as name, +lower(decode(comments,null,table_name,comments)) as title +from USER_TAB_COMMENTS where table_type = 'TABLE'""" + return sqlcmd + + def fieldsSQL(self,tablename=None): + sqlcmd="""select lower(utc.COLUMN_NAME) name + ,utc.DATA_TYPE type + ,utc.DATA_LENGTH length + ,utc.data_scale dec + ,case when utc.nullable = 'Y' then 'yes' else 'no' end nullable + ,lower(nvl(ucc.comments,utc.COLUMN_NAME)) title + ,lower(utc.table_name) as table_name + from user_tab_cols utc left join USER_COL_COMMENTS ucc on utc.table_name = ucc.table_name and utc.COLUMN_NAME = ucc.COLUMN_NAME""" + if tablename is not None: + sqlcmd = sqlcmd + """ where lower(utc.table_name) = '%s'""" % tablename.lower() + return sqlcmd + + def fkSQL(self,tablename=None): + tablename = tablename.lower() + sqlcmd = """select + distinct(ucc.column_name) as field,rela.table_name as fk_table,rela.column_name as fk_field +from + user_constraints uc,user_cons_columns ucc, + ( + select t2.table_name,t2.column_name,t1.r_constraint_name + from user_constraints t1,user_cons_columns t2 + where t1.r_constraint_name=t2.constraint_name +) rela +where + uc.constraint_name=ucc.constraint_name + and uc.r_constraint_name=rela.r_constraint_name""" + if tablename is not None: + sqlcmd = sqlcmd + """ and lower(uc.table_name)='%s'""" % tablename.lower() + return sqlcmd + + def pkSQL(self,tablename=None): + sqlcmd = """ +select + lower(col.table_name) table_name, + lower(col.column_name) as field_name +from + user_constraints con,user_cons_columns col +where + con.constraint_name=col.constraint_name and con.constraint_type='P'""" + if tablename is not None: + sqlcmd = sqlcmd + """ and lower(col.table_name)='%s'""" % tablename.lower() + return sqlcmd + + def indexesSQL(self,tablename=None): + sqlcmd = """select + lower(a.index_name) index_name, + lower(a.UNIQUENESS) index_type, + lower(a.table_name) table_name, + lower(b.column_name) column_name +from user_indexes a, user_ind_columns b +where a.index_name = b.index_name""" + if tablename is not None: + sqlcmd += """ and lower(a.table_name) = lower('%s')""" % tablename.lower() + return sqlcmd + diff --git a/sqlor/postgresqlor.py b/sqlor/postgresqlor.py index 0fd3033..e758cd3 100644 --- a/sqlor/postgresqlor.py +++ b/sqlor/postgresqlor.py @@ -1,199 +1,199 @@ -from .sor import SQLor -from .ddl_template_postgresql import postgresql_ddl_tmpl - -class PostgreSQLor(SQLor): - ddl_template = postgresql_ddl_tmpl - db2modelTypeMapping = { - 'smallint':'short', - 'integer':'long', - 'bigint':'llong', - 'decimal':'float', - 'numeric':'float', - 'real':'float', - 'double':'float', - 'serial':'long', - 'bigserial':'llong', - 'char':'char', - 'character':'char', - 'varchar':'str', - 'character varying':'str', - 'text':'text', - 'timestamp':'timestamp', - 'date':'date', - 'time':'time', - 'boolean':'char', - 'bytea':'file' - } - model2dbTypemapping = { - 'date':'date', - 'time':'date', - 'timestamp':'timestamp', - 'str':'varchar', - 'char':'char', - 'short':'smallint', - 'long':'integer', - 'float':'numeric', - 'text':'text', - 'file':'bytea', - } - @classmethod - def isMe(self,name): - return name=='psycopg2' - - def grammar(self): - return { - 'select':select_stmt, - } - - def placeHolder(self,varname): - if varname=='__mainsql__' : - return '' - return ':%s' % varname - - def dataConvert(self,dataList): - if type(dataList) == type({}): - return dataList - d = {} - [ d.update({i['name']:i['value']}) for i in dataList ] - return d - - def pagingSQLmodel(self): - return u"""select * -from ( - select page_s.*,rownum row_id - from (%s) page_s - order by $[sort]$ $[order]$ - ) -where row_id >=$[from_line]$ and row_id < $[end_line]$""" - - def tablesSQL(self): - """ - 列出表名 - SELECT tablename FROM pg_tables; - WHERE tablename NOT LIKE 'pg%' - AND tablename NOT LIKE 'sql_%' - ORDER BY tablename; - """ - sqlcmd = """select -lower(table_name) as name, -lower(decode(comments,null,table_name,comments)) as title -from USER_TAB_COMMENTS where table_type = 'TABLE'""" - return sqlcmd - - def fieldsSQL(self,tablename=None): - """SELECT col_description(a.attrelid,a.attnum) as comment,pg_type.typname as typename,a.attname as name, a.attnotnull as notnull -FROM pg_class as c,pg_attribute as a inner join pg_type on pg_type.oid = a.atttypid -where c.relname = 'tablename' and a.attrelid = c.oid and a.attnum>0 - """ - sqlcmd="""select lower(utc.COLUMN_NAME) name - ,utc.DATA_TYPE type - ,utc.DATA_LENGTH length - ,utc.data_scale dec - ,case when utc.nullable = 'Y' then 'yes' else 'no' end nullable - ,lower(nvl(ucc.comments,utc.COLUMN_NAME)) title - ,lower(utc.table_name) as table_name - from user_tab_cols utc left join USER_COL_COMMENTS ucc on utc.table_name = ucc.table_name and utc.COLUMN_NAME = ucc.COLUMN_NAME""" - if tablename is not None: - sqlcmd = sqlcmd + """ where lower(utc.table_name) = '%s'""" % tablename.lower() - return sqlcmd - - def fkSQL(self,tablename=None): - tablename = tablename.lower() - sqlcmd = """select - distinct(ucc.column_name) as field,rela.table_name as fk_table,rela.column_name as fk_field -from - user_constraints uc,user_cons_columns ucc, - ( - select t2.table_name,t2.column_name,t1.r_constraint_name - from user_constraints t1,user_cons_columns t2 - where t1.r_constraint_name=t2.constraint_name -) rela -where - uc.constraint_name=ucc.constraint_name - and uc.r_constraint_name=rela.r_constraint_name""" - if tablename is not None: - sqlcmd = sqlcmd + """ and lower(uc.table_name)='%s'""" % tablename.lower() - return sqlcmd - - def pkSQL(self,tablename=None): - """ - select pg_attribute.attname as colname,pg_type.typname as typename,pg_constraint.conname as pk_name from -pg_constraint inner join pg_class -on pg_constraint.conrelid = pg_class.oid -inner join pg_attribute on pg_attribute.attrelid = pg_class.oid -and pg_attribute.attnum = pg_constraint.conkey[1] -inner join pg_type on pg_type.oid = pg_attribute.atttypid -where pg_class.relname = 'tablename' -and pg_constraint.contype='p' - """ - sqlcmd = """ -select - lower(col.table_name) table_name, - lower(col.column_name) as field_name -from - user_constraints con,user_cons_columns col -where - con.constraint_name=col.constraint_name and con.constraint_type='P'""" - if tablename is not None: - sqlcmd = sqlcmd + """ and lower(col.table_name)='%s'""" % tablename.lower() - return sqlcmd - - def indexesSQL(self,tablename=None): - """ -SELECT - -A.SCHEMANAME, - -A.TABLENAME, - -A.INDEXNAME, - -A.TABLESPACE, - -A.INDEXDEF, - -B.AMNAME, - -C.INDEXRELID, - -C.INDNATTS, - -C.INDISUNIQUE, - -C.INDISPRIMARY, - -C.INDISCLUSTERED, - -D.DESCRIPTION - -FROM - -PG_AM B - -LEFT JOIN PG_CLASS F ON B.OID = F.RELAM - -LEFT JOIN PG_STAT_ALL_INDEXES E ON F.OID = E.INDEXRELID - -LEFT JOIN PG_INDEX C ON E.INDEXRELID = C.INDEXRELID - -LEFT OUTER JOIN PG_DESCRIPTION D ON C.INDEXRELID = D.OBJOID, - -PG_INDEXES A - -WHERE - -A.SCHEMANAME = E.SCHEMANAME AND A.TABLENAME = E.RELNAME AND A.INDEXNAME = E.INDEXRELNAME - -AND E.SCHEMANAME = 'public' AND E.RELNAME = 'table_name' -""" - sqlcmd = """select - lower(a.index_name) index_name, - lower(a.UNIQUENESS) index_type, - lower(a.table_name) table_name, - lower(b.column_name) column_name -from user_indexes a, user_ind_columns b -where a.index_name = b.index_name""" - if tablename is not None: - sqlcmd += """ and lower(a.table_name) = lower('%s')""" % tablename.lower() - return sqlcmd - +from .sor import SQLor +from .ddl_template_postgresql import postgresql_ddl_tmpl + +class PostgreSQLor(SQLor): + ddl_template = postgresql_ddl_tmpl + db2modelTypeMapping = { + 'smallint':'short', + 'integer':'long', + 'bigint':'llong', + 'decimal':'float', + 'numeric':'float', + 'real':'float', + 'double':'float', + 'serial':'long', + 'bigserial':'llong', + 'char':'char', + 'character':'char', + 'varchar':'str', + 'character varying':'str', + 'text':'text', + 'timestamp':'timestamp', + 'date':'date', + 'time':'time', + 'boolean':'char', + 'bytea':'file' + } + model2dbTypemapping = { + 'date':'date', + 'time':'date', + 'timestamp':'timestamp', + 'str':'varchar', + 'char':'char', + 'short':'smallint', + 'long':'integer', + 'float':'numeric', + 'text':'text', + 'file':'bytea', + } + @classmethod + def isMe(self,name): + return name=='psycopg2' + + def grammar(self): + return { + 'select':select_stmt, + } + + def placeHolder(self,varname): + if varname=='__mainsql__' : + return '' + return ':%s' % varname + + def dataConvert(self,dataList): + if type(dataList) == type({}): + return dataList + d = {} + [ d.update({i['name']:i['value']}) for i in dataList ] + return d + + def pagingSQLmodel(self): + return u"""select * +from ( + select page_s.*,rownum row_id + from (%s) page_s + order by $[sort]$ $[order]$ + ) +where row_id >=$[from_line]$ and row_id < $[end_line]$""" + + def tablesSQL(self): + """ + 列出表名 + SELECT tablename FROM pg_tables; + WHERE tablename NOT LIKE 'pg%' + AND tablename NOT LIKE 'sql_%' + ORDER BY tablename; + """ + sqlcmd = """select +lower(table_name) as name, +lower(decode(comments,null,table_name,comments)) as title +from USER_TAB_COMMENTS where table_type = 'TABLE'""" + return sqlcmd + + def fieldsSQL(self,tablename=None): + """SELECT col_description(a.attrelid,a.attnum) as comment,pg_type.typname as typename,a.attname as name, a.attnotnull as notnull +FROM pg_class as c,pg_attribute as a inner join pg_type on pg_type.oid = a.atttypid +where c.relname = 'tablename' and a.attrelid = c.oid and a.attnum>0 + """ + sqlcmd="""select lower(utc.COLUMN_NAME) name + ,utc.DATA_TYPE type + ,utc.DATA_LENGTH length + ,utc.data_scale dec + ,case when utc.nullable = 'Y' then 'yes' else 'no' end nullable + ,lower(nvl(ucc.comments,utc.COLUMN_NAME)) title + ,lower(utc.table_name) as table_name + from user_tab_cols utc left join USER_COL_COMMENTS ucc on utc.table_name = ucc.table_name and utc.COLUMN_NAME = ucc.COLUMN_NAME""" + if tablename is not None: + sqlcmd = sqlcmd + """ where lower(utc.table_name) = '%s'""" % tablename.lower() + return sqlcmd + + def fkSQL(self,tablename=None): + tablename = tablename.lower() + sqlcmd = """select + distinct(ucc.column_name) as field,rela.table_name as fk_table,rela.column_name as fk_field +from + user_constraints uc,user_cons_columns ucc, + ( + select t2.table_name,t2.column_name,t1.r_constraint_name + from user_constraints t1,user_cons_columns t2 + where t1.r_constraint_name=t2.constraint_name +) rela +where + uc.constraint_name=ucc.constraint_name + and uc.r_constraint_name=rela.r_constraint_name""" + if tablename is not None: + sqlcmd = sqlcmd + """ and lower(uc.table_name)='%s'""" % tablename.lower() + return sqlcmd + + def pkSQL(self,tablename=None): + """ + select pg_attribute.attname as colname,pg_type.typname as typename,pg_constraint.conname as pk_name from +pg_constraint inner join pg_class +on pg_constraint.conrelid = pg_class.oid +inner join pg_attribute on pg_attribute.attrelid = pg_class.oid +and pg_attribute.attnum = pg_constraint.conkey[1] +inner join pg_type on pg_type.oid = pg_attribute.atttypid +where pg_class.relname = 'tablename' +and pg_constraint.contype='p' + """ + sqlcmd = """ +select + lower(col.table_name) table_name, + lower(col.column_name) as field_name +from + user_constraints con,user_cons_columns col +where + con.constraint_name=col.constraint_name and con.constraint_type='P'""" + if tablename is not None: + sqlcmd = sqlcmd + """ and lower(col.table_name)='%s'""" % tablename.lower() + return sqlcmd + + def indexesSQL(self,tablename=None): + """ +SELECT + +A.SCHEMANAME, + +A.TABLENAME, + +A.INDEXNAME, + +A.TABLESPACE, + +A.INDEXDEF, + +B.AMNAME, + +C.INDEXRELID, + +C.INDNATTS, + +C.INDISUNIQUE, + +C.INDISPRIMARY, + +C.INDISCLUSTERED, + +D.DESCRIPTION + +FROM + +PG_AM B + +LEFT JOIN PG_CLASS F ON B.OID = F.RELAM + +LEFT JOIN PG_STAT_ALL_INDEXES E ON F.OID = E.INDEXRELID + +LEFT JOIN PG_INDEX C ON E.INDEXRELID = C.INDEXRELID + +LEFT OUTER JOIN PG_DESCRIPTION D ON C.INDEXRELID = D.OBJOID, + +PG_INDEXES A + +WHERE + +A.SCHEMANAME = E.SCHEMANAME AND A.TABLENAME = E.RELNAME AND A.INDEXNAME = E.INDEXRELNAME + +AND E.SCHEMANAME = 'public' AND E.RELNAME = 'table_name' +""" + sqlcmd = """select + lower(a.index_name) index_name, + lower(a.UNIQUENESS) index_type, + lower(a.table_name) table_name, + lower(b.column_name) column_name +from user_indexes a, user_ind_columns b +where a.index_name = b.index_name""" + if tablename is not None: + sqlcmd += """ and lower(a.table_name) = lower('%s')""" % tablename.lower() + return sqlcmd + diff --git a/sqlor/sor.py b/sqlor/sor.py index cd80550..9d5a151 100644 --- a/sqlor/sor.py +++ b/sqlor/sor.py @@ -160,14 +160,14 @@ class SQLor(object): markedSQL,datas = self.maskingSQL(sql,NS) datas = self.dataConvert(datas) try: - markedSQL = markedSQL.encode('utf8') + # markedSQL = markedSQL.encode('utf8') if self.async_mode: await cursor.execute(markedSQL,datas) else: cursor.execute(markedSQL,datas) except Exception as e: - print( "markedSQL=",markedSQL,datas,e) + print( "markedSQL=",markedSQL,':',datas,':',e) raise e return diff --git a/sqlor/sqlite3or.py b/sqlor/sqlite3or.py index adb065d..5283012 100644 --- a/sqlor/sqlite3or.py +++ b/sqlor/sqlite3or.py @@ -1,100 +1,100 @@ -import re -from .sor import SQLor - -class SQLite3or(SQLor): - db2modelTypeMapping = { - 'char':'char', - 'nchar':'str', - 'text':'text', - 'ntext':'text', - 'varchar':'str', - 'nvarchar':'str', - 'blob':'file', - 'integer':'long', - 'double':'float', - 'date':'date', - 'time':'time', - 'timestamp':'timestamp', - 'number':'long', - } - model2dbTypemapping = { - 'date':'date', - 'time':'time', - 'timestamp':'timestamp', - 'str':'nvarchar', - 'char':'char', - 'short':'int', - 'long':'integer', - 'float':'double', - 'text':'ntext', - 'file':'blob', - } - @classmethod - def isMe(self,name): - return name=='sqlite3' - - def placeHolder(self,varname): - if varname=='__mainsql__' : - return '' - return '?' - - def dataConvert(self,dataList): - if type(dataList) == type({}): - d = [ i for i in dataList.values()] - else: - d = [ i['value'] for i in dataList] - return tuple(d) - - def pagingSQLmodel(self): - sql = u"""select * from (%s) order by $[sort]$ $[order]$ limit $[from_line]$,$[end_line]$""" - return sql - - def tablesSQL(self): - sqlcmd = u"""select name, tbl_name as title from sqlite_master where upper(type) = 'TABLE'""" - return sqlcmd - - def fieldsSQL(self,tablename): - sqlcmd="""PRAGMA table_info('%s')""" % tablename.lower() - return sqlcmd - - def fields(self,tablename): - m = u'(\w+)\(((\d+)(,(\d+)){0,1})\){0,1}' - k = re.compile(m) - def typesplit(typ): - d = k.search(typ) - if d is None: - return typ,0,0 - - return d.group(1),int(d.group(3) if d.group(3) is not None else 0 ),int(d.group(5) if d.group(5) is not None else 0) - - sqlstring = self.fieldsSQL(tablename) - recs = [] - self.execute(sqlstring,callback=lambda x:recs.append(x)) - for r in recs: - t,l,d = typesplit(r['type']) - r['type'] = t - r['length'] = int(l) - r['dec'] = int(d) - r['title'] = r['name'] - ret = [] - for r in recs: - r.update({'type':self.db2modelTypeMapping[r['type'].lower()]}) - r.update({'name':r['name'].lower()}) - ret.append(r) - return ret - - def fkSQL(self,tablename): - sqlcmd = "" - return sqlcmd - - def fkeys(self,tablename): - return [] - - def primary(self,tablename): - recs = self.fields(tablename) - ret = [ {'field':r['name']} for r in recs if r['pk'] == 1 ] - return ret - - def pkSQL(self,tablename): - sqlcmd = "" - return sqlcmd +import re +from .sor import SQLor + +class SQLite3or(SQLor): + db2modelTypeMapping = { + 'char':'char', + 'nchar':'str', + 'text':'text', + 'ntext':'text', + 'varchar':'str', + 'nvarchar':'str', + 'blob':'file', + 'integer':'long', + 'double':'float', + 'date':'date', + 'time':'time', + 'timestamp':'timestamp', + 'number':'long', + } + model2dbTypemapping = { + 'date':'date', + 'time':'time', + 'timestamp':'timestamp', + 'str':'nvarchar', + 'char':'char', + 'short':'int', + 'long':'integer', + 'float':'double', + 'text':'ntext', + 'file':'blob', + } + @classmethod + def isMe(self,name): + return name=='sqlite3' + + def placeHolder(self,varname): + if varname=='__mainsql__' : + return '' + return '?' + + def dataConvert(self,dataList): + if type(dataList) == type({}): + d = [ i for i in dataList.values()] + else: + d = [ i['value'] for i in dataList] + return tuple(d) + + def pagingSQLmodel(self): + sql = u"""select * from (%s) order by $[sort]$ $[order]$ limit $[from_line]$,$[end_line]$""" + return sql + + def tablesSQL(self): + sqlcmd = u"""select name, tbl_name as title from sqlite_master where upper(type) = 'TABLE'""" + return sqlcmd + + def fieldsSQL(self,tablename): + sqlcmd="""PRAGMA table_info('%s')""" % tablename.lower() + return sqlcmd + + def fields(self,tablename): + m = u'(\w+)\(((\d+)(,(\d+)){0,1})\){0,1}' + k = re.compile(m) + def typesplit(typ): + d = k.search(typ) + if d is None: + return typ,0,0 + + return d.group(1),int(d.group(3) if d.group(3) is not None else 0 ),int(d.group(5) if d.group(5) is not None else 0) + + sqlstring = self.fieldsSQL(tablename) + recs = [] + self.execute(sqlstring,callback=lambda x:recs.append(x)) + for r in recs: + t,l,d = typesplit(r['type']) + r['type'] = t + r['length'] = int(l) + r['dec'] = int(d) + r['title'] = r['name'] + ret = [] + for r in recs: + r.update({'type':self.db2modelTypeMapping[r['type'].lower()]}) + r.update({'name':r['name'].lower()}) + ret.append(r) + return ret + + def fkSQL(self,tablename): + sqlcmd = "" + return sqlcmd + + def fkeys(self,tablename): + return [] + + def primary(self,tablename): + recs = self.fields(tablename) + ret = [ {'field':r['name']} for r in recs if r['pk'] == 1 ] + return ret + + def pkSQL(self,tablename): + sqlcmd = "" + return sqlcmd diff --git a/test/t2.py b/test/t2.py index 9a45d0a..ac09b60 100644 --- a/test/t2.py +++ b/test/t2.py @@ -3,6 +3,18 @@ import asyncio from sqlor.dbpools import DBPools dbs={ + "tasks":{ + "driver":"aiomysql", + "async_mode":True, + "coding":"utf8", + "dbname":"tasks", + "kwargs":{ + "user":"test", + "db":"tasks", + "password":"test123", + "host":"localhost" + } + }, "aiocfae":{ "driver":"aiomysql", "async_mode":True, @@ -39,4 +51,14 @@ async def testfunc(): } x = await sql('cfae',{},print) +async def testfunc1(): + @pool.runSQL + def sql(db,ns,callback): + return { + "sql_string":"select * from timeobjects", + } + print('testfunc1(),test tasks database select') + x = await sql('tasks',{},print) + loop.run_until_complete(testfunc()) +loop.run_until_complete(testfunc1())