Skip to content

Commit

Permalink
Merge pull request #17 from nox7/1.4.2
Browse files Browse the repository at this point in the history
1.4.2
  • Loading branch information
nox7 authored Sep 15, 2021
2 parents 8fcd4b0 + 0af8377 commit 85dd601
Showing 2 changed files with 88 additions and 36 deletions.
90 changes: 54 additions & 36 deletions src/ORM/Abyss.php
Original file line number Diff line number Diff line change
@@ -193,6 +193,59 @@ public function fetchInstanceByModelPrimaryKey(MySQLModelInterface $model, mixed
return null;
}

/**
* Builds a WHERE clause from a ColumnQuery
* @return array{string, string}
*/
public function buildWhereClause(MySQLModelInterface $model, ColumnQuery $columnQuery): array
{
$whereClause = "WHERE ";
$preparedBindDataTypes = "";
$boundValues = [];

/** @var array $clause */
foreach ($columnQuery->whereClauses as $clause) {
$clauseType = $clause['clauseType'];
if ($clauseType === "conditionGroup") {
$groupPosition = $clause['conditionGroupPosition'];
if ($groupPosition === "start") {
$whereClause .= "(";
} elseif ($groupPosition === "end") {
$whereClause .= ")";
}
} elseif ($clauseType === "joinCondition") {
$whereClause .= sprintf(" %s ", $clause['clauseJoinWord']);
} elseif ($clauseType === "where") {
$columnName = $clause['column'];
$condition = trim(strtolower($clause['condition']));
$value = $clause['value'];
if ($condition !== "is" && $condition !== "is not") {

// Find the data type flag for this column name
/** @var ColumnDefinition $columnDefinition */
foreach ($model->getColumns() as $columnDefinition) {
if ($columnDefinition->name === $columnName) {
$preparedBindDataTypes .= $columnDefinition->dataType->mySQLBoundParameterType;
break;
}
}

$boundValues[] = $value;
$whereClause .= sprintf("`%s` %s ?", $columnName, $condition);
} else {
// IS or IS NOT null checks
$whereClause .= sprintf("`%s` %s %s", $columnName, $condition, $value);
}
}
}

return [
$whereClause,
$preparedBindDataTypes,
$boundValues,
];
}

/**
* Returns class instances that match the keyValueColumns pairs.
* Identified by a primary key.
@@ -214,42 +267,7 @@ public function fetchInstances(
$preparedBindDataTypes = "";
$boundValues = [];
if ($columnQuery !== null) {
$whereClause = "WHERE ";
/** @var array $clause */
foreach ($columnQuery->whereClauses as $clause) {
$clauseType = $clause['clauseType'];
if ($clauseType === "conditionGroup") {
$groupPosition = $clause['conditionGroupPosition'];
if ($groupPosition === "start") {
$whereClause .= "(";
} elseif ($groupPosition === "end") {
$whereClause .= ")";
}
} elseif ($clauseType === "joinCondition") {
$whereClause .= sprintf(" %s ", $clause['clauseJoinWord']);
} elseif ($clauseType === "where") {
$columnName = $clause['column'];
$condition = trim(strtolower($clause['condition']));
$value = $clause['value'];
if ($condition !== "is" && $condition !== "is not") {

// Find the data type flag for this column name
/** @var ColumnDefinition $columnDefinition */
foreach ($model->getColumns() as $columnDefinition) {
if ($columnDefinition->name === $columnName) {
$preparedBindDataTypes .= $columnDefinition->dataType->mySQLBoundParameterType;
break;
}
}

$boundValues[] = $value;
$whereClause .= sprintf("`%s` %s ?", $columnName, $condition);
} else {
// IS or IS NOT null checks
$whereClause .= sprintf("`%s` %s %s", $columnName, $condition, $value);
}
}
}
list($whereClause, $preparedBindDataTypes, $boundValues) = $this->buildWhereClause($model, $columnQuery);
}

// Build the ORDER BY clause
34 changes: 34 additions & 0 deletions src/ORM/ModelClass.php
Original file line number Diff line number Diff line change
@@ -38,6 +38,40 @@ public static function query(
);
}

/**
* Utilizes the MySQL COUNT() function to quickly
* fetch the number of results returned on this instance
* and provided $columnQuery
*/
public static function count(
ColumnQuery $columnQuery = null,
): int {
$abyss = new Abyss();
$model = static::getModel();

$whereClause = "";
$preparedStatementBindFlags = "";
$boundValues = [];
if ($columnQuery !== null){
list($whereClause,$preparedStatementBindFlags, $boundValues) = $abyss->buildWhereClause($model, $columnQuery);
}

$query = sprintf(
"SELECT COUNT(*) AS totalCount FROM `%s` %s",
$model->getName(),
$whereClause,
);
$statement = $abyss->getConnection()->prepare($query);
if ($columnQuery !== null) {
$statement->bind_param($preparedStatementBindFlags, $boundValues);
}
$statement->execute();
$result = $statement->get_result();

$row = $result->fetch_assoc();
return (int) $row['totalCount'];
}

/**
* Runs a large-scale UPDATE query to save all of the
* ModelClass instances by their primary key

0 comments on commit 85dd601

Please sign in to comment.