Implement ResultRow opcode

This commit is contained in:
Pekka Enberg
2023-09-02 10:54:26 +03:00
parent 6e748a066e
commit 3ec9c0be7c
8 changed files with 240 additions and 58 deletions

View File

@@ -17,6 +17,7 @@ path = "main.rs"
[dependencies]
anyhow = "1.0.75"
clap = { version = "4.4.0", features = ["derive"] }
cli-table = "0.4.7"
dirs = "5.0.1"
lig_core = { path = "../core" }
rustyline = "12.0.0"

View File

@@ -1,6 +1,7 @@
use anyhow::Result;
use clap::Parser;
use lig_core::{Database, DatabaseRef};
use cli_table::{Cell, Table};
use lig_core::{Database, DatabaseRef, Value};
use rustyline::{error::ReadlineError, DefaultEditor};
use std::cell::RefCell;
use std::collections::HashMap;
@@ -31,10 +32,31 @@ fn main() -> anyhow::Result<()> {
let readline = rl.readline("\x1b[90m>\x1b[0m ");
match readline {
Ok(line) => {
if let Err(err) = conn.execute(&line) {
eprintln!("{}", err);
} else {
rl.add_history_entry(line)?;
rl.add_history_entry(line.to_owned())?;
match conn.query(line) {
Ok(Some(ref mut rows)) => {
let mut table_rows: Vec<Vec<_>> = vec![];
while let Some(row) = rows.next()? {
table_rows.push(
row.values
.iter()
.map(|value| match value {
Value::Null => "NULL".cell(),
Value::Integer(i) => i.to_string().cell(),
Value::Float(f) => f.to_string().cell(),
Value::Text(s) => s.cell(),
Value::Blob(b) => format!("{:?}", b).cell(),
})
.collect(),
);
}
let table = table_rows.table();
cli_table::print_stdout(table).unwrap();
}
Ok(None) => {}
Err(err) => {
eprintln!("{}", err);
}
}
}
Err(ReadlineError::Interrupted) => {