parsql_tokio_postgres/
lib.rs

1//! # parsql-tokio-postgres
2//! 
3//! Asynchronous PostgreSQL integration for parsql using tokio-postgres.
4//! This crate provides async/await APIs for working with PostgreSQL databases.
5//! 
6//! ## Features
7//! 
8//! - Asynchronous PostgreSQL operations
9//! - Automatic SQL query generation
10//! - Secure parameter management
11//! - Generic CRUD operations
12//! - Deadpool connection pool support
13//! - SQL Injection protection
14//! - Detailed error reporting
15//! 
16//! ## Usage
17//! 
18//! ```rust,no_run
19//! use tokio_postgres::{NoTls, Error};
20//! use parsql::tokio_postgres::{CrudOps};
21//! use parsql::macros::{Insertable, Queryable, SqlParams, FromRow};
22//! 
23//! #[derive(Insertable, SqlParams)]
24//! #[table("users")]
25//! pub struct InsertUser {
26//!     pub name: String,
27//!     pub email: String,
28//! }
29//! 
30//! #[derive(Queryable, SqlParams, FromRow)]
31//! #[table("users")]
32//! #[where_clause("id = $")]
33//! pub struct GetUser {
34//!     pub id: i32,
35//!     pub name: String,
36//!     pub email: String,
37//! }
38//! 
39//! impl GetUser {
40//!     pub fn new(id: i32) -> Self {
41//!         Self {
42//!             id,
43//!             name: Default::default(),
44//!             email: Default::default(),
45//!         }
46//!     }
47//! }
48//! 
49//! #[tokio::main]
50//! async fn main() -> Result<(), Error> {
51//!     let (client, connection) = tokio_postgres::connect(
52//!         "host=localhost user=postgres dbname=test",
53//!         NoTls,
54//!     ).await?;
55//!     
56//!     tokio::spawn(async move {
57//!         if let Err(e) = connection.await {
58//!             eprintln!("Connection error: {}", e);
59//!         }
60//!     });
61//!     
62//!     // Insert a new user
63//!     let insert_user = InsertUser {
64//!         name: "John".to_string(),
65//!         email: "john@example.com".to_string(),
66//!     };
67//!     
68//!     // Extension method style
69//!     let id = client.insert(insert_user).await?;
70//!     
71//!     // Get the user back
72//!     let get_user = GetUser::new(id as i32);
73//!     let user = client.fetch(get_user).await?;
74//!     
75//!     println!("User: {:?}", user);
76//!     Ok(())
77//! }
78//! ```
79//!
80//! The trait-based extension methods can also be used with client objects from deadpool-postgres:
81//!
82//! ```rust,no_run
83//! use parsql::tokio_postgres::CrudOps;
84//! use deadpool_postgres::{Config, Pool};
85//! use tokio_postgres::NoTls;
86//!
87//! async fn example() -> Result<(), Box<dyn std::error::Error>> {
88//!     let mut cfg = Config::new();
89//!     cfg.host = Some("localhost".to_string());
90//!     cfg.user = Some("postgres".to_string());
91//!     
92//!     let pool = cfg.create_pool(None, NoTls)?;
93//!     
94//!     // Get client from pool
95//!     let client = pool.get().await?;
96//!     
97//!     // Use extension methods
98//!     let users = client.fetch_all(active_users_query).await?;
99//!     
100//!     Ok(())
101//! }
102//! ```
103
104pub mod crud_ops;
105pub mod traits;
106pub mod macros;
107
108/// Transaction support module 
109/// 
110/// This module provides support for database transactions, including:
111/// - Transaction management functions
112/// - Implementation of `CrudOps` trait for `Transaction` objects
113/// - Helper functions for working with transactions
114///
115/// There are two ways to use transactions:
116/// 1. Using the `CrudOps` trait methods directly on a `Transaction` object
117/// 2. Using the transaction helper functions from the `transactional` module
118///
119/// ```rust,no_run
120/// use tokio_postgres::{NoTls, Error};
121/// use parsql::tokio_postgres::{CrudOps, transactional};
122/// use parsql::macros::{Insertable, SqlParams};
123/// 
124/// #[derive(Insertable, SqlParams)]
125/// #[table("users")]
126/// struct InsertUser {
127///     name: String,
128///     email: String,
129/// }
130/// 
131/// #[tokio::main]
132/// async fn main() -> Result<(), Error> {
133///     let (mut client, connection) = tokio_postgres::connect("", NoTls).await?;
134///     tokio::spawn(async move { connection.await; });
135///     
136///     // Approach 1: CrudOps trait on Transaction
137///     let tx = client.transaction().await?;
138///     let rows = tx.insert(InsertUser {
139///         name: "John".to_string(),
140///         email: "john@example.com".to_string(),
141///     }).await?;
142///     tx.commit().await?;
143///     
144///     // Approach 2: Helper functions
145///     let tx = transactional::begin(&mut client).await?;
146///     let (tx, rows) = transactional::tx_insert(tx, InsertUser {
147///         name: "Jane".to_string(),
148///         email: "jane@example.com".to_string(),
149///     }).await?;
150///     tx.commit().await?;
151///     
152///     Ok(())
153/// }
154/// ```
155pub mod transaction_ops;
156
157// Re-export tokio-postgres types that might be needed
158pub use tokio_postgres::{types::ToSql, Row, Error, Client};
159pub use macros::*;
160// Re-export crud operations
161pub use crate::crud_ops::{
162    insert,
163    update,
164    delete,
165    fetch,
166    fetch_all,
167    select,
168    select_all
169};
170
171// Geriye dönük uyumluluk için eski fonksiyonları deprecated olarak dışa aktaralım
172#[allow(deprecated)]
173pub use crate::crud_ops::{
174    get,
175    get_all
176};
177
178/// Re-export transaction modules
179/// 
180/// This provides easy access to transaction functions via `transactional` namespace.
181/// Functions include:
182/// - `begin`: Begin a new transaction
183/// - `tx_insert`: Insert a record within a transaction
184/// - `tx_update`: Update records within a transaction
185/// - `tx_delete`: Delete records within a transaction
186/// - `tx_fetch`: Get a single record within a transaction  
187/// - `tx_fetch_all`: Get multiple records within a transaction
188/// - `tx_select`: Execute a custom query and transform a single result within a transaction
189/// - `tx_select_all`: Execute a custom query and transform multiple results within a transaction
190/// - `tx_get`: (Deprecated) Get a single record within a transaction
191/// - `tx_get_all`: (Deprecated) Get multiple records within a transaction
192pub use transaction_ops as transactional;
193