Struct Query

Source
pub struct Query(/* private fields */);
Expand description

A Firestore query.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#structuredquery

§Examples

use firestore_structured_query::{FieldPath, Filter, Query};
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{
    structured_query, value::ValueType, ArrayValue, Cursor, StructuredQuery, Value,
};

let _ = StructuredQuery::from(
    // or Query::collection_group(...)
    Query::collection("collection_id1")
        .select([FieldPath::raw("field1"), FieldPath::raw("field2")])
        .r#where(Filter::and([
            // field filters
            FieldPath::raw("field1").less_than(Value { value_type: Some(ValueType::IntegerValue(1)) })?,
            FieldPath::raw("field2").less_than_or_equal(Value { value_type: Some(ValueType::IntegerValue(2)) })?,
            FieldPath::raw("field3").greater_than(Value { value_type: Some(ValueType::IntegerValue(3)) })?,
            FieldPath::raw("field4").greater_than_or_equal(Value { value_type: Some(ValueType::IntegerValue(4)) })?,
            FieldPath::raw("field5").equal(Value { value_type: Some(ValueType::IntegerValue(5)) })?,
            FieldPath::raw("field6").not_equal(Value { value_type: Some(ValueType::IntegerValue(6)) })?,
            FieldPath::raw("field7").array_contains(Value { value_type: Some(ValueType::IntegerValue(7)) })?,
            FieldPath::raw("field8").r#in(Value { value_type: Some(ValueType::ArrayValue(ArrayValue { values: vec![Value { value_type: Some(ValueType::IntegerValue(8)) }] })) })?,
            FieldPath::raw("field9").array_contains_any(Value { value_type: Some(ValueType::ArrayValue(ArrayValue { values: vec![Value { value_type: Some(ValueType::IntegerValue(9)) }] })) })?,
            FieldPath::raw("field10").not_in(Value { value_type: Some(ValueType::ArrayValue(ArrayValue { values: vec![Value { value_type: Some(ValueType::IntegerValue(10)) }] })) })?,
            // unary filters
            FieldPath::raw("field11").is_nan()?,
            FieldPath::raw("field12").is_not_nan()?,
            FieldPath::raw("field13").is_not_null()?,
            FieldPath::raw("field14").is_null()?,
            // composite filters
            Filter::and([
                FieldPath::raw("f").equal(Value { value_type: Some(ValueType::StringValue("a".to_string())) })?,
                FieldPath::raw("f").equal(Value { value_type: Some(ValueType::StringValue("b".to_string())) })?,
            ]),
            Filter::or([
                FieldPath::raw("f").equal(Value { value_type: Some(ValueType::StringValue("a".to_string())) })?,
                FieldPath::raw("f").equal(Value { value_type: Some(ValueType::StringValue("b".to_string())) })?,
            ]),
        ]))
        .order_by([
            FieldPath::raw("field1").ascending(),
            FieldPath::raw("field2").descending(),
        ])
        // .start_after(...)
        .start_at([
            Value { value_type: Some(ValueType::IntegerValue(1))},
            Value { value_type: Some(ValueType::IntegerValue(2))},
        ])
        // .end_before(...)
        .end_at([
            Value { value_type: Some(ValueType::IntegerValue(1))},
            Value { value_type: Some(ValueType::IntegerValue(2))},
        ])
        .offset(1_i32)
        .limit(2_i32),
);

Implementations§

Source§

impl Query

Source

pub fn collection<S>(collection_id: S) -> Self
where S: Into<String>,

Creates a new Query for a collection.

The query that internally holds a CollectionSelector with all_descendants set to false.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.CollectionSelector

§Examples
use firestore_structured_query::Query;
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{structured_query, StructuredQuery};
let query1 = Query::collection("collection_id1");
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: false,
        }],
        r#where: None,
        order_by: vec![],
        start_at: None,
        end_at: None,
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);
Source

pub fn collection_group<S>(collection_id: S) -> Self
where S: Into<String>,

Creates a new Query for a collection group.

The query that internally holds a CollectionSelector with all_descendants set to true.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.CollectionSelector

§Examples
use firestore_structured_query::Query;
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{structured_query, StructuredQuery};
let query1 = Query::collection_group("collection_id1");
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: None,
        order_by: vec![],
        start_at: None,
        end_at: None,
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);
Source

pub fn end_at<I>(self, values: I) -> Self
where I: IntoIterator<Item = Value>,

Sets the specified value to end_at and returns the Query.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.FIELDS.google.firestore.v1.Cursor.google.firestore.v1.StructuredQuery.end_at https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.Cursor

