关于SQL语句的自动生成!(五)
2008-04-02 07:21
751 查看
作者:PeterXu 来源:Blog.CSDN Blog: http://blog.csdn.net/peterreg/
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。本文地址:http://blog.csdn.net/peterreg/archive/2008/04/02/2243132.aspx
接我的上一篇文章《关于SQL语句的自动生成!(四)》,虽然在DotNet Framework3.5 面世以后,可通过LINQ支持SQL的直接查询,但LINQ只支持SQL系列的,SQL2K,SQL2K5,对于ACCESS,ORACLE,MYSQL等不支持。同时平常的一些工具的创作,对于我们的软件水平也大有好处
下面附上DBACCESS的整套代码,供大家分享
public interface IClause
...{
void Add(string name, object val);
void AddWhere(string name, object val);
void Clear();
string TableName ...{ set;}
}
public abstract class ClauseFactory
...{
public enum Flag
...{
Insert,
Update,
Select,
Delete,
}
Create#region Create
public static IClause Create(string table, Flag category)
...{
BaseClause ret = null;
switch (category)
...{
case Flag.Insert:
ret = new Insert();
break;
case Flag.Update:
ret = new Update();
break;
case Flag.Select:
ret = new Select();
break;
case Flag.Delete:
ret = new Delete();
break;
default:
break;
}
if (ret != null)
...{
ret.TableName = table;
}
return ret;
}
#endregion
BaseClause#region BaseClause
private abstract class BaseClause : IClause
...{
private string m_strTable = string.Empty;
private Where m_oWhere = new Where();
protected BaseClause()
...{
this.m_strTable = string.Empty;
this.Clear();
}
public virtual void Add(string name, object val)
...{
}
public void AddWhere(string name, object val)
...{
this.m_oWhere.Add(name, val);
}
public void Clear()
...{
this.m_oWhere.Clear();
this.auxClear();
}
public string TableName
...{
set ...{ this.m_strTable = value; }
protected get ...{ return this.m_strTable; }
}
public sealed override string ToString()
...{
return this.ToStr + this.m_oWhere.ToString();
}
protected abstract string ToStr ...{ get;}
protected virtual void auxClear() ...{ }
}
#endregion
Insert, Update..Join#region Insert, Update..Join
private class Insert : BaseClause
...{
private string m_strName;
private string m_strValue;
public override void Add(string name, object val)
...{
if (val == null)
...{
this.Add(name, "null", false);
}
else
...{
this.Add(name, val.ToString(), val is string || val is DateTime);
}
}
private void Add(string name, string val, bool isref)
...{
if (isref)
...{
val = "'" + val + "'";
}
if (this.m_strName == string.Empty)
...{
this.m_strName = "[" + name + "]";
this.m_strValue = val;
}
else
...{
this.m_strName += ",[" + name + "]";
this.m_strValue += "," + val;
}
}
protected override string ToStr
...{
get ...{ return "INSERT INTO [" + base.TableName + "] ( " + this.m_strName + " ) values ( " + this.m_strValue + " )"; }
}
protected override void auxClear()
...{
this.m_strValue = string.Empty;
this.m_strName = string.Empty;
}
}
private class Delete : BaseClause
...{
public Delete()
...{
}
protected override string ToStr
...{
get ...{ return "DELETE FROM [" + base.TableName + "]"; }
}
}
private class Update : BaseClause
...{
private string m_strUpdate;
public override void Add(string name, object val)
...{
if (val == null)
...{
this.Add(name, "null", false);
}
else
...{
this.Add(name, val.ToString(), val is string || val is DateTime);
}
}
private void Add(string name, string val, bool isref)
...{
if (isref)
...{
val = "'" + val + "'";
}
if (this.m_strUpdate == string.Empty)
...{
this.m_strUpdate = "[" + name + "]=" + val;
}
else
...{
this.m_strUpdate += ",[" + name + "]=" + val;
}
}
protected override string ToStr
...{
get ...{ return "UPDATE [" + base.TableName + "] SET " + this.m_strUpdate; }
}
protected override void auxClear()
...{
this.m_strUpdate = string.Empty;
}
}
private class Select : BaseClause
...{
private string m_strSelect;
public override void Add(string name, object alis)
...{
if (alis == null)
...{
this.Add(name, name, false);
}
else
...{
this.Add(name, alis.ToString(), false);
}
}
private void Add(string name, string alis, bool isref)
...{
if (this.m_strSelect == string.Empty)
...{
this.m_strSelect = "[" + name + "] as [" + alis + "]";
}
else
...{
this.m_strSelect += ",[" + name + "] as [" + alis + "]";
}
}
protected override string ToStr
...{
get
...{
if (this.m_strSelect == string.Empty)
...{
this.m_strSelect = "*";
}
return "SELECT " + this.m_strSelect + " FROM [" + base.TableName + "]";
}
}
protected override void auxClear()
...{
this.m_strSelect = string.Empty;
}
}
private class Where
...{
private string m_strWhere;
public Where()
...{
this.Clear();
}
public void Add(string name, object val)
...{
if (val == null)
...{
this.Add(name, "null", false);
}
else
...{
this.Add(name, val.ToString(), val is string || val is DateTime);
}
}
private void Add(string name, string val, bool isref)
...{
if (isref)
...{
val = "'" + val + "'";
}
if (this.m_strWhere == string.Empty)
...{
this.m_strWhere = "[" + name + "]=" + val;
}
else
...{
this.m_strWhere += " and [" + name + "]=" + val;
}
}
public override string ToString()
...{
string strRet = string.Empty;
if (this.m_strWhere != string.Empty)
...{
strRet = " Where " + this.m_strWhere;
}
return strRet;
}
public void Clear()
...{
this.m_strWhere = string.Empty;
}
}
#endregion
}
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。本文地址:http://blog.csdn.net/peterreg/archive/2008/04/02/2243132.aspx
接我的上一篇文章《关于SQL语句的自动生成!(四)》,虽然在DotNet Framework3.5 面世以后,可通过LINQ支持SQL的直接查询,但LINQ只支持SQL系列的,SQL2K,SQL2K5,对于ACCESS,ORACLE,MYSQL等不支持。同时平常的一些工具的创作,对于我们的软件水平也大有好处
下面附上DBACCESS的整套代码,供大家分享
public interface IClause
...{
void Add(string name, object val);
void AddWhere(string name, object val);
void Clear();
string TableName ...{ set;}
}
public abstract class ClauseFactory
...{
public enum Flag
...{
Insert,
Update,
Select,
Delete,
}
Create#region Create
public static IClause Create(string table, Flag category)
...{
BaseClause ret = null;
switch (category)
...{
case Flag.Insert:
ret = new Insert();
break;
case Flag.Update:
ret = new Update();
break;
case Flag.Select:
ret = new Select();
break;
case Flag.Delete:
ret = new Delete();
break;
default:
break;
}
if (ret != null)
...{
ret.TableName = table;
}
return ret;
}
#endregion
BaseClause#region BaseClause
private abstract class BaseClause : IClause
...{
private string m_strTable = string.Empty;
private Where m_oWhere = new Where();
protected BaseClause()
...{
this.m_strTable = string.Empty;
this.Clear();
}
public virtual void Add(string name, object val)
...{
}
public void AddWhere(string name, object val)
...{
this.m_oWhere.Add(name, val);
}
public void Clear()
...{
this.m_oWhere.Clear();
this.auxClear();
}
public string TableName
...{
set ...{ this.m_strTable = value; }
protected get ...{ return this.m_strTable; }
}
public sealed override string ToString()
...{
return this.ToStr + this.m_oWhere.ToString();
}
protected abstract string ToStr ...{ get;}
protected virtual void auxClear() ...{ }
}
#endregion
Insert, Update..Join#region Insert, Update..Join
private class Insert : BaseClause
...{
private string m_strName;
private string m_strValue;
public override void Add(string name, object val)
...{
if (val == null)
...{
this.Add(name, "null", false);
}
else
...{
this.Add(name, val.ToString(), val is string || val is DateTime);
}
}
private void Add(string name, string val, bool isref)
...{
if (isref)
...{
val = "'" + val + "'";
}
if (this.m_strName == string.Empty)
...{
this.m_strName = "[" + name + "]";
this.m_strValue = val;
}
else
...{
this.m_strName += ",[" + name + "]";
this.m_strValue += "," + val;
}
}
protected override string ToStr
...{
get ...{ return "INSERT INTO [" + base.TableName + "] ( " + this.m_strName + " ) values ( " + this.m_strValue + " )"; }
}
protected override void auxClear()
...{
this.m_strValue = string.Empty;
this.m_strName = string.Empty;
}
}
private class Delete : BaseClause
...{
public Delete()
...{
}
protected override string ToStr
...{
get ...{ return "DELETE FROM [" + base.TableName + "]"; }
}
}
private class Update : BaseClause
...{
private string m_strUpdate;
public override void Add(string name, object val)
...{
if (val == null)
...{
this.Add(name, "null", false);
}
else
...{
this.Add(name, val.ToString(), val is string || val is DateTime);
}
}
private void Add(string name, string val, bool isref)
...{
if (isref)
...{
val = "'" + val + "'";
}
if (this.m_strUpdate == string.Empty)
...{
this.m_strUpdate = "[" + name + "]=" + val;
}
else
...{
this.m_strUpdate += ",[" + name + "]=" + val;
}
}
protected override string ToStr
...{
get ...{ return "UPDATE [" + base.TableName + "] SET " + this.m_strUpdate; }
}
protected override void auxClear()
...{
this.m_strUpdate = string.Empty;
}
}
private class Select : BaseClause
...{
private string m_strSelect;
public override void Add(string name, object alis)
...{
if (alis == null)
...{
this.Add(name, name, false);
}
else
...{
this.Add(name, alis.ToString(), false);
}
}
private void Add(string name, string alis, bool isref)
...{
if (this.m_strSelect == string.Empty)
...{
this.m_strSelect = "[" + name + "] as [" + alis + "]";
}
else
...{
this.m_strSelect += ",[" + name + "] as [" + alis + "]";
}
}
protected override string ToStr
...{
get
...{
if (this.m_strSelect == string.Empty)
...{
this.m_strSelect = "*";
}
return "SELECT " + this.m_strSelect + " FROM [" + base.TableName + "]";
}
}
protected override void auxClear()
...{
this.m_strSelect = string.Empty;
}
}
private class Where
...{
private string m_strWhere;
public Where()
...{
this.Clear();
}
public void Add(string name, object val)
...{
if (val == null)
...{
this.Add(name, "null", false);
}
else
...{
this.Add(name, val.ToString(), val is string || val is DateTime);
}
}
private void Add(string name, string val, bool isref)
...{
if (isref)
...{
val = "'" + val + "'";
}
if (this.m_strWhere == string.Empty)
...{
this.m_strWhere = "[" + name + "]=" + val;
}
else
...{
this.m_strWhere += " and [" + name + "]=" + val;
}
}
public override string ToString()
...{
string strRet = string.Empty;
if (this.m_strWhere != string.Empty)
...{
strRet = " Where " + this.m_strWhere;
}
return strRet;
}
public void Clear()
...{
this.m_strWhere = string.Empty;
}
}
#endregion
}
相关文章推荐
- 关于SQL语句的自动生成!(三)
- 关于SQL语句的自动生成
- 关于SQL语句的自动生成!(一)
- 关于SQL语句的自动生成!(四)
- 关于SQL语句的自动生成
- 《关于SQL语句的自动生成!》一文的代码调用示例
- 关于SQL语句的自动生成!(二)
- 利用"SQL"语句自动生成序号的两种方式
- 根据DELTA自动生成SQL语句
- php-自动生成sql语句
- 多条件搜索时sql语句的自动生成
- sql 语句拼装类-自动智能拼装生成常用sql语句
- 遍历当前目录下的所有子目录例子(自动生成SQl语句工具)
- JAVA自定义注解实现SQL语句自动生成DEMO(2)
- Oracle自动生成数据字典的SQL语句
- 利用"SQL"语句自动生成序号的两种方式
- 自动生成清空数据库的SQL语句 ----C#.NET 通用权限管理系统,.NET 开发者的福音
- 自动生成清空数据库的SQL语句
- 打开powerDesigner时,创建table对应的自动生成sql语句没有注释
- Mybatis自动生成sql语句