Commit e825963b authored by Pavel Vainerman's avatar Pavel Vainerman

(DBInterace): первая версия реализации получения значений по имени столбца,

а не по индексу setbug #12672
parent fba36c3e
......@@ -180,7 +180,7 @@ string MySQLInterface::addslashes( const string& str )
return tmp.str();
}
// -----------------------------------------------------------------------------------------
void MySQLInterface::makeResult(DBResult& dbres, MYSQL_RES* myres, bool finalize )
void MySQLInterface::makeResult( DBResult& dbres, MYSQL_RES* myres, bool finalize )
{
if( !myres )
{
......@@ -198,7 +198,12 @@ void MySQLInterface::makeResult(DBResult& dbres, MYSQL_RES* myres, bool finalize
DBResult::COL c;
for( unsigned int i = 0; i < nfields; i++ )
{
MYSQL_FIELD* field_info = mysql_fetch_field_direct(myres, i);
dbres.setColName( i, std::string(field_info->name) );
c.emplace_back( (mysql_row[i] != 0 ? string(mysql_row[i]) : "") );
}
dbres.row().emplace_back(c);
}
......
......@@ -42,8 +42,9 @@ int main(int argc, char** argv)
for( DBResult::COL::iterator cit = it->begin(); cit != it->end(); cit++ )
cout << DBResult::as_string(cit) << "(" << DBResult::as_double(cit) << ") | ";
cout << endl;
cout << "ID: " << r.as_string(it,"id") << endl;
}
db.close();
......
......@@ -218,14 +218,19 @@ bool PostgreSQLInterface::isConnection() const
return (db && db->is_open());
}
// -----------------------------------------------------------------------------------------
void PostgreSQLInterface::makeResult(DBResult& dbres, const pqxx::result& res )
void PostgreSQLInterface::makeResult( DBResult& dbres, const pqxx::result& res )
{
for( result::const_iterator c = res.begin(); c != res.end(); ++c )
{
DBResult::COL col;
for( pqxx::result::tuple::const_iterator i = c.begin(); i != c.end(); i++ )
{
if( !i.is_null() )
dbres.setColName(i.num(),i.name());
col.push_back( (i.is_null() ? "" : i.as<string>()) );
}
dbres.row().push_back( std::move(col) );
}
......
......@@ -64,7 +64,7 @@ namespace uniset
private:
void makeResult(DBResult& dbres, const pqxx::result& res );
void makeResult( DBResult& dbres, const pqxx::result& res );
std::shared_ptr<pqxx::connection> db;
std::string lastQ;
std::string lastE;
......
......@@ -253,7 +253,12 @@ void SQLiteInterface::makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize
char* p = (char*)sqlite3_column_text(s, i);
if( p )
{
const char* cname = (const char*)sqlite3_column_name(s,i);
if( cname )
dbres.setColName(i,cname);
c.emplace_back(p);
}
else
c.emplace_back("");
}
......
......@@ -126,7 +126,7 @@ namespace uniset
private:
void makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize = true );
void makeResult( DBResult& dbres, sqlite3_stmt* s, bool finalize = true );
sqlite3* db;
// sqlite3_stmt* curStmt;
......
......@@ -38,6 +38,8 @@ int main(int argc, char** argv)
cout << DBResult::as_string(cit) << "(" << DBResult::as_double(cit) << ") | ";
cout << endl;
cout << "ID: " << r.as_string(it,"id") << endl;
}
db.close();
......
......@@ -4,6 +4,7 @@
#include <string>
#include <deque>
#include <vector>
#include <unordered_map>
#include "UniSetTypes.h"
// --------------------------------------------------------------------------
namespace uniset
......@@ -67,6 +68,10 @@ namespace uniset
static int as_int( const DBResult::iterator& it, int col );
static double as_double( const DBResult::iterator& it, int col );
static std::string as_string( const DBResult::iterator& it, int col );
int as_int( const DBResult::iterator& it, const std::string& colname );
double as_double( const DBResult::iterator& it, const std::string& colname );
std::string as_string(const DBResult::iterator& it, const std::string& colname );
// ----------------------------------------------------------------------------
// COL
static int as_int( const DBResult::COL::iterator& it );
......@@ -75,9 +80,16 @@ namespace uniset
static size_t num_cols( const DBResult::iterator& it );
// ----------------------------------------------------------------------------
// установить соответсвие индекса и имени поля
void setColName( int index, const std::string& name );
std::string getColName( int index ); // slow function
protected:
ROW row_;
std::unordered_map<std::string, int> colname;
};
// ----------------------------------------------------------------------------------
struct DBInterfaceDeleter
......
......@@ -93,12 +93,55 @@ namespace uniset
{
return ((*it)[col]);
}
int DBResult::as_int( const DBResult::iterator& it, const std::string& cname )
{
auto i = colname.find(cname);
if( i == colname.end() )
throw std::runtime_error("(DBInterface): Unknown field ='" + cname + "'");
return as_int(it,i->second);
}
double DBResult::as_double(const DBResult::iterator& it, const std::string& cname)
{
auto i = colname.find(cname);
if( i == colname.end() )
throw std::runtime_error("(DBInterface): Unknown field ='" + cname + "'");
return as_double(it, i->second);
}
std::string DBResult::as_string(const DBResult::iterator& it, const std::string& cname )
{
auto i = colname.find(cname);
if( i == colname.end() )
throw std::runtime_error("(DBInterface): Unknown field ='" + cname + "'");
return as_string(it, i->second);
}
// ----------------------------------------------------------------------------
size_t DBResult::num_cols( const DBResult::iterator& it )
{
return it->size();
}
// ----------------------------------------------------------------------------
void DBResult::setColName( int index, const std::string& name )
{
colname[name] = index;
}
// ----------------------------------------------------------------------------
std::string DBResult::getColName( int index )
{
for( auto&& c: colname )
{
if( c.second == index )
return c.first;
}
return "";
}
// ----------------------------------------------------------------------------
int DBResult::as_int( const DBResult::COL::iterator& it )
{
return uniset::uni_atoi( (*it) );
......
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