From: Norbert Moutarde Date: Sat, 29 Jun 2013 11:29:15 +0000 (+0200) Subject: file to handle database added ! X-Git-Url: https://git.nothing2do.fr/?a=commitdiff_plain;h=c23791f695686d40da0829ef9ce5b202924d950e;p=diary-shell.git file to handle database added ! --- diff --git a/sqldb.cpp b/sqldb.cpp new file mode 100644 index 0000000..85d28f2 --- /dev/null +++ b/sqldb.cpp @@ -0,0 +1,274 @@ +#ifndef sql +#define sql 1 +#include "sqldb.h" +#include +#include +#include +#include +#include +#include +using namespace std; + +sqlpg::sqlpg() { + string var; + //var = "host=postgresql1.alwaysdata.com port=5432 dbname=nothing2do.eu_diary user=nothing2do.eu password=x connect_timeout=10"; + var = "dbname=bob user=bob password=pass connect_timeout=10"; + cout << "Chaîne de connection: " + var << endl; + //conn = PQconnectStart(var.c_str()); + conn = PQconnectdb(var.c_str()); +} + +int sqlpg::status(){ + string err=toString(PQstatus(conn)); + switch(PQstatus(conn)) { + case CONNECTION_OK: + cout << "La connection à la base est Ok" << endl; + return 0; + break; + case CONNECTION_BAD: + cout << "La connection à la base est KO : " << PQerrorMessage(conn) << endl; + return 1; + break; + default: + cout << "Erreur de connection non répertoriée : (" + err + ") "<< PQerrorMessage(conn) << endl; + return -1; + } +} +template string sqlpg::toString( const T & Value ){ + // utiliser un flux de sortie pour créer la chaîne + ostringstream oss; + // écrire la valeur dans le flux + oss << Value; + // renvoyer une string + return oss.str(); +} +int sqlpg::insert(vector text) { + string command; + string tmp=""; + + if (text.size()>2){ + if ((isdate(text[0]))+(isdate(text[1]))==3){ + cout<<"date detecte\n"; + date=text[0]; + tmp=text[2]; + for (int a=3;atm_year, 1+ltm->tm_mon, ltm->tm_mday, 1+ltm->tm_hour, 1+ltm->tm_min, 1+ltm->tm_sec); + string d(date); + return d; +} +int isdate(string a){ + if (((a[4])=='-')&&((a[7])=='-')){ + return 1; + } + if (((a[2])==':')&&((a[5])==':')){ + return 2; + } +} +int sqlpg::hmRecord(){ + string command; + command="select date from raw;"; + //res=PQexec(conn, command.c_str()); + exec(command); + a=PQntuples(res); + return a; +} +void sqlpg::var(){ + cout<<"date="< a){ + cout<<"a.size()=="<> buf) {//exec for each words in tuples + string a="select time,txt from words where txt='"+buf+"';", command; + res2=PQexec(conn, a.c_str()); + testSQL(res2); + int tmp=PQntuples(res2); + if(tmp==0){ + command="insert into words (time, txt) values ('1', '"+buf+"')"; + } + else if(tmp==1){ + stringstream t; + t<<"update words set time="< +#include +std::string now(); +int isdate(std::string);//return 1 for YYYY-MM-DD and 2 for HH:MM:SS + +class sqlpg { +public: + sqlpg (); + ~sqlpg(); + template static std::string toString( const T & Value ); + int status();// return 0 if success, 1 for error, -1 + int insert(std::vector);// format=yyyy-month-day hour:min:sec text + std::vector < std::vector > list(std::string);// to list the content of table "string" + std::vector < std::vector > listdate(std::string, std::string=now());// to list the content of table "raw" between 2 date (date and now, by défault) + //std::vector > size(std::string="");//size of each tuple + void deletetuple(std::string); + void deletedate(std::string, std::string);// delete tuple between two date + void print();// print result of exec(string) call + int hmRecord();// How Many records in "raw" table + //std::vector search(std::string);//return id of record containing string, useless (SQL deo the same) + void var(); + inline int testSQL(PGresult*);// test if all gone good + void exec(std::vector);// first string is for SQL command, other are to replace %1,%2, etc... + void exec(std::string); + bool alias(std::vector); + void compulse();//populate table words +private: + PGconn *conn; + PGresult *res, *res2, *res3; + std::vector < std::vector > ret;//useless ? + std::string b, date; + int a; +}; + +#endif // SQLDB_H