throw new Exception("Configuration parameter missing: \$Config['DocumentStorage']['Path']. Expected value for this parameter is the path to the collections folders.");
use Waystone\Workspaces\Engines\Framework\Context;
+use Exception;
+use InvalidArgumentException;
+use Iterator;
+
/**
* MySQL Collection class
*
* This class represents a collection of documents, stored on MySQL.
*/
class MySQLCollection extends SQLCollection {
+
///
/// Singleton pattern to get the MySQLDatabase instance
///
/**
- * @var MySQLDatabase The mongo client to the database the collection is hosted
+ * @var MySQLDatabase The mongo client to the database the collection is
+ * hosted
*/
public static $client = null;
/**
- * Gets the existing MySQLDatabase instance, or if not available, initializes one.
+ * Gets the existing MySQLDatabase instance, or if not available,
+ * initializes one.
*
* @param Context $context
+ *
* @return MySQLDatabase The MySQLDatabase instance
*/
public static function getCurrentSiteDatabaseClient () {
@@ -48,6 +58,7 @@
throw new InvalidArgumentException("The MySQLDatabase driver is intended to be used when your main database product is MySQL. We recommend whether you pick the same engine for collections and other db use, whether you use a key/store storage solution for collections, like MongoDB.");
}
}
+
return self::$client;
}
@@ -60,7 +71,11 @@
*
* @param string $id the collection identifiant
*/
- public function __construct ($id, ?MySQLDatabase $client = null, $table = '') {
+ public function __construct (
+ $id,
+ ?MySQLDatabase $client = null,
+ $table = '',
+ ) {
global $Config;
if ($client === null) {
@@ -70,7 +85,8 @@
}
if ($table == '') {
- if (!array_key_exists('DocumentStorage', $Config) || !array_key_exists('Table', $Config['DocumentStorage'])) {
+ if (!array_key_exists('DocumentStorage', $Config)
throw new Exception("Configuration parameter missing: \$Config['DocumentStorage']['Table']. Expected value for this parameter is the table to store the collections documents.");
protected function initializeCollectionsTable () {
- if (defined('COLLECTIONS_MYSQL_DATABASE_READY') && COLLECTIONS_MYSQL_DATABASE_READY) {
+ if (defined('COLLECTIONS_MYSQL_DATABASE_READY')
+ && COLLECTIONS_MYSQL_DATABASE_READY) {
return;
}
@@ -100,7 +117,7 @@
document_id VARCHAR(255),
document_value BLOB,
PRIMARY KEY (collection_id, document_id)
- );"
+ );",
);
define('COLLECTIONS_MYSQL_DATABASE_READY', true);
@@ -114,7 +131,10 @@
* Executes a SQL query
*
* @param string $sql The SQL query
- * @return mixed If the query doesn't return any result, null. If the query return a row with one field, the scalar value. Otherwise, an associative array, the fields as keys, the row as values.
+ *
+ * @return mixed If the query doesn't return any result, null. If the query
+ * return a row with one field, the scalar value. Otherwise, an
+ * associative array, the fields as keys, the row as values.
*/
public function query ($sql) {
if ($sql == "") {
@@ -141,6 +161,7 @@
* Escapes the SQL string
*
* @param string $value The value to escape
+ *
* @return string The escaped value
*/
public function escape ($value) {
@@ -150,13 +171,15 @@
/**
* Gets all the documents from the collection
*
- * @return Iterator An iterator to the documents, each item an instance of CollectionDocument
+ * @return Iterator An iterator to the documents, each item an instance of
+ * CollectionDocument
*/
public function getAll () {
$db = self::$client;
$collectionId = $this->escape($this->id);
- $sql = "SELECT * FROM $this->table WHERE collection_id = '$collectionId'";
+ $sql =
+ "SELECT * FROM $this->table WHERE collection_id = '$collectionId'";
if (!$result = $db->query($sql, MYSQL_ASSOC)) {
throw new Exception("Can't get each collection documents.");
- * Abstract class with SQL standard implementation of CRUD features for collections using a SQL database.
+ * Abstract class with SQL standard implementation of CRUD features for
+ * collections using a SQL database.
*/
abstract class SQLCollection extends Collection {
+
/**
* @var string The SQL collections table
*/
@@ -28,7 +34,10 @@
* Executes a SQL query
*
* @param string $sql The SQL query
- * @return mixed If the query doesn't return any null, nothing. If the query return a row with one field, the scalar value. Otherwise, an associative array, the fields as keys, the row as values.
+ *
+ * @return mixed If the query doesn't return any null, nothing. If the
+ * query return a row with one field, the scalar value. Otherwise, an
+ * associative array, the fields as keys, the row as values.
*/
public abstract function query ($sql);
@@ -36,6 +45,7 @@
* Escapes the SQL string
*
* @param string $value The value to escape
+ *
* @return string The escaped value
*/
public abstract function escape ($value);
@@ -44,6 +54,7 @@
* Adds a document to the collection
*
* @param CollectionDocument $document The document to add
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function add (CollectionDocument &$document) {
throw new Exception("Configuration parameter missing: \$Config['DocumentStorage']['File']. Expected value for this parameter is the path to the SQLite database file.");
}
@@ -81,7 +87,8 @@
* Initializes collections table
*/
protected function initializeCollectionsTable () {
- if (defined('COLLECTIONS_SQLITE_DATABASE_READY') && COLLECTIONS_SQLITE_DATABASE_READY) {
+ if (defined('COLLECTIONS_SQLITE_DATABASE_READY')
- * @return mixed If the query doesn't return any result, null. If the query return a row with one field, the scalar value. Otherwise, an associative array, the fields as keys, the row as values.
+ *
+ * @return mixed If the query doesn't return any result, null. If the query
+ * return a row with one field, the scalar value. Otherwise, an
+ * associative array, the fields as keys, the row as values.
*/
public function query ($sql) {
$client = static::getClient();
@@ -126,9 +138,10 @@
if (!$client->exec($sql)) {
throw new Exception(
"Can't execute collection query. "
- . $client->lastErrorMsg()
+ . $client->lastErrorMsg(),
);
}
+
return null;
}
@@ -139,7 +152,7 @@
if ($result === false) {
throw new Exception(
"Can't execute collection query. "
- . $client->lastErrorMsg()
+ . $client->lastErrorMsg(),
);
}
@@ -158,6 +171,7 @@
* Escapes the SQL string
*
* @param string $value The value to escape
+ *
* @return string The escaped value
*/
public function escape ($value) {
@@ -167,11 +181,13 @@
/**
* Gets all the documents from the collection
*
- * @return Iterator An iterator to the documents, each item an instance of CollectionDocument
+ * @return Iterator An iterator to the documents, each item an instance of
+ * CollectionDocument
*/
public function getAll () {
$collectionId = $this->escape($this->id);
- $sql = "SELECT document_value FROM $this->table WHERE collection_id = '$collectionId'";
+ $sql =
+ "SELECT document_value FROM $this->table WHERE collection_id = '$collectionId'";