--- /dev/null
+#ifndef sql
+#define sql 1
+#include "sqldb.h"
+#include <sstream>
+#include <vector>
+#include <cstdlib>
+#include <sys/types.h>
+#include <stdlib.h>
+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<typename T> 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<string> 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;a<text.size(); a++){
+ tmp=tmp+" "+text[a];
+ }
+ command="INSERT INTO raw (date, texte) VALUES ('"+date+" "+text[1]+"', '"+tmp+"')";
+ }
+ else if((text[0]=="-")&&(isdate(text[1])==2)){
+ cout<<"date partielle\n";
+ tmp=text[2];
+ for (int a=3;a<text.size(); a++){
+ tmp=tmp+" "+text[a];
+ }
+ command="INSERT INTO raw (date, texte) VALUES ('"+date+" "+text[1]+"', '"+tmp+"')";
+ }
+ else{
+ tmp=text[0];
+ for (int a=1;a<text.size(); a++){
+ tmp=tmp+" "+text[a];
+ }
+ command="INSERT INTO raw (date, texte) VALUES ('now()', '"+tmp+"')";
+ };
+
+ }
+ else {
+ tmp=text[0];
+ for (int a=1;a<text.size(); a++){
+ tmp=tmp+" "+text[a];
+ };
+ command="INSERT INTO raw (date, texte) VALUES ('now()', '"+tmp+"')";
+ };
+
+ //cout<<command<<endl;
+ /*res=PQexec(conn, command.c_str());
+ testSQL();*/
+ exec(command);
+ return 0;
+}
+vector< vector <string> > sqlpg::list(string a){
+ vector<string> tmp;
+ string command;
+ ret.clear();
+ command="SELECT * FROM "+a+" ORDER BY date ASC;";//PQfname(res, 0) can't works here !
+
+ //cout<<command<<endl;
+
+ //res=PQexec(conn, command.c_str());
+ exec(command);
+ for (int i=0; i<PQntuples(res); i++){
+ for (int j=0; j<PQnfields(res); j++){
+ tmp.push_back(PQgetvalue(res, i, j));
+ };
+ ret.push_back(tmp);
+ tmp.clear();
+ };
+ return ret;
+}
+std::vector < std::vector <std::string> > sqlpg::listdate(string begin, string end){
+ vector<string> tmp;
+ string command;
+ ret.clear();
+ command="SELECT * FROM raw WHERE date BETWEEN '"+begin+"' AND '"+end+"';";
+ //cout<<command<<endl;
+ /*res=PQexec(conn, command.c_str());
+ testSQL();*/
+ exec(command);
+ for (int i=0; i<PQntuples(res); i++){
+ for (int j=0; j<PQnfields(res); j++){
+ tmp.push_back(PQgetvalue(res, i, j));
+ };
+ ret.push_back(tmp);
+ tmp.clear();
+ };
+ return ret;
+};
+void sqlpg::deletetuple(string tmp){
+ string command;
+ command="DELETE FROM raw WHERE id ='" + tmp + "'";
+ /*res=PQexec(conn, command.c_str());
+ testSQL();*/
+ exec(command);
+ //cout<<command<<endl;
+}
+void sqlpg::deletedate(string a, string b){
+ string command;
+ command="delete from raw where date between '"+a+"' and '"+b+"';";
+ /*res=PQexec(conn,command.c_str());
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ {
+ cout<<"command failed: "<< PQerrorMessage(conn)<<endl;
+ }*/
+ exec(command);
+ //cout<<command<<endl;
+}
+string now(){//SQL function NOW() exist !
+// current date/time based on current system
+ time_t now = time(0);
+ tm *ltm = localtime(&now);
+ char * date;
+ // print various components of tm structure.
+ sprintf(date,"%d-%d-%d %d:%d:%d", 1900+ltm->tm_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="<<date<<endl;
+}
+sqlpg::~sqlpg() {
+ PQclear(res);
+ PQfinish(conn);
+}
+void sqlpg::print(){
+for (int i=0; i<PQntuples(res); i++){
+ for (int j=0; j<PQnfields(res)-1; j++){
+ cout<<PQgetvalue(res, i, j)<<"+";
+ };
+ cout<<PQgetvalue(res, i, PQnfields(res)-1)<<" & ";
+ };
+
+cout<<endl;
+}
+void sqlpg::exec(string a){
+ //cout<<"exec("<<a<<")\n";
+
+ //res=PQexec(conn, a.c_str());
+ res=PQexec(conn, a.c_str());
+ //cout<<"exec:1/2\n";
+ if (!((PQresultStatus(res) == PGRES_COMMAND_OK)||(PQresultStatus(res) == PGRES_TUPLES_OK)))
+ {
+ cout<<"PQerrorMessage(conn) : "<< PQerrorMessage(conn)<<endl;
+ cout<<"PQresStatus(res)="<<PQresStatus(PQresultStatus(res))<<endl;
+ };
+ //cout<<"exec() finish\n";
+}
+void sqlpg::exec(vector<string> a){
+ cout<<"a.size()=="<<a.size()<<"\n";
+ /*const char *paramValues[1];
+ int paramLengths[1];
+ int paramFormats[1];
+ uint32_t binaryIntVal;
+
+res = PQexecParams(conn,
+ "SELECT * FROM test1 WHERE t = $1",
+ 1, /* one param
+ NULL, /* let the backend deduce param type
+ paramValues,
+ NULL, /* don't need param lengths since text
+ NULL, /* default to all text params
+ 0); /* ask for text results
+
+*/
+ const char *paramValues[1];
+ int paramLengths[1];
+ int paramFormats[1];
+ //uint32_t binaryIntVal;
+paramValues[0]=a[1].c_str();
+ res = PQexecParams(conn,
+ "SELECT * FROM raw WHERE texte like %$1%",
+ 1, /* one param */
+ NULL, /* let the backend deduce param type */
+ paramValues,
+ NULL, /* don't need param lengths since text */
+ NULL, /* default to all text params */
+ 0); /* ask for text results */
+ cout<<"exec() finish\n";
+}
+inline int sqlpg::testSQL(PGresult* a){
+ if (!((PQresultStatus(a) == PGRES_COMMAND_OK)||(PQresultStatus(a) == PGRES_TUPLES_OK)))
+ {
+ cout<<"PQerrorMessage(conn): "<< PQerrorMessage(conn)<<endl;
+ cout<<"PQresStatus(res)="<<PQresStatus(PQresultStatus(a))<<endl;
+ };
+}
+bool sqlpg::alias(vector<string> a){
+ string cmd;
+ cmd="select command from alias where nom='"+a[0]+"';";
+
+ //cout<<"cmd="<<cmd<<endl;
+ exec(cmd);
+ if (PQntuples(res)<1)return 0;
+ string alias;
+ alias=PQgetvalue(res, 0, 0);
+ //cout<<"alias="<<alias<<endl;
+ int pos=0;
+ stringstream tmp;
+ for(int i=1; i<a.size(); i++){
+ for (int j=1; j<=10; j++){// 10 is arbitrarily choosed
+ tmp<<"$"<<j;
+ int pos=alias.find(tmp.str());
+ //cout<<"tmp.str()="<<tmp.str()<<endl;
+ if(!((pos==0)||(pos==-1))){
+ alias.replace(pos, 2, a[i]);
+ //cout<<"pos="<<pos<<", alias="<<alias<<endl;
+ }
+ tmp.flush();
+ }
+ }
+ //cout<<"alias="<<alias<<"\n";
+
+ if (PQresultStatus(res)==PGRES_TUPLES_OK){
+ exec(alias);
+ print();
+ return 1;
+ };
+ return 0;
+}
+void sqlpg::compulse(){
+ string cmd="select * from raw where analyzed='0' order by date asc;";
+ exec(cmd);
+ //cout<<"exec1("<<cmd<<")\n";
+ cout<<"PQntuples(res)="<<PQntuples(res)<<endl;
+ int tuples=PQntuples(res);
+ for (int i=0; i<tuples;i++){
+ string buf, txt=PQgetvalue(res, i, PQfnumber(res, "texte"));
+ stringstream ss(txt);
+ while (ss >> 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="<<atoi(PQgetvalue(res2, 0, 0))+1<<" where txt='"<<buf<<"';";
+ command=t.str();
+ }
+ else{
+ cout<<"erreur de logique (txt est une clé primaire...)\nPQntuple(res)=="<<tmp<<"\n";
+ };
+ testSQL(PQexec(conn, command.c_str()));
+ };
+ stringstream y;
+ y<<"update raw set analyzed='1' where date='"<<(PQgetvalue(res, i, PQfnumber(res, "date")))<<"';";
+ //cout<<"t="<<t<<endl;
+ testSQL(PQexec(conn, y.str().c_str()));
+ //exec(y.str()); it don't works and i don't know why
+ }
+};
+
+
+
+#endif