1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
fn print_help()
{
println!(
"List of commands:
.once ?FILE? save result of next query in FILE.
.open ?FILE? close existing connection and reopen FILE.
.quit Exit this program
.help show this message
To execute a query, write the query and end it with a ';'"
);
}
fn main_loop(rl: &mut rustyline::DefaultEditor) -> rustyline::Result<()>
{
let mut connection: Option<gqlitedb::Connection> = None;
let mut args = std::env::args();
args.next(); // remove program name
if let Some(filename) = args.next()
{
let connection_res = gqlitedb::Connection::open(filename, gqlitedb::ValueObject::new());
match connection_res
{
Ok(c) =>
{
connection = Some(c);
}
Err(msg) =>
{
println!("{:?}", msg);
}
}
}
loop
{
let line = rl.readline("gqlite> ")?;
if line.len() > 0
{
if line.starts_with(".")
{
rl.add_history_entry(line.as_str())?;
let splited_line: Vec<_> = line.split(" ").collect();
match splited_line[0]
{
".help" =>
{
print_help();
}
".open" =>
{
if line.len() < 2
{
println!("Missing argument to .open");
}
let connection_res =
gqlitedb::Connection::open(splited_line[1], gqlitedb::ValueObject::new());
match connection_res
{
Ok(c) =>
{
connection = Some(c);
}
Err(msg) =>
{
println!("{:?}", msg);
}
}
}
".quit" =>
{
return Ok(());
}
".once" =>
{
println!("'.once' is not implemented yet.");
}
unknown_command =>
{
println!(
"Unknown command '{}', use '.help' to see the list of commands.",
unknown_command
);
}
}
}
else
{
let mut query = line;
while !query.ends_with(";")
{
let readline = rl.readline(" ...> ");
match readline
{
Ok(line) =>
{
if line.len() == 0
{
break;
}
else
{
query += "\n";
query += &line;
}
}
Err(rustyline::error::ReadlineError::Interrupted) =>
{
query = String::new();
break;
}
Err(other_error) =>
{
return Err(other_error);
}
}
}
if query.len() > 0
{
rl.add_history_entry(query.as_str())?;
match connection
{
Some(ref c) =>
{
let qr = c.execute_query(query, gqlitedb::ValueObject::new());
match qr
{
Ok(value) =>
{
match value
{
gqlitedb::Value::Array(arr) =>
{
let mut builder = tabled::builder::Builder::new();
// let mut it = arr.iter();
// let keys = it.next().map_or(Vec::<String>::new(), |v| {
// match v {
// gqlitedb::Value::Array(arr) => arr.iter().map(|x| x.to_string()).collect(),
// _ => vec![]
// }
// });
// builder.push_record(keys.iter());
// it.for_each(|row| {
// match row {
// gqlitedb::Value::Object(obj) => {
// builder.push_record(keys.iter().map(|k| obj[k].to_string()));
// },
// _ => {
// println!("Unexpected: {}", row);
// }
// }
// });
arr.iter().for_each(|row| match row
{
gqlitedb::Value::Array(arr) =>
{
builder.push_record(arr.iter().map(|x| x.to_string()))
}
_ =>
{
println!("Unexpected: {}", row);
}
});
let table = builder
.build()
.with(tabled::settings::Style::ascii_rounded())
.to_string();
println!("{}", table);
}
_ =>
{}
}
}
Err(err) => match err
{
gqlitedb::Error::CompileTime(ct) =>
{
println!("Compilation error:\n{}", ct.to_string());
}
_ =>
{
println!("Query execution failed: {:?}", err);
}
},
}
}
None =>
{
println!("No database connection, use '.open' before executing a query.");
}
}
}
}
}
}
}
fn main() -> rustyline::Result<()>
{
// `()` can be used when no completer is required
let mut rl = rustyline::DefaultEditor::new()?;
let gqlite_history = standard_paths::StandardPaths::new("gqlitecli", "gqlite.org")
.writable_location(standard_paths::LocationType::ConfigLocation)?
.join("gqlite_history");
if rl.load_history(&gqlite_history).is_err()
{}
println!("Enter '.help' for usage hints.");
match main_loop(&mut rl)
{
Ok(_) =>
{}
Err(rustyline::error::ReadlineError::Interrupted) =>
{
println!("CTRL-C");
}
Err(rustyline::error::ReadlineError::Eof) =>
{
println!("CTRL-D");
}
Err(err) =>
{
println!("Error: {:?}", err);
}
}
rl.save_history(&gqlite_history)?;
Ok(())
}