pgsql_builder/
lib.rs

1#[macro_use]
2extern crate itertools;
3extern crate modifier;
4extern crate postgres;
5
6use postgres::types::ToSql;
7use std::borrow::Cow;
8use std::fmt;
9
10pub use self::delete::Delete;
11pub use self::insert::Insert;
12pub use self::predicate::Predicate;
13pub use self::select::Select;
14pub use self::update::Update;
15pub use modifier::Set;
16pub use util::escape;
17
18mod delete;
19mod insert;
20mod predicate;
21mod select;
22mod update;
23mod util;
24
25#[derive(Debug)]
26pub struct Assignment<'a>(pub Cow<'static, str>, pub &'a ToSql);
27pub type Assignments<'a> = Vec<Assignment<'a>>;
28
29#[derive(Debug)]
30pub struct Condition<'a>(pub Cow<'static, str>, pub Predicate<'a>);
31pub type Conditions<'a> = Vec<Condition<'a>>;
32
33#[derive(Debug)]
34pub struct Selection(pub Cow<'static, str>);
35pub type Selections = Vec<Selection>;
36
37#[derive(Debug)]
38pub struct Order(pub Cow<'static, str>, pub Dir);
39pub type Orders = Vec<Order>;
40
41#[derive(Debug)]
42pub struct Query<'a>(pub String, pub Vec<&'a ToSql>);
43
44#[derive(Debug)]
45pub struct Limit(pub usize);
46
47impl fmt::Display for Limit {
48    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49        fmt::Display::fmt(&self.0, f)
50    }
51}
52
53#[derive(Debug)]
54pub struct Offset(pub usize);
55
56impl fmt::Display for Offset {
57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58        fmt::Display::fmt(&self.0, f)
59    }
60}
61
62pub trait Statement<'a> {
63    fn into_query(self) -> Query<'a>;
64}
65
66#[derive(Debug)]
67pub enum Dir {
68    Asc,
69    Desc,
70}
71
72impl fmt::Display for Dir {
73    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
74        match *self {
75            Dir::Asc => fmt::Display::fmt("ASC", f),
76            Dir::Desc => fmt::Display::fmt("DESC", f),
77        }
78    }
79}