]> git.nothing2do.fr Git - diary-shell.git/commitdiff
added some function
authorNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Fri, 5 Jul 2013 17:22:02 +0000 (19:22 +0200)
committerNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Fri, 5 Jul 2013 17:22:02 +0000 (19:22 +0200)
alias.sql
main.cpp
sqldb.cpp
sqldb.h
tables1.sql

index 3f5449628e8351c9550f66c173a697f98a5c5c96..8b426c9a6618e9a97e74bbcae58fef78f26e4ffa 100644 (file)
--- a/alias.sql
+++ b/alias.sql
@@ -1,5 +1,7 @@
 INSERT INTO alias (nom, command, argc) VALUES ('ls', 'select id,texte from raw order by date asc;', 0);
 INSERT INTO alias (nom, command, argc) VALUES ('search', 'select id,texte from raw where texte ~ $1 order by date asc;', 1);
-INSERT INTO alias (nom, command, argc) VALUES ('search', 'select id,texte from raw where texte ~ $1 order by date asc', 1);
 INSERT INTO alias (nom, command, argc) VALUES ('rm', 'DELETE FROM raw WHERE id =''%1'';', 1);
 INSERT INTO alias (nom, command, argc) VALUES ('alias', 'select nom from alias;', 0);
+insert into alias (nom, command, argc) values ('memo', 'select word from memo;', 0);
+insert into alias (nom, command, argc) values ('memo', 'select id, word, rem from memo;"', 1);
+insert into alias (nom, command, argc) values ('rdv',
index a5601143756869b3bbf9ba77773657d171cecf85..03763b163c97df29eb3e0315f10cdd51811b8ceb 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -43,7 +43,7 @@ do{
     else if (words[0]=="status") diary.status();
 
     else if (words[0]=="var") diary.var();
-    else if (words[0]=="now") cout<<now()<<endl;
+    //else if (words[0]=="now") cout<<diary.now()<<endl;
     else if (words[0]=="alias") {
         if (words.size()==1) {diary.exec("select nom from alias;");diary.print();}
         else if (words[1]=="add") {
index 315a013bb963bde72f915af5d13860c592f8f826..f921ef21a84fc6d13c8273a60393e9213ff4bd42 100644 (file)
--- a/sqldb.cpp
+++ b/sqldb.cpp
 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;
+    connect = "dbname=bob user=bob password=pass connect_timeout=10";
     //conn =  PQconnectStart(var.c_str());
-    conn =  PQconnectdb(var.c_str());
+    conn =  PQconnectdb(connect.c_str());
 }
 
 int sqlpg::status(){
@@ -43,77 +42,109 @@ template<typename T> string sqlpg::toString( const T & Value ){
     return oss.str();
 }
 int sqlpg::insert(vector<string> text) {
-    string command;
+    cerr<<"insert(vector<string>) begin\n";
+    string command="INSERT INTO raw (date, texte) VALUES ($1::timestamp, $2);";
     string tmp="";
+    int argc=2;
+    const char * value[2];
+    int length[2];
+    int binary[2];
 
     if (text.size()>2){
         if ((isdate(text[0]))+(isdate(text[1]))==3){
-            cout<<"date detecte\n";
+            cerr<<"text[0]+text[1]==3(L55)\n";
             date=text[0];
+            string a=date+" "+text[1];
+            value[0]=a.c_str();
+            length[0]=a.size();
+            binary[0]=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+"')";
+            value[1]=tmp.c_str();
+            length[1]=tmp.size();
+            binary[1]=0;
+            //command="INSERT INTO raw (date, texte) VALUES ('"+date+" "+text[1]+"', '"+tmp+"')";
+
         }
         else if((text[0]=="-")&&(isdate(text[1])==2)){
-            cout<<"date partielle\n";
+            cerr<<"date partielle(L72)\n";
+            tmp=text[2];
+            string a=date+" "+text[1];
+            value[0]=a.c_str();
+            length[0]=a.size();
+            binary[0]=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+"')";
+            value[1]=tmp.c_str();
+            length[1]=tmp.size();
+            binary[1]=0;
+            //command="INSERT INTO raw (date, texte) VALUES ('"+date+" "+text[1]+"', '"+tmp+"')";
+
         }
         else{
+            cerr<<"text.size()>2; text[0] & text[1] isn't date. L89";
             tmp=text[0];
+            string a="1971-11-11 22:22:22";
+            value[0]=a.c_str();
+            length[0]=a.size();
+            binary[0]=0;
+            //tmp=text[2];
+            value[1]=tmp.c_str();
+            length[1]=tmp.size();
+            binary[1]=0;
             for (int a=1;a<text.size(); a++){
                 tmp=tmp+" "+text[a];
             }
-            command="INSERT INTO raw (date, texte) VALUES ('now()', '"+tmp+"')";
+            //command="INSERT INTO raw (date, texte) VALUES ('now()', '"+tmp+"')";
+
         };
 
     }
     else {
+        cerr<<"text.size()<2 L111\n";
         tmp=text[0];
-        for (int a=1;a<text.size(); a++){
+        cerr<<"113\n";
+        string a="now";
+        cerr<<"115\n";
+        value[0]=a.c_str();
+        length[0]=a.size();
+        binary[0]=0;
+        for (int a=0;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);
+        }
+        value[1]=tmp.c_str();
+        length[1]=tmp.size();
+        binary[1]=0;
+        //value[1]=now().c_str();
+        length[1]=19;
+        binary[1]=0;
+        cerr<<"text.size()<2 finish\n";
+    };
+    res=PQexecParams(conn, command.c_str(), argc, NULL, value, length, binary, 0);
+    testSQL(res);
+    cerr<<"insert(vector<string>) end\n";
+    //exec(command);
     return 0;
 }
 
-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 !
+string sqlpg::now(int day){//day=0 return only HH:MM:SS, day=1 return YYYY-MM-DD HH:MM:SS, day=2 YYYY-MM-DD
 // 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);
+   if (day==0)sprintf(date,"%d:%d:%d", 1+ltm->tm_hour, 1+ltm->tm_min, 1+ltm->tm_sec);
+   else if (day==1)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);
+   else if (day==2)sprintf(date,"%d-%d-%d", 1900+ltm->tm_year, 1+ltm->tm_mon, ltm->tm_mday);
+   else {
+       cout<<"error in date";
+       date="0000-00-00 00;00;00";
+   }
+
    string d(date);
    return d;
 }
@@ -134,7 +165,7 @@ int sqlpg::hmRecord(){
     return a;
 }
 void sqlpg::var(){
-    cout<<"date="<<date<<endl;
+    cout<<"date="<<date<<", chaîne de connection: " + connect << endl;;
 }
 sqlpg::~sqlpg() {
     PQclear(res);
@@ -146,24 +177,24 @@ for (int i=0; i<PQntuples(res); i++){
     for (int j=0; j<lim-1; j++){
             cout<<PQgetvalue(res, i, j)<<"+";
         };
-        cout<<PQgetvalue(res, i, lim-1)<<"|";
+        cout<<PQgetvalue(res, i, lim-1)<<"\n";
     };
 
 cout<<endl;
 }
 void sqlpg::exec(string a){
-    //cout<<"exec("<<a<<")\n";
-    res=PQexec(conn, a.c_str());
-    //cout<<"exec:1/2\n";
+    cerr<<"exec(string "<<a<<")\n";
+    res=PQexecParams(conn, a.c_str(), 0, NULL, 0, NULL, NULL, 0);
+    cerr<<"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";
+    cerr<<"exec() finish\n";
 }
 void sqlpg::exec(vector<string> a){
-    cout<<"a.size()=="<<a.size()<<"\n";
+    cerr<<"exec(vector<string>: a.size()=="<<a.size()<<"\n";
     /*const char *paramValues[1];
     int         paramLengths[1];
     int         paramFormats[1];
@@ -179,20 +210,21 @@ res = PQexecParams(conn,
     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";
+    int argc=a.size()-1;
+    cerr<<"argc="<<argc<<endl;
+    const char *value[argc];
+    int length[argc];
+    int binary[argc];
+    for(int i=0; i<argc; i++){
+        value[i]=a[i+1].c_str();
+        length[i]=a[i+1].size();
+        binary[i]=0;
+        cerr<<"i="<<i<<" value="<<value[i]<<" length="<<length[i]<<"\n";
+    }
+    res=PQexecParams(conn, a[0].c_str(), argc, NULL, value, length, binary, 0);
+    testSQL(res);
+    print();
+    cerr<<"exec() finish\n";
 }
 inline int sqlpg::testSQL(PGresult* a){
     if (!((PQresultStatus(a) == PGRES_COMMAND_OK)||(PQresultStatus(a) == PGRES_TUPLES_OK)))
@@ -202,43 +234,42 @@ inline int sqlpg::testSQL(PGresult* a){
     };
 }
 bool sqlpg::alias(vector<string> a){
-    //cout<<"alias start here\n";
+    cerr<<"alias start here\n";
     string cmd;
 
     cmd="select command,argc from alias where nom='"+a[0]+"' and argc = '"+toString(a.size()-1)+"';";// I need to select a function with argc too !
-    //cout<<"cmd="<<cmd<<endl;
+    cerr<<"cmd="<<cmd<<endl;
     exec(cmd);
     if (PQntuples(res)<1){
-        //cout<<"alias end pqntuples(res)="<<PQntuples(res)<<endl;
+        cerr<<"alias end pqntuples(res)="<<PQntuples(res)<<endl;
         return 0;
     }
     string cmd2;
     cmd2=PQgetvalue(res, 0, 0);
-    //cout<<"cmd2="<<cmd2<<endl;
+    cerr<<"cmd2="<<cmd2<<endl;
     int pos=0;
     int argc=atoi(PQgetvalue(res, 0, 1));
-    //cout<<"argc="<<argc<<endl;
-    char *value[argc];
+    cerr<<"argc="<<argc<<endl;
+    const char *value[argc];
     int length[argc];
     int binary[argc];
-    //cout<<"for argc="<<argc<<"\n";
+    cerr<<"for argc="<<argc<<"\n";
     for(int i=0; i<argc; i++){
         value[i]=a[i+1].c_str();
         length[i]=a[i+1].size();
         binary[i]=0;
-        //cout<<"i="<<i<<" value="<<value[i]<<" length="<<length[i]<<"\n";
+        cerr<<"i="<<i<<" value="<<value[i]<<" length="<<length[i]<<"\n";
     }
     res=PQexecParams(conn, cmd2.c_str(), argc, NULL, value, length, binary, 0);
     testSQL(res);
     print();
-    //cout<<"and end here\n";
+    cerr<<"alias() end here\n";
     return 1;
 }
 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;
+    cerr<<"exec1("<<cmd<<") 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"));
@@ -257,15 +288,15 @@ void sqlpg::compulse(){
                 command=t.str();
             }
             else{
-                cout<<"erreur de logique (txt est une clé primaire...)\nPQntuple(res)=="<<tmp<<"\n";
+                cerr<<"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;
+        cerr<<"y="<<y<<endl;
         testSQL(PQexec(conn, y.str().c_str()));
-        //exec(y.str()); it don't works and i don't know why
+        //ex ec(y.str()); it don't works and i don't know why
     }
 };
 
diff --git a/sqldb.h b/sqldb.h
index 374a212c83462a204f650ded6552398e47162e51..0c9a084e529e49ee32690b53a5dddb8e651cd3c2 100644 (file)
--- a/sqldb.h
+++ b/sqldb.h
@@ -6,7 +6,7 @@
 #include "libpq-fe.h"
 #include <vector>
 #include <cstdio>
-std::string now();
+
 int isdate(std::string);//return 1 for YYYY-MM-DD and 2 for HH:MM:SS
 
 class sqlpg {
@@ -14,16 +14,12 @@ public:
     sqlpg ();
     ~sqlpg();
     template<typename T> static std::string toString( const T & Value );
+    std::string now(int=1);
     int status();// return 0 if success, 1 for error, -1
     int insert(std::vector<std::string>);// format=yyyy-month-day hour:min:sec text
-    std::vector < std::vector <std::string> > list(std::string);// to list the content of table "string"
-    std::vector < std::vector <std::string> > listdate(std::string, std::string=now());// to list the content of table "raw" between 2 date (date and now, by défault)
-    //std::vector<std::vector<int> > 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<int> 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<std::string>);// first string is for SQL command, other are to replace %1,%2, etc...
@@ -34,7 +30,7 @@ private:
     PGconn *conn;
     PGresult *res, *res2, *res3;
     std::vector < std::vector<std::string> > ret;//useless ?
-    std::string b, date;
+    std::string b, date, connect;
     int a;
 };
 
index 370ccfa201531a6d69df27b4e57c3f8bd82d760f..91c2c61db9b179242150bc9be5ae5b0cc049d79c 100755 (executable)
@@ -21,3 +21,14 @@ analyzed bit(1),
 texte text
 );
 
+create table memo (
+id serial primary key,
+word text,
+rem text
+);
+
+create table var (
+id serial primary key,
+nom text,
+value text
+);