| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\QueryBuilder\Concerns; |
| 4 |
|
| 5 |
use Give\Framework\QueryBuilder\Clauses\MetaTable; |
| 6 |
use Give\Framework\QueryBuilder\Clauses\RawSQL; |
| 7 |
use Give\Framework\QueryBuilder\JoinQueryBuilder; |
| 8 |
use Give\Framework\QueryBuilder\QueryBuilder; |
| 9 |
|
| 10 |
/** |
| 11 |
* @since 2.19.0 |
| 12 |
*/ |
| 13 |
trait MetaQuery |
| 14 |
{ |
| 15 |
|
| 16 |
/** |
| 17 |
* @var MetaTable[] |
| 18 |
*/ |
| 19 |
private $metaTablesConfigs = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private $defaultMetaKeyColumn = 'meta_key'; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
private $defaultMetaValueColumn = 'meta_value'; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param string|RawSQL $table |
| 33 |
* @param string $metaKeyColumn |
| 34 |
* @param string $metaValueColumn |
| 35 |
* |
| 36 |
* @return $this |
| 37 |
*/ |
| 38 |
public function configureMetaTable($table, $metaKeyColumn, $metaValueColumn) |
| 39 |
{ |
| 40 |
$this->metaTablesConfigs[] = new MetaTable( |
| 41 |
$table, |
| 42 |
$metaKeyColumn, |
| 43 |
$metaValueColumn |
| 44 |
); |
| 45 |
|
| 46 |
return $this; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param string|RawSQL $table |
| 51 |
* |
| 52 |
* @return MetaTable |
| 53 |
*/ |
| 54 |
protected function getMetaTable($table) |
| 55 |
{ |
| 56 |
$tableName = QueryBuilder::prefixTable($table); |
| 57 |
|
| 58 |
foreach ($this->metaTablesConfigs as $metaTable) { |
| 59 |
if ($metaTable->tableName === $tableName) { |
| 60 |
return $metaTable; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return new MetaTable( |
| 65 |
$table, |
| 66 |
$this->defaultMetaKeyColumn, |
| 67 |
$this->defaultMetaValueColumn |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Select meta columns |
| 73 |
* |
| 74 |
* @since 2.21.1 optimize group concat functionality |
| 75 |
* @since 2.19.6 add group concat functionality |
| 76 |
* @since 2.19.0 |
| 77 |
* |
| 78 |
* @param string|RawSQL $table |
| 79 |
* @param string $foreignKey |
| 80 |
* @param string $primaryKey |
| 81 |
* @param array $columns |
| 82 |
* |
| 83 |
* @return $this |
| 84 |
*/ |
| 85 |
public function attachMeta($table, $foreignKey, $primaryKey, ...$columns) |
| 86 |
{ |
| 87 |
$metaTable = $this->getMetaTable($table); |
| 88 |
|
| 89 |
foreach ($columns as $i => $definition) { |
| 90 |
if (is_array($definition)) { |
| 91 |
list ($column, $columnAlias, $concat) = array_pad($definition, 3, false); |
| 92 |
} else { |
| 93 |
$column = $definition; |
| 94 |
$columnAlias = $concat = false; |
| 95 |
} |
| 96 |
|
| 97 |
// Set dynamic alias |
| 98 |
$tableAlias = sprintf('%s_%s_%s', ($table instanceof RawSQL) ? $table->sql : $table, 'attach_meta', $columnAlias ?: $column); |
| 99 |
|
| 100 |
// Check if we have meta columns that dev wants to group concat |
| 101 |
if ($concat) { |
| 102 |
/** |
| 103 |
* Include foreign key to prevent errors if sql_mode is only_full_group_by |
| 104 |
* |
| 105 |
* @see https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html |
| 106 |
*/ |
| 107 |
$this->groupBy($foreignKey); |
| 108 |
|
| 109 |
// Group concat same key values into faux array |
| 110 |
// @example key => ["value1", "value2"] |
| 111 |
$this->selectRaw( |
| 112 |
"CONCAT('[',GROUP_CONCAT(DISTINCT CONCAT('\"',%1s,'\"')),']') AS %2s", |
| 113 |
$tableAlias . '.' . $metaTable->valueColumnName, |
| 114 |
$columnAlias ?: $column |
| 115 |
); |
| 116 |
} else { |
| 117 |
$this->select(["{$tableAlias}.{$metaTable->valueColumnName}", $columnAlias ?: $column]); |
| 118 |
} |
| 119 |
|
| 120 |
$this->join( |
| 121 |
function (JoinQueryBuilder $builder) use ( |
| 122 |
$table, |
| 123 |
$foreignKey, |
| 124 |
$primaryKey, |
| 125 |
$tableAlias, |
| 126 |
$column, |
| 127 |
$metaTable |
| 128 |
) { |
| 129 |
$builder |
| 130 |
->leftJoin($table, $tableAlias) |
| 131 |
->on($foreignKey, "{$tableAlias}.{$primaryKey}") |
| 132 |
->andOn("{$tableAlias}.{$metaTable->keyColumnName}", $column, true); |
| 133 |
} |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
return $this; |
| 138 |
} |
| 139 |
} |
| 140 |
|