pub fn validate_unique_constraint(
        &mut self,
        cols: &Vec<String>,
        values: &Vec<String>,
    ) -> Result<()> {
        for (idx, name) in cols.iter().enumerate() {
            let column = self.get_column_mut(name.to_string()).unwrap();
            // println!(
            //     "name: {} | is_pk: {} | is_unique: {}, not_null: {}",
            //     name, column.is_pk, column.is_unique, column.not_null
            // );
            if column.is_unique {
                let col_idx = &column.index;
                if *name == *column.column_name {
                    let val = &values[idx];
                    match col_idx {
                        Index::Integer(index) => {
                            if index.contains_key(&val.parse::<i32>().unwrap()) {
                                return Err(SQLRiteError::General(format!(
                                    "Error: unique constraint violation for column {}.
                        Value {} already exists for column {}",
                                    *name, val, *name
                                )));
                            }
                        }
                        Index::Text(index) => {
                            if index.contains_key(val) {
                                return Err(SQLRiteError::General(format!(
                                    "Error: unique constraint violation for column {}.
                        Value {} already exists for column {}",
                                    *name, val, *name
                                )));
                            }
                        }
                        Index::None => {
                            return Err(SQLRiteError::General(format!(
                                "Error: cannot find index for column {}",
                                name
                            )));
                        }
                    };
                }
            }
        }
        return Ok(());
    }
/// Validates if columns and values being inserted violate the UNIQUE constraint
/// As a reminder the PRIMARY KEY column automatically also is a UNIQUE column.
///
Validates if columns and values being inserted violate the UNIQUE constraint
As a reminder the PRIMARY KEY column automatically also is a UNIQUE column.
Validates if columns and values being inserted violate the UNIQUE constraint As a reminder the PRIMARY KEY column automatically also is a UNIQUE column .
pub fn validate_unique_constraint(
        &mut self,
        cols: &Vec<String>,
        values: &Vec<String>,
    ) -> Result<()> {
        for (idx, name) in cols.iter().enumerate() {
            let column = self.get_column_mut(name.to_string()).unwrap();
            // println!(
            //     "name: {} | is_pk: {} | is_unique: {}, not_null: {}",
            //     name, column.is_pk, column.is_unique, column.not_null
            // );
            if column.is_unique {
                let col_idx = &column.index;
                if *name == *column.column_name {
                    let val = &values[idx];
                    match col_idx {
                        Index::Integer(index) => {
                            if index.contains_key(&val.parse::<i32>().unwrap()) {
                                return Err(SQLRiteError::General(format!(
                                    "Error: unique constraint violation for column {}.
                        Value {} already exists for column {}",
                                    *name, val, *name
                                )));
                            }
                        }
                        Index::Text(index) => {
                            if index.contains_key(val) {
                                return Err(SQLRiteError::General(format!(
                                    "Error: unique constraint violation for column {}.
                        Value {} already exists for column {}",
                                    *name, val, *name
                                )));
                            }
                        }
                        Index::None => {
                            return Err(SQLRiteError::General(format!(
                                "Error: cannot find index for column {}",
                                name
                            )));
                        }
                    };
                }
            }
        }
        return Ok(());
    }
pub fn validate_unique_constraint ( & mut self , cols : & Vec < String > , values : & Vec < String > , ) -> Result < ( ) > { for ( idx , name ) in cols . iter ( ) . enumerate ( ) { let column = self . get_column_mut ( name . to_string ( ) ) . unwrap ( ) ; // println!( //     "name: {} | is_pk: {} | is_unique: {}, not_null: {}", //     name, column.is_pk, column.is_unique, column.not_null // ); if column . is_unique { let col_idx = & column . index ; if * name == * column . column_name { let val = & values [ idx ] ; match col_idx { Index :: Integer ( index ) => { if index . contains_key ( & val . parse :: < i32 > ( ) . unwrap ( ) ) { return Err ( SQLRiteError :: General ( format ! ( " " name val name ) ) ) ; } } Index :: Text ( index ) => { if index . contains_key ( val ) { return Err ( SQLRiteError :: General ( format ! ( " " name val name ) ) ) ; } } Index :: None => { return Err ( SQLRiteError :: General ( format ! ( " " name ) ) ) ; } } ; } } } return Ok ( ( ) ) ; }
Validates if columns and values being inserted violate the UNIQUE constraint
As a reminder the PRIMARY KEY column automatically also is a UNIQUE column.
Validates if columns and values being inserted violate the UNIQUE constraint As a reminder the PRIMARY KEY column automatically also is a UNIQUE column .
&mut self
cols
&Vec<String>
values
&Vec<String>
&mut self
cols
&Vec<String>
values
&Vec<String>