Jelajahi Sumber

Orm增加Column类,用来表示列以及生成sql表达式

tanghai 14 tahun lalu
induk
melakukan
41dcfc0dd3

+ 141 - 0
Cpp/Platform/Orm/Column.cc

@@ -0,0 +1,141 @@
+#include "Orm/Column.h"
+
+namespace Egametang {
+
+Column::Column(const std::string name)
+{
+	columns += name;
+}
+
+Column::~Column()
+{
+}
+
+Column& Column::operator()(std::string& column)
+{
+	columns = columns + ", " + 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);
+}
+
+} // namespace Egametang

+ 49 - 0
Cpp/Platform/Orm/Column.h

@@ -0,0 +1,49 @@
+#ifndef ORM_COLUMN_H
+#define ORM_COLUMN_H
+
+#include <string>
+#include "Orm/Expr.h"
+
+namespace Egametang {
+
+class Column
+{
+private:
+	std::string columns;
+
+public:
+	Column(const std::string name);
+	~Column();
+	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);
+};
+
+} // namespace Egametang
+#endif // ORM_COLUMN_H

+ 5 - 0
Cpp/Platform/Orm/ColumnTest.cc

@@ -0,0 +1,5 @@
+#include "Orm/Column.h"
+
+namespace Egametang {
+
+} // namespace Egametang

+ 16 - 0
Cpp/Platform/Orm/Expr.cc

@@ -0,0 +1,16 @@
+// Copyright: All Rights Reserved
+// Author: egametang@gmail.com (tanghai)
+
+#include "Orm/Expr.h"
+
+namespace Egametang {
+
+Expr::Expr()
+{
+}
+
+Expr::~Expr()
+{
+}
+
+} // namespace Egametang

+ 104 - 0
Cpp/Platform/Orm/Expr.h

