Skip to content

Instantly share code, notes, and snippets.

@sergant210
Created June 30, 2021 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergant210/13367f63f03ec8e2866a801eaafe3dca to your computer and use it in GitHub Desktop.
Save sergant210/13367f63f03ec8e2866a801eaafe3dca to your computer and use it in GitHub Desktop.
Refactored pdoFetch class
<?php
class_exists('pdoFetch') or require __DIR__ . '/pdofetch.class.php';
class pdoFetchOpt extends pdoFetch
{
/**
* Main method for query processing and fetching rows
* It can return string with SQL query, array or raw rows or processed HTML chunks
*
* @return array|bool|string
*/
public function run()
{
$this->makeQuery();
$this->addTVFilters();
$this->addTVs();
$this->addJoins();
$this->addGrouping();
$this->addSelects();
$this->addFrom(); // Add FROM Data
$this->addWhere();
$this->addSort();
$this->prepareQuery();
$output = '';
if (strtolower($this->config['return']) === 'sql') {
$this->addTime('Returning raw sql query');
$output = $this->query->toSQL();
} else {
$this->modx->exec('SET SQL_BIG_SELECTS = 1');
$this->addTime('SQL prepared <small>"' . $this->query->toSQL() . '"</small>');
$tstart = microtime(true);
if ($this->query->stmt->execute()) {
$this->modx->queryTime += microtime(true) - $tstart;
$this->modx->executedQueries++;
$this->addTime('SQL executed', microtime(true) - $tstart);
$this->setTotal();
$rows = $this->query->stmt->fetchAll(PDO::FETCH_ASSOC);
$this->addTime('Rows fetched');
$rows = $this->checkPermissions($rows);
$this->count = count($rows);
if (strtolower($this->config['return']) === 'ids') {
$ids = array();
foreach ($rows as $row) {
$ids[] = $row[$this->pk];
}
$output = implode(',', $ids);
} elseif (strtolower($this->config['return']) === 'data') {
$rows = $this->prepareRows($rows);
$this->addTime('Returning raw data');
$output = &$rows;
} elseif (strtolower($this->config['return']) === 'json') {
$rows = $this->prepareRows($rows);
$this->addTime('Returning raw data as JSON string');
$output = json_encode($rows);
} elseif (strtolower($this->config['return']) === 'serialize') {
$rows = $this->prepareRows($rows);
$this->addTime('Returning raw data as serialized string');
$output = serialize($rows);
} else {
$rows = $this->prepareRows($rows);
$time = microtime(true);
$output = array();
foreach ($rows as $row) {
if (!empty($this->config['additionalPlaceholders'])) {
$row = array_merge($this->config['additionalPlaceholders'], $row);
}
$row['idx'] = $this->idx++;
// Add placeholder [[+link]] if specified
if (!empty($this->config['useWeblinkUrl'])) {
if (!isset($row['context_key'])) {
$row['context_key'] = '';
}
if (isset($row['class_key']) && ($row['class_key'] === 'modWebLink')) {
$row['link'] = isset($row['content']) && is_numeric(trim($row['content'], '[]~ '))
? $this->makeUrl((int)trim($row['content'], '[]~ '), $row)
: (isset($row['content']) ? $row['content'] : '');
} else {
$row['link'] = $this->makeUrl($row['id'], $row);
}
} elseif (!isset($row['link'])) {
$row['link'] = '';
}
$tpl = $this->defineChunk($row);
if (empty($tpl)) {
$output[] = '<pre>' . $this->getChunk('', $row) . '</pre>';
} else {
$output[] = $this->getChunk($tpl, $row, $this->config['fastMode']);
}
}
$this->addTime('Returning processed chunks', microtime(true) - $time);
if (!empty($this->config['toSeparatePlaceholders'])) {
$this->modx->setPlaceholders($output, $this->config['toSeparatePlaceholders']);
$output = '';
} else {
$output = implode($this->config['outputSeparator'], $output);
}
}
} else {
$this->modx->log(modX::LOG_LEVEL_INFO, '[pdoTools] ' . $this->query->toSQL());
$errors = $this->query->stmt->errorInfo();
$this->modx->log(modX::LOG_LEVEL_ERROR, '[pdoTools] Error ' . $errors[0] . ': ' . $errors[2]);
$this->addTime('Could not process query, error #' . $errors[1] . ': ' . $errors[2]);
}
}
return $output;
}
/**
* Adds from table conditions
*/
public function addFrom()
{
$time = microtime(true);
if (!empty($this->config['from'])) {
$this->query->query['from']['tables'] = [];
$this->query->query['from']['tables'][] = ['table' => $this->config['from'], 'alias' => 'modResource'];
$this->addTime('Added FROM data: <b>' . $this->config['from'] . '</b>', microtime(true) - $time);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment