compact_sql_traits 0.0.1

Traits set for code generated by `compact_sql` macro
Documentation
//! This crate provides a set of traits used for code generated by the [`compact_sql`][url] crate.
//!
//! Please read the full docs of the mentioned crate with examples there.
//!
//! [url]: https://crates.io/crates/compact_sql

pub use tokio_postgres::types::{
	BorrowToSql,
	Type,
};

pub type NamesList = &'static [&'static str];
pub type ReqTypeChecks = &'static [Option<fn(&Type) -> bool>];
pub type RespTypeChecks = &'static [fn(&Type) -> bool];



/// Trait for code generated by the `compact_sql::PgResultRow` derive macro.
pub trait PgResultRow {
	const COLUMN_NAMES: NamesList;
	const COL_RS_TYPES: NamesList;
	const TYPE_CHECKS: RespTypeChecks;

	fn from_row(row: tokio_postgres::Row) -> Result<Self, tokio_postgres::Error>
	where
		Self: Sized;
}


/// Trait for schema definition/modification queries.
/// ```ignore
/// pg_sql! {
///     impl QueryId::SqlShortName {
///         CREATE SCHEMA IF NOT EXISTS public
///     }
/// }
/// ```
pub trait QueryDDL {
	type SqlIdType;

	const SQL_ID: Self::SqlIdType;
	const SQL_TEXT: &'static str;
}


/// Trait for `COPY` queries.
///
/// It is a special case and Postgres' server/client behavior.
/// ```ignore
/// pg_sql! {
///     impl QueryId::SqlShortName {
///         COPY pg_catalog.pg_namespace TO stdout
///     }
/// }
/// ```
pub trait QueryCopy {
	type SqlIdType;

	const SQL_ID: Self::SqlIdType;
	const SQL_TEXT: &'static str;
}


/// Trait for queries with `?` type prefix: `?RetType`.
/// ```ignore
/// pg_sql! {
///     impl ... for ?RetType {
///         //
///     }
/// }
/// ```
pub trait QueryMaybeOne {
	type ResultRow: PgResultRow;
	type SqlIdType;

	const SQL_ID: Self::SqlIdType;
	const SQL_TEXT: &'static str;

	const PARAM_NAMES: NamesList;
	const TYPE_CHECKS: ReqTypeChecks;

	fn params(&self) -> impl ExactSizeIterator<Item = impl BorrowToSql>;
}


/// Trait for queries with `=` type prefix: `=RetType`.
/// ```ignore
/// pg_sql! {
///     impl ... for =RetType {
///         //
///     }
/// }
/// ```
pub trait QueryExactOne {
	type ResultRow: PgResultRow;
	type SqlIdType;

	const SQL_ID: Self::SqlIdType;
	const SQL_TEXT: &'static str;

	const PARAM_NAMES: NamesList;
	const TYPE_CHECKS: ReqTypeChecks;

	fn params(&self) -> impl ExactSizeIterator<Item = impl BorrowToSql>;
}


/// Trait for queries with `*` type prefix: `*RetType`.
/// ```ignore
/// pg_sql! {
///     impl ... for *RetType {
///         //
///     }
/// }
/// ```
pub trait QueryMany {
	type ResultRow: PgResultRow;
	type SqlIdType;

	const SQL_ID: Self::SqlIdType;
	const SQL_TEXT: &'static str;

	const PARAM_NAMES: NamesList;
	const TYPE_CHECKS: ReqTypeChecks;

	fn params(&self) -> impl ExactSizeIterator<Item = impl BorrowToSql>;
}


/// Trait for queries with `#` type prefix: `#RetType`.
/// ```ignore
/// pg_sql! {
///     impl ... for #RetType {
///         //
///     }
/// }
/// ```
pub trait QueryStream {
	type ResultRow: PgResultRow;
	type SqlIdType;

	const SQL_ID: Self::SqlIdType;
	const SQL_TEXT: &'static str;

	const PARAM_NAMES: NamesList;
	const TYPE_CHECKS: ReqTypeChecks;

	fn params(&self) -> impl ExactSizeIterator<Item = impl BorrowToSql>;
}


/// Trait for queries with `!` instead of type (ignore result rows).
/// ```ignore
/// pg_sql! {
///     impl ... for ! {
///         //
///     }
/// }
/// ```
pub trait QueryIgnore {
	type SqlIdType;

	const SQL_ID: Self::SqlIdType;
	const SQL_TEXT: &'static str;

	const PARAM_NAMES: NamesList;
	const TYPE_CHECKS: ReqTypeChecks;

	fn params(&self) -> impl ExactSizeIterator<Item = impl BorrowToSql>;
}

/// Trait for queries with data modification without the "RETURNING" clause.
/// ```ignore
/// pg_sql! {
///     impl QueryId::SqlShortName {
///         INSERT INTO tablename DEFAULT VALUES
///     }
/// }
/// ```
pub trait QueryDML {
	type SqlIdType;

	const SQL_ID: Self::SqlIdType;
	const SQL_TEXT: &'static str;

	const PARAM_NAMES: NamesList;
	const TYPE_CHECKS: ReqTypeChecks;

	fn params(&self) -> impl ExactSizeIterator<Item = impl BorrowToSql>;
}