§Examples
use firestore_structured_query::Query;
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{
    structured_query, value::ValueType, Cursor, StructuredQuery, Value,
};
let query1 = Query::collection_group("collection_id1").end_at([
    Value {
        value_type: Some(ValueType::IntegerValue(1)),
    },
    Value {
        value_type: Some(ValueType::IntegerValue(2)),
    },
]);
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: None,
        order_by: vec![],
        start_at: None,
        end_at: Some(Cursor {
            values: vec![
                Value {
                    value_type: Some(ValueType::IntegerValue(1)),
                },
                Value {
                    value_type: Some(ValueType::IntegerValue(2)),
                },
            ],
            before: false,
        }),
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);
Source

pub fn end_before<I>(self, values: I) -> Self
where I: IntoIterator<Item = Value>,

Sets the specified value to end_at and returns the Query.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.FIELDS.google.firestore.v1.Cursor.google.firestore.v1.StructuredQuery.end_at https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.Cursor

§Examples
use firestore_structured_query::Query;
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{
    structured_query, value::ValueType, Cursor, StructuredQuery, Value,
};
let query1 = Query::collection_group("collection_id1").end_before([
    Value {
        value_type: Some(ValueType::IntegerValue(1)),
    },
    Value {
        value_type: Some(ValueType::IntegerValue(2)),
    },
]);
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: None,
        order_by: vec![],
        start_at: None,
        end_at: Some(Cursor {
            values: vec![
                Value {
                    value_type: Some(ValueType::IntegerValue(1)),
                },
                Value {
                    value_type: Some(ValueType::IntegerValue(2)),
                },
            ],
            before: true,
        }),
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);
Source

pub fn limit(self, limit: i32) -> Self

Sets the specified value to limit and returns the Query.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.FIELDS.google.protobuf.Int32Value.google.firestore.v1.StructuredQuery.limit

§Examples
use firestore_structured_query::Query;
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{structured_query, StructuredQuery};
let query1 = Query::collection_group("collection_id1").limit(1_i32);
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: None,
        order_by: vec![],
        start_at: None,
        end_at: None,
        offset: 0_i32,
        limit: Some(1_i32),
        find_nearest: None,
    }
);
Source

pub fn offset(self, offset: i32) -> Self

Sets the specified value to offset and returns the Query.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.FIELDS.int32.google.firestore.v1.StructuredQuery.offset

§Examples
use firestore_structured_query::Query;
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{structured_query, StructuredQuery};
let query1 = Query::collection_group("collection_id1").offset(1_i32);
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: None,
        order_by: vec![],
        start_at: None,
        end_at: None,
        offset: 1_i32,
        limit: None,
        find_nearest: None,
    }
);
Source

pub fn order_by<I>(self, order_by: I) -> Self
where I: IntoIterator, I::Item: Into<Order>,

Sets the specified value to order_by and returns the Query.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.FIELDS.repeated.google.firestore.v1.StructuredQuery.Order.google.firestore.v1.StructuredQuery.order_by

§Examples
use firestore_structured_query::{FieldPath, Query};
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{structured_query, StructuredQuery};
let query1 = Query::collection_group("collection_id1").order_by([
    FieldPath::raw("field1").ascending(),
    FieldPath::raw("field2").descending(),
]);
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: None,
        order_by: vec![
            structured_query::Order::from(FieldPath::raw("field1").ascending()),
            structured_query::Order::from(FieldPath::raw("field2").descending()),
        ],
        start_at: None,
        end_at: None,
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);
let order_by1 = vec![
    structured_query::Order {
        field: Some(structured_query::FieldReference {
            field_path: "field1".to_string(),
        }),
        direction: structured_query::Direction::Ascending as i32,
    },
    structured_query::Order {
        field: Some(structured_query::FieldReference {
            field_path: "field2".to_string(),
        }),
        direction: structured_query::Direction::Descending as i32,
    },
];
let query2 = Query::collection_group("collection_id1").order_by(order_by1.clone());
assert_eq!(
    StructuredQuery::from(query2),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: None,
        order_by: order_by1,
        start_at: None,
        end_at: None,
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);
Source

pub fn select<I>(self, fields: I) -> Self

Sets the specified value to select and returns the Query.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.FIELDS.google.firestore.v1.StructuredQuery.Projection.google.firestore.v1.StructuredQuery.select

§Examples
use firestore_structured_query::{FieldPath, Query};
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{structured_query, StructuredQuery};
let query1 = Query::collection_group("collection_id1")
    .select([FieldPath::raw("field1"), FieldPath::raw("field2")]);
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: Some(structured_query::Projection {
            fields: vec![
                structured_query::FieldReference {
                    field_path: "field1".to_string(),
                },
                structured_query::FieldReference {
                    field_path: "field2".to_string(),
                },
            ],
        }),
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: None,
        order_by: vec![],
        start_at: None,
        end_at: None,
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);
Source

