<commit_before># Quibble\Query
Query builder for Quibble adapters.

## Usage
Create a custom class extending your desired adapter which uses the
`Quibble\Query\Buildable` trait:

```php
<?php

// PHP 5.x
class MyAdapter extends Quibble\Postgresql\Adapter
{
    use Quibble\Query\Buildable;
}

$adapter = new MyAdapter(/* connection params */);

// PHP 7
$adapter = new class(/* connection params */) extends Quibble\Postgresql\Adapter
{
    use Quibble\Query\Buildable;
};

```

To create a Query Builder, call one of the convenience methods the trait adds.
Their only parameter is the base table name you want your query to operate on:

```php
<?php

$query = $adapter->selectFrom('foo');

```

You can now pass the `$query` around, call methods to add stuff like conditions,
options, joins etc. and eventually fetch the result(s). Most calls will return
the object itself so you can chain calls.

You can also directly instantiate one of the `Query` SQL classes, e.g.:

```php
<?php

$query = new Quibble\Query\Select($pdo, $tableName);
```

## Selecting from more than one table
Use the `andFrom` method:

```php
<?php

$query = $adapter->selectFrom('foo')
    ->andFrom('bar');
// SELECT * FROM foo, bar

```

## Joining
Low-level:

```php
<?php

$query->join('bar USING(baz)', 'LEFT');

```

The second parameter is the join-style. If omitted defaults to a straight join.
If you join contains placeholders, add them as subsequent parameters.

> The Query builder does not support, anywhere you can pass bindings for
> placeholders, the use of named parameters. _Always_ use question marks. Named
> parameters are handy when manually constructing a large blob of SQL with many
> of them, but using the query builder you typically only pass one or two
> bindings per method call so it is not needed to support this.

Shorthands:

```php
<?php

$query->leftJoin('bar1 USING(baz1)')
    ->rightJoin('bar2 USING(baz2)')
    ->outerJoin('bar3 USING(baz3)')
    ->fullOuterJoin('bar4 ON baz4 = ?', $baz4);

```

Again, any subsequent parameters are bindings to placeholders.

## Subqueries
If any bindable is itself a `Select` query builder, it becomes a _subquery_ and
the associated bindings are "hoisted" to the parent query.

## Choosing which fields to select
The default is to select `*` so if that's what you want you don't need to
specify anything. To fine-tune, use the `select` method:

```php
<?php

$query->select('foo', 'bar', 'baz AS buzz');

```

`select` takes an arbitrary number of arguments representing the fields you wish
to select in your query. Hence, the above example would translate to:

```sql
SELECT foo, bar, baz AS buzz FROM ...
```

Note that if `select` is called multiple times, all fields are _appended_ to
the query. The only exception if when it detects the fields were in a "pristine"
state (i.e. `*`) in which case it acts as an override. So if mid-construction
you need to reset your fields you can do it like so:

```php
<?php

$query->select('foo', 'bar');
// oops...
$query->select('*')->select('baz');

```

## Adding WHERE clauses
```php
<?php

$query->where('foo = ?', $bar);

```

A WHERE clause is an AND clause by default. To add an OR clause, use `orWhere`:

```php
<?php

$query->orWhere('foo = ?', $bar);

```

WHERE clauses are wrapped in parentheses and appended verbatim. Hence, to nest
calls one would simply pass in multiple clauses in parantheses where applicable:

```php
<?php

$query->where('foo = ? AND (bar = ? OR bar = ?)', $foo, $bar1, $bar2);<commit_msg>add correct decoration<commit_after># Quibble\Query
Query builder for Quibble adapters.

## Usage
Create a custom class extending your desired adapter which uses the
`Quibble\Query\Buildable` trait:

```php
<?php

// PHP 5.x
class MyAdapter extends Quibble\Postgresql\Adapter
{
    use Quibble\Query\Buildable;
}

$adapter = new MyAdapter(/* connection params */);

// PHP 7
$adapter = new class(/* connection params */) extends Quibble\Postgresql\Adapter
{
    use Quibble\Query\Buildable;
};

```

To create a Query Builder, call one of the convenience methods the trait adds.
Their only parameter is the base table name you want your query to operate on:

```php
<?php

$query = $adapter->selectFrom('foo');

```

You can now pass the `$query` around, call methods to add stuff like conditions,
options, joins etc. and eventually fetch the result(s). Most calls will return
the object itself so you can chain calls.

You can also directly instantiate one of the `Query` SQL classes, e.g.:

```php
<?php

$query = new Quibble\Query\Select($pdo, $tableName);
```

## Selecting from more than one table
Use the `andFrom` method:

```php
<?php

$query = $adapter->selectFrom('foo')
    ->andFrom('bar');
// SELECT * FROM foo, bar

```

## Joining
Low-level:

```php
<?php

$query->join('bar USING(baz)', 'LEFT');

```

The second parameter is the join-style. If omitted defaults to a straight join.
If you join contains placeholders, add them as subsequent parameters.

> The Query builder does not support, anywhere you can pass bindings for
> placeholders, the use of named parameters. _Always_ use question marks. Named
> parameters are handy when manually constructing a large blob of SQL with many
> of them, but using the query builder you typically only pass one or two
> bindings per method call so it is not needed to support this.

Shorthands:

```php
<?php

$query->leftJoin('bar1 USING(baz1)')
    ->rightJoin('bar2 USING(baz2)')
    ->outerJoin('bar3 USING(baz3)')
    ->fullOuterJoin('bar4 ON baz4 = ?', $baz4);

```

Again, any subsequent parameters are bindings to placeholders.

## Subqueries
If any bindable is itself a `Select` query builder, it becomes a _subquery_ and
the associated bindings are "hoisted" to the parent query.

## Choosing which fields to select
The default is to select `*` so if that's what you want you don't need to
specify anything. To fine-tune, use the `select` method:

```php
<?php

$query->select('foo', 'bar', 'baz AS buzz');

```

`select` takes an arbitrary number of arguments representing the fields you wish
to select in your query. Hence, the above example would translate to:

```sql
SELECT foo, bar, baz AS buzz FROM ...
```

Note that if `select` is called multiple times, all fields are _appended_ to
the query. The only exception if when it detects the fields were in a "pristine"
state (i.e. `*`) in which case it acts as an override. So if mid-construction
you need to reset your fields you can do it like so:

```php
<?php

$query->select('foo', 'bar');
// oops...
$query->select('*')->select('baz');

```

## Decorating fields
A decorator class is anything that wraps a value and can be `__toString`'d. For
bindings this is done automatically.

To automatically wrap a _selected_ field in a decorator, take a look at the
`quibble/transformer` package.

## Adding WHERE clauses
```php
<?php

$query->where('foo = ?', $bar);

```

A WHERE clause is an AND clause by default. To add an OR clause, use `orWhere`:

```php
<?php

$query->orWhere('foo = ?', $bar);

```

WHERE clauses are wrapped in parentheses and appended verbatim. Hence, to nest
calls one would simply pass in multiple clauses in parantheses where applicable:

```php
<?php

$query->where('foo = ? AND (bar = ? OR bar = ?)', $foo, $bar1, $bar2);
{'fraction_non_alphanumeric': 0.11851851851851852, 'fraction_numerical': 0.0035665294924554186, 'mean_word_length': 4.219040801717967, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4678511', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2184, 'n_words': 988}