// Persist a predicate into the database
// This function is internal because it does not add it to the object, it
// _only_ puts record of the predicate into the database.
Persist a predicate into the database
This function is internal because it does not add it to the object, it
_only_ puts record of the predicate into the database.
Persist a predicate into the database This function is internal because it does not add it to the object it _only_ puts record of the predicate into the database .
fn insert_predicate(&self, pred: &Predicate) -> Result<()> {
        let &Predicate {
            ref name,
            ref description,
            ref fields,
        } = pred;
        let conn = self.conn_pool.get()?;
        let stmt = conn.prepare(
            "insert into predicates (name, description) values ($1, $2) returning id",
        )?;
        let pred_id: i32 = stmt.query(&[name, description])?.get(0).get(0);
        for (ordinal, field) in fields.iter().enumerate() {
            try!(conn.execute(
                "insert into fields (pred_id, name, description, type, ordinal) \
                          values ($1, $2, $3, $4, $5)",
                &[
                    &pred_id,
                    &field.name,
                    &field.description,
                    &field.type_.name().ok_or(ErrorKind::Arg(
                        "Field type had no name".to_string(),
                    ))?,
                    &(ordinal as i32),
                ],
            ));
        }
        let table_str = fields
            .iter()
            .map(|field| field.type_.repr())
            .enumerate()
            .map(|(ord, repr)| format!("arg{} {}", ord, repr))
            .collect::<Vec<_>>()
            .join(", ");
        let col_str = fields
            .iter()
            .map(|field| {
                (field.type_.large(), field.type_.repr().contains("[]"))
            })
            .enumerate()
            .map(|(ord, (large, is_array))| if large {
                if is_array {
                    format!("md5(array_to_string(arg{}, ','))", ord)
                } else {
                    format!("md5(arg{}::text)", ord)
                }
            } else {
                format!("arg{}", ord)
            })
            .collect::<Vec<_>>()
            .join(", ");
        self.conn_pool.get()?.execute(
            &format!(
                "create table facts.{} (id INT8 DEFAULT nextval('fact_id') NOT \
                               NULL primary key, {})",
                name,
                table_str
            ),
            &[],
        )?;
        if col_str != "" {
            self.conn_pool.get()?.execute(
                &format!(
                    "create unique index on facts.{} ({})",
                    name,
                    col_str
                ),
                &[],
            )?;
        }
        Ok(())
    }
fn insert_predicate ( & self , pred : & Predicate ) -> Result < ( ) > { let & Predicate { ref name , ref description , ref fields , } = pred ; let conn = self . conn_pool . get ( ) ? ; let stmt = conn . prepare ( " " , ) ? ; let pred_id : i32 = stmt . query ( & [ name , description ] ) ? . get ( 0 ) . get ( 0 ) ; for ( ordinal , field ) in fields . iter ( ) . enumerate ( ) { try ! ( conn execute ( " \
 " [ pred_id field name field description field type_ name ( ) ok_or ( ErrorKind Arg ( " " to_string ( ) ) ) ( ordinal as i32 ) ] ) ) ; } let table_str = fields . iter ( ) . map ( | field | field . type_ . repr ( ) ) . enumerate ( ) . map ( | ( ord , repr ) | format ! ( " " ord repr ) ) . collect :: < Vec < _ > > ( ) . join ( " " ) ; let col_str = fields . iter ( ) . map ( | field | { ( field . type_ . large ( ) , field . type_ . repr ( ) . contains ( " " ) ) } ) . enumerate ( ) . map ( | ( ord , ( large , is_array ) ) | if large { if is_array { format ! ( " " ord ) } else { format ! ( " " ord ) } } else { format ! ( " " ord ) } ) . collect :: < Vec < _ > > ( ) . join ( " " ) ; self . conn_pool . get ( ) ? . execute ( & format ! ( " \
 " name table_str ) , & [ ] , ) ? ; if col_str != " " { self . conn_pool . get ( ) ? . execute ( & format ! ( " " name col_str ) , & [ ] , ) ? ; } Ok ( ( ) ) }
Persist a predicate into the database
This function is internal because it does not add it to the object, it
_only_ puts record of the predicate into the database.
Persist a predicate into the database This function is internal because it does not add it to the object it _only_ puts record of the predicate into the database .
&self
pred
&Predicate