pub fn start_after<I>(self, values: I) -> Self
where I: IntoIterator<Item = Value>,

Sets the specified value to start_at and returns the Query.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.FIELDS.google.firestore.v1.Cursor.google.firestore.v1.StructuredQuery.start_at

§Examples
use firestore_structured_query::Query;
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{
    structured_query, value::ValueType, Cursor, StructuredQuery, Value,
};
let query1 = Query::collection_group("collection_id1").start_after([
    Value {
        value_type: Some(ValueType::IntegerValue(1)),
    },
    Value {
        value_type: Some(ValueType::IntegerValue(2)),
    },
]);
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: None,
        order_by: vec![],
        start_at: Some(Cursor {
            values: vec![
                Value {
                    value_type: Some(ValueType::IntegerValue(1)),
                },
                Value {
                    value_type: Some(ValueType::IntegerValue(2)),
                },
            ],
            before: false,
        }),
        end_at: None,
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);
Source

pub fn start_at<I>(self, values: I) -> Self
where I: IntoIterator<Item = Value>,

Sets the specified value to start_at and returns the Query.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.FIELDS.google.firestore.v1.Cursor.google.firestore.v1.StructuredQuery.start_at

§Examples
use firestore_structured_query::Query;
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{
    structured_query, value::ValueType, Cursor, StructuredQuery, Value,
};
let query1 = Query::collection_group("collection_id1").start_at([
    Value {
        value_type: Some(ValueType::IntegerValue(1)),
    },
    Value {
        value_type: Some(ValueType::IntegerValue(2)),
    },
]);
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: None,
        order_by: vec![],
        start_at: Some(Cursor {
            values: vec![
                Value {
                    value_type: Some(ValueType::IntegerValue(1)),
                },
                Value {
                    value_type: Some(ValueType::IntegerValue(2)),
                },
            ],
            before: true,
        }),
        end_at: None,
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);
Source

pub fn where<F>(self, filter: F) -> Self
where F: Into<Filter>,

Sets the specified value to where and returns the Query.

https://firebase.google.com/docs/firestore/reference/rpc/google.firestore.v1#google.firestore.v1.StructuredQuery.FIELDS.google.firestore.v1.StructuredQuery.Filter.google.firestore.v1.StructuredQuery.where

§Examples
use firestore_structured_query::{FieldPath, Query};
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{structured_query, StructuredQuery};
let query1 =
    Query::collection_group("collection_id1").r#where(FieldPath::raw("field1").is_nan()?);
assert_eq!(
    StructuredQuery::from(query1),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: Some(structured_query::Filter {
            filter_type: Some(structured_query::filter::FilterType::UnaryFilter(
                structured_query::UnaryFilter {
                    op: structured_query::unary_filter::Operator::IsNan as i32,
                    operand_type: Some(structured_query::unary_filter::OperandType::Field(
                        structured_query::FieldReference {
                            field_path: "field1".to_string(),
                        }
                    ))
                }
            ))
        }),
        order_by: vec![],
        start_at: None,
        end_at: None,
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);
let filter1 = structured_query::Filter {
    filter_type: Some(structured_query::filter::FilterType::UnaryFilter(
        structured_query::UnaryFilter {
            op: structured_query::unary_filter::Operator::IsNan as i32,
            operand_type: Some(structured_query::unary_filter::OperandType::Field(
                structured_query::FieldReference {
                    field_path: "field1".to_string(),
                },
            )),
        },
    )),
};
let query2 = Query::collection_group("collection_id1").r#where(filter1.clone());
assert_eq!(
    StructuredQuery::from(query2),
    StructuredQuery {
        select: None,
        from: vec![structured_query::CollectionSelector {
            collection_id: "collection_id1".to_string(),
            all_descendants: true,
        }],
        r#where: Some(filter1),
        order_by: vec![],
        start_at: None,
        end_at: None,
        offset: 0_i32,
        limit: None,
        find_nearest: None,
    }
);

Trait Implementations§

Source§

impl Clone for Query

Source§

fn clone(&self) -> Query

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Query

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Query> for StructuredQuery

Source§

fn from(query: Query) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Query

Source§

fn eq(&self, other: &Query) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Query

Auto Trait Implementations§

§

impl Freeze for Query

§

impl RefUnwindSafe for Query

§

impl Send for Query

§

impl Sync for Query

§

impl Unpin for Query

§

impl UnwindSafe for Query

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more