Commit 892cd666 authored by Pavel Vainerman's avatar Pavel Vainerman

(DBInterface): небольшой рефакторинг функций makeResult()

parent bb2f6345
...@@ -98,9 +98,7 @@ DBResult MySQLInterface::query( const std::string& q ) ...@@ -98,9 +98,7 @@ DBResult MySQLInterface::query( const std::string& q )
if( !res || mysql_num_rows(res) == 0 ) if( !res || mysql_num_rows(res) == 0 )
return DBResult(); return DBResult();
DBResult dbres; return makeResult(res, true);
makeResult(dbres, res, true);
return dbres;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
bool MySQLInterface::query_ok( const string& q ) bool MySQLInterface::query_ok( const string& q )
...@@ -180,14 +178,16 @@ string MySQLInterface::addslashes( const string& str ) ...@@ -180,14 +178,16 @@ string MySQLInterface::addslashes( const string& str )
return tmp.str(); return tmp.str();
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
void MySQLInterface::makeResult( DBResult& dbres, MYSQL_RES* myres, bool finalize ) DBResult MySQLInterface::makeResult( MYSQL_RES* myres, bool finalize )
{ {
DBResult result;
if( !myres ) if( !myres )
{ {
if( finalize ) if( finalize )
mysql_free_result(myres); mysql_free_result(myres);
return; return result;
} }
MYSQL_ROW mysql_row; MYSQL_ROW mysql_row;
...@@ -200,16 +200,18 @@ void MySQLInterface::makeResult( DBResult& dbres, MYSQL_RES* myres, bool finaliz ...@@ -200,16 +200,18 @@ void MySQLInterface::makeResult( DBResult& dbres, MYSQL_RES* myres, bool finaliz
for( unsigned int i = 0; i < nfields; i++ ) for( unsigned int i = 0; i < nfields; i++ )
{ {
MYSQL_FIELD* field_info = mysql_fetch_field_direct(myres, i); MYSQL_FIELD* field_info = mysql_fetch_field_direct(myres, i);
dbres.setColName( i, std::string(field_info->name) ); result.setColName( i, std::string(field_info->name) );
c.emplace_back( (mysql_row[i] != 0 ? string(mysql_row[i]) : "") ); c.emplace_back( (mysql_row[i] != 0 ? string(mysql_row[i]) : "") );
} }
dbres.row().emplace_back(c); result.row().emplace_back(c);
} }
if( finalize ) if( finalize )
mysql_free_result(myres); mysql_free_result(myres);
return result;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
extern "C" std::shared_ptr<DBInterface> create_mysqlinterface() extern "C" std::shared_ptr<DBInterface> create_mysqlinterface()
......
...@@ -76,7 +76,7 @@ namespace uniset ...@@ -76,7 +76,7 @@ namespace uniset
private: private:
void makeResult(DBResult& dbres, MYSQL_RES* r, bool finalize = true ); DBResult makeResult( MYSQL_RES* r, bool finalize = true );
MYSQL* mysql; MYSQL* mysql;
std::string lastQ; std::string lastQ;
bool connected; bool connected;
......
...@@ -180,9 +180,7 @@ DBResult PostgreSQLInterface::query( const string& q ) ...@@ -180,9 +180,7 @@ DBResult PostgreSQLInterface::query( const string& q )
/* Execute SQL query */ /* Execute SQL query */
result res( n.exec(q) ); result res( n.exec(q) );
DBResult dbres; return makeResult(res);
makeResult(dbres, res);
return dbres;
} }
catch( const std::exception& e ) catch( const std::exception& e )
{ {
...@@ -218,8 +216,10 @@ bool PostgreSQLInterface::isConnection() const ...@@ -218,8 +216,10 @@ bool PostgreSQLInterface::isConnection() const
return (db && db->is_open()); return (db && db->is_open());
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
void PostgreSQLInterface::makeResult( DBResult& dbres, const pqxx::result& res ) DBResult PostgreSQLInterface::makeResult( const pqxx::result& res )
{ {
DBResult result;
for( result::const_iterator c = res.begin(); c != res.end(); ++c ) for( result::const_iterator c = res.begin(); c != res.end(); ++c )
{ {
DBResult::COL col; DBResult::COL col;
...@@ -230,13 +230,15 @@ void PostgreSQLInterface::makeResult( DBResult& dbres, const pqxx::result& res ) ...@@ -230,13 +230,15 @@ void PostgreSQLInterface::makeResult( DBResult& dbres, const pqxx::result& res )
col.push_back(""); col.push_back("");
else else
{ {
dbres.setColName(i.num(),i.name()); result.setColName(i.num(),i.name());
col.push_back( i.as<string>() ); col.push_back( i.as<string>() );
} }
} }
dbres.row().push_back( std::move(col) ); result.row().push_back( std::move(col) );
} }
return result;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
extern "C" std::shared_ptr<DBInterface> create_postgresqlinterface() extern "C" std::shared_ptr<DBInterface> create_postgresqlinterface()
......
...@@ -64,7 +64,7 @@ namespace uniset ...@@ -64,7 +64,7 @@ namespace uniset
private: private:
void makeResult( DBResult& dbres, const pqxx::result& res ); DBResult makeResult( const pqxx::result& res );
std::shared_ptr<pqxx::connection> db; std::shared_ptr<pqxx::connection> db;
std::string lastQ; std::string lastQ;
std::string lastE; std::string lastE;
......
...@@ -175,9 +175,7 @@ DBResult SQLiteInterface::query( const string& q ) ...@@ -175,9 +175,7 @@ DBResult SQLiteInterface::query( const string& q )
lastQ = q; lastQ = q;
queryok = true; queryok = true;
DBResult dbres; return makeResult(pStmt, true);
makeResult(dbres, pStmt, true);
return dbres;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
bool SQLiteInterface::wait( sqlite3_stmt* stmt, int result ) bool SQLiteInterface::wait( sqlite3_stmt* stmt, int result )
...@@ -224,14 +222,15 @@ bool SQLiteInterface::isConnection() const ...@@ -224,14 +222,15 @@ bool SQLiteInterface::isConnection() const
return connected; return connected;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
void SQLiteInterface::makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize ) DBResult SQLiteInterface::makeResult( sqlite3_stmt* s, bool finalize )
{ {
DBResult result;
if( !s ) if( !s )
{ {
if( finalize ) if( finalize )
sqlite3_finalize(s); sqlite3_finalize(s);
return; return result;
} }
do do
...@@ -243,7 +242,7 @@ void SQLiteInterface::makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize ...@@ -243,7 +242,7 @@ void SQLiteInterface::makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize
if( finalize ) if( finalize )
sqlite3_finalize(s); sqlite3_finalize(s);
return; return result;
} }
DBResult::COL c; DBResult::COL c;
...@@ -256,7 +255,7 @@ void SQLiteInterface::makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize ...@@ -256,7 +255,7 @@ void SQLiteInterface::makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize
{ {
const char* cname = (const char*)sqlite3_column_name(s,i); const char* cname = (const char*)sqlite3_column_name(s,i);
if( cname ) if( cname )
dbres.setColName(i,cname); result.setColName(i,cname);
c.emplace_back(p); c.emplace_back(p);
} }
...@@ -264,12 +263,14 @@ void SQLiteInterface::makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize ...@@ -264,12 +263,14 @@ void SQLiteInterface::makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize
c.emplace_back(""); c.emplace_back("");
} }
dbres.row().emplace_back(c); result.row().emplace_back(c);
} }
while( sqlite3_step(s) == SQLITE_ROW ); while( sqlite3_step(s) == SQLITE_ROW );
if( finalize ) if( finalize )
sqlite3_finalize(s); sqlite3_finalize(s);
return result;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
extern "C" std::shared_ptr<DBInterface> create_sqliteinterface() extern "C" std::shared_ptr<DBInterface> create_sqliteinterface()
......
...@@ -126,7 +126,7 @@ namespace uniset ...@@ -126,7 +126,7 @@ namespace uniset
private: private:
void makeResult( DBResult& dbres, sqlite3_stmt* s, bool finalize = true ); DBResult makeResult( sqlite3_stmt* s, bool finalize = true );
sqlite3* db; sqlite3* db;
// sqlite3_stmt* curStmt; // sqlite3_stmt* curStmt;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment