Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDEA(dbal): A set of interfaces for constructing queries #7562

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions engine/classes/Elgg/Database/AccessCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ function getAccessArray($user_guid = 0, $site_guid = 0, $flush = false) {
$access_array[] = ACCESS_LOGGED_IN;

// Get ACL memberships
// $collections = $db->from('access_collection_membership', $am)
// ->leftJoin('access_collections', $ag)->on($ag->id->equals($am->access_collection_id)
// ->where($am->user_guid->equals($user_guid)->and(
// $ag->site_guid->equals($site_guid)->or($ag->site_guid->equals(0))))
// ->select($am->access_collection_id)->pluck('access_collection_id')

$query = "SELECT am.access_collection_id"
. " FROM {$this->CONFIG->dbprefix}access_collection_membership am"
. " LEFT JOIN {$this->CONFIG->dbprefix}access_collections ag ON ag.id = am.access_collection_id"
Expand All @@ -148,6 +154,9 @@ function getAccessArray($user_guid = 0, $site_guid = 0, $flush = false) {
}

// Get ACLs owned.
// $db->from('access_collections', $ag)
// ->where($ag->owner_guid->equals($user_guid)->and($ag->site_guid->equals($site_guid)->or($ag->site_guid->equals(0))))
// ->select($ag->id)->pluck('id');
$query = "SELECT ag.id FROM {$this->CONFIG->dbprefix}access_collections ag ";
$query .= "WHERE ag.owner_guid = $user_guid AND (ag.site_guid = $site_guid OR ag.site_guid = 0)";

Expand Down Expand Up @@ -393,6 +402,9 @@ function getWriteAccessArray($user_guid = 0, $site_guid = 0, $flush = false) {
ACCESS_PUBLIC => _elgg_services()->translator->translate("PUBLIC")
);

// $db->from('access_collections', $ag)
// ->where($ag->owner_guid->equals($user_guid)->and($ag->site_guid->equals($site_guid)->or($ag->site_guid->equals(0))))
// ->select($ag->{'*'});
$query = "SELECT ag.* FROM {$this->CONFIG->dbprefix}access_collections ag ";
$query .= " WHERE (ag.site_guid = $site_guid OR ag.site_guid = 0)";
$query .= " AND (ag.owner_guid = $user_guid)";
Expand Down Expand Up @@ -485,8 +497,13 @@ function create($name, $owner_guid = 0, $site_guid = 0) {
if (($site_guid == 0) && (isset($this->CONFIG->site_guid))) {
$site_guid = $this->CONFIG->site_guid;
}

// $this->sql->insertInto('access_collections', [
// 'name' => $name,
// 'owner_guid' => $owner_guid,
// 'site_guid' => $site_guid,
// ]);
$name = sanitise_string($name);

$q = "INSERT INTO {$this->CONFIG->dbprefix}access_collections
SET name = '{$name}',
owner_guid = {$owner_guid},
Expand Down Expand Up @@ -565,11 +582,18 @@ function delete($collection_id) {
return false;
}

// $this->aclMembershipSqlTable->fromSelf($c)
// ->where($c->access_collection_id->equals($collection_id))
// ->delete();

// Deleting membership doesn't affect result of deleting ACL.
$q = "DELETE FROM {$this->CONFIG->dbprefix}access_collection_membership
WHERE access_collection_id = {$collection_id}";
_elgg_services()->db->deleteData($q);

// $this->aclsSqlTable->fromSelf($c)
// ->where($c->access_collection_id->equals($collection_id))
// ->delete();
$q = "DELETE FROM {$this->CONFIG->dbprefix}access_collections
WHERE id = {$collection_id}";
$result = _elgg_services()->db->deleteData($q);
Expand All @@ -590,9 +614,10 @@ function delete($collection_id) {
* @return object|false
*/
function get($collection_id) {

// $this->aclsSqlTable->fromSelf($acls)
// ->where($acls->id->equals($collection_id))
// ->select('*')
$collection_id = (int) $collection_id;

$query = "SELECT * FROM {$this->CONFIG->dbprefix}access_collections WHERE id = {$collection_id}";
$get_collection = _elgg_services()->db->getDataRow($query);

Expand Down Expand Up @@ -632,6 +657,10 @@ function addUser($user_guid, $collection_id) {
return false;
}

// $this->sql->insertInto('access_collection_membership', [
// 'access_collection_id' => $collection_id,
// 'user_guid' => $user_guid,
// ]);
// if someone tries to insert the same data twice, we do a no-op on duplicate key
$q = "INSERT INTO {$this->CONFIG->dbprefix}access_collection_membership
SET access_collection_id = $collection_id, user_guid = $user_guid
Expand Down Expand Up @@ -673,6 +702,10 @@ function removeUser($user_guid, $collection_id) {
return false;
}

// $sqlDb->from('access_collection_membership', $aclm)
// ->where($aclm->access_collection_id->equals($collection_id)
// ->and($aclm->user_guid->equals($user_guid));
// ->delete($aclm)
$q = "DELETE FROM {$this->CONFIG->dbprefix}access_collection_membership
WHERE access_collection_id = {$collection_id}
AND user_guid = {$user_guid}";
Expand All @@ -697,6 +730,10 @@ function getUserCollections($owner_guid, $site_guid = 0) {
$site_guid = $this->CONFIG->site_guid;
}

// $db->from('access_collections', $acls)
// ->where($acls->owner_guid->equals($owner_guid)
// ->and($acls->site_guid->equals($site_guid))
// ->orderBy($acls->name->asc())
$query = "SELECT * FROM {$this->CONFIG->dbprefix}access_collections
WHERE owner_guid = {$owner_guid}
AND site_guid = {$site_guid}
Expand All @@ -719,12 +756,17 @@ function getMembers($collection, $idonly = false) {

$collection = (int)$collection;

// $query = $db->from('access_collection_membership', $m)
// ->join('entities', $e)->on($e->guid->equals($m->user_guid))
// ->where($m->access_collection_id->equals($collection))
if (!$idonly) {
// return $query->select($e->{'*'})->map('entity_row_to_elggstar');
$query = "SELECT e.* FROM {$this->CONFIG->dbprefix}access_collection_membership m"
. " JOIN {$this->CONFIG->dbprefix}entities e ON e.guid = m.user_guid"
. " WHERE m.access_collection_id = {$collection}";
$collection_members = _elgg_services()->db->getData($query, "entity_row_to_elggstar");
} else {
// return $query->select($e->guid)->pluck('guid');
$query = "SELECT e.guid FROM {$this->CONFIG->dbprefix}access_collection_membership m"
. " JOIN {$this->CONFIG->dbprefix}entities e ON e.guid = m.user_guid"
. " WHERE m.access_collection_id = {$collection}";
Expand Down
9 changes: 9 additions & 0 deletions engine/classes/Elgg/Database/Annotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ function create($entity_guid, $name, $value, $value_type = '', $owner_guid = 0,
$entity = get_entity($entity_guid);

if (_elgg_services()->events->trigger('annotate', $entity->type, $entity)) {
// $this->sql->insertInto('annotations', [
// 'entity_guid' => $entity_guid,
// 'name_id' => $name_id,
// 'value_id' => $value_id,
// 'value_type' => $value_type,
// 'owner_guid' => $owner_guid,
// 'time_created' => $time,
// 'access_id' => $access_id,
// ])
$result = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}annotations
(entity_guid, name_id, value_id, value_type, owner_guid, time_created, access_id) VALUES
($entity_guid, $name_id, $value_id, '$value_type', $owner_guid, $time, $access_id)");
Expand Down
8 changes: 8 additions & 0 deletions engine/classes/Elgg/Database/ConfigTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ function set($name, $value, $site_guid = 0) {

$escaped_name = sanitize_string($name);
$escaped_value = sanitize_string(serialize($value));
// $db->insertInto('config')->onDuplicateKeyUpdate([
// 'value' => $value,
// ]), [
// 'name' => $name,
// 'value' => $value,
// 'site_guid' => $site_guid,
// ''
// ])
$result = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}config
SET name = '$escaped_name', value = '$escaped_value', site_guid = $site_guid
ON DUPLICATE KEY UPDATE value = '$escaped_value'");
Expand Down
8 changes: 8 additions & 0 deletions engine/classes/Elgg/Database/Datalist.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ function get($name) {

return $this->cache->get($name, function() use ($name) {
$escaped_name = $this->db->sanitizeString($name);

// $this->table->fromSelf($d)->where($d->column('name')->equals($name))->select('*');
$result = $this->db->getDataRow("SELECT * FROM {$this->table} WHERE name = '$escaped_name'");
return $result ? $result->value : null;
});
Expand Down Expand Up @@ -102,6 +104,11 @@ function set($name, $value) {

$escaped_name = $this->db->sanitizeString($name);
$escaped_value = $this->db->sanitizeString($value);

// $this->table->insert([
// 'name' => $name,
// 'value' => $value,
// ], ['value' => $value]);
$success = $this->db->insertData("INSERT INTO {$this->table}"
. " SET name = '$escaped_name', value = '$escaped_value'"
. " ON DUPLICATE KEY UPDATE value = '$escaped_value'");
Expand All @@ -122,6 +129,7 @@ function set($name, $value) {
* @access private
*/
function loadAll() {
// $this->table->fromSelf($d)->select('*');
$result = $this->db->getData("SELECT * FROM {$this->table}");
$map = array();
if (is_array($result)) {
Expand Down
9 changes: 9 additions & 0 deletions engine/classes/Elgg/Database/SubtypeTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ function retrieveFromCache($type, $subtype) {
function populateCache() {
global $SUBTYPE_CACHE;

// $this->table->fromSelf($s)->select('*');
$results = _elgg_services()->db->getData("SELECT * FROM {$this->CONFIG->dbprefix}entity_subtypes");

$SUBTYPE_CACHE = array();
Expand Down Expand Up @@ -242,6 +243,7 @@ function add($type, $subtype, $class = "") {
$subtype = sanitise_string($subtype);
$class = sanitise_string($class);

// $this->table->insert(['type' => $type, 'subtype' => $subtype, 'class' => $class])
$id = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}entity_subtypes"
. " (type, subtype, class) VALUES ('$type', '$subtype', '$class')");

Expand Down Expand Up @@ -273,6 +275,10 @@ function remove($type, $subtype) {
$type = sanitise_string($type);
$subtype = sanitise_string($subtype);

// $this->table->fromSelf($s)
// ->where($s->type->equals($type)->and($s->subtype->equals($subtype)))
// ->limit(1)
// ->delete();
$success = _elgg_services()->db->deleteData("DELETE FROM {$this->CONFIG->dbprefix}entity_subtypes"
. " WHERE type = '$type' AND subtype = '$subtype'");

Expand Down Expand Up @@ -311,6 +317,9 @@ function update($type, $subtype, $class = '') {
$subtype = sanitise_string($subtype);
$class = sanitise_string($class);

// $this->table->fromSelf($s)
// ->where($s->id->equals($id))
// ->update(['type' => $type, 'subtype' => $subtype, 'class' => $class]);
$success = _elgg_services()->db->updateData("UPDATE {$this->CONFIG->dbprefix}entity_subtypes
SET type = '$type', subtype = '$subtype', class = '$class'
WHERE id = $id
Expand Down
7 changes: 6 additions & 1 deletion engine/classes/Elgg/PersistentLoginService.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ public function getUserFromHash($hash) {
*/
protected function storeHash(\ElggUser $user, $hash) {
$time = time();
$hash = $this->db->sanitizeString($hash);

// $db->insert($this->table, [
// 'code' => $hash,
// 'guid' => $user->guid,
// 'time' => $time,
// ]);
$hash = $this->db->sanitizeString($hash);
$query = "
INSERT INTO {$this->table} (code, guid, timestamp)
VALUES ('$hash', {$user->guid}, $time)
Expand Down
32 changes: 32 additions & 0 deletions engine/classes/Elgg/Sql/ColumnReference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace Elgg\Sql;

/**
* API IN FLUX. DO NOT USE DIRECTLY.
*
* @package Elgg.Core
* @subpackage Sql
* @since 1.11
*
* @access private
*/
class ColumnReference {
/** @return OrderByExpression */
public function asc() {
return new OrderByExpression($this, OrderByDirection::ASC);
}

/** @return OrderByExpression */
public function desc() {
return new OrderByExpression($this, OrderByDirection::DESC);
}

/**
* @param mixed $value A value expression
*
* @return ComparisonExpression
*/
public function equals($value) {
return new ComparisonExpression($this, ComparisonExpression::EQUALS, $value);
}
}
61 changes: 61 additions & 0 deletions engine/classes/Elgg/Sql/ComparisonExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace Elgg\Sql;

/**
* API IN FLUX. DO NOT USE DIRECTLY.
*
* @package Elgg.Core
* @subpackage Sql
* @since 1.11
*
* @access private
*/
class Comparison implements WhereExpression {
private $left;
private $type;
private $right;

const EQUALS = 'equals';

/**
* @param mixed $left
* @param string $type
* @param mixed $right
*/
public function __construct($left, $type, $right) {
$this->left = $left;
$this->type = $type;
$this->right = $right;
}

/**
* @return mixed
*/
public function getLeft() {
return $this->left;
}

/**
* @return mixed
*/
public function getRight() {
return $this->right;
}

/**
* @return string
*/
public function getType() {
return $this->type;
}

/** @inheritDoc */
public function and(WhereExpression $expr) {
return new DnfExpression($this)->and($expr);
}

/** @inheritDoc */
public function or(WhereExpression $expr) {
return new DnfExpression($this)->or($expr);
}
}
24 changes: 24 additions & 0 deletions engine/classes/Elgg/Sql/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Elgg\Sql;

/**
* API IN FLUX. DO NOT USE DIRECTLY.
*
* @package Elgg.Core
* @subpackage Sql
* @since 1.11
*
* @access private
*/
interface Database {
/**
* Insert the values into the given table.
*
* Column names are trusted
*
*
* @param string $tableName The unprefixed table name to insert values into.
* @param array $values columnName => value map. Will auto-escape raw input.
*/
public function insertInto($tableName, array $values);
}
Loading