]> git.nothing2do.fr Git - diary-shell.git/commitdiff
added var.sql (and map<string, string>value)
authorNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Tue, 16 Jul 2013 11:59:33 +0000 (13:59 +0200)
committerNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Tue, 16 Jul 2013 11:59:33 +0000 (13:59 +0200)
alias.sql
main.cpp
sqldb.cpp
sqldb.h
tables1.sql
var.sql [new file with mode: 0644]

index 81c7d62bc16271b104131494344c15ba1886717a..e4ed9af65cd1f3508ff8b03c56e89def035ea4b6 100644 (file)
--- a/alias.sql
+++ b/alias.sql
@@ -1,15 +1,16 @@
 INSERT INTO alias (nom, cmd, argc, help) VALUES ('ls', 'select id,texte from raw order by date asc;', 0, 'list id,text of every records');
 insert into alias (nom, cmd, argc, help) values ('ls', 'select * from raw where id=$1 order by date asc;', 1, 'print id,date,text of one record');
 insert into alias (nom, cmd, argc, help) values ('ls', 'select * from raw where date between $1 and $2 order by date asc;', 2, 'liste record with date between $1 and $2');
-INSERT INTO alias (nom, cmd, argc, help) VALUES ('search', 'select id,texte from raw where texte ~ $1 order by date asc;', 1, 'search record who contains your arg (only one');
+INSERT INTO alias (nom, cmd, argc, help) VALUES ('search', 'select id,texte from raw where texte ~ $1 order by date asc;', 1, 'search record who contains your arg (only one arg)');
 INSERT INTO alias (nom, cmd, argc, help) VALUES ('rm', 'DELETE FROM raw WHERE id =$1;', 1, 'delete record by ID');
-INSERT INTO alias (nom, cmd, argc, help) VALUES ('alias', 'select nom from alias;', 0, 'print every alias''s name');
 insert into alias (nom, cmd, argc, help) values ('var', 'select nom from var', 0, 'print avery var');
-insert into alias (nom, cmd, argc, help) values ('var', 'select nom,value from var where nom=$1 order by id', 1, 'print named var content');
-insert into alias (nom, cmd, argc, inf, help) values ('memo', 'insert into memo (word, rem) values ($1, $2);', 2, '1', 'list memo''s name');
+insert into alias (nom, cmd, argc, help) values ('var', 'select value from var where nom=$1', 1, 'print named var content');
+insert into alias (nom, cmd, argc, inf, help) values ('var', 'insert into var (nom, value) values ($1, $2)', 2, '1', 'add new var and it''s content');
+insert into alias (nom, cmd, argc, inf, help) values ('memo', 'insert into memo (word, rem) values ($1, $2);', 2, '1', 'insert new memo');
 insert into alias (nom, cmd, argc, help) values ('memo', 'select distinct word from memo', 0, 'list all word with memo attached');
 insert into alias (nom, cmd, argc, help) values ('memo', 'select id,rem from memo where word=$1 order by id', 1, 'print memo content');
 insert into alias (nom, cmd, argc, help) values ('help', 'select distinct nom from alias', 0, 'help itself (add "cmd" to get specific help)');
-insert into alias (nom, cmd, argc, help) values ('help', 'select argc from alias where nom=$1 order by argc', 1, 'print id,argc of named alias');
+insert into alias (nom, cmd, argc, help) values ('help', 'select argc from alias where nom=$1 order by argc', 1, 'print id,argc of named alias by ID');
 insert into alias (nom, cmd, argc, help) values ('help', 'select help from alias where nom=$1 and argc=$2', 2, 'print help for only one alias (as "nom" and "argc" is a key)');
 insert into alias (nom, cmd, argc, help) values ('print', 'select cmd,inf from alias where nom=$1 and argc=$2', 2, 'print SQL command of an alias (...)');
+insert into alias (nom, cmd, argc, inf, help) values ('default', 'INSERT INTO raw (date, texte) VALUES ($1::timestamp, $2::text);', 2 , '1', 'default command');
index c66d4a855e633e3d57827e2df2a1bc7bec41a26d..d3a05a27583199e4bf231216ddd3b25fbd19a54f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -19,8 +19,9 @@ int main(int argc, char *argv[])
     sqlpg diary;
     string tmp, rep, shell="? ";
     int hm;
