Index: trunk/skirmish/maxdb.cc |
— | — | @@ -92,7 +92,6 @@ |
93 | 93 | , conn(conn) |
94 | 94 | , sql(q) |
95 | 95 | { |
96 | | -std::cout << "q = ["<<q<<"]\n"; |
97 | 96 | if ((stmt = conn->conn->createPreparedStatement()) == NULL) |
98 | 97 | throw db::error(conn->error(conn->conn->error())); |
99 | 98 | if (stmt->prepare(q.c_str(), q.size(), SQLDBC_StringEncodingUTF8) != SQLDBC_OK) |
— | — | @@ -109,13 +108,11 @@ |
110 | 109 | throw db::error("could not retrieve parameter length"); |
111 | 110 | #endif |
112 | 111 | std::vector<char> paramname(namelen + 1); |
113 | | -std::cout << "namelen = " << namelen << '\n'; |
114 | 112 | if (pmd->getParameterName(i, ¶mname[0], SQLDBC_StringEncodingUTF8, paramname.size() + 1, &namelen) != SQLDBC_OK) |
115 | 113 | throw db::error("could not retrieve parameter name"); |
116 | 114 | param p; |
117 | 115 | p.name.assign(¶mname[0], ¶mname[0] + namelen); |
118 | 116 | p.pos = i; |
119 | | -std::cout << "i = " << i << " nparams = " << nparams << " name = [" << p.name << "] len = " << namelen << "\n"; |
120 | 117 | params.push_back(p); |
121 | 118 | } |
122 | 119 | } |
Index: trunk/skirmish/sqlite.cc |
— | — | @@ -164,7 +164,6 @@ |
165 | 165 | std::vector<db::table> ret; |
166 | 166 | result::iterator it = r->begin(), end = r->end(); |
167 | 167 | for (; it != end; ++it) { |
168 | | -std::cout << "got row\n"; |
169 | 168 | names.push_back(it->string_value(0)); |
170 | 169 | } |
171 | 170 | |
Index: trunk/skirmish/linereader.cc |
— | — | @@ -3,6 +3,7 @@ |
4 | 4 | #include <readline/history.h> |
5 | 5 | |
6 | 6 | #include <cstdlib> |
| 7 | +#include <iostream> |
7 | 8 | |
8 | 9 | #include "linereader.h" |
9 | 10 | |
— | — | @@ -17,7 +18,7 @@ |
18 | 19 | bool |
19 | 20 | linereader::readline(std::string &r, std::string const &prompt) |
20 | 21 | { |
21 | | - char *s = ::readline(prompt.c_str()); |
| 22 | + char *s = ::readline(form_prompt(prompt).c_str()); |
22 | 23 | if (s == NULL) |
23 | 24 | return false; |
24 | 25 | r = s; |
— | — | @@ -26,3 +27,43 @@ |
27 | 28 | std::free(s); |
28 | 29 | return true; |
29 | 30 | } |
| 31 | + |
| 32 | +std::string |
| 33 | +linereader::form_prompt(std::string const &src) |
| 34 | +{ |
| 35 | + std::string::size_type i, last = 0; |
| 36 | + std::string pr; |
| 37 | + |
| 38 | + while ((i = src.find('$', last)) != std::string::npos) { |
| 39 | + pr.append(src.substr(last, i - last)); |
| 40 | + if (i == src.size()-1) |
| 41 | + return pr + "$"; |
| 42 | + switch (src[i + 1]) { |
| 43 | + case '$': |
| 44 | + pr += '$'; |
| 45 | + last = i + 2; |
| 46 | + continue; |
| 47 | + case '(': |
| 48 | + break; |
| 49 | + default: |
| 50 | + return src; |
| 51 | + } |
| 52 | + if ((last = src.find(')', i)) == std::string::npos) |
| 53 | + return src; |
| 54 | + std::string key = src.substr(i + 2, last - i - 2); |
| 55 | + std::string value; |
| 56 | + std::map<std::string, std::string>::iterator it; |
| 57 | + if ((it = promptvars.find(key)) != promptvars.end()) |
| 58 | + value = it->second; |
| 59 | + pr += value; |
| 60 | + last++; |
| 61 | + } |
| 62 | + pr += src.substr(last); |
| 63 | + return pr; |
| 64 | +} |
| 65 | + |
| 66 | +void |
| 67 | +linereader::set_prompt_variable(std::string const &var, std::string const &key) |
| 68 | +{ |
| 69 | + promptvars[var] = key; |
| 70 | +} |
Index: trunk/skirmish/linereader.h |
— | — | @@ -2,12 +2,19 @@ |
3 | 3 | #define LINEREADER_H |
4 | 4 | |
5 | 5 | #include <string> |
| 6 | +#include <map> |
6 | 7 | |
7 | 8 | struct linereader { |
8 | 9 | linereader(); |
9 | 10 | ~linereader(); |
10 | 11 | |
11 | 12 | bool readline(std::string &, std::string const &); |
| 13 | + void set_prompt_variable(std::string const &var, std::string const &value); |
| 14 | + |
| 15 | +private: |
| 16 | + std::string form_prompt(std::string const &); |
| 17 | + |
| 18 | + std::map<std::string, std::string> promptvars; |
12 | 19 | }; |
13 | 20 | |
14 | 21 | #endif |
Index: trunk/skirmish/skirmish.cc |
— | — | @@ -18,11 +18,14 @@ |
19 | 19 | static void switch_connection(std::string const &); |
20 | 20 | static void close_connection(std::string const &); |
21 | 21 | static void list_tables(std::string const &); |
| 22 | +static void c_prompt(std::string const &); |
22 | 23 | static void describe_table(std::string const &); |
23 | 24 | |
24 | 25 | typedef std::vector<std::vector<std::string> > tabulated_t; |
25 | 26 | static void show_tabulated(tabulated_t const &); |
26 | 27 | |
| 28 | +static std::string prompt = "skirmish [$(cnr)]>"; |
| 29 | + |
27 | 30 | struct conndesc { |
28 | 31 | db::connectionptr conn; |
29 | 32 | std::string desc; |
— | — | @@ -39,6 +42,8 @@ |
40 | 43 | ("\\close", close_connection) |
41 | 44 | ("\\lt", list_tables) |
42 | 45 | ("\\dt", describe_table) |
| 46 | + ("\\pr", c_prompt) |
| 47 | + ("\\prompt", c_prompt) |
43 | 48 | ; |
44 | 49 | |
45 | 50 | static db::connectionptr |
— | — | @@ -74,12 +79,16 @@ |
75 | 80 | add_connection(argv[a]); |
76 | 81 | |
77 | 82 | db::connectionptr conn; |
78 | | - std::string input, prompt; |
| 83 | + std::string input; |
79 | 84 | for (;;) { |
80 | | - if (cnr == -1) |
81 | | - prompt = "skirmish (none)>"; |
82 | | - else |
83 | | - prompt = str(boost::format("skirmish [%d]>") % cnr); |
| 85 | + std::string cnrs; |
| 86 | + if (cnr == -1) { |
| 87 | + rl.set_prompt_variable("desc", "not connected"); |
| 88 | + rl.set_prompt_variable("cnr", "none"); |
| 89 | + } else { |
| 90 | + rl.set_prompt_variable("cnr", boost::lexical_cast<std::string>(cnr)); |
| 91 | + rl.set_prompt_variable("desc", conns[cnr]->desc); |
| 92 | + } |
84 | 93 | |
85 | 94 | if (!rl.readline(input, prompt)) |
86 | 95 | break; |
— | — | @@ -381,3 +390,12 @@ |
382 | 391 | } |
383 | 392 | } |
384 | 393 | } |
| 394 | + |
| 395 | +static void |
| 396 | +c_prompt(std::string const &arg) |
| 397 | +{ |
| 398 | + if (arg.empty()) |
| 399 | + std::cout << prompt << '\n'; |
| 400 | + else |
| 401 | + prompt = arg; |
| 402 | +} |