Просмотр исходного кода

完善了Expression和Column类

tanghai 14 лет назад
Родитель
Сommit
03fc430ff1
6 измененных файлов с 148 добавлено и 288 удалено
  1. 3 120
      Cpp/Platform/Orm/Column.cc
  2. 41 27
      Cpp/Platform/Orm/Column.h
  3. 37 2
      Cpp/Platform/Orm/Expr.cc
  4. 16 72
      Cpp/Platform/Orm/Expr.h
  5. 34 46
      Cpp/Platform/Orm/Query.cc
  6. 17 21
      Cpp/Platform/Orm/Query.h

+ 3 - 120
Cpp/Platform/Orm/Column.cc

@@ -2,9 +2,8 @@
 
 namespace Egametang {
 
-Column::Column(const std::string name)
+Column::Column(const std::string name): columnStr(name)
 {
-	columns += name;
 }
 
 Column::~Column()
@@ -13,129 +12,13 @@ Column::~Column()
 
 Column& Column::operator()(std::string& column)
 {
-	columns = columns + ", " + column;
+	columnStr = columnStr + ", " + column;
 	return *this;
 }
 
 std::string Column::ToString() const
 {
-	return columns;
-}
-
-
-Gt Column::operator>(const std::string& value)
-{
-	value = "'" + value + "'";
-	return Gt(*this, value);
-}
-Gt Column::operator>(int value)
-{
-	std::string strRight = boost::lexical_cast<std::string>(value);
-	return Gt(*this, strRight);
-}
-Gt Column::operator>(double value)
-{
-	std::string strRight = boost::lexical_cast<std::string>(value);
-	return Gt(*this, strRight);
-}
-Gt Column::operator>(const Column value)
-{
-	std::string strRight = value.ToString();
-	return Gt(*this, strRight);
-}
-
-
-Ge Column::operator>=(const std::string& value)
-{
-	value = "'" + value + "'";
-	return Ge(*this, value);
-}
-Ge Column::operator>=(int value)
-{
-	std::string strRight = boost::lexical_cast<std::string>(value);
-	return Ge(*this, strRight);
-}
-Ge Column::operator>=(double value)
-{
-	std::string strRight = boost::lexical_cast<std::string>(value);
-	return Ge(*this, strRight);
-}
-Ge Column::operator>=(const Column value)
-{
-	std::string strRight = value.ToString();
-	return Ge(*this, strRight);
-}
-
-
-Lt Column::operator<(const std::string& value)
-{
-	value = "'" + value + "'";
-	return Lt(*this, value);
-}
-Lt Column::operator<(int value)
-{
-	std::string strRight = boost::lexical_cast<std::string>(value);
-	return Lt(*this, strRight);
-}
-Lt Column::operator<(double value)
-{
-	std::string strRight = boost::lexical_cast<std::string>(value);
-	return Lt(*this, strRight);
-}
-Lt Column::operator<(const Column value)
-{
-	std::string strRight = value.ToString();
-	return Lt(*this, strRight);
-}
-
-
-Le Column::operator<=(const std::string& value)
-{
-	value = "'" + value + "'";
-	return Le(*this, value);
-}
-Le Column::operator<=(int value)
-{
-	std::string strRight = boost::lexical_cast<std::string>(value);
-	return Le(*this, strRight);
-}
-Le Column::operator<=(double value)
-{
-	std::string strRight = boost::lexical_cast<std::string>(value);
-	return Le(*this, strRight);
-}
-Le Column::operator<=(const Column value)
-{
-	std::string strRight = value.ToString();
-	return Le(*this, strRight);
-}
-
-
-Ne Column::operator!=(const std::string& value)
-{
-	value = "'" + value + "'";
-	return Ne(*this, value);
-}
-Ne Column::operator!=(int value)
-{
-	std::string strRight = boost::lexical_cast<std::string>(value);
-	return Ne(*this, strRight);
-}
-Ne Column::operator!=(double value)
-{
-	std::string strRight = boost::lexical_cast<std::string>(value);
-	return Ne(*this, strRight);
-}
-Ne Column::operator!=(const Column value)
-{
-	std::string strRight = value.ToString();
-	return Ne(*this, strRight);
-}
-
-Like like(const std::string value)
-{
-	value = "'" + value + "'";
-	return Like(*this, value);
+	return columnStr;
 }
 
 } // namespace Egametang

+ 41 - 27
Cpp/Platform/Orm/Column.h