+    diary.getvar();
 do{
-    clog<<"\n";
+    clog<<"again\n";
     hm=diary.hmRecord();
     cout<<hm<<shell;
     getline(cin,rep,'\n');
@@ -35,7 +36,7 @@ do{
     //else if (words[0]=="help") diary.help(); now  into SQL
     else if (words[0]=="status") diary.status();
 
-    else if (words[0]=="value") diary.var();
+    else if (words[0]=="con") diary.printvar();
     else if ((words[0]=="alias")&&(words.size()>1)) {
         if (words[1]=="add") {
             string cmd="";
@@ -55,7 +56,6 @@ do{
     else if (diary.alias(words));
     else {
         diary.insert(words);
-
     }
     words.clear();
 }while(1);
index c1b7363f4c42db053f21b02ae2242d1222f6bcd5..405d976536122ffe9277c81f1738e7924f0e1833 100644 (file)
--- a/sqldb.cpp
+++ b/sqldb.cpp
@@ -21,7 +21,6 @@ sqlpg::sqlpg() {
     clog<<"SQL statement="<<cmd<<endl;
     res=PQprepare(conn, "selectalias", cmd.c_str(), 2, NULL);
     testSQL(res);
-
 }
 
 int sqlpg::status(){
@@ -50,6 +49,8 @@ template<typename T> string sqlpg::toString( const T & Value ){
 }
 int sqlpg::insert(vector<string> text) {
     clog<<"insert(vector<string>) begin\n";
+
+
     string command="INSERT INTO raw (date, texte) VALUES ($1::timestamp, $2::text);";
     string tmp="";
     int argc=2;
@@ -125,8 +126,8 @@ int sqlpg::insert(vector<string> text) {
         value[1]=tmp.c_str();
         length[1]=tmp.size();
         binary[1]=0;
-        //value[1]=now().c_str();
-        length[1]=19;
+        value[1]=a.c_str();
+        length[1]=a.size();
         binary[1]=0;
         clog<<"text.size()<2 finish\n";
     };
@@ -172,8 +173,11 @@ int sqlpg::hmRecord(){
     clog<<"hmRecord()="<<a<<endl;
     return a;
 }
-void sqlpg::var(){
-    cout<<"date="<<date<<", chaîne de connection: " + connect << endl;;
+void sqlpg::printvar(){
+    cout<<"date="<<date<<", chaîne de connection: " + connect << endl;
+    for(map<string, string>::iterator i=value.begin(); i!=value.end(); ++i){
+        cout<<"value["<<i->first<<"]="<<i->second<<endl;
+    }
 }
 sqlpg::~sqlpg() {
     PQclear(res);
@@ -184,13 +188,13 @@ int l=PQntuples(res);
 for (int i=0; i<l; i++){
     int lim=PQnfields(res);
     for (int j=0; j<lim-1; j++){
-            cout<<PQgetvalue(res, i, j)<<"+";
+        cout<<PQgetvalue(res, i, j)<<value.find("hs")->second;
         };
         cout<<PQgetvalue(res, i, lim-1);
-        if(i<l-1)cout<<"|";
+        if(i<l-1)cout<<value.find("vs")->second;
     };
 
-cout<<endl;
+    cout<<endl;
 }
 void sqlpg::exec(string a){
     clog<<"exec(string["<<a<<"])\n";
@@ -342,7 +346,19 @@ void sqlpg::compulse(){
         //ex ec(y.str()); it don't works and i don't know why
     }
 };
+void sqlpg::getvar(){
+    clog<<"getvar\n";
+    exec("select nom,value from var;");
+    int l=PQntuples(res);
+    for (int i=0; i<l; i++){
+        string a, b;
+        a=PQgetvalue(res, i, 0);
+        b=PQgetvalue(res, i, 1);
+        value.insert(pair<string, string> (a, b) );
 
+    };
+    clog<<"value.size="<<value.size();
+};
 
 
 #endif
diff --git a/sqldb.h b/sqldb.h
index 0c9a084e529e49ee32690b53a5dddb8e651cd3c2..816c575d6d8edc40508d07768a8ec8f57d309d4c 100644 (file)
--- a/sqldb.h
+++ b/sqldb.h
@@ -6,6 +6,7 @@
 #include "libpq-fe.h"
 #include <vector>
 #include <cstdio>
+#include <map>
 
 int isdate(std::string);//return 1 for YYYY-MM-DD and 2 for HH:MM:SS
 
@@ -17,10 +18,10 @@ public:
     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
-
+    void getvar();
     void print();// print result of exec(string) call
     int hmRecord();// How Many records in "raw" table
-    void var();
+    void printvar();
     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...
     void exec(std::string);
@@ -32,6 +33,7 @@ private:
     std::vector < std::vector<std::string> > ret;//useless ?
     std::string b, date, connect;
     int a;
+    std::map<std::string,std::string>value;
 };
 
 #endif // SQLDB_H
index a0b16dd741858ed44c5e69e7c9e56c62140cdf57..fc2a63e7a50420a1da7906a2317adad59f09e359 100755 (executable)
@@ -19,7 +19,7 @@ CONSTRAINT uc_aliasID UNIQUE (nom, argc)
 create table IF NOT EXISTS raw (
 id serial primary key,
 date timestamp,
-analyzed bit(1),
+analyzed bit(1) default '0',
 texte text
 );
 
diff --git a/var.sql b/var.sql
new file mode 100644 (file)
index 0000000..2318ed0
--- /dev/null
+++ b/var.sql
@@ -0,0 +1,3 @@
+insert into var (nom, value) values ('hs', '+');
+insert into var (nom, value) values ('vs', '|');
+-- Vertical and Horizontal Spacing car