Skip to content

Commit

Permalink
Remove 'parts lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Sep 21, 2024
1 parent adadfe2 commit ba046ac
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl Client {
}

/// Starts a new SELECT/DDL query.
pub fn query<'a>(&self, query: &'a str) -> query::Query<'a> {
pub fn query(&self, query: &str) -> query::Query {
query::Query::new(self, query)
}

Expand All @@ -308,7 +308,7 @@ impl Client {
/// The `query` can be either the table name or a SELECT query.
/// In the second case, a new LV table is created.
#[cfg(feature = "watch")]
pub fn watch<'a>(&self, query: &'a str) -> watch::Watch<'a> {
pub fn watch(&self, query: &str) -> watch::Watch {
watch::Watch::new(self, query)
}

Expand Down
8 changes: 4 additions & 4 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ const MAX_QUERY_LEN_TO_USE_GET: usize = 8192;

#[must_use]
#[derive(Clone)]
pub struct Query<'a> {
pub struct Query {
client: Client,
sql: SqlBuilder<'a>,
sql: SqlBuilder,
}

impl<'parts> Query<'parts> {
pub(crate) fn new(client: &Client, template: &'parts str) -> Self {
impl Query {
pub(crate) fn new(client: &Client, template: &str) -> Self {
Self {
client: client.clone(),
sql: SqlBuilder::new(template),
Expand Down
30 changes: 14 additions & 16 deletions src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,29 @@ pub(crate) mod escape;
mod ser;

#[derive(Clone)]
pub(crate) enum SqlBuilder<'a> {
InProgress(Vec<Part<'a>>),
pub(crate) enum SqlBuilder {
InProgress(Vec<Part>),
Failed(String),
}

#[derive(Clone)]
pub(crate) enum Part<'a> {
pub(crate) enum Part {
Arg,
Fields,
Str(&'a str),
String(String),
Text(String),
}

impl<'parts> SqlBuilder<'parts> {
pub(crate) fn new(template: &'parts str) -> Self {
impl SqlBuilder {
pub(crate) fn new(template: &str) -> Self {
let mut parts = Vec::new();
let mut rest = template;
while let Some(idx) = rest.find('?') {
if rest[idx + 1..].starts_with('?') {
parts.push(Part::Str(&rest[..idx + 1]));
parts.push(Part::Text(rest[..idx + 1].to_string()));
rest = &rest[idx + 2..];
continue;
} else if idx != 0 {
parts.push(Part::Str(&rest[..idx]));
parts.push(Part::Text(rest[..idx].to_string()));
}

rest = &rest[idx + 1..];
Expand All @@ -48,7 +47,7 @@ impl<'parts> SqlBuilder<'parts> {
}

if !rest.is_empty() {
parts.push(Part::Str(rest));
parts.push(Part::Text(rest.to_string()));
}

SqlBuilder::InProgress(parts)
Expand All @@ -66,7 +65,7 @@ impl<'parts> SqlBuilder<'parts> {
return self.error(format_args!("invalid argument: {err}"));
}

*part = Part::String(s);
*part = Part::Text(s);
} else {
self.error("unexpected bind(), all arguments are already bound");
}
Expand All @@ -79,19 +78,19 @@ impl<'parts> SqlBuilder<'parts> {

if let Some(fields) = row::join_column_names::<T>() {
for part in parts.iter_mut().filter(|p| matches!(p, Part::Fields)) {
*part = Part::String(fields.clone());
*part = Part::Text(fields.clone());
}
} else if parts.iter().any(|p| matches!(p, Part::Fields)) {
self.error("argument ?fields cannot be used with non-struct row types");
}
}

pub(crate) fn append(&mut self, part: &'static str) {
pub(crate) fn append(&mut self, part: &str) {
let Self::InProgress(parts) = self else {
return;
};

parts.push(Part::Str(part));
parts.push(Part::Text(part.to_string()));
}

pub(crate) fn finish(mut self) -> Result<String> {
Expand All @@ -100,8 +99,7 @@ impl<'parts> SqlBuilder<'parts> {
if let Self::InProgress(parts) = &self {
for part in parts {
match part {
Part::Str(text) => sql.push_str(text),
Part::String(text) => sql.push_str(&text[..]),
Part::Text(text) => sql.push_str(text),
Part::Arg => {
self.error("unbound query argument");
break;
Expand Down
14 changes: 7 additions & 7 deletions src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use crate::{
};

#[must_use]
pub struct Watch<'parts, V = Rows> {
pub struct Watch<V = Rows> {
client: Client,
sql: SqlBuilder<'parts>,
sql: SqlBuilder,
refresh: Option<Duration>,
limit: Option<usize>,
_kind: V,
Expand All @@ -23,7 +23,7 @@ pub struct Watch<'parts, V = Rows> {
pub struct Rows;
pub struct Events;

impl<'parts, V> Watch<'parts, V> {
impl<V> Watch<V> {
/// See [`Query::bind()`] for details.
///
/// [`Query::bind()`]: crate::query::Query::bind
Expand Down Expand Up @@ -71,8 +71,8 @@ impl<'parts, V> Watch<'parts, V> {
}
}

impl<'a> Watch<'a, Rows> {
pub(crate) fn new(client: &Client, template: &'a str) -> Self {
impl Watch<Rows> {
pub(crate) fn new(client: &Client, template: &str) -> Self {
let client = client
.clone()
// TODO: check again.
Expand All @@ -91,7 +91,7 @@ impl<'a> Watch<'a, Rows> {
}
}

pub fn only_events(self) -> Watch<'a, Events> {
pub fn only_events(self) -> Watch<Events> {
Watch {
client: self.client,
sql: self.sql,
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<'a> Watch<'a, Rows> {
}
}

impl<'a> Watch<'a, Events> {
impl Watch<Events> {
pub fn fetch(self) -> Result<EventCursor> {
Ok(EventCursor(self.cursor(true)?))
}
Expand Down

0 comments on commit ba046ac

Please sign in to comment.