-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathmain.rs
More file actions
231 lines (204 loc) · 9.57 KB
/
Copy pathmain.rs
File metadata and controls
231 lines (204 loc) · 9.57 KB
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
228
229
230
231
//! REPL binary — the `sqlrite` CLI.
//!
//! Thin wrapper around the [`sqlrite`] library: rustyline for input, the
//! engine for parsing + execution + persistence.
mod meta_command;
mod repl;
use std::path::PathBuf;
use meta_command::handle_meta_command;
use repl::{CommandType, REPLHelper, ReplState, get_command_type, get_config};
use sqlrite::Connection;
use rustyline::Editor;
use rustyline::error::ReadlineError;
use rustyline::history::DefaultHistory;
use clap::{Arg, ArgAction, Command, crate_authors, crate_name, crate_version};
const ABOUT_SHORT: &str = "A small SQLite-like embedded database and REPL, written in Rust.";
const ABOUT_LONG: &str = "\
A small SQLite-like embedded database and REPL, written in Rust.
Passing a FILE argument is equivalent to launching the REPL and then running
`.open FILE` — existing files are loaded, missing files are created fresh.
Without a FILE the REPL starts in transient in-memory mode.
Add --readonly to open the FILE with a shared OS-level lock. Multiple
read-only REPLs on the same file coexist; any write attempt returns a
'database is opened read-only' error. Useful for poking at a DB while
another process holds the writer lock.
Once in the REPL, meta commands start with a dot:
.help Show the meta-command list
.open <FILENAME> Open (or create) a .sqlrite database file
.save <FILENAME> Write the current DB to FILENAME (rarely needed —
once .open is in play, every write auto-saves)
.tables List tables in the current database
.spawn Mint a sibling connection sharing this database
.use <NAME> Switch the active handle (A, B, ...) — see .conns
.conns List every handle, marking the active one
.exit Quit
Supported SQL: CREATE TABLE / CREATE [UNIQUE] INDEX / INSERT / SELECT /
UPDATE / DELETE with WHERE, ORDER BY, LIMIT, arithmetic (+ - * / %),
logical (AND / OR / NOT), string concat (||), and NULL-aware comparisons.
Index-probe fast path activates for `WHERE col = literal` on indexed
columns.
For the full reference see docs/usage.md; for end-to-end testing walk
through docs/smoke-test.md.";
fn main() -> rustyline::Result<()> {
env_logger::init();
let matches = Command::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(ABOUT_SHORT)
.long_about(ABOUT_LONG)
.arg(
Arg::new("FILE")
.help("Path to a .sqlrite database file. If it exists, it's opened; if not, it's created.")
.value_parser(clap::value_parser!(PathBuf))
.index(1),
)
.arg(
Arg::new("readonly")
.long("readonly")
.short('r')
.help("Open the file with a shared lock (read-only). Coexists with other readers; any write returns an error. Requires FILE.")
.action(ArgAction::SetTrue),
)
.arg_required_else_help(false)
.get_matches();
let initial_db_path = matches.get_one::<PathBuf>("FILE").cloned();
let read_only = matches.get_flag("readonly");
// Starting Rustyline with a default configuration
let config = get_config();
// Getting a new Rustyline Helper
let helper = REPLHelper::default();
// Initiatlizing Rustyline Editor with set config and setting helper
let mut repl: Editor<REPLHelper, DefaultHistory> = Editor::with_config(config)?;
repl.set_helper(Some(helper));
// This method loads history file into memory
// If it doesn't exist, creates one
// TODO: Check history file size and if too big, clean it.
if repl.load_history("history").is_err() {
println!("No previous history.");
}
// Either open/create the requested file, or drop into a transient
// in-memory database (the legacy default when no FILE is given).
// We track whether the open succeeded so the banner doesn't claim
// "Opened …" when we actually fell back to in-memory after a lock
// contention or other failure.
if read_only && initial_db_path.is_none() {
eprintln!("--readonly requires a FILE argument");
std::process::exit(1);
}
let (initial_conn, opened_path): (Connection, Option<&std::path::PathBuf>) =
match &initial_db_path {
Some(path) => match open_or_create(path, read_only) {
Ok(conn) => (conn, Some(path)),
Err(err) => {
eprintln!("Could not open '{}': {err}", path.display());
eprintln!("Falling back to a transient in-memory database.");
(
Connection::open_in_memory().expect("in-memory open never fails"),
None,
)
}
},
None => (
Connection::open_in_memory().expect("in-memory open never fails"),
None,
),
};
let mut state = ReplState::new(initial_conn);
// Friendly intro message for the user
let connection_line = match opened_path {
Some(path) if read_only => format!("Opened '{}' (read-only).", path.display()),
Some(path) => format!("Opened '{}' — auto-save enabled.", path.display()),
None => "Connected to a transient in-memory database.\nUse '.open FILENAME' to reopen on a persistent database.".to_string(),
};
println!(
"{} - {}\nEnter .exit to quit.\nEnter .help for usage hints.\n{}",
crate_name!(),
crate_version!(),
connection_line,
);
loop {
// Prompt shows the active handle so multi-handle demos
// (`.spawn` / `.use`) make it obvious which connection is
// about to execute the next statement.
let prompt = format!("sqlrite[{}]> ", state.active_name());
repl.helper_mut().expect("No helper found").colored_prompt =
format!("\x1b[1;32m{prompt}\x1b[0m");
// Source for ANSI Color information: http://www.perpetualpc.net/6429_colors.html#color_list
// http://bixense.com/clicolors/
let readline = repl.readline(&prompt);
match readline {
Ok(command) => {
let _ = repl.add_history_entry(command.as_str());
// Parsing user's input and returning and enum of repl::CommandType
match get_command_type(command.trim()) {
CommandType::SQLCommand(_cmd) => {
// Route through `Connection::execute_with_render`
// so `BEGIN CONCURRENT` / `COMMIT` / `ROLLBACK`
// hit the per-connection MVCC state, and reads
// inside an open concurrent transaction see the
// BEGIN-time snapshot. SELECTs come back with
// the pre-rendered prettytable; we print that
// first so the user sees the rows above the
// confirmation. Prior to the engine-stdout-
// pollution cleanup the engine printed the table
// itself, which corrupted any non-REPL stdout
// channel — the REPL owns the printing now.
match state.active_conn_mut().execute_with_render(&command) {
Ok(output) => {
if let Some(rendered) = output.rendered.as_deref() {
print!("{rendered}");
}
println!("{}", output.status);
}
Err(err) => eprintln!("An error occured: {err}"),
}
}
CommandType::MetaCommand(cmd) => {
// handle_meta_command parses and executes the MetaCommand
// and returns a Result<String, SQLRiteError>
match handle_meta_command(cmd, &mut repl, &mut state) {
Ok(response) => println!("{response}"),
Err(err) => eprintln!("An error occured: {err}"),
}
}
}
}
Err(ReadlineError::Interrupted) => {
break;
}
Err(ReadlineError::Eof) => {
break;
}
Err(err) => {
eprintln!("An error occured: {err:?}");
break;
}
}
}
repl.append_history("history").unwrap();
Ok(())
}
/// Equivalent to typing `.open FILE` at the REPL: load if present,
/// materialize an empty DB on disk if missing. Returns a fresh
/// `Connection` whose backing `Arc<Mutex<Database>>` carries the
/// long-lived pager so subsequent writes auto-save.
///
/// When `read_only` is set we take a shared advisory lock and never
/// materialize a missing file — read-only mode must fail cleanly if
/// the target doesn't exist rather than silently creating one.
fn open_or_create(path: &std::path::Path, read_only: bool) -> sqlrite::Result<Connection> {
if read_only {
if !path.exists() {
return Err(sqlrite::SQLRiteError::General(format!(
"read-only open requested but '{}' does not exist",
path.display()
)));
}
Connection::open_read_only(path)
} else {
// `Connection::open` already does the create-on-missing
// dance (materialize a fresh DB + save_database) and
// returns the attached pager either way.
Connection::open(path)
}
}