From f809e76743a6ccab2badf69633bb6132c1358f2e Mon Sep 17 00:00:00 2001 From: ewinslow Date: Wed, 15 Sep 2010 19:31:39 +0000 Subject: Refs #2220: Pulled remaining classes out of lib files. Core classes now autoloaded via __autoload(). git-svn-id: http://code.elgg.org/elgg/trunk@6941 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/classes/APIException.php | 10 + engine/classes/AccessControlQueryComponent.php | 40 ++++ engine/classes/CallException.php | 10 + engine/classes/ClassException.php | 10 + engine/classes/ClassNotFoundException.php | 10 + engine/classes/ConfigurationException.php | 10 + engine/classes/DataFormatException.php | 10 + engine/classes/DatabaseException.php | 10 + engine/classes/DeleteQueryTypeQueryComponent.php | 14 ++ engine/classes/IOException.php | 10 + engine/classes/InsertQueryTypeQueryComponent.php | 14 ++ engine/classes/InstallationException.php | 10 + engine/classes/InvalidClassException.php | 10 + engine/classes/InvalidParameterException.php | 10 + engine/classes/JoinQueryComponent.php | 33 +++ engine/classes/LimitOffsetQueryComponent.php | 26 +++ engine/classes/NotImplementedException.php | 11 + engine/classes/NotificationException.php | 6 + engine/classes/OrderQueryComponent.php | 23 ++ engine/classes/PluginException.php | 10 + engine/classes/Query.php | 286 +++++++++++++++++++++++ engine/classes/QueryComponent.php | 42 ++++ engine/classes/QueryTypeQueryComponent.php | 14 ++ engine/classes/RegistrationException.php | 10 + engine/classes/SecurityException.php | 10 + engine/classes/SelectFieldQueryComponent.php | 28 +++ engine/classes/SelectQueryTypeQueryComponent.php | 14 ++ engine/classes/SetQueryComponent.php | 33 +++ engine/classes/SimpleQuery.php | 152 ++++++++++++ engine/classes/TableQueryComponent.php | 21 ++ engine/classes/UpdateQueryTypeQueryComponent.php | 14 ++ engine/classes/WhereQueryComponent.php | 44 ++++ engine/classes/WhereSetQueryComponent.php | 41 ++++ engine/classes/WhereStaticQueryComponent.php | 40 ++++ 34 files changed, 1036 insertions(+) create mode 100644 engine/classes/APIException.php create mode 100644 engine/classes/AccessControlQueryComponent.php create mode 100644 engine/classes/CallException.php create mode 100644 engine/classes/ClassException.php create mode 100644 engine/classes/ClassNotFoundException.php create mode 100644 engine/classes/ConfigurationException.php create mode 100644 engine/classes/DataFormatException.php create mode 100644 engine/classes/DatabaseException.php create mode 100644 engine/classes/DeleteQueryTypeQueryComponent.php create mode 100644 engine/classes/IOException.php create mode 100644 engine/classes/InsertQueryTypeQueryComponent.php create mode 100644 engine/classes/InstallationException.php create mode 100644 engine/classes/InvalidClassException.php create mode 100644 engine/classes/InvalidParameterException.php create mode 100644 engine/classes/JoinQueryComponent.php create mode 100644 engine/classes/LimitOffsetQueryComponent.php create mode 100644 engine/classes/NotImplementedException.php create mode 100644 engine/classes/NotificationException.php create mode 100644 engine/classes/OrderQueryComponent.php create mode 100644 engine/classes/PluginException.php create mode 100644 engine/classes/Query.php create mode 100644 engine/classes/QueryComponent.php create mode 100644 engine/classes/QueryTypeQueryComponent.php create mode 100644 engine/classes/RegistrationException.php create mode 100644 engine/classes/SecurityException.php create mode 100644 engine/classes/SelectFieldQueryComponent.php create mode 100644 engine/classes/SelectQueryTypeQueryComponent.php create mode 100644 engine/classes/SetQueryComponent.php create mode 100644 engine/classes/SimpleQuery.php create mode 100644 engine/classes/TableQueryComponent.php create mode 100644 engine/classes/UpdateQueryTypeQueryComponent.php create mode 100644 engine/classes/WhereQueryComponent.php create mode 100644 engine/classes/WhereSetQueryComponent.php create mode 100644 engine/classes/WhereStaticQueryComponent.php (limited to 'engine/classes') diff --git a/engine/classes/APIException.php b/engine/classes/APIException.php new file mode 100644 index 000000000..d6eb9ce52 --- /dev/null +++ b/engine/classes/APIException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class APIException extends Exception {} diff --git a/engine/classes/AccessControlQueryComponent.php b/engine/classes/AccessControlQueryComponent.php new file mode 100644 index 000000000..ad5410ddc --- /dev/null +++ b/engine/classes/AccessControlQueryComponent.php @@ -0,0 +1,40 @@ +acl_table = $CONFIG->dbprefix . sanitise_string($acl_table); + $this->acl_field = sanitise_string($acl_field); + $this->object_owner_table = $CONFIG->dbprefix . sanitise_string($object_owner_table); + $this->object_owner_id_field = sanitise_string($object_owner_id_field); + } + + function __toString() + { + //$access = get_access_list(); + // KJ - changed to use get_access_sql_suffix + // Note: currently get_access_sql_suffix is hardwired to use + // $acl_field = "access_id", $object_owner_table = $acl_table, and + // $object_owner_id_field = "owner_guid" + // @todo recode get_access_sql_suffix to make it possible to specify alternate field names + return "and ".get_access_sql_suffix($this->acl_table); // Add access controls + + //return "and ({$this->acl_table}.{$this->acl_field} in {$access} or ({$this->acl_table}.{$this->acl_field} = 0 and {$this->object_owner_table}.{$this->object_owner_id_field} = {$_SESSION['id']}))"; + } +} diff --git a/engine/classes/CallException.php b/engine/classes/CallException.php new file mode 100644 index 000000000..369ce19bd --- /dev/null +++ b/engine/classes/CallException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class CallException extends Exception {} diff --git a/engine/classes/ClassException.php b/engine/classes/ClassException.php new file mode 100644 index 000000000..6dfb57b0e --- /dev/null +++ b/engine/classes/ClassException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class ClassException extends Exception {} diff --git a/engine/classes/ClassNotFoundException.php b/engine/classes/ClassNotFoundException.php new file mode 100644 index 000000000..f344e6b28 --- /dev/null +++ b/engine/classes/ClassNotFoundException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class ClassNotFoundException extends ClassException {} \ No newline at end of file diff --git a/engine/classes/ConfigurationException.php b/engine/classes/ConfigurationException.php new file mode 100644 index 000000000..a3c7c38cc --- /dev/null +++ b/engine/classes/ConfigurationException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class ConfigurationException extends Exception {} diff --git a/engine/classes/DataFormatException.php b/engine/classes/DataFormatException.php new file mode 100644 index 000000000..2038f1fb7 --- /dev/null +++ b/engine/classes/DataFormatException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class DataFormatException extends Exception {} diff --git a/engine/classes/DatabaseException.php b/engine/classes/DatabaseException.php new file mode 100644 index 000000000..ffc414abf --- /dev/null +++ b/engine/classes/DatabaseException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class DatabaseException extends Exception {} diff --git a/engine/classes/DeleteQueryTypeQueryComponent.php b/engine/classes/DeleteQueryTypeQueryComponent.php new file mode 100644 index 000000000..645e95cb5 --- /dev/null +++ b/engine/classes/DeleteQueryTypeQueryComponent.php @@ -0,0 +1,14 @@ +query_type = "DELETE FROM"; + } +} diff --git a/engine/classes/IOException.php b/engine/classes/IOException.php new file mode 100644 index 000000000..683be32a1 --- /dev/null +++ b/engine/classes/IOException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class IOException extends Exception {} diff --git a/engine/classes/InsertQueryTypeQueryComponent.php b/engine/classes/InsertQueryTypeQueryComponent.php new file mode 100644 index 000000000..20d1a7adf --- /dev/null +++ b/engine/classes/InsertQueryTypeQueryComponent.php @@ -0,0 +1,14 @@ +query_type = "INSERT INTO"; + } +} diff --git a/engine/classes/InstallationException.php b/engine/classes/InstallationException.php new file mode 100644 index 000000000..96bd9beff --- /dev/null +++ b/engine/classes/InstallationException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class InstallationException extends ConfigurationException {} diff --git a/engine/classes/InvalidClassException.php b/engine/classes/InvalidClassException.php new file mode 100644 index 000000000..84d3b3625 --- /dev/null +++ b/engine/classes/InvalidClassException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class InvalidClassException extends ClassException {} diff --git a/engine/classes/InvalidParameterException.php b/engine/classes/InvalidParameterException.php new file mode 100644 index 000000000..a94904da0 --- /dev/null +++ b/engine/classes/InvalidParameterException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class InvalidParameterException extends CallException {} diff --git a/engine/classes/JoinQueryComponent.php b/engine/classes/JoinQueryComponent.php new file mode 100644 index 000000000..8300cf710 --- /dev/null +++ b/engine/classes/JoinQueryComponent.php @@ -0,0 +1,33 @@ +table1 = $CONFIG->dbprefix . sanitise_string($table1); + $this->field1 = sanitise_string($field1); + $this->table2 = $CONFIG->dbprefix . sanitise_string($table2); + $this->field2 = sanitise_string($field2); + $this->operator = sanitise_string($operator); + } + + function __toString() + { + return "join {$this->table2} on {$this->$table}.{$this->$field} {$this->$operator} {$this->$table2}.{$this->$field2}"; + } +} diff --git a/engine/classes/LimitOffsetQueryComponent.php b/engine/classes/LimitOffsetQueryComponent.php new file mode 100644 index 000000000..2cc77dd47 --- /dev/null +++ b/engine/classes/LimitOffsetQueryComponent.php @@ -0,0 +1,26 @@ +limit = (int)$limit; + $this->offset = (int)$offset; + } + + function __toString() + { + return "limit {$this->offset}, {$this->limit}"; + } +} diff --git a/engine/classes/NotImplementedException.php b/engine/classes/NotImplementedException.php new file mode 100644 index 000000000..f95c88c47 --- /dev/null +++ b/engine/classes/NotImplementedException.php @@ -0,0 +1,11 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class NotImplementedException extends CallException {} diff --git a/engine/classes/NotificationException.php b/engine/classes/NotificationException.php new file mode 100644 index 000000000..78ea4c57e --- /dev/null +++ b/engine/classes/NotificationException.php @@ -0,0 +1,6 @@ +table = $CONFIG->dbprefix . sanitise_string($table); + $this->field = sanitise_string($field); + $this->order = sanitise_string($order); + } + + function __toString() + { + return "order by {$this->table}.{$this->field} {$this->order}"; + } +} diff --git a/engine/classes/PluginException.php b/engine/classes/PluginException.php new file mode 100644 index 000000000..4da15791d --- /dev/null +++ b/engine/classes/PluginException.php @@ -0,0 +1,10 @@ + + * // Construct the query + * $query = new Query(); + * + * // Say which table we're interested in + * $query->addTable(new TableQueryComponent("entities")); + * + * // What fields are we interested in + * $query->addSelectField(new SelectFieldQueryComponent("entities","*")); + * + * // Add access control (Default access control uses default fields on entities table. + * // Note that it will error without something specified here! + * $query->setAccessControl(new AccessControlQueryComponent()); + * + * // Set a limit and offset, may be omitted. + * $query->setLimitAndOffset(new LimitOffsetQueryComponent(10,0)); + * + * // Specify the order, may be omitted + * $query->setOrder(new OrderQueryComponent("entities", "subtype", "desc")); + * + * // Construct a where query + * // + * // This demonstrates a WhereSet which lets you have sub wheres, a + * // WhereStatic which lets you compare a table field against a value and a + * // Where which lets you compare a table/field with another table/field. + * $query->addWhere( + * new WhereSetQueryComponent( + * array( + * new WhereStaticQueryComponent("entities", "subtype","=", 1), + * new WhereQueryComponent("entities","subtype","=", "entities", "subtype") + * ) + * ) + * ); + * + * get_data($query); + * + * + * @author Curverider Ltd + */ +class Query +{ + + /// The limit of the query + private $limit_and_offset; + + /// Fields to return on a query + private $fields; + + /// Tables to use in a from query + private $tables; + + /// Join tables + private $joins; + + /// Set values + private $sets; + + /// Where query + private $where; + + /// Order by + private $order; + + /// The query type + private $query_type; + + /// ACL + private $access_control; + + /** + * Construct query & initialise variables + */ + function __construct() + { + $this->fields = array(); + $this->tables = array(); + $this->joins = array(); + $this->where = array(); + $this->sets = array(); + + $this->setQueryType(new SelectQueryTypeQueryComponent()); + } + + /** + * Add limits and offsets to the query. + * + * @param LimitOffsetQueryComponent $component The limit and offset. + */ + public function setLimitAndOffset(LimitOffsetQueryComponent $component) { $this->limit_and_offset = $component; } + + /** + * Reset and set the field to the select statement. + * + * @param SelectFieldQueryComponent $component Table and field component. + */ + public function setSelectField(SelectFieldQueryComponent $component) + { + $this->fields = array(); + return $this->addSelectField($component); + } + + /** + * Add a select field. + * + * @param SelectFieldQueryComponent $component Add a component. + */ + public function addSelectField(SelectFieldQueryComponent $component) { $this->fields[] = $component; } + + /** + * Add a join to the component. + * + * @param JoinQueryComponent $component The join. + */ + public function addJoin(JoinQueryComponent $component) { $this->joins[] = $component; } + + /** + * Set a field value in an update or insert statement. + * + * @param SetQueryComponent $component Fields to set. + */ + public function addSet(SetQueryComponent $component) { $this->sets[] = $component; } + + /** + * Set the query type, i.e. "select", "update", "insert" & "delete". + * + * @param QueryTypeQueryComponent $component The query type. + */ + public function setQueryType(QueryTypeQueryComponent $component) { $this->query_type = $component; } + + /** + * Attach an order component. + * + * @param OrderQueryComponent $component The order component. + */ + public function setOrder(OrderQueryComponent $component) { $this->order = $component; } + + /** + * Add a table to the query. + * + * @param TableQueryComponent $component Table to add. + */ + public function addTable(TableQueryComponent $component) { $this->tables[] = $component; } + + /** + * Add a where clause to the query. + * + * @param WhereQueryComponent $component The where component + */ + public function addWhere(WhereQueryComponent $component) { $this->where[] = $component; } + + /** + * Set access control. + * + * @param AccessControlQueryComponent $component Access control. + */ + public function setAccessControl(AccessControlQueryComponent $component) { $this->access_control = $component; } + + public function __toString() + { + global $CONFIG; + + $sql = ""; + + try + { + // Query prefix & fields + if (!empty($this->query_type)) + { + $sql .= "{$this->query_type} "; + + if (!empty($this->fields)) + { + $fields = ""; + + foreach ($this->fields as $field) + $fields .= "$field"; + + $sql .= " $fields from "; + } + else + throw new DatabaseException(elgg_echo('DatabaseException:SelectFieldsMissing')); + } + else + throw new DatabaseException(elgg_echo('DatabaseException:UnspecifiedQueryType')); + + // Tables + if (!empty($this->tables)) + { + foreach($this->tables as $table) + $sql .= "$table, "; + + $sql = trim($sql, ", "); + } + else + throw new DatabaseException(elgg_echo('DatabaseException:NoTablesSpecified')); + + // Joins on select queries + if ($this->query_type->query_type == 'select') + { + if (!empty($this->joins)) + { + foreach($this->joins as $join) + $sql .= "$join "; + } + } + + // Setting values + if ( + ($this->query_type->query_type == 'update') || + ($this->query_type->query_type == 'insert') + ) + { + $sql .= "set "; + + foreach ($this->sets as $set) + $sql .= "$set, "; + + $sql = trim($sql, ", ") . " "; + } + + // Where + if (!empty($this->where)) + { + $sql .= " where 1 "; + + foreach ($this->where as $where) + $sql .= "$where "; + } + + // Access control + if (!empty($this->access_control)) + { + + // Catch missing Where + if (empty($this->where)) + $sql .= " where 1 "; + + $sql .= "{$this->access_control} "; + } + else + throw new DatabaseException(elgg_echo('DatabaseException:NoACL')); + + // Order by + if (!empty($this->order)) + $sql .= "{$this->order} "; + + // Limits + if (!empty($this->limit_and_offset)) + $sql .= "{$this->limit_and_offset} "; + + + + } catch (Exception $e) { + trigger_error($e, E_USER_WARNING); + } + + + return $sql; + } + +} + diff --git a/engine/classes/QueryComponent.php b/engine/classes/QueryComponent.php new file mode 100644 index 000000000..cfe7683b1 --- /dev/null +++ b/engine/classes/QueryComponent.php @@ -0,0 +1,42 @@ +fields = array(); + } + + /** + * Class member get overloading + * + * @param string $name + * @return mixed + */ + function __get($name) { + return $this->fields[$name]; + } + + /** + * Class member set overloading + * + * @param string $name + * @param mixed $value + * @return void + */ + function __set($name, $value) { + $this->fields[$name] = $value; + + return true; + } +} diff --git a/engine/classes/QueryTypeQueryComponent.php b/engine/classes/QueryTypeQueryComponent.php new file mode 100644 index 000000000..231faa733 --- /dev/null +++ b/engine/classes/QueryTypeQueryComponent.php @@ -0,0 +1,14 @@ +query_type; + } +} diff --git a/engine/classes/RegistrationException.php b/engine/classes/RegistrationException.php new file mode 100644 index 000000000..5efea3904 --- /dev/null +++ b/engine/classes/RegistrationException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class RegistrationException extends InstallationException {} \ No newline at end of file diff --git a/engine/classes/SecurityException.php b/engine/classes/SecurityException.php new file mode 100644 index 000000000..3075f0b8e --- /dev/null +++ b/engine/classes/SecurityException.php @@ -0,0 +1,10 @@ + + * @package Elgg + * @subpackage Exceptions + */ +class SecurityException extends Exception {} diff --git a/engine/classes/SelectFieldQueryComponent.php b/engine/classes/SelectFieldQueryComponent.php new file mode 100644 index 000000000..fbc4a81c1 --- /dev/null +++ b/engine/classes/SelectFieldQueryComponent.php @@ -0,0 +1,28 @@ +table = $CONFIG->dbprefix . sanitise_string($table); + $this->field = sanitise_string($field); + } + + function __toString() + { + return "{$this->table}.{$this->field}"; + } +} diff --git a/engine/classes/SelectQueryTypeQueryComponent.php b/engine/classes/SelectQueryTypeQueryComponent.php new file mode 100644 index 000000000..61e7e5017 --- /dev/null +++ b/engine/classes/SelectQueryTypeQueryComponent.php @@ -0,0 +1,14 @@ +query_type = "SELECT"; + } +} diff --git a/engine/classes/SetQueryComponent.php b/engine/classes/SetQueryComponent.php new file mode 100644 index 000000000..0acd5718c --- /dev/null +++ b/engine/classes/SetQueryComponent.php @@ -0,0 +1,33 @@ +table = $CONFIG->dbprefix . sanitise_string($table); + $this->field = sanitise_string($field); + if (is_numeric($value)) + $this->value = (int)$value; + else + $this->value = "'".sanitise_string($value)."'"; + } + + function __toString() + { + return "{$this->table}.{$this->field}={$this->value}"; + } +} diff --git a/engine/classes/SimpleQuery.php b/engine/classes/SimpleQuery.php new file mode 100644 index 000000000..cf5a18a50 --- /dev/null +++ b/engine/classes/SimpleQuery.php @@ -0,0 +1,152 @@ +simpleQueryType(); + + // Set a default access control + $this->simpleAccessControl(); + + // Set default limit and offset + $this->simpleLimitAndOffset(); + } + + /** + * Set the query type. + * + * @param string $type The type of search - available are "select", "update", "delete", "insert". + */ + public function simpleQueryType($type = "select") + { + $type = strtolower(sanitise_string($type)); + + switch ($type) + { + case "insert" : + return $this->setQueryType(InsertQueryTypeQueryComponent()); + break; + case "delete" : + return $this->setQueryType(DeleteQueryTypeQueryComponent()); + break; + case "update" : + return $this->setQueryType(UpdateQueryTypeQueryComponent()); + break; + default: return $this->setQueryType(SelectQueryTypeQueryComponent()); + } + } + + /** + * Set a field to query in a select statement. + * + * @param string $table Table to query. + * @param string $field Field in that table. + */ + public function simpleSelectField($table, $field) { return $this->setSelectField(new SelectFieldQueryComponent($table, $field)); } + + /** + * Add a select field to query in a select statement. + * + * @param string $table Table to query. + * @param string $field Field in that table. + */ + public function simpleAddSelectField($table, $field) { return $this->addSelectField(new SelectFieldQueryComponent($table, $field)); } + + /** + * Add a set value to an update query. + * + * @param string $table The table to update. + * @param string $field The field in the table. + * @param mixed $value The value to set it to. + */ + public function simpleSet($table, $field, $value) { return $this->addSet(new SetQueryComponent($table, $field, $value)); } + + /** + * Add a join to the table. + * + * @param string $table Table one to join... + * @param string $field Field 1 with... + * @param string $table2 Table 2 ... + * @param string $field2 Field... + * @param string $operator Using this operator + */ + public function simpleJoin($table1, $field1, $table2, $field2, $operator = "=") { return $this->addJoin(new JoinQueryComponent($table1, $field1, $table2, $field2, $operator)); } + + /** + * Add a table to the query. + * + * @param string $table The table. + */ + public function simpleTable($table) { return $this->addTable(new TableQueryComponent($table)); } + + /** + * Compare one table/field to another table/field. + * + * @param string $left_table The table on the left of the operator + * @param string $left_field The left field + * @param string $operator The operator eg "=" or "<" + * @param string $right_table The table on the right of the operator + * @param string $right_field The right field + * @param string $link_operator How this where clause links with the previous clause, eg. "and" "or" + */ + public function simpleWhereOnTable($left_table, $left_field, $operator, $right_table, $right_field, $link_operator = "and") { return $this->addWhere(new WhereQueryComponent($left_table, $left_field, $operator, $right_table, $right_field, $link_operator)); } + + /** + * Compare one table/field to a value. + * + * @param string $left_table The table on the left of the operator + * @param string $left_field The left field + * @param string $operator The operator eg "=" or "<" + * @param string $value The value + * @param string $link_operator How this where clause links with the previous clause, eg. "and" "or" + */ + public function simpleWhereOnValue($left_table, $left_field, $operator, $value, $link_operator = "and") { return $this->addWhere(new WhereStaticQueryComponent($left_table, $left_field, $operator, $value, $link_operator)); } + + /** + * Set access control. + * + * @param string $acl_table The table where the access control field is. + * @param string $acl_field The field containing the access control. + * @param string $object_owner_id_field The field in $object_owner_table containing the owner information. + */ + public function simpleAccessControl($acl_table = "entities", $acl_field = "access_id", $object_owner_id_field = "owner_guid") { return $this->setAccessControl(new AccessControlQueryComponent($acl_table, $acl_field, $acl_table, $object_owner_id_field)); } + + /** + * Set the limit and offset. + * + * @param int $limit The limit. + * @param int $offset The offset. + */ + public function simpleLimitAndOffset($limit = 25, $offset = 0) { return $this->setLimitAndOffset(new LimitOffsetQueryComponent($limit, $offset)); } + + /** + * Set the order query. + * + * @param string $table The table to query + * @param string $field The field to query + * @param string $order Order the query + */ + public function simpleOrder($table, $field, $order = "desc") + { + $table = sanitise_string($table); + $field = sanitise_string($field); + $order = strtolower(sanitise_string($order)); + + return $this->setOrder(new OrderQueryComponent($table, $field, $order)); break; + } +} diff --git a/engine/classes/TableQueryComponent.php b/engine/classes/TableQueryComponent.php new file mode 100644 index 000000000..54e6ab021 --- /dev/null +++ b/engine/classes/TableQueryComponent.php @@ -0,0 +1,21 @@ +table = $CONFIG->dbprefix . sanitise_string($table); + } + + function __toString() + { + return $this->table; + } +} diff --git a/engine/classes/UpdateQueryTypeQueryComponent.php b/engine/classes/UpdateQueryTypeQueryComponent.php new file mode 100644 index 000000000..226b14be7 --- /dev/null +++ b/engine/classes/UpdateQueryTypeQueryComponent.php @@ -0,0 +1,14 @@ +query_type = "UPDATE"; + } +} diff --git a/engine/classes/WhereQueryComponent.php b/engine/classes/WhereQueryComponent.php new file mode 100644 index 000000000..3130be7f8 --- /dev/null +++ b/engine/classes/WhereQueryComponent.php @@ -0,0 +1,44 @@ +link_operator = sanitise_string($link_operator); + $this->left_table = $CONFIG->dbprefix . sanitise_string($left_table); + $this->left_field = sanitise_string($left_field); + $this->operator = sanitise_string($operator); + $this->right_table = $CONFIG->dbprefix . sanitise_string($right_table); + $this->right_field = sanitise_string($right_field); + } + + /** + * Return the SQL without the link operator. + */ + public function toStringNoLink() + { + return "{$this->left_table }.{$this->left_field} {$this->operator} {$this->right_table}.{$this->right_field}"; + } + + function __toString() + { + return "{$this->link_operator} " . $this->toStringNoLink(); + } +} diff --git a/engine/classes/WhereSetQueryComponent.php b/engine/classes/WhereSetQueryComponent.php new file mode 100644 index 000000000..b5fe439a0 --- /dev/null +++ b/engine/classes/WhereSetQueryComponent.php @@ -0,0 +1,41 @@ +link_operator = sanitise_string($link_operator); + $this->wheres = $wheres; + } + + public function toStringNoLink() + { + $cnt = 0; + $string = " ("; + foreach ($this->wheres as $where) { + if (!($where instanceof WhereQueryComponent)) + throw new DatabaseException(elgg_echo('DatabaseException:WhereSetNonQuery')); + + if (!$cnt) + $string.= $where->toStringNoLink(); + else + $string.=" $where "; + + $cnt ++; + } + $string .= ")"; + + return $string; + } +} diff --git a/engine/classes/WhereStaticQueryComponent.php b/engine/classes/WhereStaticQueryComponent.php new file mode 100644 index 000000000..ddc036fd1 --- /dev/null +++ b/engine/classes/WhereStaticQueryComponent.php @@ -0,0 +1,40 @@ +link_operator = sanitise_string($link_operator); + $this->left_table = $CONFIG->dbprefix . sanitise_string($left_table); + $this->left_field = sanitise_string($left_field); + $this->operator = sanitise_string($operator); + if (is_numeric($value)) + $this->value = (int)$value; + else + $this->value = "'".sanitise_string($value)."'"; + } + + /** + * Return the SQL without the link operator. + */ + public function toStringNoLink() + { + return "{$this->left_table }.{$this->left_field} {$this->operator} {$this->value}"; + } +} -- cgit v1.2.3