SQLBuilder类的设计思路

 
2月1日
SQLBuilder类的设计思路

现在我需要完成CRUD操作的具体功能了。
这时我面对的问题是选择SqlDataAdapter与SqlDataReader哪个做数据库操作。经过考虑我决定使用SqlDataReader。另外我还需要一个用于生成SQL语句的工具类,我将它命名为SQLBuilder。而用于提供对数据库访问的类,我将它命名为SQLExecutor。从名称上我们就可以了解这两个类的功能了。另一个需要协作的类是连接池Pool类,这个类为SQLExecutor提供数据库连接池中的一个连接实例。
SqlConnection-->DBConnection-->Pool---------->|
                                                                   |----->SQLExecutor
                                               SqlBuilder---->|
                     (服务提供关系图<箭头方向为服务提供的方向>)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
关于SQLExecutor和SqlBuilder的工作方式.
回顾Item类里面的Find方法.
public static Item [] Find() //FindAll()
public static Item FindByPKey(int id)
public static Item [] FindByPKey(int [] ids)
public static Item [] FindByAttribute(string attrName,ArrayList attrValues)
public static Item [] FindByAttributes(string [] attrNames,ArrayList attrValues)
public static Item [] Find(Item s_item,Item e_item)
public static Item [] Find(Item s_item,Item e_item,string attrName,ArrayList attrValues)
public static Item [] Find(Item s_item,Item e_item,string [] attrNames,ArrayList attrValues)
对于Find方法,其关心的问题是生成SQL语句中的'where'条件.
所以我提供这样的一个方法:CreateWhereCondition()
根据提供参数的同,我定义以下重载方法.
public static string CreateWhereCondition() //'where 1=1'     (方法1)
public static string CreateWhereCondition(int id)//       (方法2)
public static string CreateWhereCondition(int [] ids)         (方法3)
public static string CreateWhereCondition(string [] attrNames,ArrayList attrValues) (方法4)
public static string CreateWhereCondition(string attrName,ArrayList attrValues) //(方法5)
public static string CreateWhereCondition(Model s_model,Model e_model) //(方法6)
public static string CreateWhereCondition(Model s_model,Model e_model,string attrName,ArrayList attrValues)//(方法7)
public static string CreateWhereCondition(Item s_item,Item e_item,string [] attrNames,ArrayList attrValues)//(方法8)
其实:(2)可以并到(3)中.
     (5)可以并到(4)中.
     (7)可以并到(8)中.
     (8)可以看做是(6)+(4).
实际上就是需要完成方法(1),(3),(4),(6)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在来看一下用来创建对象的方法.
public static Save(Item [] items)
public int Save()
public static Item [] Create(string [] attrNames,ArrayList attrValues)
public static Item Create(string Item_name,double item_price,string item_type_no)
可以看出我需要关心的是生成一个Insert Into 语句.
我定义以下重载的方法:
public static string CreateInsert(Model [] model) //(方法9)
public static string CreateInsert(string [] attrNames,ArrayList attrValues)//(方法10)
public static string CreateInsert()//
注意:对于方法public static Item Create(string Item_name,double item_price,string item_type_no)的处理.
public static Item Create(string item_name,double item_price,string item_type_no)
{
 Item item=Item.New();
 Item.ItemName=Item_name;
 Item.ItemPrice=item_price;
 Item.ItemTypeNo=item_type_no;
        Item.Save(new Item()[]{item});//或者item.save();
 return item;
}
通过这种方法并到方法(9)中.
对于Insert操作,需要方法(9),(10)就够了.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
接着来看Update操作.
public int Update()
public static int Update(Item [] items)//方法(11)
public static int Update(string [] attrNames,ArrayList attrValues,string [] u_attrName,ArrayList u_attrValues)//方法(12)
public static int Update(Item s_item,Item e_item,string [] attrNames,ArrayList attrValues,string [] u_attrName,ArrayList u_attrValues)//方法(13)
对于Update语句我们关心的两个部分,一部分是对字段赋值,一部分是where条件.所以我们可以将方法CreateUpdate()分解为两个方法,一个是CreateUpdatePart(),一个是复用CreateWhere()方法.
Update(Item [] items)==>Update(new string ()[]{"id"},ArrayList IdList,string [] u_attrName,ArrayList u_attrValues)
及将方法(11)转化为方法(12)这种更一般的形式.而(13)的可以看作(12)和一个CreateWhere(Model s_model,Model e_model)即方法(6)的组合.
而(12)又可以看作是CreateUpdatePart(string [] u_attrName,ArrayList u_attrValue)与CreateWhereCondition(string [] attrNames,ArrayList attrValues) 及方法(4).
注意:对于离散的数据返回的很可能是string []数组类型给SQLExecutor执行.而SQLExecutor执行的时候将执行数据中的每一个SQL语句.
所以,Update()方法所需要的方法只有一个了.
public static string CreateUpdatePart(string [] u_attrName,ArrayList u_attrValue)//方法(14)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
最后是Delete操作了:
回顾一下.
public Item Delete()
public static Item [] Delete(Item [] items)
public static Item Delete(int id)//方法名称改了,与前面文章有所不同.
public static Item Delete(int [] ids)//方法名称改了,与前面文章有所不同.
public static Item [] Delete(Item s_item,Item e_item)
public static Item [] Delete(string [] attrNames,ArrayList attrValues)
public statci Item [] Delete(Item s_item,Item e_item,string [] attrNames,ArrayList attrValues)
OK!所有生成Delete操作SQL语句的方法都可以看作是由两个方法的组合,即一个CreateDeletePart()方法(其实就是返回一个"Delete From TableName")和一个CreateWherePart()(统一在名称后面加上part,注意方法名称改变).
所以,需要返回Delete操作SQL的方法就只有:
public static string CreateDeletePart()//(可能需要传入类的类型或者实例)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
到这里我们再做一些新约定.
(约定二)
数据库中所有表的名称为首字母大写的英文代词组成,且没有复数形式.举例:User,UserType,OrderLine,Order等.
(约定三)
数据库中表字段的名称为下划线连接的小写英文单词.举例:item_type_id,role_id,user_name,user_pwd等.
(约定四)
实体类的名称与表名称相同.
(约定五)
实体类字段的名称与其对应表字段名称相同.
(约定六)
实体类属性的名称为其对应字段名称除掉下划线以后所得单词将首字母大写的连接.举例:item_type_id==>ItemTypeId,role_id==>RoleId.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值