utopia-query 0.1.0

Queries as data, compiled to SQL (MySQL, MariaDB, PostgreSQL, SQLite, ClickHouse) and MongoDB commands; schema builders; a SQL tokenizer, parser, serializer and statement classifier.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! [`Value`]: the PHP values queries carry, and the objects among them.

use php_std::json::{self, Flags};
use php_std::{ArrayKey, Extension, Str};

use crate::enums::NullsPosition;
use crate::query::Query;

/// A value a query or a statement carries: a PHP value (bytes, integers
/// distinct from floats, ordered arrays with integer and string keys,
/// `stdClass`), where objects are [`Object`]s.
pub type Value = php_std::Value<Object>;

/// A PHP array of [`Value`]s.
pub type Array = php_std::Array<Object>;

/// An object among a query's values.
#[derive(Debug, Clone, PartialEq)]
pub enum Object {
    /// A nested query (the children of `and`, `or`, `having`, joins).
    ///
    /// PHP: `Utopia\Query\Query`.
    Query(Box<Query>),
    /// The `NULLS FIRST` / `NULLS LAST` of an order query.
    ///
    /// PHP: `Utopia\Query\NullsPosition`.
    Nulls(NullsPosition),
    /// A row read through array access, such as a document used as a cursor:
    /// its class and its fields.
    ///
    /// PHP: an object implementing `ArrayAccess` (`ArrayObject`, `Utopia\Database\Document`).
    Row {
        /// The PHP class.
        class: String,
        /// The fields.
        fields: Array,
    },
}

impl Extension for Object {
    /// The PHP class.
    fn class(&self) -> &str {
        match self {
            Object::Query(_) => "Utopia\\Query\\Query",
            Object::Nulls(_) => NullsPosition::PHP_CLASS,
            Object::Row { class, .. } => class,
        }
    }

    /// What `json_encode` writes for the object: a query has no public
    /// properties, a row its fields.
    fn to_array(&self) -> Array {
        match self {
            Object::Row { fields, .. } => fields.clone(),
            _ => Array::default(),
        }
    }
}

impl From<Query> for Value {
    /// A nested query.
    fn from(query: Query) -> Self {
        Value::Ext(Object::Query(Box::new(query)))
    }
}

impl From<NullsPosition> for Value {
    /// The enum case.
    fn from(nulls: NullsPosition) -> Self {
        Value::Ext(Object::Nulls(nulls))
    }
}

/// A string value.
pub fn string(s: impl AsRef<[u8]>) -> Value {
    Value::Str(Str::copy_from(s.as_ref()))
}

/// A list of values.
pub fn list<V: Into<Value>>(items: impl IntoIterator<Item = V>) -> Array {
    Array::from_list(items.into_iter().map(Into::into).collect())
}

/// The value as PHP's `(string)` cast prints it (lossy for bytes that are not
/// UTF-8, which PHP would keep).
pub(crate) fn to_php_string(value: &Value) -> String {
    match value.to_php_string() {
        Ok(s) => String::from_utf8_lossy(s.as_bytes()).into_owned(),
        Err(_) => String::new(),
    }
}

/// `json_encode($value)` with `$flags`: enums encode as their value, as PHP's
/// backed enums do.
pub(crate) fn json_encode(value: &Value, flags: Flags) -> Result<String, json::Error> {
    json::encode(&enums_as_values(value), flags, json::DEFAULT_DEPTH)
}

/// Backed enum cases replaced by their values (what `json_encode` writes for them).
fn enums_as_values(value: &Value) -> Value {
    match value {
        Value::Ext(Object::Nulls(n)) => Value::from(n.as_str()),
        Value::Array(a) => Value::Array(map_array(a)),
        Value::Object(a) => Value::Object(map_array(a)),
        other => other.clone(),
    }
}

fn map_array(a: &Array) -> Array {
    let mut out = Array::with_capacity(a.len());
    for (k, v) in a.iter() {
        out.set(k.to_owned(), enums_as_values(v));
    }
    out
}

/// `$a === $b` where enum cases are identical to themselves (PHP's enum
/// cases are singletons) and other objects to nothing.
pub(crate) fn identical(a: &Value, b: &Value) -> bool {
    match (a, b) {
        (Value::Ext(Object::Nulls(x)), Value::Ext(Object::Nulls(y))) => x == y,
        (Value::Array(x), Value::Array(y)) => {
            x.len() == y.len() && x.iter().zip(y.iter()).all(|((ka, va), (kb, vb))| ka == kb && identical(va, vb))
        }
        _ => a.strict_eq(b),
    }
}

