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

Orm DbHelper准备提供四个接口分别操作Select, Insert, Update, Delete

tanghai 14 лет назад
Родитель
Сommit
8028957f33

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

@@ -16,6 +16,11 @@ Column& Column::operator()(std::string& name)
 	return *this;
 }
 
+bool Column::Empty()
+{
+	return columnStr.empty();
+}
+
 std::string Column::ToString() const
 {
 	return columnStr;

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

@@ -15,6 +15,7 @@ public:
 	Column(const std::string name);
 	~Column();
 	Column& operator()(std::string& name);
+	bool Empty();
 	std::string ToString() const;
 
 	template <typename T>

+ 1 - 0
Cpp/Platform/Orm/DbHelper.cc

@@ -9,6 +9,7 @@ DbHelper::DbHelper(std::string url, std::string username, std::string password)
 {
 	sql::Driver* driver = get_driver_instance();
 	connection.reset(driver->connect(url, username, password));
+	statement.reset(connection->createStatement());
 }
 
 DbHelper::~DbHelper()

+ 7 - 5
Cpp/Platform/Orm/DbHelper.h

@@ -10,8 +10,9 @@
 #include <cppconn/resultset.h>
 #include <cppconn/statement.h>
 #include <mysql_connection.h>
+#include "Orm/DbResult.h"
 #include "Orm/OrmTypedef.h"
-#include "Orm/Query.h"
+#include "Orm/Select.h"
 
 namespace Egametang {
 
@@ -19,17 +20,18 @@ class DbHelper
 {
 private:
 	boost::scoped_ptr<sql::Connection> connection;
+	boost::scoped_ptr<sql::Statement> statement;
 
 public:
 	DbHelper(std::string url, std::string username, std::string password);
 	virtual ~DbHelper();
 
 	template <typename Table>
-	ResultSetPtr ExecuteQuery(Query<Table>& query)
+	DbResultPtr Execute(Select<Table> select)
 	{
-		StatementPtr statemet(connection->createStatement());
-		ResultSetPtr resultSet(statemet->executeQuery(query.ToString()));
-		return resultSet;
+		ResultSetPtr resultSet(statement->executeQuery(select.ToString()));
+		DbResultPtr dbResult(new DbResult(resultSet));
+		return dbResult;
 	}
 };
 

+ 6 - 2
Cpp/Platform/Orm/DbResult.cc

@@ -28,8 +28,12 @@ void DbResult::FillMessage(ProtobufMessagePtr message)
 
 void DbResult::All(std::vector<ProtobufMessagePtr>& messages)
 {
-	for (int i = 0; resultSet->next(); ++i)
+	for (int i = 0; i < messages.size(); ++i)
 	{
+		if (!resultSet->next())
+		{
+			return;
+		}
 		ProtobufMessagePtr message = messages[i];
 		FillMessage(message);
 	}
@@ -37,7 +41,7 @@ void DbResult::All(std::vector<ProtobufMessagePtr>& messages)
 
 void DbResult::One(ProtobufMessagePtr message)
 {
-	if (!resultSet->first())
+	if (!resultSet->next())
 	{
 		return;
 	}

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

@@ -11,6 +11,17 @@ Expr::~Expr()
 {
 }
 
+Expr& Expr::operator=(const Expr& expr)
+{
+	exprStr = expr.exprStr;
+	return *this;
+}
+
+bool Expr::Empty()
+{
+	return exprStr.empty();
+}
+
 std::string Expr::ToString() const
 {
 	return exprStr;

+ 3 - 1
Cpp/Platform/Orm/Expr.h

@@ -15,7 +15,9 @@ protected:
 
 public:
 	virtual ~Expr();
-	virtual std::string ToString() const;
+	Expr& operator=(const Expr& expr);
+	std::string ToString() const;
+	bool Empty();
 };
 
 class Not: public Expr

+ 3 - 0
Cpp/Platform/Orm/OrmTypedef.h

@@ -9,8 +9,11 @@
 
 namespace Egametang {
 
+class DbResult;
+
 typedef boost::shared_ptr<sql::ResultSet> ResultSetPtr;
 typedef boost::shared_ptr<sql::Statement> StatementPtr;
+typedef boost::shared_ptr<DbResult> DbResultPtr;
 
 } // namespace Egametang
 

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

@@ -1,127 +0,0 @@
-#ifndef ORM_QUERY_H
-#define ORM_QUERY_H
-
-#include <string>
-#include <vector>
-#include <boost/lexical_cast.hpp>
-#include "Orm/Expr.h"
-#include "Orm/Column.h"
-
-namespace Egametang {
-
-template <typename Table>
-class Query
-{
-	std::string columns;
-	bool distinct;
-	std::string where;
-	std::string groupBy;
-	std::string having;
-	std::string orderBy;
-	int limit;
-	int offset;
-
-public:
-	Query(): distinct(false), where("true"), limit(0), offset(0)
-	{
-	}
-
-	~Query()
-	{
-	}
-
-	Query<Table>& Select(Column column)
-	{
-		columns = column.ToString();
-		return *this;
-	}
-
-	Query<Table>& Distinct(bool value)
-	{
-		distinct = value;
-		return *this;
-	}
-
-	Query<Table>& Where(const Expr& expr)
-	{
-		where = expr.ToString();
-		return *this;
-	}
-
-	Query<Table>& GroupBy(Column column)
-	{
-		groupBy = column.ToString();
-		return *this;
-	}
-
-	Query<Table>& Having(const Expr& having)
-	{
-		having = having.ToString();
-		return *this;
-	}
-
-	Query<Table>& OrderBy(Column column, bool ascending)
-	{
-		std::string value = column.ToString();
-		if (!ascending)
-		{
-			value += " desc";
-		}
-		orderBy = value;
-		return *this;
-	}
-
-	Query<Table>& Limit(int value)
-	{
-		limit = value;
-		return *this;
-	}
-
-	Query<Table>& Offset(int value)
-	{
-		offset = value;
-		return *this;
-	}
-
-	std::string ToString() const
-	{
-		std::string sql = "select ";
-		if (!columns.empty())
-		{
-			sql += columns;
-		}
-		if (distinct)
-		{
-			sql += " distinct";
-		}
-		sql += " from " + Table::descriptor()->full_name();
-		if (where != "true")
-		{
-			sql += " where " + where;
-		}
-		if (!groupBy.empty())
-		{
-			sql += " group by " + groupBy;
-		}
-		if (!having.empty())
-		{
-			sql += " having " + having;
-		}
-		if (!orderBy.empty())
-		{
-			sql += " order by " + orderBy;
-		}
-		if (limit)
-		{
-			sql += " limit " + boost::lexical_cast<std::string>(limit);
-		}
-		if (offset)
-		{
-			sql += " offset " + boost::lexical_cast<std::string>(offset);
-		}
-		return sql;
-	}
-};
-
-} // namespace Egametang
-#endif // ORM_QUERY_H

+ 129 - 0
Cpp/Platform/Orm/Select.h

@@ -0,0 +1,129 @@
+#ifndef ORM_QUERY_H
+#define ORM_QUERY_H
+
+#include <string>
+#include <vector>
+#include <boost/lexical_cast.hpp>
+#include "Orm/Expr.h"
+#include "Orm/Column.h"
+
+namespace Egametang {
+
+template <typename Table>
+class Select
+{
+private:
+	Column select;
+	bool distinct;
+	Expr where;
+	Column groupBy;
+	Expr having;
+	Column orderBy;
+	bool desc;
+	int limit;
+	int offset;
+
+public:
+	Select(Column columns):
+			select(columns), distinct(false),
+			desc(false), limit(0),
+			offset(0)
+	{
+	}
+
+	Select<Table>& Distinct()
+	{
+		distinct = true;
+		return *this;
+	}
+
+	Select<Table>& Where(Expr expr)
+	{
+		where = expr;
+		return *this;
+	}
+
+	Select<Table>& GroupBy(Column columns)
+	{
+		groupBy = columns;
+		return *this;
+	}
+
+	Select<Table>& Having(Expr expr)
+	{
+		having = expr;
+		return *this;
+	}
+
+	Select<Table>& OrderBy(Column columns)
+	{
+		orderBy = columns;
+		return *this;
+	}
+
+	Select<Table>& Desc()
+	{
+		desc = true;
+		return *this;
+	}
+
+	Select<Table>& Limit(int value)
+	{
+		limit = value;
+		return *this;
+	}
+
+	Select<Table>& Offset(int value)
+	{
+		offset = value;
+		return *this;
+	}
+
+	std::string ToString() const
+	{
+		// TODO: 加入异常处理机制
+		std::string sql = "select ";
+		if (!select.Empty())
+		{
+			sql += select.ToString();
+		}
+		if (distinct)
+		{
+			sql += " distinct";
+		}
+		sql += " from " + Table::descriptor()->full_name();
+		if (!where.Empty())
+		{
+			sql += " where " + where.ToString();
+		}
+		if (!groupBy.Empty())
+		{
+			sql += " group by " + groupBy.ToString();
+
+			if (!having.Empty())
+			{
+				sql += " having " + having.ToString();
+			}
+			if (!orderBy.Empty())
+			{
+				sql += " order by " + orderBy.ToString();
+			}
+			if (desc)
+			{
+				sql += " desc ";
+			}
+		}
+		if (limit)
+		{
+			sql += " limit " + boost::lexical_cast<std::string>(limit);
+		}
+		if (offset)
+		{
+			sql += " offset " + boost::lexical_cast<std::string>(offset);
+		}
+		return sql;
+	}
+};
+
+} // namespace Egametang
+#endif // ORM_QUERY_H

+ 1 - 1
Cpp/Platform/Orm/QueryTest.cc → Cpp/Platform/Orm/SelectTest.cc

@@ -1,4 +1,4 @@
-#include "Orm/OrmConn.h"
+#include "Orm/Select.h"
 
 namespace Egametang {