Skip to content

Commit 688bee9

Browse files
authored
[Cargo{.toml,.lock}] Upgrade all dependencies; add "cargo" feature to clap; [src/**.rs] rustfmt; conform to new dependency APIs; lint (#13)
1 parent 2e257c5 commit 688bee9

11 files changed

Lines changed: 446 additions & 232 deletions

File tree

Cargo.lock

Lines changed: 278 additions & 119 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ license = "MIT"
1111
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1212

1313
[dependencies]
14-
rustyline = "7.1.0"
15-
log = "0.4.14"
16-
env_logger = "0.8.3"
17-
rustyline-derive = "0.4.0"
18-
clap = "2.33.3"
19-
sqlparser = "0.8.0"
20-
thiserror = "1.0.24"
21-
serde = { version = "1.0.123", features = ["derive", "rc"] }
22-
prettytable-rs = "^0.8"
14+
rustyline = "9.1.2"
15+
log = "0.4.17"
16+
env_logger = "0.9.0"
17+
rustyline-derive = "0.6.0"
18+
clap = { version = "3.1.18", features = ["cargo"] }
19+
sqlparser = "0.17.0"
20+
thiserror = "1.0.31"
21+
serde = { version = "1.0.137", features = ["derive", "rc"] }
22+
prettytable-rs = "0.8.0"

src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extern crate clap;
2-
#[macro_use] extern crate prettytable;
2+
#[macro_use]
3+
extern crate prettytable;
34

45
mod error;
56
mod meta_command;
@@ -8,18 +9,18 @@ mod sql;
89

910
use meta_command::handle_meta_command;
1011
use repl::{get_command_type, get_config, CommandType, REPLHelper};
11-
use sql::process_command;
1212
use sql::db::database::Database;
13+
use sql::process_command;
1314

1415
use rustyline::error::ReadlineError;
1516
use rustyline::Editor;
1617

17-
use clap::{crate_name ,crate_authors, crate_description, crate_version, App};
18+
use clap::{crate_authors, crate_description, crate_name, crate_version, Command};
1819

1920
fn main() -> rustyline::Result<()> {
2021
env_logger::init();
2122

22-
let _matches = App::new(crate_name!())
23+
let _matches = Command::new(crate_name!())
2324
.version(crate_version!())
2425
.author(crate_authors!())
2526
.about(crate_description!())

src/meta_command/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::error::{Result, SQLRiteError};
22

3-
use std::fmt;
3+
use crate::repl::REPLHelper;
44
use rustyline::Editor;
5-
use crate::repl::{REPLHelper};
5+
use std::fmt;
66

77
#[derive(Debug, PartialEq)]
88
pub enum MetaCommand {
@@ -42,7 +42,7 @@ pub fn handle_meta_command(command: MetaCommand, repl: &mut Editor<REPLHelper>)
4242
MetaCommand::Exit => {
4343
repl.append_history("history").unwrap();
4444
std::process::exit(0)
45-
},
45+
}
4646
MetaCommand::Help => Ok(format!(
4747
"{}{}{}{}{}{}{}{}",
4848
"Special commands:\n",

src/sql/db/database.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use serde::{Deserialize, Serialize};
2-
use std::collections::HashMap;
31
use crate::error::{Result, SQLRiteError};
42
use crate::sql::db::table::Table;
3+
use serde::{Deserialize, Serialize};
4+
use std::collections::HashMap;
55

66
/// The database is represented by this structure.assert_eq!
77
#[derive(Serialize, Deserialize, PartialEq, Debug)]
@@ -61,7 +61,7 @@ mod tests {
6161
use super::*;
6262
use crate::sql::parser::create::CreateQuery;
6363
use sqlparser::dialect::SQLiteDialect;
64-
use sqlparser::parser::{Parser};
64+
use sqlparser::parser::Parser;
6565

6666
#[test]
6767
fn new_database_create_test() {
@@ -90,7 +90,8 @@ mod tests {
9090

9191
let create_query = CreateQuery::new(&query).unwrap();
9292
let table_name = &create_query.table_name;
93-
db.tables.insert(table_name.to_string(), Table::new(create_query));
93+
db.tables
94+
.insert(table_name.to_string(), Table::new(create_query));
9495

9596
assert!(db.contains_table("contacts".to_string()));
9697
}
@@ -113,17 +114,17 @@ mod tests {
113114
}
114115
let query = ast.pop().unwrap();
115116

116-
let create_query = CreateQuery::new(&query).unwrap();
117+
let create_query = CreateQuery::new(&query).unwrap();
117118
let table_name = &create_query.table_name;
118-
db.tables.insert(table_name.to_string(), Table::new(create_query));
119+
db.tables
120+
.insert(table_name.to_string(), Table::new(create_query));
119121

120122
let table = db.get_table(String::from("contacts")).unwrap();
121123
assert_eq!(table.columns.len(), 4);
122124

123125
let mut table = db.get_table_mut(String::from("contacts")).unwrap();
124126
table.last_rowid += 1;
125-
assert_eq!(table.columns.len(), 4);
126-
assert_eq!(table.last_rowid, 1);
127+
assert_eq!(table.columns.len(), 4);
128+
assert_eq!(table.last_rowid, 1);
127129
}
128-
129-
}
130+
}

src/sql/db/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pub mod database;
2-
pub mod table;
2+
pub mod table;

src/sql/db/table.rs

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,13 @@ impl Table {
8181
if col.is_pk {
8282
primary_key = col_name.to_string();
8383
}
84-
table_cols.push(
85-
Column::new(
86-
col_name.to_string(),
87-
col.datatype.to_string(),
88-
col.is_pk,
89-
col.not_null,
90-
col.is_unique,
91-
),
92-
);
84+
table_cols.push(Column::new(
85+
col_name.to_string(),
86+
col.datatype.to_string(),
87+
col.is_pk,
88+
col.not_null,
89+
col.is_unique,
90+
));
9391

9492
match DataType::new(col.datatype.to_string()) {
9593
DataType::Integer => table_rows
@@ -139,27 +137,29 @@ impl Table {
139137
/// column with the specified key as a column name.
140138
///
141139
pub fn get_column(&mut self, column_name: String) -> Result<&Column> {
142-
if let Some(column) = self.columns
140+
if let Some(column) = self
141+
.columns
143142
.iter()
144143
.filter(|c| c.column_name == column_name)
145144
.collect::<Vec<&Column>>()
146-
.first(){
147-
Ok(column)
148-
} else {
149-
Err(SQLRiteError::General(String::from("Column not found.")))
150-
}
145+
.first()
146+
{
147+
Ok(column)
148+
} else {
149+
Err(SQLRiteError::General(String::from("Column not found.")))
150+
}
151151
}
152152

153153
/// Returns an mutable reference of `sql::db::table::Column` if the table contains a
154154
/// column with the specified key as a column name.
155155
///
156156
pub fn get_column_mut<'a>(&mut self, column_name: String) -> Result<&mut Column> {
157-
for elem in self.columns.iter_mut() {
158-
if elem.column_name == column_name{
159-
return Ok(elem)
160-
}
157+
for elem in self.columns.iter_mut() {
158+
if elem.column_name == column_name {
159+
return Ok(elem);
161160
}
162-
Err(SQLRiteError::General(String::from("Column not found.")))
161+
}
162+
Err(SQLRiteError::General(String::from("Column not found.")))
163163
}
164164

165165
/// Validates if columns and values being inserted violate the UNIQUE constraint
@@ -224,7 +224,7 @@ impl Table {
224224
let mut next_rowid = self.last_rowid + i64::from(1);
225225

226226
// Checks if table has a PRIMARY KEY
227-
if self.primary_key != "-1"{
227+
if self.primary_key != "-1" {
228228
// Checking if primary key is in INSERT QUERY columns
229229
// If it is not, assign the next_rowid to it
230230
if !cols.iter().any(|col| col == &self.primary_key) {
@@ -288,21 +288,21 @@ impl Table {
288288
// For every column in the INSERT statement
289289
for i in 0..column_names.len() {
290290
let mut val = String::from("Null");
291-
let mut key = &column_names[i];
291+
let key = &column_names[i];
292292

293-
if let Some(key) = &cols.get(j){
293+
if let Some(key) = &cols.get(j) {
294294
if &key.to_string() == &column_names[i] {
295295
// Getting column name
296296
val = values[j].to_string();
297297
j += 1;
298-
} else{
299-
if &self.primary_key == &column_names[i]{
300-
continue
298+
} else {
299+
if &self.primary_key == &column_names[i] {
300+
continue;
301301
}
302302
}
303-
}else{
304-
if &self.primary_key == &column_names[i]{
305-
continue
303+
} else {
304+
if &self.primary_key == &column_names[i] {
305+
continue;
306306
}
307307
}
308308

@@ -367,10 +367,22 @@ impl Table {
367367
///
368368
pub fn print_table_schema(&self) -> Result<usize> {
369369
let mut table = PrintTable::new();
370-
table.add_row(row!["Column Name", "Data Type", "PRIMARY KEY", "UNIQUE", "NOT NULL"]);
370+
table.add_row(row![
371+
"Column Name",
372+
"Data Type",
373+
"PRIMARY KEY",
374+
"UNIQUE",
375+
"NOT NULL"
376+
]);
371377

372378
for col in &self.columns {
373-
table.add_row(row![col.column_name, col.datatype, col.is_pk, col.is_unique, col.not_null]);
379+
table.add_row(row![
380+
col.column_name,
381+
col.datatype,
382+
col.is_pk,
383+
col.is_unique,
384+
col.not_null
385+
]);
374386
}
375387

376388
let lines = table.printstd();
@@ -415,7 +427,9 @@ impl Table {
415427

416428
let rows_clone = Rc::clone(&self.rows);
417429
let row_data = rows_clone.as_ref().borrow();
418-
let first_col_data = row_data.get(&self.columns.first().unwrap().column_name).unwrap();
430+
let first_col_data = row_data
431+
.get(&self.columns.first().unwrap().column_name)
432+
.unwrap();
419433
let num_rows = first_col_data.count();
420434
let mut print_table_rows: Vec<PrintRow> = vec![PrintRow::new(vec![]); num_rows];
421435

@@ -426,9 +440,9 @@ impl Table {
426440
let columns: Vec<String> = col_val.get_serialized_col_data();
427441

428442
for i in 0..num_rows {
429-
if let Some(cell) = &columns.get(i){
443+
if let Some(cell) = &columns.get(i) {
430444
print_table_rows[i].add_cell(PrintCell::new(cell));
431-
}else{
445+
} else {
432446
print_table_rows[i].add_cell(PrintCell::new(""));
433447
}
434448
}
@@ -524,10 +538,10 @@ pub enum Row {
524538
impl Row {
525539
fn get_serialized_col_data(&self) -> Vec<String> {
526540
match self {
527-
Row::Integer(cd) => cd.iter().map(|(i, v)| v.to_string()).collect(),
528-
Row::Real(cd) => cd.iter().map(|(i, v)| v.to_string()).collect(),
529-
Row::Text(cd) => cd.iter().map(|(i, v)| v.to_string()).collect(),
530-
Row::Bool(cd) => cd.iter().map(|(i, v)| v.to_string()).collect(),
541+
Row::Integer(cd) => cd.iter().map(|(_i, v)| v.to_string()).collect(),
542+
Row::Real(cd) => cd.iter().map(|(_i, v)| v.to_string()).collect(),
543+
Row::Text(cd) => cd.iter().map(|(_i, v)| v.to_string()).collect(),
544+
Row::Bool(cd) => cd.iter().map(|(_i, v)| v.to_string()).collect(),
531545
Row::None => panic!("Found None in columns"),
532546
}
533547
}
@@ -591,16 +605,18 @@ mod tests {
591605
assert_eq!(table.last_rowid, 0);
592606

593607
let id_column = "id".to_string();
594-
if let Some(column) = table.columns
608+
if let Some(column) = table
609+
.columns
595610
.iter()
596611
.filter(|c| c.column_name == id_column)
597612
.collect::<Vec<&Column>>()
598-
.first() {
599-
assert_eq!(column.is_pk, true);
600-
assert_eq!(column.datatype, DataType::Integer);
601-
} else {
602-
panic!("column not found");
603-
}
613+
.first()
614+
{
615+
assert_eq!(column.is_pk, true);
616+
assert_eq!(column.datatype, DataType::Integer);
617+
} else {
618+
panic!("column not found");
619+
}
604620
}
605621

606622
#[test]

0 commit comments

Comments
 (0)