/// Something usable as one PHP value: integers, floats, booleans, strings,
/// arrays, queries, `None` for `null`.
pub trait IntoValue {
    /// The value.
    fn into_value(self) -> Value;
}

impl IntoValue for Value {
    fn into_value(self) -> Value {
        self
    }
}

impl IntoValue for &Value {
    fn into_value(self) -> Value {
        self.clone()
    }
}

macro_rules! into_value_int {
    ($($t:ty),*) => {$(
        impl IntoValue for $t {
            fn into_value(self) -> Value {
                Value::Int(self as i64)
            }
        }
    )*};
}

into_value_int!(i8, i16, i32, i64, u8, u16, u32, isize);

impl IntoValue for f64 {
    fn into_value(self) -> Value {
        Value::Float(self)
    }
}

impl IntoValue for f32 {
    fn into_value(self) -> Value {
        Value::Float(self as f64)
    }
}

impl IntoValue for bool {
    fn into_value(self) -> Value {
        Value::Bool(self)
    }
}

impl IntoValue for &str {
    fn into_value(self) -> Value {
        string(self)
    }
}

impl IntoValue for String {
    fn into_value(self) -> Value {
        Value::Str(Str::from(self))
    }
}

impl IntoValue for &String {
    fn into_value(self) -> Value {
        string(self)
    }
}

impl IntoValue for Str {
    fn into_value(self) -> Value {
        Value::Str(self)
    }
}

impl IntoValue for Array {
    fn into_value(self) -> Value {
        Value::Array(self)
    }
}

impl IntoValue for Query {
    fn into_value(self) -> Value {
        Value::from(self)
    }
}

impl IntoValue for NullsPosition {
    fn into_value(self) -> Value {
        Value::from(self)
    }
}

impl<T: IntoValue> IntoValue for Option<T> {
    fn into_value(self) -> Value {
        self.map(IntoValue::into_value).unwrap_or(Value::Null)
    }
}

/// Something usable as a PHP array of values: a Rust array, a `Vec` or a
/// slice of [`IntoValue`]s (a list), or an [`Array`] (keys kept).
pub trait IntoValues {
    /// The array.
    fn into_values(self) -> Array;
}

impl IntoValues for Array {
    fn into_values(self) -> Array {
        self
    }
}

impl<T: IntoValue, const N: usize> IntoValues for [T; N] {
    fn into_values(self) -> Array {
        Array::from_list(self.into_iter().map(IntoValue::into_value).collect())
    }
}

impl<T: IntoValue> IntoValues for Vec<T> {
    fn into_values(self) -> Array {
        Array::from_list(self.into_iter().map(IntoValue::into_value).collect())
    }
}

impl<T: IntoValue + Clone> IntoValues for &[T] {
    fn into_values(self) -> Array {
        Array::from_list(self.iter().cloned().map(IntoValue::into_value).collect())
    }
}

/// The value as PHP's string concatenation (`'x' . $value`) writes it: arrays
/// as `"Array"` (PHP warns), objects fail as PHP's `Error` does.
pub(crate) fn concat(value: &Value) -> Result<std::borrow::Cow<'_, str>, crate::Error> {
    use std::borrow::Cow;
    match value {
        Value::Str(s) => Ok(String::from_utf8_lossy(s.as_bytes())),
        Value::Object(_) | Value::Ext(_) => Err(crate::Error::Engine(format!(
            "Object of class {} could not be converted to string",
            value.debug_type()
        ))),
        other => Ok(Cow::Owned(to_php_string(other))),
    }
}

/// The key `$array[$value] = ...` writes: numeric strings as integers,
/// `null` as `""`, booleans as `0` / `1`, floats truncated (PHP deprecates
/// it); arrays and objects fail with PHP's `TypeError`.
pub(crate) fn array_key(value: &Value) -> Result<ArrayKey, crate::Error> {
    Ok(match value {
        Value::Str(s) => ArrayKey::normalize(s.clone()),
        Value::Int(i) => ArrayKey::Int(*i),
        Value::Float(f) => ArrayKey::Int(php_std::format::dval_to_lval(*f)),
        Value::Bool(b) => ArrayKey::Int(i64::from(*b)),
        Value::Null => ArrayKey::Str(Str::EMPTY),
        other => {
            return Err(crate::Error::Type(format!("Cannot access offset of type {} on array", value_name(other))));
        }
    })
}

/// How PHP names a value's type in a `TypeError`: `true` / `false` for
/// booleans, the class for objects, else the type (`null`, `int`, `array`).
///
/// PHP: `zend_zval_value_name()`.
pub(crate) fn value_name(value: &Value) -> &str {
    match value {
        Value::Bool(true) => "true",
        Value::Bool(false) => "false",
        other => other.debug_type(),
    }
}

