method getSelect documentation in bbn\Db\Languages\Sql

Generates a string starting with SELECT ... FROM with corresponding parameters

function(array $cfg) { $res = ''; if (\is_array($cfg['tables']) && !empty($cfg['tables'])) { $res = 'SELECT '; if (!empty($cfg['count'])) { if (!empty($cfg['group_by'])) { $indexes = []; $idxs = []; foreach ($cfg['group_by'] as $g) { // Alias if (isset($cfg['fields'][$g])) { $g = $cfg['fields'][$g]; } if ((!empty($cfg['available_fields'][$g]) && $t = $cfg['available_fields'][$g]) && ($cfn = $this->colFullName($g, $t)) ) { $indexes[] = $cfn; //$idxs[] = $this->colSimpleName($g, true); // Changed by Mirko $idxs[] = $this->colSimpleName($cfg['aliases'][$g] ?? $g, true); } else { $indexes[] = $g; $idxs[] = $cfg['aliases'][$g] ?? $g; } } if (!empty($cfg['having'])) { if (count($indexes) === count($cfg['group_by'])) { $res .= 'COUNT(*) FROM ( SELECT '; $tmp = []; if ($extracted_fields = $this->extractFields($cfg, $cfg['having']['conditions'])) { //die(var_dump($extracted_fields)); foreach ($extracted_fields as $ef) { if (!in_array($ef, $indexes)) { if (!empty($cfg['fields'][$ef])) { $tmp[$ef] = $cfg['fields'][$ef]; } else { $tmp[] = $ef; } } } } $cfg['fields'] = $indexes; foreach ($tmp as $k => $v) { if (is_string($k)) { $cfg['fields'][$k] = $v; } else { $cfg['fields'][] = $v; } } } else { $res .= 'COUNT(*) FROM ( SELECT '; } } else { if (count($indexes) === count($cfg['group_by'])) { $res .= 'COUNT(*) FROM ( SELECT '; //$cfg['fields'] = $indexes; // Changed by Mirko $cfg['fields'] = array_combine($idxs, $indexes); } else { $res .= 'COUNT(*) FROM ( SELECT '; } } } else { $res .= 'COUNT(*)'; $cfg['fields'] = []; } } if (!empty($cfg['fields'])) { $fields_to_put = []; // Checking the selected fields foreach ($cfg['fields'] as $alias => $f) { $is_distinct = false; $f = trim($f); $bits = explode(' ', $f); if ((count($bits) > 1) && (strtolower($bits[0]) === 'distinct')) { $is_distinct = true; array_shift($bits); $f = implode(' ', $bits); } // Adding the alias in $fields if (strpos($f, '(')) { $fields_to_put[] = ($is_distinct ? 'DISTINCT ' : '') . $f . (\is_string($alias) ? ' AS ' . $this->escape($alias) : ''); } elseif (isset($cfg['available_fields']) && array_key_exists($f, $cfg['available_fields'])) { $idx = $cfg['available_fields'][$f]; if ($idx && isset($cfg['tables_full'][$idx])) { $idx = $cfg['tables_full'][$idx]; } $csn = $this->colSimpleName($f); $is_uid = false; //die(var_dump($idx, $f, $tables[$idx])); if (($idx !== false) && isset($cfg['models'][$idx]['fields'][$csn])) { $column = $cfg['models'][$idx]['fields'][$csn]; if (($column['type'] === 'binary') && ($column['maxlength'] === 16)) { $is_uid = true; if (!\is_string($alias)) { $alias = $csn; } } } //$res['fields'][$alias] = $this->cfn($f, $fields[$f]); if ($is_uid) { if (method_exists($this, 'getHexStatement')) { $st = 'LOWER(' . $this->getHexStatement($this->colFullName($csn, $cfg['available_fields'][$f], true)) . ')'; } else { $st = 'LOWER(HEX(' . $this->colFullName($csn, $cfg['available_fields'][$f], true) . '))'; } } // For JSON fields elseif ($cfg['available_fields'][$f] === false) { $st = $f; } else { $st = $this->colFullName($csn, $cfg['available_fields'][$f], true); } if (\is_string($alias)) { $st .= ' AS ' . $this->escape($alias); } $fields_to_put[] = ($is_distinct ? 'DISTINCT ' : '') . $st; } elseif (isset($cfg['available_fields'][$f]) && ($cfg['available_fields'][$f] === false)) { $this->error("Error! The column '$f' exists on several tables in '" . implode(', ', $cfg['tables'])); } else { $this->error("Error! The column '$f' doesn't exist in '" . implode(', ', $cfg['tables'])); } } $res .= implode(', ', $fields_to_put); } $res .= PHP_EOL; $tables_to_put = []; foreach ($cfg['tables'] as $alias => $tfn) { $st = $this->tableFullName($tfn, true); if (is_string($alias) && $alias !== $tfn) { $st .= ' AS ' . $this->escape($alias); } $tables_to_put[] = $st; } $res .= 'FROM ' . implode(', ', $tables_to_put) . PHP_EOL; return $res; } return $res; }

Generates a string starting with SELECT ... FROM with corresponding parameters BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way

This website uses cookies to ensure you get the best experience on our website.