sql/
lib.rs

1//! A constructor of SQL statements.
2//!
3//! ## Example
4//!
5//! ```
6//! use sql::prelude::*;
7//!
8//! // CREATE TABLE `users` (`id` INTEGER NOT NULL, `name` TEXT, `photo` BLOB)
9//! println!("{}", create_table("users").column("id".integer().not_null())
10//!                                     .column("name".string())
11//!                                     .column("photo".binary())
12//!                                     .compile().unwrap());
13//!
14//! // DELETE FROM `users`
15//! println!("{}", delete_from("users").compile().unwrap());
16//!
17//! // INSERT INTO `users` (`id`, `name`) VALUES (?, ?), (?, ?)
18//! println!("{}", insert_into("users").columns(&["id", "name"]).batch(2)
19//!                                    .compile().unwrap());
20//!
21//! // SELECT * FROM `users` WHERE `name` LIKE 'A%'
22//! println!("{}", select_from("users").so_that(column("name").like("A%"))
23//!                                    .compile().unwrap());
24//!
25//! // SELECT * FROM `users` ORDER BY `name` DESC
26//! println!("{}", select_from("users").order_by(column("name").descend())
27//!                                    .compile().unwrap());
28//!
29//! // SELECT `name`, `photo` FROM `users` LIMIT 1
30//! println!("{}", select_from("users").columns(&["name", "photo"]).limit(1)
31//!                                    .compile().unwrap());
32//! ```
33
34use std::{error, fmt, result};
35
36/// An error.
37pub struct Error(String);
38
39/// A result.
40pub type Result<T> = result::Result<T, Error>;
41
42/// A data type.
43#[derive(Clone, Copy, Debug, Eq, PartialEq)]
44pub enum Type {
45    /// The binary type.
46    Binary,
47    /// The floating-point type.
48    Float,
49    /// The integer type.
50    Integer,
51    /// The string type.
52    String,
53}
54
55/// An object that can be assigend a type.
56pub trait Typable where Self: Sized {
57    /// The type produced after setting a type.
58    type Output;
59
60    /// Set the type.
61    fn kind(self, value: Type) -> Self::Output;
62
63    /// Set the type to `Binary`.
64    #[inline]
65    fn binary(self) -> Self::Output {
66        self.kind(Type::Binary)
67    }
68
69    /// Set the type to `Float`.
70    #[inline]
71    fn float(self) -> Self::Output {
72        self.kind(Type::Float)
73    }
74
75    /// Set the type to `Integer`.
76    #[inline]
77    fn integer(self) -> Self::Output {
78        self.kind(Type::Integer)
79    }
80
81    /// Set the type to `String`.
82    #[inline]
83    fn string(self) -> Self::Output {
84        self.kind(Type::String)
85    }
86}
87
88impl fmt::Debug for Error {
89    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
90        self.0.fmt(formatter)
91    }
92}
93
94impl fmt::Display for Error {
95    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
96        self.0.fmt(formatter)
97    }
98}
99
100impl error::Error for Error {
101    fn description(&self) -> &str {
102        &self.0
103    }
104}
105
106macro_rules! raise(
107    ($message:expr) => (
108        return Err(::Error($message.to_string()));
109    );
110);
111
112pub mod grammar;
113pub mod language;
114pub mod prelude;