parsql_postgres/lib.rs
1//! # parsql-postgres
2//!
3//! Synchronous PostgreSQL integration for parsql.
4//! This crate provides synchronous APIs for working with PostgreSQL databases.
5//!
6//! ## Features
7//!
8//! - Synchronous PostgreSQL operations
9//! - Automatic SQL query generation
10//! - Secure parameter management
11//! - Generic CRUD operations
12//! - Transaction support
13//! - Extension methods for the Client object
14//!
15//! ## Usage
16//!
17//! ```rust,no_run
18//! use postgres::{Client, NoTls, Error};
19//! use parsql::postgres::{fetch, insert};
20//!
21//! #[derive(Insertable, SqlParams)]
22//! #[table("users")]
23//! pub struct InsertUser {
24//! pub name: String,
25//! pub email: String,
26//! }
27//!
28//! #[derive(Queryable, SqlParams, FromRow)]
29//! #[table("users")]
30//! #[where_clause("id = $")]
31//! pub struct GetUser {
32//! pub id: i32,
33//! pub name: String,
34//! pub email: String,
35//! }
36//!
37//! fn main() -> Result<(), Error> {
38//! let mut client = Client::connect(
39//! "host=localhost user=postgres dbname=test",
40//! NoTls,
41//! )?;
42//!
43//! // Insert a new user
44//! let insert_user = InsertUser {
45//! name: "John".to_string(),
46//! email: "[email protected]".to_string(),
47//! };
48//!
49//! let id = insert(&mut client, insert_user)?;
50//!
51//! // Get the user back
52//! let get_user = GetUser::new(id as i32);
53//! let user = fetch(&mut client, &get_user)?;
54//!
55//! println!("User: {:?}", user);
56//! Ok(())
57//! }
58//! ```
59//!
60//! ## Using Extension Methods
61//!
62//! You can also use the extension methods directly on the Client object:
63//!
64//! ```rust,no_run
65//! use postgres::{Client, NoTls, Error};
66//! use parsql::postgres::CrudOps; // Import the trait
67//! use parsql::macros::{Insertable, SqlParams, Queryable, FromRow};
68//!
69//! #[derive(Insertable, SqlParams)]
70//! #[table("users")]
71//! pub struct InsertUser {
72//! pub name: String,
73//! pub email: String,
74//! }
75//!
76//! #[derive(Queryable, FromRow, SqlParams)]
77//! #[table("users")]
78//! #[where_clause("id = $1")]
79//! pub struct GetUser {
80//! pub id: i32,
81//! pub name: String,
82//! pub email: String,
83//! }
84//!
85//! fn main() -> Result<(), Error> {
86//! let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
87//!
88//! // Insert a new user using extension method
89//! let insert_user = InsertUser {
90//! name: "John".to_string(),
91//! email: "[email protected]".to_string(),
92//! };
93//!
94//! let rows_affected = client.insert(insert_user)?;
95//!
96//! // Get the user back using extension method
97//! let get_user = GetUser {
98//! id: 1,
99//! name: String::new(),
100//! email: String::new(),
101//! };
102//!
103//! let user = client.fetch(&get_user)?;
104//!
105//! println!("User: {:?}", user);
106//! Ok(())
107//! }
108//! ```
109//!
110//! ## Using Transactions
111//!
112//! You can also use transactions to ensure atomicity of operations:
113//!
114//! ```rust,no_run
115//! use postgres::{Client, NoTls, Error};
116//! use parsql::postgres::transactional::{begin, tx_insert, tx_update};
117//!
118//! #[derive(Insertable, SqlParams)]
119//! #[table("users")]
120//! pub struct InsertUser {
121//! pub name: String,
122//! pub email: String,
123//! }
124//!
125//! #[derive(Updateable, UpdateParams)]
126//! #[table("users")]
127//! #[where_clause("id = $1")]
128//! pub struct UpdateUser {
129//! pub id: i32,
130//! pub email: String,
131//! }
132//!
133//! fn main() -> Result<(), Error> {
134//! let mut client = Client::connect(
135//! "host=localhost user=postgres dbname=test",
136//! NoTls,
137//! )?;
138//!
139//! // Start a transaction
140//! let tx = begin(&mut client)?;
141//!
142//! // Insert a new user within the transaction
143//! let insert_user = InsertUser {
144//! name: "John".to_string(),
145//! email: "[email protected]".to_string(),
146//! };
147//!
148//! let (tx, _) = tx_insert(tx, insert_user)?;
149//!
150//! // Update the user within the same transaction
151//! let update_user = UpdateUser {
152//! id: 1,
153//! email: "[email protected]".to_string(),
154//! };
155//!
156//! let (tx, _) = tx_update(tx, update_user)?;
157//!
158//! // Commit the transaction
159//! tx.commit()?;
160//! Ok(())
161//! }
162//! ```
163
164pub mod crud_ops;
165pub mod transaction_ops;
166pub mod traits;
167pub mod macros;
168
169pub use postgres::types::ToSql;
170pub use postgres::Transaction;
171pub use postgres::{Client, Error, Row};
172pub use macros::*;
173
174// Re-export crud operations
175pub use crud_ops::{
176 delete, fetch, fetch_all, get_by_query, insert, select, select_all, update,
177};
178
179// Eski isimlerle fonksiyonları deprecated olarak dışa aktar
180#[allow(deprecated)]
181pub use crud_ops::{get, get_all};
182
183// Re-export transaction operations in a transactional module
184pub mod transactional {
185 pub use crate::transaction_ops::{
186 begin, tx_delete, tx_fetch, tx_fetch_all, tx_insert, tx_select, tx_select_all, tx_update,
187 };
188
189 // Eski isimlerle fonksiyonları deprecated olarak dışa aktar
190 #[allow(deprecated)]
191 pub use crate::transaction_ops::{tx_get, tx_get_all};
192}