/// Create a new PgDB object by passing in a Postgres connection string
// At the moment, persistence with custom types will result in failures
// on a reconnect, so use a fresh database every time.
// There's not a good way to persist custom types, so that fix will likely
// come with optional parameters to seed types in at db startup.
Create a new PgDB object by passing in a Postgres connection string
At the moment, persistence with custom types will result in failures
on a reconnect, so use a fresh database every time.
There's not a good way to persist custom types, so that fix will likely
come with optional parameters to seed types in at db startup.
Create a new PgDB object by passing in a Postgres connection string At the moment persistence with custom types will result in failures on a reconnect so use a fresh database every time . There ' s not a good way to persist custom types so that fix will likely come with optional parameters to seed types in at db startup .
pub fn new(uri: &str) -> Result<PgDB> {
        // Create database if it doesn't already exist and we can
        let params = try!(uri.into_connect_params().map_err(|_| ErrorKind::UriParse));
        match Connection::connect(params.clone(), ::postgres::TlsMode::None) {
            // Database not found
            Err(ref db_error)
                if db_error.code() == Some(&::postgres::error::UNDEFINED_DATABASE) &&
                       params.database().is_some() => {
                let pg_params = param_into_builder(&params).database("postgres").build(
                    params
                        .host()
                        .clone(),
                );
                let create_query = format!("CREATE DATABASE {}", params.database().unwrap());
                let conn = Connection::connect(pg_params, ::postgres::TlsMode::None)?;
                conn.execute(&create_query, &[])?;
            }
            // If it's not a database not found error, we don't know how to recover, rethrow
            Err(db_error) => Err(db_error)?,
            // The test connection succeeded
            Ok(_) => (),
        }
        // Establish the pool
        let manager = PostgresConnectionManager::new(uri, TlsMode::None)?;
        let pool = r2d2::Pool::new(r2d2::Config::default(), manager)?;
        let conn = pool.get()?;
        // Create schemas
        try!(conn.execute("create schema if not exists facts", &[]));
        // Create Tables
        try!(conn.execute(
            "create table if not exists predicates (id serial primary key, \
                           name varchar not null, \
                           description varchar)",
            &[],
        ));
        try!(conn.execute(
            "create table if not exists fields (\
                           pred_id serial references predicates(id), \
                           ordinal int4 not null, \
                           type varchar not null, \
                           name varchar, \
                           description varchar)",
            &[],
        ));
        try!(conn.execute("create sequence if not exists fact_id", &[]));
        // Make array_to_string immutable to legalize index shenanigans
        // array_to_string is not actually immutable for some arrays (namely when ::text for the
        // element type is not immutable) so this is kind of taking off the safety rails
        try!(conn.execute(
            "alter function array_to_string(anyarray, text) IMMUTABLE",
            &[],
        ));
        // Create incremental PgDB object
        let db = PgDB {
            conn_pool: pool,
            pred_by_name: RefCell::new(HashMap::new()),
            insert_by_name: RefCell::new(HashMap::new()),
            named_types: RefCell::new(
                types::default_types()
                    .iter()
                    .filter_map(|type_| {
                        type_.name().map(|name| (name.to_owned(), type_.clone()))
                    })
                    .collect(),
            ),
        };
        try!(db.rebuild_predicate_cache());
        Ok(db)
    }
pub fn new ( uri : & str ) -> Result < PgDB > { // Create database if it doesn't already exist and we can let params = try ! ( uri into_connect_params ( ) map_err ( ErrorKind UriParse ) ) ; match Connection :: connect ( params . clone ( ) , :: postgres :: TlsMode :: None ) { // Database not found Err ( ref db_error ) if db_error . code ( ) == Some ( & :: postgres :: error :: UNDEFINED_DATABASE ) && params . database ( ) . is_some ( ) => { let pg_params = param_into_builder ( & params ) . database ( " " ) . build ( params . host ( ) . clone ( ) , ) ; let create_query = format ! ( " " params database ( ) unwrap ( ) ) ; let conn = Connection :: connect ( pg_params , :: postgres :: TlsMode :: None ) ? ; conn . execute ( & create_query , & [ ] ) ? ; } // If it's not a database not found error, we don't know how to recover, rethrow Err ( db_error ) => Err ( db_error ) ? , // The test connection succeeded Ok ( _ ) => ( ) , } // Establish the pool let manager = PostgresConnectionManager :: new ( uri , TlsMode :: None ) ? ; let pool = r2d2 :: Pool :: new ( r2d2 :: Config :: default ( ) , manager ) ? ; let conn = pool . get ( ) ? ; // Create schemas try ! ( conn execute ( " " [ ] ) ) ; // Create Tables try ! ( conn execute ( " \
 \
 " [ ] ) ) ; try ! ( conn execute ( " \
 \
 \
 \
 \
 " [ ] ) ) ; try ! ( conn execute ( " " [ ] ) ) ; // Make array_to_string immutable to legalize index shenanigans // array_to_string is not actually immutable for some arrays (namely when ::text for the // element type is not immutable) so this is kind of taking off the safety rails try ! ( conn execute ( " " [ ] ) ) ; // Create incremental PgDB object let db = PgDB { conn_pool : pool , pred_by_name : RefCell :: new ( HashMap :: new ( ) ) , insert_by_name : RefCell :: new ( HashMap :: new ( ) ) , named_types : RefCell :: new ( types :: default_types ( ) . iter ( ) . filter_map ( | type_ | { type_ . name ( ) . map ( | name | ( name . to_owned ( ) , type_ . clone ( ) ) ) } ) . collect ( ) , ) , } ; try ! ( db rebuild_predicate_cache ( ) ) ; Ok ( db ) }
Create a new PgDB object by passing in a Postgres connection string
At the moment, persistence with custom types will result in failures
on a reconnect, so use a fresh database every time.
Create a new PgDB object by passing in a Postgres connection string At the moment persistence with custom types will result in failures on a reconnect so use a fresh database every time .
uri
&str