@@ -9,7 +9,7 @@ namespace Egametang {
 class Column
 {
 private:
-	std::string columns;
+	std::string columnStr;
 
 public:
 	Column(const std::string name);
@@ -17,32 +17,46 @@ public:
 	Column& operator()(std::string& column);
 	std::string ToString() const;
 
-	Gt operator>(const std::string& value);
-	Gt operator>(int value);
-	Gt operator>(double value);
-	Gt operator>(const Column value);
-
-	Ge operator>=(const std::string& value);
-	Ge operator>=(int value);
-	Ge operator>=(double value);
-	Ge operator>=(const Column value);
-
-	Lt operator<(const std::string& value);
-	Lt operator<(int value);
-	Lt operator<(double value);
-	Lt operator<(const Column value);
-
-	Le operator<=(const std::string& value);
-	Le operator<=(int value);
-	Le operator<=(double value);
-	Le operator<=(const Column value);
-
-	Ne operator!=(const std::string& value);
-	Ne operator!=(int value);
-	Ne operator!=(double value);
-	Ne operator!=(const Column value);
-
-	Like like(const std::string value);
+	template <typename T>
+	Expr operator>(const T& value)
+	{
+		return Oper(*this,">", value);
+	}
+
+	template <typename T>
+	Expr operator>=(const T& value)
+	{
+		return Oper(*this, ">=", value);
+	}
+
+	template <typename T>
+	Expr operator<(const T& value)
+	{
+		return Oper(*this, "<", value);
+	}
+
+	template <typename T>
+	Expr operator<=(const T& value)
+	{
+		return Oper(*this, "<=", value);
+	}
+
+	template <typename T>
+	Expr operator!=(const T& value)
+	{
+		return Oper(*this, "<>", value);
+	}
+
+	template <typename T>
+	Expr operator==(const T& value)
+	{
+		return Oper(*this, "=", value);
+	}
+
+	Expr like(const std::string value)
+	{
+		return Oper(*this, "like", value);
+	}
 };
 
 } // namespace Egametang

+ 37 - 2
Cpp/Platform/Orm/Expr.cc

@@ -5,12 +5,47 @@
 
 namespace Egametang {
 
-Expr::Expr()
+Expr::~Expr()
+{
+}
+std::string Expr::ToString()
 {
+	return exprStr;
 }
 
-Expr::~Expr()
+Not::Not(const Expr& expr)
+{
+	exprStr = "not (" + expr + ") ";
+}
+
+And::And(const Expr& left, const Expr& right)
+{
+	exprStr = "(" + left.ToString() + ") and" + " (" + right.ToString() + ") ";
+}
+
+Or::Or(const Expr& left, const Expr& right)
+{
+	exprStr = "(" + left.ToString() + ") or" + " (" + right.ToString() + ") ";
+}
+
+Oper::Oper(const Column& left, const std::string& op, const std::string& right)
+{
+	exprStr = left.ToString() + " " + op + " '" + right + "'";
+}
+
+Oper::Oper(const Column& left, const std::string& op, const Column& right)
+{
+	exprStr = left.ToString() + " " + op + " " + right.ToString();
+}
+
+Oper::Oper(const Column& left, const std::string& op, int right)
+{
+	exprStr = left.ToString() + " " + op + " " + boost::lexical_cast<std::string>(right);
+}
+
+Oper::Oper(const Column& left, const std::string& op, double right)
 {
+	exprStr = left.ToString() + " " + op + " " + boost::lexical_cast<std::string>(right);
 }
 
 } // namespace Egametang

+ 16 - 72
Cpp/Platform/Orm/Expr.h

@@ -8,96 +8,40 @@ namespace Egametang {
 
 class Expr
 {
-public:
-	virtual ~Expr()
-	{
-	}
-	virtual std::string ToString()
-	{
-		return "true";
-	}
-};
-
-class Oper: public Expr
-{
 protected:
-	std::string left;
-	std::string op;
-	std::string right;
-
-	Oper(const std::string& left, const std::string& o, const std::string& right):
-			left(left), op(o), right(right)
-	{
-	}
+	std::string exprStr;
 
 public:
-	std::string ToString() const
-	{
-		return left + " " + op + " " + right;
-	}
+	virtual ~Expr();
+	virtual std::string ToString();
 };
 
-class Eq: public Oper
+class Not: public Expr
 {
 public:
-	Eq(const std::string& left, const std::string& right):
-			Oper(left, "=", right)
-	{
-	}
+	Not(const Expr& expr);
 };
 
-class Ne: public Oper
+class And: public Expr
 {
 public:
-	Ne(const std::string& left, const std::string& right):
-			Oper(left, "<>", right)
-	{
-	}
+	And(const Expr& left, const Expr& right);
 };
 
-class Gt: public Oper
+class Or: public Expr
 {
 public:
-	Gt(const std::string& left, const std::string& right):
-			Oper(left, ">", right)
-	{
-	}
+	Or(const Expr& left, const Expr& right);
 };
 
-class Ge: public Oper
-{
-public:
-	Ge(const std::string& left, const std::string& right):
-			Oper(left, ">=", right)
-	{
-	}
-};
-
-class Lt: public Oper
-{
-public:
-	Lt(const std::string& left, const std::string& right):
-			Oper(left, "<", right)
-	{
-	}
-};
-
-class Le: public Oper
-{
-public:
-	Le(const std::string& left, const std::string& right):
-			Oper(left, "<=", right)
-	{
-	}
-};
-
-class Like: public Oper
+// > < >= <= != like
+class Oper: public Expr
 {
-public:
-	Like(const std::string& left, const std::string& right):
-			Oper(left, "like", right)
-	{
-	}
+protected:
+	Oper(const Column& left, const std::string& op, const std::string& right);
+	Oper(const Column& left, const std::string& op, const Column& right);
+	Oper(const Column& left, const std::string& op, int right);
+	Oper(const Column& left, const std::string& op, double right);
 };
 
 } // namespace Egametang

