pub struct PostgresBase { /* private fields */ }
Expand description
Represents a connection config to a PostgreSQL database.
§Example
use safety_postgres::access::postgres_base::PostgresBase;
use safety_postgres::access::sql_base::QueryColumns;
async fn postgres_query() {
let mut postgres = PostgresBase::new("table_name")
.expect("postgres base init failed");
postgres.connect().await.expect("connect failed");
let query_columns = QueryColumns::new(true);
postgres.query_raw(&query_columns).await.expect("query failed");
}
Implementations§
Source§impl PostgresBase
impl PostgresBase
Sourcepub fn new(table_name: &str) -> Result<Self, PostgresBaseError>
pub fn new(table_name: &str) -> Result<Self, PostgresBaseError>
Creates a new instance of PostgresBase
for interacting with a PostgreSQL database.
§Arguments
table_name
- The name of the table to interact with.
§Returns
Returns a Result
containing the new PostgresBase
instance if successful,
or a PostgresBaseError
if an error occurs.
§Example
use safety_postgres::access::postgres_base::PostgresBase;
let mut postgres = PostgresBase::new("table_name").expect("PostgresBase init failed");
Sourcepub async fn connect(&mut self) -> Result<(), PGError>
pub async fn connect(&mut self) -> Result<(), PGError>
Connects to a PostgreSQL database using the provided configuration.
§Returns
Returns a result indicating whether the connection was successful or an error occurred.
If the connection is successful, Ok(())
is returned.
If an error occurs, Err(PGError)
is returned.
§Example
use safety_postgres::access::postgres_base::PostgresBase;
async fn postgres_connect() {
let mut postgres = PostgresBase::new("your_table_name").expect("PostgresBase struct return error");
let _ = postgres.connect().await.expect("connect failed");
}
Sourcepub async fn query_raw(
&self,
query_columns: &QueryColumns,
) -> Result<Vec<Row>, PostgresBaseError>
pub async fn query_raw( &self, query_columns: &QueryColumns, ) -> Result<Vec<Row>, PostgresBaseError>
Executes a raw query on the database and returns the result.
§Arguments
query_columns
- AQueryColumns
struct reference specifying the columns to query.
§Returns
Ok(Vec<Row>)
- Get the values if the query was successful.Err(PostgresBaseError)
- If an error occurred during the query process.
§Errors
Returns a PostgresBaseError
if there was an error executing the query.
Sourcepub async fn query_condition_raw(
&self,
query_column: &QueryColumns,
conditions: &Conditions,
) -> Result<Vec<Row>, PostgresBaseError>
pub async fn query_condition_raw( &self, query_column: &QueryColumns, conditions: &Conditions, ) -> Result<Vec<Row>, PostgresBaseError>
Queries the database for data based on the provided query column and conditions.
§Arguments
query_column
- The columns using reference of theQueryColumns
struct to query.conditions
- The conditions using reference of theConditions
to apply to the query.
§Returns
Ok(Vec<Row>)
- Get the values if the query was successful.Err(PostgresBaseError)
- If an error occurred during the query process.
§Errors
Returns a PostgresBaseError
if there was an error querying the database.
Sourcepub async fn query_inner_join_conditions(
&self,
query_columns: &QueryColumns,
join_tables: &JoinTables,
conditions: &Conditions,
) -> Result<Vec<Row>, PostgresBaseError>
pub async fn query_inner_join_conditions( &self, query_columns: &QueryColumns, join_tables: &JoinTables, conditions: &Conditions, ) -> Result<Vec<Row>, PostgresBaseError>
Queries the database with inner join and conditions.
§Arguments
query_columns
- The columns using reference of theQueryColumns
struct to query.join_tables
- The tables collection using reference of theJoinTables
to join.conditions
- The conditions using reference of theConditions
to apply to the query.
§Returns
Ok(Vec<Row>)
- Get the values if the query was successfulErr(PostgresBaseError)
- If an error occurred during the query process.
§Examples
use safety_postgres::access::conditions::Conditions;
use safety_postgres::access::join_tables::JoinTables;
use safety_postgres::access::postgres_base::PostgresBase;
use safety_postgres::access::sql_base::QueryColumns;
async fn postgres_query() {
let mut db = PostgresBase::new("table_name").unwrap();
db.connect().await.expect("connection failed");
let query_column = QueryColumns::new(true);
let join_tables = JoinTables::new();
let conditions = Conditions::new();
/**
* Your code....
*/
let result = db.query_condition_raw(&query_column, &conditions).await;
match result {
Ok(rows) => {
for row in rows {
// Do something with the row
}
}
Err(error) => {
// Handle the error
}
}
}
Sourcepub async fn insert(
&self,
insert_records: &InsertRecords,
) -> Result<(), PostgresBaseError>
pub async fn insert( &self, insert_records: &InsertRecords, ) -> Result<(), PostgresBaseError>
Inserts records into the database table.
§Arguments
insert_records
- AnInsertRecords
object reference containing the records to be inserted.
§Returns
Ok(())
- If the records were inserted successfully.Err(PostgresBaseError)
- If an error occurred during the insertion process.
§Examples
use safety_postgres::access::postgres_base::PostgresBase;
use safety_postgres::access::sql_base::InsertRecords;
async fn postgres_insert() {
let mut db = PostgresBase::new("my_table").expect("db struct init failed");
db.connect().await.expect("connection failed");
let mut insert_records = InsertRecords::new(&["column1", "column2"]);
insert_records.add_record(&["value1", "value2"]).expect("add record failed");
let result = db.insert(&insert_records).await.expect("insert failed");
}
Sourcepub async fn update(
&self,
update_set: &UpdateSets,
allow_all_update: bool,
) -> Result<(), PostgresBaseError>
pub async fn update( &self, update_set: &UpdateSets, allow_all_update: bool, ) -> Result<(), PostgresBaseError>
Updates records in the specified table based on the given update sets.
§Arguments
update_set
: AnUpdateSets
object reference which containing the fields to update.allow_all_update
: A boolean flag indicating whether updating all records is allowed.
§Returns
Ok(())
if the update is successful.Err(PostgresBaseError)
if an error occurs during the update.
Sourcepub async fn update_condition(
&self,
update_set: &UpdateSets,
conditions: &Conditions,
) -> Result<(), PostgresBaseError>
pub async fn update_condition( &self, update_set: &UpdateSets, conditions: &Conditions, ) -> Result<(), PostgresBaseError>
Updates records in the table based on the specified update set and conditions.
§Arguments
update_set
- TheUpdateSets
reference specifying the columns and values to update.conditions
- TheConditions
reference specifying the records to update.
§Returns
Ok(())
- If the update operation is successful.Err(PostgresBaseError)
- If an error occurs during the update operation.
§Example
use safety_postgres::access::conditions::{Conditions, IsInJoinedTable};
use safety_postgres::access::postgres_base::PostgresBase;
use safety_postgres::access::sql_base::UpdateSets;
async fn postgres_update() {
let mut database = PostgresBase::new("my_table").expect("postgres base init failed");
database.connect().await.expect("connection failed");
let mut update_set = UpdateSets::new();
update_set.add_set("column1", "value1").unwrap();
let mut conditions = Conditions::new();
conditions.add_condition_from_str(
"column1",
"value1",
"eq",
"",
IsInJoinedTable::No)
.expect("adding condition failed");
database.update_condition(&update_set, &conditions).await.expect("update failed");
}
Sourcepub async fn delete(
&self,
conditions: &Conditions,
) -> Result<(), PostgresBaseError>
pub async fn delete( &self, conditions: &Conditions, ) -> Result<(), PostgresBaseError>
Delete records from the database table based on given conditions.
§Arguments
conditions
- The reference of the conditions used to filter the records to be deleted.
§Returns
Ok(())
- Returns this if the deletion is successfulPostgresBaseError
- Returns an error of typePostgresBaseError
when deletion process failed.
§Examples
use safety_postgres::access::conditions::ComparisonOperator::Grater;
use safety_postgres::access::conditions::{Conditions, IsInJoinedTable};
use safety_postgres::access::conditions::LogicalOperator::FirstCondition;
use safety_postgres::access::postgres_base::PostgresBase;
async fn postgres_delete() {
let mut database = PostgresBase::new("my_table").expect("db init failed");
database.connect().await.expect("connecting failed");
let mut conditions = Conditions::new();
conditions.add_condition(
"column1",
"value1",
Grater,
FirstCondition,
IsInJoinedTable::No).expect("adding condition failed");
database.delete(&conditions).await.expect("delete failed");
}
Sourcepub fn set_dbname(&mut self, dbname: &str) -> &mut Self
pub fn set_dbname(&mut self, dbname: &str) -> &mut Self
Sets the name of the database.
This method validates the given dbname
parameter to ensure it consists only of alphanumeric characters and underscores.
If the validation fails, an error message is printed to the standard error output and the change is rejected.
§Arguments
dbname
- The new name of the database.
§Returns
The updated self
object.
Sourcepub fn set_schema(&mut self, schema_name: &str) -> &mut Self
pub fn set_schema(&mut self, schema_name: &str) -> &mut Self
Sourcepub fn get_config(&self) -> String
pub fn get_config(&self) -> String
Get the configuration string for connecting to the PostgreSQL database.
§Returns
- Configuration - Representing the configuration for connecting to the database.