Skip to content

Commit

Permalink
Merge pull request #51 from gwenn/indexmap
Browse files Browse the repository at this point in the history
Use IndexMap for column definitions
  • Loading branch information
gwenn committed Mar 17, 2024
2 parents b1224f7 + 2666def commit 6e5fd27
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/parser/ast/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl CreateTableBody {
} = self
{
let mut generated_count = 0;
for c in columns {
for c in columns.values() {
for cs in c.constraints.iter() {
if let ColumnConstraint::Generated { .. } = cs.constraint {
generated_count += 1;
Expand All @@ -205,7 +205,7 @@ impl CreateTableBody {
}

if options.contains(TableOptions::STRICT) {
for c in columns {
for c in columns.values() {
match &c.col_type {
Some(Type { name, .. }) => {
// The datatype must be one of following: INT INTEGER REAL TEXT BLOB ANY
Expand Down Expand Up @@ -250,7 +250,7 @@ impl CreateTableBody {
..
} = self
{
for col in columns {
for col in columns.values() {
for c in col {
if let ColumnConstraint::PrimaryKey { .. } = c {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/parser/ast/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ impl ToTokens for CreateTableBody {
options,
} => {
s.append(TK_LP, None)?;
comma(columns, s)?;
comma(columns.values(), s)?;
if let Some(constraints) = constraints {
s.append(TK_COMMA, None)?;
comma(constraints, s)?;
Expand Down
12 changes: 6 additions & 6 deletions src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::num::ParseIntError;
use std::str::FromStr;

use fmt::{ToTokens, TokenStream};
use indexmap::IndexSet;
use indexmap::{IndexMap, IndexSet};
use uncased::Uncased;

use crate::custom_err;
Expand Down Expand Up @@ -1073,7 +1073,7 @@ pub enum CreateTableBody {
/// columns and constraints
ColumnsAndConstraints {
/// table column definitions
columns: Vec<ColumnDefinition>,
columns: IndexMap<Name, ColumnDefinition>,
/// table constraints
constraints: Option<Vec<NamedTableConstraint>>,
/// table options
Expand All @@ -1086,7 +1086,7 @@ pub enum CreateTableBody {
impl CreateTableBody {
/// Constructor
pub fn columns_and_constraints(
columns: Vec<ColumnDefinition>,
columns: IndexMap<Name, ColumnDefinition>,
constraints: Option<Vec<NamedTableConstraint>>,
options: TableOptions,
) -> Result<CreateTableBody, ParserError> {
Expand All @@ -1113,10 +1113,10 @@ pub struct ColumnDefinition {
impl ColumnDefinition {
/// Constructor
pub fn add_column(
columns: &mut Vec<ColumnDefinition>,
columns: &mut IndexMap<Name, ColumnDefinition>,
mut cd: ColumnDefinition,
) -> Result<(), ParserError> {
if columns.iter().any(|c| c.col_name == cd.col_name) {
if columns.contains_key(&cd.col_name) {
// TODO unquote
return Err(custom_err!("duplicate column name: {}", cd.col_name));
}
Expand Down Expand Up @@ -1147,7 +1147,7 @@ impl ColumnDefinition {
col_type.name = new_type.join(" ");
}
}
columns.push(cd);
columns.insert(cd.col_name.clone(), cd);
Ok(())
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/parser/parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use crate::custom_err;
use crate::parser::ast::*;
use crate::parser::{Context, ParserError};
use crate::dialect::{from_token, Token, TokenType};
use indexmap::IndexMap;
use log::{debug, error, log_enabled};
use uncased::Uncased;

Expand Down Expand Up @@ -157,15 +158,17 @@ table_option(A) ::= nm(X). {
return Err(custom_err!("unknown table option: {}", option));
}
}
%type columnlist {Vec<ColumnDefinition>}
%type columnlist {IndexMap<Name,ColumnDefinition>}
columnlist(A) ::= columnlist(A) COMMA columnname(X) carglist(Y). {
let col = X;
let cd = ColumnDefinition{ col_name: col.0, col_type: col.1, constraints: Y };
ColumnDefinition::add_column(A, cd)?;
}
columnlist(A) ::= columnname(X) carglist(Y). {
let col = X;
A = vec![ColumnDefinition{ col_name: col.0, col_type: col.1, constraints: Y }];
let mut map = IndexMap::new();
map.insert(col.0.clone(), ColumnDefinition{ col_name: col.0, col_type: col.1, constraints: Y });
A = map;
}
%type columnname {(Name, Option<Type>)}
columnname(A) ::= nm(X) typetoken(Y). {A = (X, Y);}
Expand Down

0 comments on commit 6e5fd27

Please sign in to comment.