Skip to content

Commit

Permalink
checks if schema exists and displays message
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcfrancisco committed Aug 7, 2024
1 parent 164cdd2 commit 09561a3
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/pg/crud.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
use crate::{Result, Error};
use crate::{Error, Result};
use postgres::types::Type;
use postgres::Statement;

use postgres::{Client, NoTls};
use crate::utils::cli::Cli;
use postgres::{Client, NoTls};

use crate::file_types::common::NewTableTypes;

pub fn prepare_postgis(args: &Cli, config: &[NewTableTypes]) -> Result<()> {

// Check if schema already exists
// if so, printout a message that schema already exists
// else, the below message

println!("DO FIX");

// If schema present, create schema
if let Some(schema) = &args.schema {
create_schema(schema, &args.uri)?;
println!("\nSchema '{}' created ✓", schema);
let schema_exists = create_schema(schema, &args.uri)?;
if !schema_exists {
println!("Schema '{}' already exists ✓", schema);
} else {
println!("\nSchema '{}' created ✓", schema);
}
}
create_table(&args.table, &args.schema, &config, &args.uri, &args.srid)?;
println!("Table '{}' created ✓", args.table);

Ok(())

}

pub fn create_connection(uri: &str) -> Result<Client> {
let client = Client::connect(uri, NoTls)?;
Ok(client)
}

pub fn create_schema(schema_name: &str, uri: &str) -> Result<()> {
pub fn create_schema(schema_name: &str, uri: &str) -> Result<bool> {
let mut client = create_connection(uri)?;
client.batch_execute(&format!("CREATE SCHEMA IF NOT EXISTS {}", schema_name))?;
Ok(())
let query = "SELECT EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = $1)";
let row = client.query_one(query, &[&schema_name])?;
let exists: bool = row.get(0);
if exists {
return Ok(false);
} else {
client.batch_execute(&format!("CREATE SCHEMA {}", schema_name))?;
return Ok(true);
}
}

pub fn get_stmt(table_name: &str, schema_name: &Option<String>, uri: &str) -> Result<Statement> {
Expand Down Expand Up @@ -96,7 +100,6 @@ pub fn create_table(
client.execute(&query, &[])?;

Ok(())

}

pub fn can_append(table_name: &str, schema_name: &Option<String>, uri: &str) -> Result<()> {
Expand All @@ -117,15 +120,13 @@ pub fn can_append(table_name: &str, schema_name: &Option<String>, uri: &str) ->
if exists {
return Ok(());
} else {
return Err(Error::CannotAppend("Cannot append to a table that does NOT exist ✘".into()));
return Err(Error::CannotAppend(
"Cannot append to a table that does NOT exist ✘".into(),
));
}
}

pub fn check_table_exists(
table_name: &str,
schema_name: &Option<String>,
uri: &str,
) -> Result<()> {
pub fn check_table_exists(table_name: &str, schema_name: &Option<String>, uri: &str) -> Result<()> {
let mut client = create_connection(uri)?;
let query = if let Some(schema) = schema_name {
format!(
Expand Down

0 comments on commit 09561a3

Please sign in to comment.