method processCfg documentation in bbn\Db\Languages\Sql

function(array $args, bool $force = false) { // Avoid confusion when while (isset($args[0]) && \is_array($args[0])) { $args = $args[0]; } if (!empty($args['bbn_db_processed'])) { return $args; } if (empty($args['bbn_db_treated'])) { $args = $this->_treat_arguments($args); } if (isset($args['hash'])) { if (isset($this->cfgs[$args['hash']])) { return array_merge( $this->cfgs[$args['hash']], [ 'values' => $args['values'] ?? [], 'where' => $args['where'] ?? [], 'filters' => $args['filters'] ?? [] ] ); } $tables_full = []; $res = array_merge( $args, [ 'tables' => [], 'values_desc' => [], 'bbn_db_processed' => true, 'available_fields' => [], 'generate_id' => false ] ); $models = []; foreach ($args['tables'] as $key => $tab) { if (empty($tab)) { $this->log(\debug_backtrace()); throw new \Exception("$key is not defined"); } $tfn = $this->tableFullName($tab); // 2 tables in the same statement can't have the same idx $idx = \is_string($key) ? $key : $tfn; // Error if they do if (isset($tables_full[$idx])) { $this->error('You cannot use twice the same table with the same alias'.PHP_EOL.X::getDump($args['tables']), false); return null; } $tables_full[$idx] = $tfn; $res['tables'][$idx] = $tfn; if (!isset($models[$tfn]) && ($model = $this->modelize($tfn))) { $models[$tfn] = $model; } } if ((\count($res['tables']) === 1) && ($tfn = array_values($res['tables'])[0]) && isset($models[$tfn]['keys']['PRIMARY']) && (\count($models[$tfn]['keys']['PRIMARY']['columns']) === 1) && ($res['primary'] = $models[$tfn]['keys']['PRIMARY']['columns'][0]) ) { $p = $models[$tfn]['fields'][$res['primary']]; $res['auto_increment'] = isset($p['extra']) && ($p['extra'] === 'auto_increment'); $res['primary_length'] = $p['maxlength'] ?? null; $res['primary_type'] = $p['type']; if (($res['kind'] === 'INSERT') && !$res['auto_increment'] && !\in_array($this->colSimpleName($res['primary']), $res['fields'], true) ) { $res['generate_id'] = true; $res['fields'][] = $res['primary']; } } foreach ($args['join'] as $key => $join){ if (!empty($join['table']) && !empty($join['on'])) { $tfn = $this->tableFullName($join['table']); if (!isset($models[$tfn]) && ($model = $this->modelize($tfn))) { $models[$tfn] = $model; } $idx = $join['alias'] ?? $tfn; $tables_full[$idx] = $tfn; } else{ $this->error('Error! The join array must have on and table defined'.PHP_EOL.X::getDump($join), false); } } foreach ($tables_full as $idx => $tfn){ foreach ($models[$tfn]['fields'] as $col => $cfg){ $res['available_fields'][$this->colFullName($col, $idx)] = $idx; $csn = $this->colSimpleName($col); if (!isset($res['available_fields'][$csn])) { /* $res['available_fields'][$csn] = false; } else{ */ $res['available_fields'][$csn] = $idx; } } } foreach ($res['fields'] as $idx => &$col){ if (strpos($col, '(') || strpos($col, '-') || strpos($col, "+") || strpos($col, '*') || strpos($col, "/") /* strpos($col, '->"$.') || strpos($col, "->'$.") || strpos($col, '->>"$.') || strpos($col, "->>'$.") || */ // string as value || preg_match('/^[\\\'\"]{1}[^\\\'\"]*[\\\'\"]{1}$/', $col) ) { $res['available_fields'][$col] = false; } if (\is_string($idx)) { if (!isset($res['available_fields'][$col])) { //$this->log($res); //$this->log(json_encode($res['available_fields'], JSON_PRETTY_PRINT)); $this->error("Impossible to find the column $col", false); return null; } $res['available_fields'][$idx] = $res['available_fields'][$col]; } } // From here the available fields are defined if (!empty($res['filters'])) { $this->arrangeConditions($res['filters'], $res); } unset($col); $res['models'] = $models; $res['tables_full'] = $tables_full; if (($res['kind'] === 'SELECT') && empty($res['fields'])) { foreach (array_keys($res['available_fields']) as $f){ if ($this->isColFullName($f)) { $res['fields'][] = $f; } } } if (in_array($res['kind'], ['INSERT', 'UPDATE'])) { $res = $this->removeVirtual($res); foreach ($res['fields'] as $i => $name) { $desc = [ 'name' => $name ]; if (isset($res['models'], $res['available_fields'][$name])) { $t = $res['available_fields'][$name]; if (isset($tables_full[$t]) && ($model = $res['models'][$tables_full[$t]]['fields']) && ($fname = $this->colSimpleName($name)) && !empty($model[$fname]['type']) ) { $desc['type'] = $model[$fname]['type']; $desc['maxlength'] = $model[$fname]['maxlength'] ?? null; } } if (isset($res['values'][$i]) && is_array($res['values'][$i])) { if ((count($res['values'][$i]) !== 2) || !$res['values'][$i][1]) { X::log([$res['tables'], $res['values']], 'arrays_in_db_write'); throw new Exception(X::_("Using an array for insert/update value is allowed only for expressions with a 2 value array, the second value being the expression")); } $desc['type'] = 'exp'; } $res['values_desc'][] = $desc; } } foreach ($res['join'] as $r){ $this->getValuesDesc($r['on'], $res, $res['values_desc']); } $this->getValuesDesc($res['filters'], $res, $res['values_desc']); $this->getValuesDesc($res['having'], $res, $res['values_desc']); switch ($res['kind']){ case 'SELECT': if ($res['select_st'] = $this->getSelect($res)) { $res['sql'] = $res['select_st']; } break; case 'INSERT': if ($res['insert_st'] = $this->getInsert($res)) { $res['sql'] = $res['insert_st']; } //var_dump($res); break; case 'UPDATE': if ($res['update_st'] = $this->getUpdate($res)) { $res['sql'] = $res['update_st']; } break; case 'DELETE': if ($res['delete_st'] = $this->getDelete($res)) { $res['sql'] = $res['delete_st']; } break; } $res['join_st'] = $this->getJoin($res); $res['where_st'] = $this->getWhere($res); $res['group_st'] = $this->getGroupBy($res); $res['having_st'] = $this->getHaving($res); if (empty($res['count']) && (count($res['fields']) === 1) && (self::isAggregateFunction(reset($res['fields']))) ) { $res['order_st'] = ''; $res['limit_st'] = ''; } else { $res['order_st'] = $res['count'] ? '' : $this->getOrder($res); $res['limit_st'] = $res['count'] ? '' : $this->getLimit($res); } if (!empty($res['sql'])) { $res['sql'] .= $res['join_st'].$res['where_st'].$res['group_st']; if ($res['count'] && $res['group_by']) { $res['sql'] .= ') AS t '.PHP_EOL; } $res['sql'] .= $res['having_st'].$res['order_st'].$res['limit_st']; $res['statement_hash'] = $this->makeHash($res['sql']); $this->cfgs[$res['hash']] = $res; } return $res; } $this->error('Impossible to process the config (no hash)'.PHP_EOL.print_r($args, true), false); return null; }

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.