一点构想-直观的强类型的SQL查询的一种实现

    看过很多强类型查询的实现,觉得通过层层嵌套的方法来构造,感觉很不直观,昨天下午花了点时间写了个验证的代码,现在发上来大家看看这样子实现的查询方便不方便,有什么问题,因为是突发奇想,所以未经过严格验证,所以如果发现问题请温柔提出.
    这里只是个验证想法的代码,所以没有作任何容错和扩展性处理.也不要提出OO不OO的看法,毫无疑义.
    我所设想的是一个查询 Select [Columnlist] From [TableName] Where [Exp] Order By [PK] 一般来说是这个格式,我们最难表述的其实就是[Exp]这个部分。前面的都比较格式化,所以可以通过嵌套方法来实现还是比较合适的,但是[Exp]这个部分用诸如AND(Exp1,Exp2)这样子的形式不能很直观的看出表达式的意义,所以通过重载操作符的方式来实现,这里我们假设在Entity的Class里有static成员来存储列的名称。那么
    
     Exp Rs=new Exp(User.ID) == 2 & new Exp(User.State) > 0 ;
    
     这样子的格式就能表达Where后面的 ID=2 AND State>0 这个表达式

     具体代码如下

  1 None.gif      class  Program
  2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
  3InBlock.gif        static void Main(string[] args)
  4ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
  5InBlock.gif
  6InBlock.gif            Exp rs = new Exp("C1"== 25 & new Exp("C2"> 3 | new Exp("C3"< 5 ^ new Exp("C4"% "hehe";
  7InBlock.gif            Console.WriteLine(rs.Sql);
  8InBlock.gif            foreach (SqlParameter sp in rs.Sps)
  9ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 10InBlock.gif                Console.WriteLine(sp.ParameterName);
 11ExpandedSubBlockEnd.gif            }

 12InBlock.gif            Console.Read();
 13ExpandedSubBlockEnd.gif        }

 14ExpandedBlockEnd.gif    }

 15 None.gif
 16 None.gif     class  Exp
 17 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 18InBlock.gif        private string _Sql;
 19InBlock.gif
 20InBlock.gif        private List<SqlParameter> sps;
 21InBlock.gif
 22InBlock.gif        public List<SqlParameter> Sps
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn sps; }
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ sps = value; }
 26ExpandedSubBlockEnd.gif        }

 27InBlock.gif
 28InBlock.gif        private SqlParameter sp;
 29InBlock.gif
 30InBlock.gif        public string Sql
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _Sql; }
 33ExpandedSubBlockEnd.gif        }

 34InBlock.gif
 35InBlock.gif        private Exp()
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 37InBlock.gif            sps = new List<SqlParameter>();
 38ExpandedSubBlockEnd.gif        }

 39InBlock.gif
 40InBlock.gif        public Exp(string CollumnName)
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 42InBlock.gif            _Sql = CollumnName;
 43ExpandedSubBlockEnd.gif        }

 44InBlock.gif
 45InBlock.gif        public static Exp operator ==(Exp Left, Object Value)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            Exp Next = new Exp();
 48InBlock.gif            Next.sp = new SqlParameter(Left._Sql, Value);
 49InBlock.gif            Next.sps.Add(Next.sp);
 50InBlock.gif            Next._Sql = Left._Sql + " = @" + Left.Sql;
 51InBlock.gif            return Next;
 52ExpandedSubBlockEnd.gif        }

 53InBlock.gif        public static Exp operator !=(Exp Left, Object Value)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 55InBlock.gif            Exp Next = new Exp();
 56InBlock.gif            Next.sp = new SqlParameter(Left._Sql, Value);
 57InBlock.gif            Next.sps.Add(Next.sp);
 58InBlock.gif            Next._Sql = Left._Sql + " <> @" + Left._Sql;
 59InBlock.gif            return Next;
 60ExpandedSubBlockEnd.gif        }

 61InBlock.gif
 62InBlock.gif        public static Exp operator <(Exp Left, Object Value)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 64InBlock.gif            Exp Next = new Exp();
 65InBlock.gif            Next.sp = new SqlParameter(Left._Sql, Value);
 66InBlock.gif            Next.sps.Add(Next.sp);
 67InBlock.gif            Next._Sql = Left._Sql + " < @" + Left._Sql;
 68InBlock.gif            return Next;
 69ExpandedSubBlockEnd.gif        }

 70InBlock.gif        public static Exp operator >(Exp Left, Object Value)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 72InBlock.gif            Exp Next = new Exp();
 73InBlock.gif            Next.sp = new SqlParameter(Left._Sql, Value);
 74InBlock.gif            Next.sps.Add(Next.sp);
 75InBlock.gif            Next._Sql = Left._Sql + " > @" + Left._Sql;
 76InBlock.gif            return Next;
 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif
 79InBlock.gif        public static Exp operator %(Exp Left, Object Value)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 81InBlock.gif            Exp Next = new Exp();
 82InBlock.gif            Next.sp = new SqlParameter(Left._Sql, Value);
 83InBlock.gif            Next.sps.Add(Next.sp);
 84InBlock.gif            Next._Sql = Left._Sql + " Like @" + Left._Sql;
 85InBlock.gif            return Next;
 86ExpandedSubBlockEnd.gif        }

 87InBlock.gif
 88InBlock.gif        public static Exp operator &(Exp Left, Exp Right)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 90InBlock.gif            Exp Next = new Exp();
 91InBlock.gif            foreach (SqlParameter sp in Left.sps)
 92ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 93InBlock.gif                Next.sps.Add(sp);
 94ExpandedSubBlockEnd.gif            }

 95InBlock.gif            foreach (SqlParameter sp in Right.sps)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 97InBlock.gif                Next.sps.Add(sp);
 98ExpandedSubBlockEnd.gif            }

 99InBlock.gif            Next._Sql = Left.Sql + " AND " + Right.Sql;
100InBlock.gif            return Next;
101ExpandedSubBlockEnd.gif        }

102InBlock.gif
103InBlock.gif
104InBlock.gif        public static Exp operator |(Exp Left, Exp Right)
105ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
106InBlock.gif            Exp Next = new Exp();
107InBlock.gif            foreach (SqlParameter sp in Left.sps)
108ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
109InBlock.gif                Next.sps.Add(sp);
110ExpandedSubBlockEnd.gif            }

111InBlock.gif            foreach (SqlParameter sp in Right.sps)
112ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
113InBlock.gif                Next.sps.Add(sp);
114ExpandedSubBlockEnd.gif            }

115InBlock.gif            Next._Sql = Left.Sql + " OR " + Right.Sql;
116InBlock.gif            return Next;
117ExpandedSubBlockEnd.gif        }

118InBlock.gif
119InBlock.gif        public static Exp operator ^(Exp Left, Exp Right)
120ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
121InBlock.gif            Exp Next = new Exp();
122InBlock.gif            foreach (SqlParameter sp in Left.sps)
123ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
124InBlock.gif                Next.sps.Add(sp);
125ExpandedSubBlockEnd.gif            }

126InBlock.gif            foreach (SqlParameter sp in Right.sps)
127ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
128InBlock.gif                Next.sps.Add(sp);
129ExpandedSubBlockEnd.gif            }

130InBlock.gif            Next._Sql = Left.Sql + " NOT " + Right.Sql;
131InBlock.gif            return Next;
132ExpandedSubBlockEnd.gif        }

133ExpandedBlockEnd.gif    }

134 None.gif   

    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值