+ 34 - 46
Cpp/Platform/Orm/Query.cc

@@ -4,79 +4,67 @@
 
 namespace Egametang {
 
-SelectQuery& SelectQuery::Distinct(bool d)
+Query::Query(): distinct(false), where("true"), limit(0), offset(0)
 {
-	distinct = d;
-	return *this;
-}
-SelectQuery& SelectQuery::Limit(int value)
-{
-	limit = value;
-	return *this;
-}
-SelectQuery& SelectQuery::Offset(int value)
-{
-	offset = value;
-	return *this;
-}
-SelectQuery& SelectQuery::Result(std::string r)
-{
-	results.push_back(r);
-	return *this;
 }
-SelectQuery& SelectQuery::ClearResults()
+
+Query::~Query()
 {
-	results.clear();
-	return *this;
 }
-SelectQuery& SelectQuery::Source(std::string s, std::string alias)
+
+Query& Query::Distinct(bool value)
 {
-	if (!alias.empty())
-		s += " as " + alias;
-	sources.push_back(s);
+	distinct = value;
 	return *this;
 }
-SelectQuery& SelectQuery::Where(const Expr& w)
+
+Query& Query::Where(const Expr& expr)
 {
-	where = (RawExpr(where) && w).asString();
+	where = expr.ToString();
 	return *this;
 }
-SelectQuery& SelectQuery::Where(std::string w)
+
+Query& Query::GroupBy(Column column)
 {
-	where = (RawExpr(where) && RawExpr(w)).asString();
+	groupBy = column.ToString();
 	return *this;
 }
-SelectQuery& SelectQuery::GroupBy(std::string gb)
+
+Query& Query::Having(const Expr& having)
 {
-	groupBy.push_back(gb);
+	having = having.ToString();
 	return *this;
 }
-SelectQuery& SelectQuery::Having(const Expr & h)
+
+Query& Query::OrderBy(Column column, bool ascending)
 {
-	having = h.asString();
+	std::string value = column.ToString();
+	if (!ascending)
+	{
+		value += " desc";
+	}
+	orderBy = value;
 	return *this;
 }
-SelectQuery& SelectQuery::Having(std::string h)
+
+Query& Query::Limit(int value)
 {
-	having = h;
+	limit = value;
 	return *this;
 }
-SelectQuery& SelectQuery::OrderBy(std::string ob, bool ascending)
+
+Query& Query::Offset(int value)
 {
-	std::string value = ob;
-	if (!ascending)
-	{
-		value += " desc";
-	}
-	orderBy.push_back(value);
+	offset = value;
 	return *this;
 }
-SelectQuery::operator std::string() const
+
+std::string Query::ToString() const
 {
 	std::string sql = "select ";
 	if (distinct)
 	{
-		sql += "distinct ";
+		sql += " distinct ";
 	}
 	if (where != "true")
 	{
@@ -90,9 +78,9 @@ SelectQuery::operator std::string() const
 	{
 		sql += " having " + having;
 	}
-	if (orderBy.size() > 0)
+	if (!orderBy.empty())
 	{
-		sql += " order by " + orderBy.join(",");
+		sql += " order by " + orderBy;
 	}
 	if (limit)
 	{

+ 17 - 21
Cpp/Platform/Orm/Query.h

@@ -1,38 +1,34 @@
 #ifndef ORM_QUERY_H
 #define ORM_QUERY_H
 
+#include <string>
 #include <vector>
-#include <boost/shared_ptr.hpp>
-#include <google/protobuf/message.h>
-#include "Base/Typedef.h"
+#include "Orm/Expr.h"
 
 namespace Egametang {
 
-class SelectQuery
+class Query
 {
 	bool distinct;
-	int limit;
-	int offset;
 	std::string where;
 	std::string groupBy;
 	std::string having;
 	std::string orderBy;
+	int limit;
+	int offset;
+
 public:
-	SelectQuery(): distinct(false), limit(0), offset(0), where("true")
-	{
-	}
-	SelectQuery& Distinct(bool distinct);
-	SelectQuery& Limit(int limit);
-	SelectQuery& Offset(int offset);
-	SelectQuery& Result(std::string result);
-	SelectQuery& ClearResults();
-	SelectQuery& Where(const Expr& where);
-	SelectQuery& GroupBy(std::string groupby);
-	SelectQuery& Having(const Expr& having);
-	SelectQuery& OrderBy(Column column, bool ascending = true);
-	std::string ToString() const
-	{
-	}
+	Query();
+	~Query();
+	Query& Distinct(bool distinct);
+	Query& Where(const Expr& where);
+	Query& GroupBy(Column column);
+	Query& Having(const Expr& having);
+	Query& OrderBy(Column column, bool ascending = true);
+	Query& Limit(int limit);
+	Query& Offset(int offset);
+
+	std::string ToString() const;
 };
 
 } // namespace Egametang