summaryrefslogtreecommitdiff
path: root/src/data
diff options
context:
space:
mode:
Diffstat (limited to 'src/data')
-rwxr-xr-x[-rw-r--r--]src/data/con.cpp26
-rwxr-xr-x[-rw-r--r--]src/data/con.h12
-rwxr-xr-x[-rw-r--r--]src/data/con_base.cpp0
-rwxr-xr-x[-rw-r--r--]src/data/con_base.h10
-rw-r--r--src/data/data.cpp137
-rw-r--r--src/data/data.h26
-rw-r--r--src/data/data_base.cpp151
-rw-r--r--src/data/data_base.h47
8 files changed, 226 insertions, 183 deletions
diff --git a/src/data/con.cpp b/src/data/con.cpp
index b3aebcc..316776b 100644..100755
--- a/src/data/con.cpp
+++ b/src/data/con.cpp
@@ -11,11 +11,11 @@ con::con()
p_mysql = mysql_init(NULL);
while ( !p_mysql )
- {
- wrap::system_message( MYSQLE1 );
- usleep( 30000000 );
- mysql_init(p_mysql);
- }
+ {
+ wrap::system_message( MYSQLE1 );
+ usleep( 30000000 );
+ mysql_init(p_mysql);
+ }
while ( mysql_real_connect(
p_mysql,
@@ -25,19 +25,19 @@ con::con()
(const char*)wrap::CONF->get_elem("chat.database.dbname").c_str(),
tool::string2int(wrap::CONF->get_elem("chat.database.port")),
NULL, 0 ) == NULL )
- {
- wrap::system_message( MYSQLQU + string( mysql_error(p_mysql) ) );
- usleep( 30000000 );
- }
+ {
+ wrap::system_message( MYSQLQU + string( mysql_error(p_mysql) ) );
+ usleep( 30000000 );
+ }
}
con::~con()
{
if ( p_mysql )
- {
- if (mysql_ping( p_mysql ) != 0)
- mysql_close( p_mysql );
- }
+ {
+ if (mysql_ping( p_mysql ) != 0)
+ mysql_close( p_mysql );
+ }
}
#endif
diff --git a/src/data/con.h b/src/data/con.h
index 9fda07b..5c454f8 100644..100755
--- a/src/data/con.h
+++ b/src/data/con.h
@@ -11,12 +11,12 @@
using namespace std;
class con : public con_base
-{
-public:
- MYSQL* p_mysql;
- con( );
- ~con( );
-};
+ {
+ public:
+ MYSQL* p_mysql;
+ con( );
+ ~con( );
+ };
#endif
#endif
diff --git a/src/data/con_base.cpp b/src/data/con_base.cpp
index 003a376..003a376 100644..100755
--- a/src/data/con_base.cpp
+++ b/src/data/con_base.cpp
diff --git a/src/data/con_base.h b/src/data/con_base.h
index 9ebb89e..16086db 100644..100755
--- a/src/data/con_base.h
+++ b/src/data/con_base.h
@@ -9,11 +9,11 @@
using namespace std;
class con_base : public timo
-{
-public:
- con_base( );
- ~con_base( );
-};
+ {
+ public:
+ con_base( );
+ ~con_base( );
+ };
#endif
#endif
diff --git a/src/data/data.cpp b/src/data/data.cpp
index fa509dd..340883e 100644
--- a/src/data/data.cpp
+++ b/src/data/data.cpp
@@ -12,7 +12,7 @@ data::data()
data::~data()
{}
-hashmap<string>
+map<string,string>
data::select_user_data( string s_user, string s_query)
{
string s_where_rule = " WHERE nick = \"" + s_user + "\"";
@@ -34,11 +34,11 @@ data::select_query( string s_query, string s_where_rule, vector<string>& vec_ele
iter++;
while ( iter != vec_elements.end() )
- {
- s_mysql_query.append( secure_query(*iter) );
- if ( ++iter != vec_elements.end() )
- s_mysql_query.append( ", " );
- }
+ {
+ s_mysql_query.append( secure_query(*iter) );
+ if ( ++iter != vec_elements.end() )
+ s_mysql_query.append( ", " );
+ }
s_mysql_query.append(" FROM " + s_table + s_where_rule );
print_query( MYSQLQU + s_mysql_query );
@@ -46,36 +46,37 @@ data::select_query( string s_query, string s_where_rule, vector<string>& vec_ele
MYSQL_RES* p_result = NULL;
if ( 0 == mysql_query( p_con->p_mysql, (const char*)s_mysql_query.c_str() ) )
- {
- p_result = mysql_store_result( p_con->p_mysql );
- push_con( p_con );
- }
+ {
+ p_result = mysql_store_result( p_con->p_mysql );
+ push_con( p_con );
+ }
+
else
- {
- wrap::system_message( MYSQLQU + string( mysql_error(p_con->p_mysql) ) );
- if (p_con != NULL)
- delete p_con;
- }
+ {
+ wrap::system_message( MYSQLQU + string( mysql_error(p_con->p_mysql) ) );
+ if (p_con != NULL)
+ delete p_con;
+ }
return p_result;
}
-hashmap<string>
+map<string,string>
data::parse_result( MYSQL_RES* p_result, vector<string>& vec_elements )
{
- hashmap<string> map_ret;
+ map<string,string> map_ret;
if ( p_result != NULL )
- {
- MYSQL_ROW row;
- vector<string>::iterator vec_iter = vec_elements.begin();
- vec_iter++;
+ {
+ MYSQL_ROW row;
+ vector<string>::iterator vec_iter = vec_elements.begin();
+ vec_iter++;
- while( (row = mysql_fetch_row(p_result)) )
- for ( int i=0; i < mysql_num_fields(p_result); i++, vec_iter++ )
- map_ret[*vec_iter] = string(row[i]);
+ while( (row = mysql_fetch_row(p_result)) )
+ for ( int i=0; i < mysql_num_fields(p_result); i++, vec_iter++ )
+ map_ret[*vec_iter] = string(row[i]);
- mysql_free_result( p_result );
- }
+ mysql_free_result( p_result );
+ }
return map_ret;
}
@@ -96,26 +97,26 @@ data::insert_query( string s_query, map<string,string> map_insert )
string s_mysql_query = "INSERT INTO " + s_table + " (";
while ( iter != vec_elements.end() )
- {
- s_mysql_query.append( *iter );
+ {
+ s_mysql_query.append( *iter );
- if ( ++iter != vec_elements.end() )
- s_mysql_query.append( ", " );
- else
- s_mysql_query.append( ") VALUES(" );
- }
+ if ( ++iter != vec_elements.end() )
+ s_mysql_query.append( ", " );
+ else
+ s_mysql_query.append( ") VALUES(" );
+ }
iter = vec_elements.begin();
iter++;
while ( iter != vec_elements.end() )
- {
- s_mysql_query.append( "\"" + secure_query(map_insert[*iter]) + "\"" );
- if ( ++iter != vec_elements.end() )
- s_mysql_query.append( ", " );
- else
- s_mysql_query.append( ")" );
- }
+ {
+ s_mysql_query.append( "\"" + secure_query(map_insert[*iter]) + "\"" );
+ if ( ++iter != vec_elements.end() )
+ s_mysql_query.append( ", " );
+ else
+ s_mysql_query.append( ")" );
+ }
print_query( MYSQLQU + s_mysql_query );
@@ -130,7 +131,7 @@ data::insert_query( string s_query, map<string,string> map_insert )
}
void
-data::update_user_data( string s_user, string s_query, hashmap<string> update_map )
+data::update_user_data( string s_user, string s_query, map<string,string> update_map )
{
vector<string> vec_elements = map_queries[s_query];
@@ -147,33 +148,33 @@ data::update_user_data( string s_user, string s_query, hashmap<string> update_ma
bool b_flag = 0;
while ( iter != vec_elements.end() )
- {
- if ( update_map[*iter] == "" ) // Dont update data if it has not been changed / if its empty!
{
- iter++;
- continue;
- }
+ if ( update_map[*iter] == "" ) // Dont update data if it has not been changed / if its empty!
+ {
+ iter++;
+ continue;
+ }
- if ( iter != iter_second && b_flag )
- s_mysql_query.append( ", " );
+ if ( iter != iter_second && b_flag )
+ s_mysql_query.append( ", " );
- s_mysql_query.append( *iter + "=\"" + secure_query(update_map[*iter]) + "\"" );
- b_flag = 1;
- iter++;
- }
+ s_mysql_query.append( *iter + "=\"" + secure_query(update_map[*iter]) + "\"" );
+ b_flag = 1;
+ iter++;
+ }
if ( b_flag )
- {
- s_mysql_query.append( " WHERE nick=\"" + tool::to_lower(s_user) + "\"" );
+ {
+ s_mysql_query.append( " WHERE nick=\"" + tool::to_lower(s_user) + "\"" );
- con* p_con = get_con();
- print_query( MYSQLQU + s_mysql_query );
+ con* p_con = get_con();
+ print_query( MYSQLQU + s_mysql_query );
- if ( 0 != mysql_query( p_con->p_mysql, (const char*)s_mysql_query.c_str() ) )
- wrap::system_message( MYSQLQU + string( mysql_error(p_con->p_mysql) ) );
+ if ( 0 != mysql_query( p_con->p_mysql, (const char*)s_mysql_query.c_str() ) )
+ wrap::system_message( MYSQLQU + string( mysql_error(p_con->p_mysql) ) );
- push_con( p_con );
- }
+ push_con( p_con );
+ }
}
string
@@ -183,18 +184,18 @@ data::secure_query( string s_mysql_query )
unsigned i_pos = s_mysql_query.find("\\");
while ( i_pos != string::npos )
- {
- s_mysql_query.replace( i_pos, 1, "/" );
- i_pos = s_mysql_query.find("\\");
- }
+ {
+ s_mysql_query.replace( i_pos, 1, "/" );
+ i_pos = s_mysql_query.find("\\");
+ }
i_pos = s_mysql_query.find("\"");
while ( i_pos != string::npos )
- {
- s_mysql_query.replace( i_pos, 1, "'" );
- i_pos = s_mysql_query.find("\"");
- }
+ {
+ s_mysql_query.replace( i_pos, 1, "'" );
+ i_pos = s_mysql_query.find("\"");
+ }
return s_mysql_query;
}
diff --git a/src/data/data.h b/src/data/data.h
index a6ca90f..21c03d2 100644
--- a/src/data/data.h
+++ b/src/data/data.h
@@ -10,20 +10,20 @@
using namespace std;
class data : public data_base // data implementation used in data.h
-{
-private:
- MYSQL_RES* select_query( string s_query, string s_where_rule, vector<string>& vec_elements );
- hashmap<string> parse_result( MYSQL_RES* p_result, vector<string>& vec_elements );
- void insert_query( string s_query, map<string,string> map_insert );
- string secure_query( string s_mysql_query );
-public:
- data( );
- ~data( );
+ {
+ private:
+ MYSQL_RES* select_query( string s_query, string s_where_rule, vector<string>& vec_elements );
+ map<string,string> parse_result( MYSQL_RES* p_result, vector<string>& vec_elements );
+ void insert_query( string s_query, map<string,string> map_insert );
+ string secure_query( string s_mysql_query );
+ public:
+ data( );
+ ~data( );
- hashmap<string> select_user_data( string s_user, string s_query );
- void insert_user_data( string s_user, string s_query, map<string,string> insert_map );
- void update_user_data( string s_user, string s_query, hashmap<string> update_map );
-};
+ map<string,string> select_user_data( string s_user, string s_query );
+ void insert_user_data( string s_user, string s_query, map<string,string> insert_map );
+ void update_user_data( string s_user, string s_query, map<string,string> update_map );
+ };
#endif
#endif
diff --git a/src/data/data_base.cpp b/src/data/data_base.cpp
index 61fc141..2cbe070 100644
--- a/src/data/data_base.cpp
+++ b/src/data/data_base.cpp
@@ -15,39 +15,40 @@ data_base::data_base( )
// Reads all DATA_ elements of conf.txt!
for ( iter = vec_keys.begin(); iter != vec_keys.end(); iter++ )
- {
- if ( iter->length() > 20
- && iter->compare(0, 19, "chat.database.mysql") == 0
- && iter->find(".descr") == string::npos )
{
- vector<string> vec_tokens;
- string s_element = wrap::CONF->get_elem(*iter);
- unsigned i_pos = 0;
-
- for ( bool b_find = 1; b_find; )
- {
- i_pos = s_element.find_first_of( " ", 0 );
-
- if ( i_pos != string::npos )
+ if ( iter->length() > 20
+ && iter->compare(0, 19, "chat.database.mysql") == 0
+ && iter->find(".descr") == string::npos )
{
- vec_tokens.push_back( s_element.substr(0, i_pos) );
- s_element = s_element.substr( i_pos+1 );
+ vector<string> vec_tokens;
+ string s_element = wrap::CONF->get_elem(*iter);
+ unsigned i_pos = 0;
+
+ for ( bool b_find = 1; b_find; )
+ {
+ i_pos = s_element.find_first_of( " ", 0 );
+
+ if ( i_pos != string::npos )
+ {
+ vec_tokens.push_back( s_element.substr(0, i_pos) );
+ s_element = s_element.substr( i_pos+1 );
+ }
+
+ else
+ {
+ vec_tokens.push_back( s_element );
+ b_find = 0;
+ }
+ }
+
+ string s_mysqlquery = iter->substr(20);
+ wrap::system_message(MYSQLQ2 + s_mysqlquery);
+ map_queries[s_mysqlquery] = vec_tokens;
}
- else
- {
- vec_tokens.push_back( s_element );
- b_find = 0;
- }
- }
-
- string s_mysqlquery = iter->substr(20);
- wrap::system_message(MYSQLQ2 + s_mysqlquery);
- map_queries[s_mysqlquery] = vec_tokens;
}
- }
}
-void data_base::init_connections()
+void data_base::initialize_connections()
{
int i_min_con = tool::string2int( wrap::CONF->get_elem("chat.database.mincon") ),
i_max_con = tool::string2int( wrap::CONF->get_elem("chat.database.maxcon") );
@@ -57,6 +58,11 @@ void data_base::init_connections()
for ( int i = 0; i < i_min_con && i < i_max_con; i++ )
push_back( new con() );
+
+#ifdef NCURSES
+
+ print_queue_size();
+#endif
}
data_base::~data_base()
@@ -64,23 +70,23 @@ data_base::~data_base()
pthread_mutex_destroy ( &mut_con );
}
-hashmap<string>
+map<string,string>
data_base::select_user_data( string s_user, string s_query)
{
print_query( DATAQUE + s_query );
- hashmap<string> map_ret;
+ map<string,string> map_ret;
return map_ret;
}
void
-data_base::insert_user_data( string s_user, string s_query, hashmap<string> insert_map )
+data_base::insert_user_data( string s_user, string s_query, map<string,string> insert_map )
{
print_query( DATAQUE + s_query );
}
void
-data_base::update_user_data( string s_user, string s_query, hashmap<string> update_map )
+data_base::update_user_data( string s_user, string s_query, map<string,string> update_map )
{
print_query( DATAQUE + s_query );
}
@@ -107,23 +113,29 @@ data_base::get_con()
pthread_mutex_lock( &mut_con );
if ( empty() )
- {
- wrap::system_message( DATANEW + string("(") + tool::int2string(size()+1) + ")" );
- pthread_mutex_unlock( &mut_con );
- return new con;
- }
+ {
+ wrap::system_message( DATANEW + string("(") + tool::int2string(size()+1) + ")" );
+ pthread_mutex_unlock( &mut_con );
+ return new con;
+ }
+
else if ( size() > i_max_con-1 )
- {
- wrap::system_message( DATAMAX + string("(") + tool::int2string(i_max_con) + ")" );
- usleep( 5000000 );
- pthread_mutex_unlock( &mut_con );
- return get_con();
- }
+ {
+ wrap::system_message( DATAMAX + string("(") + tool::int2string(i_max_con) + ")" );
+ usleep( 5000000 );
+ pthread_mutex_unlock( &mut_con );
+ return get_con();
+ }
con* p_con = *begin();
pop_front();
+#ifdef NCURSES
+
+ print_queue_size();
+#endif
+
pthread_mutex_unlock( &mut_con );
wrap::system_message( DATAGET );
@@ -136,11 +148,27 @@ data_base::push_con( con* p_con )
{
pthread_mutex_lock( &mut_con );
push_front( p_con );
+#ifdef NCURSES
+
+ print_queue_size();
+#endif
pthread_mutex_unlock( &mut_con );
wrap::system_message( DATAADD );
}
+#ifdef NCURSES
+void
+data_base::print_queue_size()
+{
+ if ( wrap::NCUR->is_ready() )
+ {
+ mvprintw( NCUR_CON_QUEUE_X,NCUR_CON_QUEUE_Y, "Con. queue: %d ", size());
+ refresh();
+ }
+}
+#endif
+
void
data_base::disconnect_all_connections()
{
@@ -148,13 +176,17 @@ data_base::disconnect_all_connections()
pthread_mutex_lock( &mut_con );
while ( !empty() )
- {
- con* p_con = *begin();
- pop_front();
- delete p_con;
- }
+ {
+ con* p_con = *begin();
+ pop_front();
+ delete p_con;
+ }
pthread_mutex_unlock( &mut_con );
+#ifdef NCURSES
+
+ print_queue_size();
+#endif
}
void
@@ -168,22 +200,27 @@ data_base::check_data_con_timeout()
list< list<con*>::iterator > erase_list;
for ( list<con*>::iterator iter = begin();
iter != end(); iter++ )
- {
- i_last_activity = (int) (*iter)->get_last_activity();
- if ( i_timeout_time <= i_last_activity )
{
- con* p_con = *iter;
- erase_list.push_back(iter);
- delete p_con;
- wrap::system_message(DATADI2 + tool::int2string(size()-erase_list.size()+1) + ","
- + tool::int2string(i_timeout_time) + ","
- + tool::int2string(i_last_activity) + ")");
+ i_last_activity = (int) (*iter)->get_last_activity();
+ if ( i_timeout_time <= i_last_activity )
+ {
+ con* p_con = *iter;
+ erase_list.push_back(iter);
+ delete p_con;
+ wrap::system_message(DATADI2 + tool::int2string(size()-erase_list.size()+1) + ","
+ + tool::int2string(i_timeout_time) + ","
+ + tool::int2string(i_last_activity) + ")");
+ }
}
- }
for ( list< list<con*>::iterator >::iterator erase_iter = erase_list.begin();
erase_iter != erase_list.end(); erase_iter++ )
erase( *erase_iter );
+#ifdef NCURSES
+
+ print_queue_size();
+#endif
+
pthread_mutex_unlock( &mut_con );
}
diff --git a/src/data/data_base.h b/src/data/data_base.h
index dbad3f0..97ad31e 100644
--- a/src/data/data_base.h
+++ b/src/data/data_base.h
@@ -11,33 +11,38 @@
using namespace std;
class data_base : protected list<con*>
-{
-private:
- pthread_mutex_t mut_con;
- int i_max_con;
-
-protected:
- hashmap< vector<string> > map_queries;
- void print_query( string s_query );
+ {
+ private:
+ pthread_mutex_t mut_con;
+ int i_max_con;
+
+#ifdef NCURSES
+
+ void print_queue_size();
+#endif
+
+ protected:
+ map< string, vector<string> > map_queries;
+ void print_query( string s_query );
#ifdef DATA_PRINT_QUERIES
- virtual void print_query_( string s_query );
+ virtual void print_query_( string s_query );
#endif
- con* get_con();
- void push_con( con* p_con );
+ con* get_con();
+ void push_con( con* p_con );
-public:
- data_base();
- ~data_base();
+ public:
+ data_base();
+ ~data_base();
- void init_connections();
- virtual hashmap<string> select_user_data( string s_user, string s_query );
- virtual void insert_user_data( string s_user, string s_query, hashmap<string> insert_map );
- virtual void update_user_data( string s_user, string s_query, hashmap<string> update_map );
- void disconnect_all_connections();
- void check_data_con_timeout();
-};
+ void initialize_connections();
+ virtual map<string,string> select_user_data( string s_user, string s_query );
+ virtual void insert_user_data( string s_user, string s_query, map<string,string> insert_map );
+ virtual void update_user_data( string s_user, string s_query, map<string,string> update_map );
+ void disconnect_all_connections();
+ void check_data_con_timeout();
+ };
#endif
#endif