/// [`concat`] as bytes (PHP strings are bytes).
pub(crate) fn concat_bytes(value: &Value) -> Result<std::borrow::Cow<'_, [u8]>, crate::Error> {
    use std::borrow::Cow;
    match value {
        Value::Str(s) => Ok(Cow::Borrowed(s.as_bytes())),
        other => Ok(Cow::Owned(concat(other)?.into_owned().into_bytes())),
    }
}

/// Where the library's PHP sources live in the image Appwrite runs: PHP's
/// type errors name the file and line of the call.
const PHP_SOURCES: &str = "/usr/src/code/packages/query/src/";

/// A typed PHP parameter that a query value, of whatever type the client
/// sent, is passed to: converts the value as PHP's coercive typing mode does
/// ([`Param::string`]) or checks
/// it ([`Param::array`]), failing with PHP's `TypeError`.
///
/// A library function's `string` parameter rejects `null`, arrays and
/// objects; an internal function's (`str_ends_with()`) takes `null` as `""`
/// (PHP deprecates it). Integers, floats and booleans become their
/// `(string)` form for both.
///
/// PHP: the engine's parameter type check (`zend_verify_arg_type`).
#[derive(Debug, Clone, Copy)]
pub(crate) struct Param<'f> {
    function: &'f str,
    position: u8,
    name: &'static str,
    callee: Callee,
}

#[derive(Debug, Clone, Copy)]
enum Callee {
    /// A library method, called from `file` (relative to the sources) at `line`.
    Method { file: &'static str, line: u32 },
    /// A library closure, called by an internal function (`array_map`): PHP
    /// names no call site.
    Closure,
    /// An internal PHP function.
    Internal,
}

impl<'f> Param<'f> {
    /// Parameter `position` (`$name`) of the library method `function`
    /// (`Utopia\Query\Builder::resolveAndWrap`), called from `file` at `line`.
    pub(crate) const fn method(
        function: &'f str,
        position: u8,
        name: &'static str,
        file: &'static str,
        line: u32,
    ) -> Self {
        Param { function, position, name, callee: Callee::Method { file, line } }
    }

    /// Parameter `position` (`$name`) of a library closure, named as PHP names
    /// it (`Utopia\Query\Builder::{closure:Utopia\Query\Builder::compileSelect():1362}`).
    pub(crate) const fn closure(function: &'f str, position: u8, name: &'static str) -> Self {
        Param { function, position, name, callee: Callee::Closure }
    }

    /// Parameter `position` (`$name`) of the internal PHP function `function`.
    pub(crate) const fn internal(function: &'f str, position: u8, name: &'static str) -> Self {
        Param { function, position, name, callee: Callee::Internal }
    }

    /// The string PHP passes for `value` to a `string` parameter, or its `TypeError`.
    pub(crate) fn string<'a>(&self, value: &'a Value) -> Result<std::borrow::Cow<'a, str>, crate::Error> {
        use std::borrow::Cow;
        Ok(match value {
            Value::Str(s) => String::from_utf8_lossy(s.as_bytes()),
            Value::Null if matches!(self.callee, Callee::Internal) => Cow::Borrowed(""),
            Value::Int(_) | Value::Float(_) | Value::Bool(_) => Cow::Owned(to_php_string(value)),
            other => return Err(self.type_error("string", other)),
        })
    }

    /// [`Param::string`] as bytes (PHP strings are bytes).
    pub(crate) fn bytes<'a>(&self, value: &'a Value) -> Result<std::borrow::Cow<'a, [u8]>, crate::Error> {
        use std::borrow::Cow;
        match value {
            Value::Str(s) => Ok(Cow::Borrowed(s.as_bytes())),
            other => Ok(Cow::Owned(self.string(other)?.into_owned().into_bytes())),
        }
    }

    /// The array `value` is, for an `array` parameter, or PHP's `TypeError`.
    pub(crate) fn array<'a>(&self, value: &'a Value) -> Result<&'a Array, crate::Error> {
        match value {
            Value::Array(a) => Ok(a),
            other => Err(self.type_error("array", other)),
        }
    }

    /// PHP's `TypeError` for passing `value` to a parameter of type `expected`.
    pub(crate) fn type_error(&self, expected: &str, value: &Value) -> crate::Error {
        let mut message = format!(
            "{}(): Argument #{} (${}) must be of type {expected}, {} given",
            self.function,
            self.position,
            self.name,
            value_name(value)
        );
        if let Callee::Method { file, line } = self.callee {
            message.push_str(&format!(", called in {PHP_SOURCES}{file} on line {line}"));
        }
        crate::Error::Type(message)
    }
}