@@ -0,0 +1,104 @@
+// Copyright: All Rights Reserved
+// Author: egametang@gmail.com (tanghai)
+
+#ifndef ORM_EXPRESSION_H
+#define ORM_EXPRESSION_H
+
+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)
+	{
+	}
+
+public:
+	std::string ToString() const
+	{
+		return left + " " + op + " " + right;
+	}
+};
+
+class Eq: public Oper
+{
+public:
+	Eq(const std::string& left, const std::string& right):
+			Oper(left, "=", right)
+	{
+	}
+};
+
+class Ne: public Oper
+{
+public:
+	Ne(const std::string& left, const std::string& right):
+			Oper(left, "<>", right)
+	{
+	}
+};
+
+class Gt: public Oper
+{
+public:
+	Gt(const std::string& left, const std::string& right):
+			Oper(left, ">", 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
+{
+public:
+	Like(const std::string& left, const std::string& right):
+			Oper(left, "like", right)
+	{
+	}
+};
+
+} // namespace Egametang
+#endif // ORM_EXPRESSION_H

+ 8 - 0
Cpp/Platform/Orm/ExprTest.cc

@@ -0,0 +1,8 @@
+// Copyright: All Rights Reserved
+// Author: egametang@gmail.com (tanghai)
+
+#include "Expression.h"
+
+namespace Egametang {
+
+} // namespace Egametang

+ 0 - 61
Cpp/Platform/Orm/OrmConn.cc

@@ -1,61 +0,0 @@
-#include <google/protobuf/descriptor.h>
-#include <glog/logging.h>
-#include "Orm/OrmConn.h"
-
-namespace Egametang {
-
-OrmConn::OrmConn()
-{
-}
-
-OrmConn::~OrmConn()
-{
-}
-
-// 根据message拼出类似如下的sql语句
-// insert into person(id, name, sex, age, marry) values(1, 'xiaoming', 'M', 18, 1);
-std::string OrmConn::GetInsertSQL(const google::protobuf::Message& message)
-{
-	std::string sql = "";
-	std::string tableName = message.GetTypeName();
-	const google::protobuf::Reflection* reflection = message.GetReflection();
-
-	sql += "insert into " + tableName + "(";
-	std::string columnNames = "";
-	std::string valueString = "";
-	for (int i = 0; i < message.GetDescriptor()->field_count(); ++i)
-	{
-		const google::protobuf::FieldDescriptor* field = message.GetDescriptor()->field(i);
-		if (!reflection->HasField(field))
-		{
-			continue;
-		}
-
-		std::string name = field->name();
-		columnNames += name + ", ";
-
-		MessageField msgField(message, field);
-		valueString += msgField.ValueToString() + ", ";
-	}
-	// 去除最后的逗号
-	columnNames.resize(columnNames.size() - 2);
-	valueString.resize(valueString.size() - 2);
-	sql += columnNames + ") values(";
-	sql += valueString + ");";
-	return sql;
-}
-
-void OrmConn::Select(std::vector<boost::shared_ptr<google::protobuf::Message> >& messages, Condition condition)
-{
-}
-
-void OrmConn::Insert(const google::protobuf::Message& message)
-{
-	std::string sql = GetInsertSQL(message);
-}
-
-void OrmConn::Update(const google::protobuf::Message& message, Condition condition)
-{
-}
-
-} // namespace Egametang

+ 0 - 38
Cpp/Platform/Orm/OrmConn.h

@@ -1,38 +0,0 @@
-#ifndef ORM_ORMCONN_H
-#define ORM_ORMCONN_H
-
-#include <vector>
-#include <boost/shared_ptr.hpp>
-#include <google/protobuf/message.h>
-
-namespace Egametang {
-
-class Condition;
-
-class OrmConn
-{
-private:
-	std::string GetInsertSQL(const google::protobuf::Message& message);
-
-public:
-	OrmConn();
-	virtual ~OrmConn();
-
-	void Select(std::vector<boost::shared_ptr<google::protobuf::Message> >& messages, Condition condition);
-
-	void Insert(const google::protobuf::Message& message);
-
-	void Update(const google::protobuf::Message& message, Condition condition);
-
-	template<typename T>
-	void Delete();
-};
-
-}
-
-template<typename T> inline void Egametang::OrmConn::Delete()
-{
-}
-
- // namespace Egametang
-#endif  // ORM_ORMCONN_H

+ 108 - 0
Cpp/Platform/Orm/Query.cc

@@ -0,0 +1,108 @@
+#include <google/protobuf/descriptor.h>
+#include <glog/logging.h>
+#include "Orm/Query.h"
+
+namespace Egametang {
+
+SelectQuery& SelectQuery::Distinct(bool d)
+{
+	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()
+{
+	results.clear();
+	return *this;
+}
+SelectQuery& SelectQuery::Source(std::string s, std::string alias)
+{
+	if (!alias.empty())
+		s += " as " + alias;
+	sources.push_back(s);
+	return *this;
+}
+SelectQuery& SelectQuery::Where(const Expr& w)
+{
+	where = (RawExpr(where) && w).asString();
+	return *this;
+}
+SelectQuery& SelectQuery::Where(std::string w)
+{
+	where = (RawExpr(where) && RawExpr(w)).asString();
+	return *this;
+}
+SelectQuery& SelectQuery::GroupBy(std::string gb)
+{
+	groupBy.push_back(gb);
+	return *this;
+}
+SelectQuery& SelectQuery::Having(const Expr & h)
+{
+	having = h.asString();
+	return *this;
+}
+SelectQuery& SelectQuery::Having(std::string h)
+{
+	having = h;
+	return *this;
+}
+SelectQuery& SelectQuery::OrderBy(std::string ob, bool ascending)
+{
+	std::string value = ob;
+	if (!ascending)
+	{
+		value += " desc";
+	}
+	orderBy.push_back(value);
+	return *this;
+}
+SelectQuery::operator std::string() const
+{
+	std::string sql = "select ";
+	if (distinct)
+	{
+		sql += "distinct ";
+	}
+	if (where != "true")
+	{
+		sql += " where " + where;
+	}
+	if (!groupBy.empty())
+	{
+		sql += " group by " + groupBy;
+	}
+	if (!having.empty())
+	{
+		sql += " having " + having;
+	}
+	if (orderBy.size() > 0)
+	{
+		sql += " order by " + orderBy.join(",");
+	}
+	if (limit)
+	{
+		sql += " limit " + boost::lexical_cast<std::string>(limit);
+	}
+	if (offset)
+	{
+		sql += " offset " + boost::lexical_cast<std::string>(offset);
+	}
+	return sql;
+}
+
+} // namespace Egametang

+ 39 - 0
Cpp/Platform/Orm/Query.h

@@ -0,0 +1,39 @@
+#ifndef ORM_QUERY_H
+#define ORM_QUERY_H
+
+#include <vector>
+#include <boost/shared_ptr.hpp>
+#include <google/protobuf/message.h>
+#include "Base/Typedef.h"
+
+namespace Egametang {
+
+class SelectQuery
+{
+	bool distinct;
+	int limit;
+	int offset;
+	std::string where;
+	std::string groupBy;
+	std::string having;
+	std::string orderBy;
+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
+	{
+	}
+};
+
+} // namespace Egametang
+#endif // ORM_QUERY_H

+ 0 - 0
Cpp/Platform/Orm/OrmConnTest.cc → Cpp/Platform/Orm/QueryTest.cc