فهرست منبع

DbResultTest Select One测试通过,mysql index是从1开始计算的

tanghai 14 سال پیش
والد
کامیت
5c4c7f4f54

+ 6 - 2
Cpp/Platform/Orm/DbHelper.h

@@ -5,11 +5,13 @@
 #define ORM_SQLHELPER_H
 
 #include <boost/scoped_ptr.hpp>
+#include <boost/make_shared.hpp>
 #include <cppconn/driver.h>
 #include <cppconn/exception.h>
 #include <cppconn/resultset.h>
 #include <cppconn/statement.h>
 #include <mysql_connection.h>
+#include <glog/logging.h>
 #include "Orm/DbResult.h"
 #include "Orm/Typedef.h"
 #include "Orm/Select.h"
@@ -29,8 +31,10 @@ public:
 	template <typename Table>
 	DbResultPtr Execute(Select<Table> select)
 	{
-		ResultSetPtr resultSet(statement->executeQuery(select.ToString()));
-		DbResultPtr dbResult(new DbResult(resultSet));
+		std::string sql = select.ToString();
+		VLOG(2) << "execute sql: " << sql;
+		ResultSetPtr resultSet(statement->executeQuery(sql));
+		DbResultPtr dbResult = boost::make_shared<DbResult>(resultSet);
 		return dbResult;
 	}
 };

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

@@ -1,9 +1,11 @@
 // Copyright: All Rights Reserved
 // Author: egametang@gmail.com (tanghai)
 
+#include <glog/logging.h>
 #include <google/protobuf/descriptor.h>
 #include "Orm/DbResult.h"
 #include "Orm/MessageField.h"
+#include "Orm/Exception.h"
 
 namespace Egametang {
 
@@ -16,7 +18,7 @@ void DbResult::FillMessage(ProtobufMessagePtr message)
 	const google::protobuf::Descriptor* descriptor = message->GetDescriptor();
 	for (int i = 0; i < descriptor->field_count(); ++i)
 	{
-		if (resultSet->isNull(i))
+		if (resultSet->isNull(i + 1))
 		{
 			continue;
 		}
@@ -28,6 +30,10 @@ void DbResult::FillMessage(ProtobufMessagePtr message)
 
 void DbResult::One(ProtobufMessagePtr message)
 {
+	if (resultSet->rowsCount() == 0)
+	{
+		throw SqlNoDataException() << SqlNoDataErrStr("sql return no data");
+	}
 	if (resultSet->next())
 	{
 		FillMessage(message);
@@ -39,4 +45,4 @@ std::size_t DbResult::Count()
 	return resultSet->rowsCount();
 }
 
-} // namespace Egametang
+} // namespace Egametang

+ 10 - 3
Cpp/Platform/Orm/DbResultTest.cc

@@ -1,6 +1,7 @@
 // Copyright: All Rights Reserved
 // Author: egametang@gmail.com (tanghai)
 
+#include <boost/make_shared.hpp>
 #include <gtest/gtest.h>
 #include <gflags/gflags.h>
 #include <glog/logging.h>
@@ -20,23 +21,29 @@ TEST_F(DbResultTest, One)
 {
 	try
 	{
-		DbHelper dbHelper("tcp://192.168.1.104:3306", "root", "111111");
+		DbHelper dbHelper("tcp://127.0.0.1:3306", "root", "111111");
 
 		DbResultPtr result = dbHelper.Execute(
 				Select<Person>(Column("*")).
 				Where(Column("age") > 10)
 			);
 
-		boost::shared_ptr<Person> person;
+		boost::shared_ptr<Person> person = boost::make_shared<Person>();
 		result->One(person);
+		ASSERT_EQ(26, person->age());
 	}
-	catch (Exception& e)
+	catch (const Exception& e)
 	{
 		if (const std::string* str=boost::get_error_info<ConnectionErrStr>(e))
 		{
 			LOG(FATAL) << *str;
 		}
+		if (const std::string* str=boost::get_error_info<SqlNoDataErrStr>(e))
+		{
+			LOG(WARNING) << *str;
+		}
 	}
+
 }
 
 } // namespace Egametang

+ 6 - 0
Cpp/Platform/Orm/Exception.h

@@ -14,6 +14,12 @@ struct ConnectionException: virtual Exception
 typedef boost::error_info<struct TagConnectionErrNO, int> ConnectionErrNO;
 typedef boost::error_info<struct TagConnectionErrStr, std::string> ConnectionErrStr;
 
+struct SqlNoDataException: virtual Exception
+{
+};
+typedef boost::error_info<struct TagSqlNoDataErrNO, int> SqlNoDataErrNO;
+typedef boost::error_info<struct TagSqlNoDataErrStr, std::string> SqlNoDataErrStr;
+
 }
 
 #endif // ORM_EXCEPTION_H

+ 2 - 2
Cpp/Platform/Orm/MessageField.cc

@@ -212,7 +212,7 @@ void MessageField::SetRepeatedField(ResultSetPtr resultSet)
 	google::protobuf::FieldDescriptor::Type type = field->type();
 
 	// 获取blob string(repeated字段统一存成blob type)
-	int index = field->index();
+	int index = field->index() + 1;
 	std::istream* is = resultSet->getBlob(index);
 	std::ostringstream os;
 	os << is->rdbuf();
@@ -315,7 +315,7 @@ void MessageField::SetOptionalField(ResultSetPtr resultSet)
 {
 	const google::protobuf::Reflection* reflection = message.GetReflection();
 	google::protobuf::FieldDescriptor::Type type = field->type();
-	int index = field->index();
+	int index = field->index() + 1;
 	switch (type)
 	{
 		case google::protobuf::FieldDescriptor::TYPE_BOOL:

+ 2 - 1
Cpp/Platform/Orm/Person.proto

@@ -3,7 +3,8 @@
 
 package Egametang;
 
-message Item {
+message Item
+{
 	optional int32 id = 1;
 	optional string name = 2;
 }