use crate::prelude::*;
use crate::sql::{ColumnRef, TableRef};
use crate::{OwnedParam, SQL, SQLChunk, SQLParam, ToSQL, Token};
use smallvec::SmallVec;
#[derive(Debug, Clone)]
pub enum OwnedSQLChunk<V: SQLParam> {
Token(Token),
Ident(Box<str>),
Raw(Box<str>),
Number(usize),
Param(OwnedParam<V>),
Table(TableRef),
Column(ColumnRef),
}
impl<V: SQLParam> OwnedSQLChunk<V> {
#[inline]
#[must_use]
pub const fn token(t: Token) -> Self {
Self::Token(t)
}
#[inline]
#[must_use]
pub const fn table(table: TableRef) -> Self {
Self::Table(table)
}
#[inline]
#[must_use]
pub const fn column(column: ColumnRef) -> Self {
Self::Column(column)
}
#[inline]
pub fn ident(name: impl Into<Box<str>>) -> Self {
Self::Ident(name.into())
}
#[inline]
pub fn raw(text: impl Into<Box<str>>) -> Self {
Self::Raw(text.into())
}
#[inline]
pub const fn param(value: OwnedParam<V>) -> Self {
Self::Param(value)
}
}
impl<'a, V: SQLParam> From<SQLChunk<'a, V>> for OwnedSQLChunk<V> {
fn from(value: SQLChunk<'a, V>) -> Self {
match value {
SQLChunk::Token(token) => Self::Token(token),
SQLChunk::Ident(cow) => Self::Ident(cow.into_owned().into_boxed_str()),
SQLChunk::Raw(cow) => Self::Raw(cow.into_owned().into_boxed_str()),
SQLChunk::Number(value) => Self::Number(value),
SQLChunk::Param(param) => Self::Param(param.into()),
SQLChunk::Table(t) => Self::Table(t),
SQLChunk::Column(c) => Self::Column(c),
}
}
}
impl<V: SQLParam> From<OwnedSQLChunk<V>> for SQLChunk<'static, V> {
fn from(value: OwnedSQLChunk<V>) -> Self {
match value {
OwnedSQLChunk::Token(token) => SQLChunk::Token(token),
OwnedSQLChunk::Ident(s) => SQLChunk::Ident(Cow::Owned(String::from(s))),
OwnedSQLChunk::Raw(s) => SQLChunk::Raw(Cow::Owned(String::from(s))),
OwnedSQLChunk::Number(value) => SQLChunk::Number(value),
OwnedSQLChunk::Param(param) => SQLChunk::Param(param.into()),
OwnedSQLChunk::Table(t) => SQLChunk::Table(t),
OwnedSQLChunk::Column(c) => SQLChunk::Column(c),
}
}
}
#[derive(Debug, Clone)]
pub struct OwnedSQL<V: SQLParam> {
pub chunks: SmallVec<[OwnedSQLChunk<V>; 8]>,
}
impl<V: SQLParam> Default for OwnedSQL<V> {
fn default() -> Self {
Self {
chunks: SmallVec::new(),
}
}
}
impl<'a, V: SQLParam> From<SQL<'a, V>> for OwnedSQL<V> {
fn from(value: SQL<'a, V>) -> Self {
Self {
chunks: value.chunks.into_iter().map(Into::into).collect(),
}
}
}
impl<V: SQLParam> OwnedSQL<V> {
#[inline]
#[must_use]
pub const fn empty() -> Self {
Self {
chunks: SmallVec::new_const(),
}
}
#[inline]
#[must_use]
pub fn with_capacity_chunks(capacity: usize) -> Self {
Self {
chunks: SmallVec::with_capacity(capacity),
}
}
pub fn to_sql(&self) -> SQL<'static, V> {
SQL {
chunks: self.chunks.iter().cloned().map(Into::into).collect(),
}
}
pub fn into_sql(self) -> SQL<'static, V> {
SQL {
chunks: self.chunks.into_iter().map(Into::into).collect(),
}
}
}
impl<V: SQLParam> ToSQL<'static, V> for OwnedSQL<V> {
fn to_sql(&self) -> SQL<'static, V> {
Self::to_sql(self)
}
fn into_sql(self) -> SQL<'static, V> {
self.into_sql()
}
}