From: Norbert Moutarde Date: Sat, 29 Jun 2013 09:44:33 +0000 (+0200) Subject: almost all function is in alias.sql now (func with arg too) X-Git-Url: https://git.nothing2do.fr/?a=commitdiff_plain;h=3d7c6325017b6d3c6995d750d301bfbc48b23f6c;p=diary-shell.git almost all function is in alias.sql now (func with arg too) --- diff --git a/alias.sql b/alias.sql index ffa0f1d..c21fff9 100644 --- a/alias.sql +++ b/alias.sql @@ -1,4 +1,4 @@ -INSERT INTO alias (nom, cmd, argc) VALUES ('ls', 'select id,texte from raw order by date asc;', 0); -INSERT INTO alias (nom, cmd, argc) VALUES ('search', 'select id, texte from raw where texte like \'%1\' order by date asc', 1); -INSERT INTO alias (nom, cmd, argc) VALUES ('rm', 'DELETE FROM raw WHERE id =\'%1\';', 1); -INSERT INTO alias (nom, cmd, argc) VALUES ('alias', 'select nom from alias;', 0); +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 like ''%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); diff --git a/howto-compile.sh b/howto-compile.sh index 0b93aca..dc249f5 100755 --- a/howto-compile.sh +++ b/howto-compile.sh @@ -1,2 +1,2 @@ rm diary-shell -g++ main.cpp sqldb.cpp -o diary-shell -L /usr/lib -I/usr/include/postgresql -lpq -fpermissive +g++ main.cpp ../sqldb.cpp -o diary-shell -L /usr/lib -I .. -I/usr/include/postgresql -lpq -fpermissive diff --git a/main.cpp b/main.cpp index def8c0c..a560114 100644 --- a/main.cpp +++ b/main.cpp @@ -56,8 +56,6 @@ do{ } else if (words[1]=="list") {diary.exec("select * from alias;");diary.print();} } - else if (words[0]=="ls") {diary.exec("select id,texte from raw order by date asc;");diary.print();} - else if (words[0]=="!") { string a=rep.substr(2); diary.exec(a); diff --git a/sqldb.cpp b/sqldb.cpp deleted file mode 100644 index 6ed0bc7..0000000 --- a/sqldb.cpp +++ /dev/null @@ -1,255 +0,0 @@ -#ifndef sql -#define sql 1 -#include "sqldb.h" -#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