diff -u /dev/null b/dbtng_example/src/DBTNGExampleStorage.php --- /dev/null +++ b/dbtng_example/src/DBTNGExampleStorage.php @@ -0,0 +1,227 @@ +fields($entry) + ->execute(); + } + catch (\Exception $e) { + drupal_set_message(t('db_insert failed. Message = %message, query= %query', array( + '%message' => $e->getMessage(), + '%query' => $e->query_string, + ) + ), 'error'); + } + return $return_value; + } + + /** + * Update an entry in the database. + * + * @param array $entry + * An array containing all the fields of the item to be updated. + * + * @return int + * The number of updated rows. + * + * @see db_update() + */ + public static function update($entry) { + try { + // db_update()...->execute() returns the number of rows updated. + $count = db_update('dbtng_example') + ->fields($entry) + ->condition('pid', $entry['pid']) + ->execute(); + } + catch (\Exception $e) { + drupal_set_message(t('db_update failed. Message = %message, query= %query', array( + '%message' => $e->getMessage(), + '%query' => $e->query_string, + ) + ), 'error'); + } + return $count; + } + + /** + * Delete an entry from the database. + * + * @param array $entry + * An array containing at least the person identifier 'pid' element of the + * entry to delete. + * + * @see db_delete() + */ + public static function delete($entry) { + db_delete('dbtng_example') + ->condition('pid', $entry['pid']) + ->execute(); + } + + /** + * Read from the database using a filter array. + * + * The standard function to perform reads was db_query(), and for static + * queries, it still is. + * + * db_query() used an SQL query with placeholders and arguments as parameters. + * + * Drupal DBTNG provides an abstracted interface that will work with a wide + * variety of database engines. + * + * db_query() is deprecated except when doing a static query. The following is + * perfectly acceptable in Drupal 8. See + * @link http://drupal.org/node/310072 the handbook page on static queries @endlink + * + * @code + * // SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John' + * db_query( + * "SELECT * FROM {dbtng_example} WHERE uid = :uid and name = :name", + * array(':uid' => 0, ':name' => 'John') + * )->execute(); + * @endcode + * + * But for more dynamic queries, Drupal provides the db_select() + * API method, so there are several ways to perform the same SQL query. See + * the + * @link http://drupal.org/node/310075 handbook page on dynamic queries. @endlink + * @code + * // SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John' + * db_select('dbtng_example') + * ->fields('dbtng_example') + * ->condition('uid', 0) + * ->condition('name', 'John') + * ->execute(); + * @endcode + * + * Here is db_select with named placeholders: + * @code + * // SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John' + * $arguments = array(':name' => 'John', ':uid' => 0); + * db_select('dbtng_example') + * ->fields('dbtng_example') + * ->where('uid = :uid AND name = :name', $arguments) + * ->execute(); + * @endcode + * + * Conditions are stacked and evaluated as AND and OR depending on the type of + * query. For more information, read the conditional queries handbook page at: + * http://drupal.org/node/310086 + * + * The condition argument is an 'equal' evaluation by default, but this can be + * altered: + * @code + * // SELECT * FROM {dbtng_example} WHERE age > 18 + * db_select('dbtng_example') + * ->fields('dbtng_example') + * ->condition('age', 18, '>') + * ->execute(); + * @endcode + * + * @param array $entry + * An array containing all the fields used to search the entries in the + * table. + * + * @return object + * An object containing the loaded entries if found. + * + * @see db_select() + * @see db_query() + * @see http://drupal.org/node/310072 + * @see http://drupal.org/node/310075 + */ + public static function load($entry = array()) { + // Read all fields from the dbtng_example table. + $select = db_select('dbtng_example', 'example'); + $select->fields('example'); + + // Add each field and value as a condition to this query. + foreach ($entry as $field => $value) { + $select->condition($field, $value); + } + // Return the result in object format. + return $select->execute()->fetchAll(); + } + + /** + * Load dbtng_example records joined with user records. + * + * DBTNG also helps processing queries that return several rows, providing the + * found objects in the same query execution call. + * + * This function queries the database using a JOIN between users table and the + * example entries, to provide the username that created the entry, and + * creates a table with the results, processing each row. + * + * SELECT + * e.pid as pid, e.name as name, e.surname as surname, e.age as age + * u.name as username + * FROM + * {dbtng_example} e + * JOIN + * users u ON e.uid = u.uid + * WHERE + * e.name = 'John' AND e.age > 18 + * + * @see db_select() + * @see http://drupal.org/node/310075 + */ + public static function advancedLoad() { + $select = db_select('dbtng_example', 'e'); + // Join the users table, so we can get the entry creator's username. + $select->join('users_field_data', 'u', 'e.uid = u.uid'); + // Select these specific fields for the output. + $select->addField('e', 'pid'); + $select->addField('u', 'name', 'username'); + $select->addField('e', 'name'); + $select->addField('e', 'surname'); + $select->addField('e', 'age'); + // Filter only persons named "John". + $select->condition('e.name', 'John'); + // Filter only persons older than 18 years. + $select->condition('e.age', 18, '>'); + // Make sure we only get items 0-49, for scalability reasons. + $select->range(0, 50); + + $entries = $select->execute()->fetchAll(\PDO::FETCH_ASSOC); + + return $entries; + } + +} reverted: --- b/dbtng_example/src/DbtngExampleStorage.php +++ /dev/null @@ -1,227 +0,0 @@ -fields($entry) - ->execute(); - } - catch (\Exception $e) { - drupal_set_message(t('db_insert failed. Message = %message, query= %query', array( - '%message' => $e->getMessage(), - '%query' => $e->query_string, - ) - ), 'error'); - } - return $return_value; - } - - /** - * Update an entry in the database. - * - * @param array $entry - * An array containing all the fields of the item to be updated. - * - * @return int - * The number of updated rows. - * - * @see db_update() - */ - public static function update($entry) { - try { - // db_update()...->execute() returns the number of rows updated. - $count = db_update('dbtng_example') - ->fields($entry) - ->condition('pid', $entry['pid']) - ->execute(); - } - catch (\Exception $e) { - drupal_set_message(t('db_update failed. Message = %message, query= %query', array( - '%message' => $e->getMessage(), - '%query' => $e->query_string, - ) - ), 'error'); - } - return $count; - } - - /** - * Delete an entry from the database. - * - * @param array $entry - * An array containing at least the person identifier 'pid' element of the - * entry to delete. - * - * @see db_delete() - */ - public static function delete($entry) { - db_delete('dbtng_example') - ->condition('pid', $entry['pid']) - ->execute(); - } - - /** - * Read from the database using a filter array. - * - * The standard function to perform reads was db_query(), and for static - * queries, it still is. - * - * db_query() used an SQL query with placeholders and arguments as parameters. - * - * Drupal DBTNG provides an abstracted interface that will work with a wide - * variety of database engines. - * - * db_query() is deprecated except when doing a static query. The following is - * perfectly acceptable in Drupal 8. See - * @link http://drupal.org/node/310072 the handbook page on static queries @endlink - * - * @code - * // SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John' - * db_query( - * "SELECT * FROM {dbtng_example} WHERE uid = :uid and name = :name", - * array(':uid' => 0, ':name' => 'John') - * )->execute(); - * @endcode - * - * But for more dynamic queries, Drupal provides the db_select() - * API method, so there are several ways to perform the same SQL query. See - * the - * @link http://drupal.org/node/310075 handbook page on dynamic queries. @endlink - * @code - * // SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John' - * db_select('dbtng_example') - * ->fields('dbtng_example') - * ->condition('uid', 0) - * ->condition('name', 'John') - * ->execute(); - * @endcode - * - * Here is db_select with named placeholders: - * @code - * // SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John' - * $arguments = array(':name' => 'John', ':uid' => 0); - * db_select('dbtng_example') - * ->fields('dbtng_example') - * ->where('uid = :uid AND name = :name', $arguments) - * ->execute(); - * @endcode - * - * Conditions are stacked and evaluated as AND and OR depending on the type of - * query. For more information, read the conditional queries handbook page at: - * http://drupal.org/node/310086 - * - * The condition argument is an 'equal' evaluation by default, but this can be - * altered: - * @code - * // SELECT * FROM {dbtng_example} WHERE age > 18 - * db_select('dbtng_example') - * ->fields('dbtng_example') - * ->condition('age', 18, '>') - * ->execute(); - * @endcode - * - * @param array $entry - * An array containing all the fields used to search the entries in the - * table. - * - * @return object - * An object containing the loaded entries if found. - * - * @see db_select() - * @see db_query() - * @see http://drupal.org/node/310072 - * @see http://drupal.org/node/310075 - */ - public static function load($entry = array()) { - // Read all fields from the dbtng_example table. - $select = db_select('dbtng_example', 'example'); - $select->fields('example'); - - // Add each field and value as a condition to this query. - foreach ($entry as $field => $value) { - $select->condition($field, $value); - } - // Return the result in object format. - return $select->execute()->fetchAll(); - } - - /** - * Load dbtng_example records joined with user records. - * - * DBTNG also helps processing queries that return several rows, providing the - * found objects in the same query execution call. - * - * This function queries the database using a JOIN between users table and the - * example entries, to provide the username that created the entry, and - * creates a table with the results, processing each row. - * - * SELECT - * e.pid as pid, e.name as name, e.surname as surname, e.age as age - * u.name as username - * FROM - * {dbtng_example} e - * JOIN - * users u ON e.uid = u.uid - * WHERE - * e.name = 'John' AND e.age > 18 - * - * @see db_select() - * @see http://drupal.org/node/310075 - */ - public static function advancedLoad() { - $select = db_select('dbtng_example', 'e'); - // Join the users table, so we can get the entry creator's username. - $select->join('users_field_data', 'u', 'e.uid = u.uid'); - // Select these specific fields for the output. - $select->addField('e', 'pid'); - $select->addField('u', 'name', 'username'); - $select->addField('e', 'name'); - $select->addField('e', 'surname'); - $select->addField('e', 'age'); - // Filter only persons named "John". - $select->condition('e.name', 'John'); - // Filter only persons older than 18 years. - $select->condition('e.age', 18, '>'); - // Make sure we only get items 0-49, for scalability reasons. - $select->range(0, 50); - - $entries = $select->execute()->fetchAll(\PDO::FETCH_ASSOC); - - return $entries; - } - -} only in patch2: unchanged: --- a/dbtng_example/src/Controller/DbtngExampleController.php +++ b/dbtng_example/src/Controller/DbtngExampleController.php @@ -8,7 +8,7 @@ namespace Drupal\dbtng_example\Controller; use Drupal\Core\Controller\ControllerBase; -use Drupal\dbtng_example\DbtngExampleStorage; +use Drupal\dbtng_example\DBTNGExampleStorage; /** * Controller for DBTNG Example. @@ -28,7 +28,7 @@ class DbtngExampleController extends ControllerBase { $rows = array(); $headers = array(t('Id'), t('uid'), t('Name'), t('Surname'), t('Age')); - foreach ($entries = DbtngExampleStorage::load() as $entry) { + foreach ($entries = DBTNGExampleStorage::load() as $entry) { // Sanitize each entry. $rows[] = array_map('Drupal\Component\Utility\SafeMarkup::checkPlain', (array) $entry); } @@ -64,7 +64,7 @@ class DbtngExampleController extends ControllerBase { ); $rows = array(); - foreach ($entries = DbtngExampleStorage::advancedLoad() as $entry) { + foreach ($entries = DBTNGExampleStorage::advancedLoad() as $entry) { // Sanitize each entry. $rows[] = array_map('Drupal\Component\Utility\SafeMarkup::checkPlain', $entry); } only in patch2: unchanged: --- a/dbtng_example/src/Form/DbtngExampleAddForm.php +++ b/dbtng_example/src/Form/DbtngExampleAddForm.php @@ -9,7 +9,7 @@ namespace Drupal\dbtng_example\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\dbtng_example\DbtngExampleStorage; +use Drupal\dbtng_example\DBTNGExampleStorage; /** * Simple form to add an entry, with all the interesting fields. @@ -84,7 +84,7 @@ class DbtngExampleAddForm extends FormBase { 'age' => $form_state->getValue('age'), 'uid' => $account->id(), ); - $return = DbtngExampleStorage::insert($entry); + $return = DBTNGExampleStorage::insert($entry); if ($return) { drupal_set_message(t('Created entry @entry', array('@entry' => print_r($entry, TRUE)))); } only in patch2: unchanged: --- a/dbtng_example/src/Form/DbtngExampleUpdateForm.php +++ b/dbtng_example/src/Form/DbtngExampleUpdateForm.php @@ -9,7 +9,7 @@ namespace Drupal\dbtng_example\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\dbtng_example\DbtngExampleStorage; +use Drupal\dbtng_example\DBTNGExampleStorage; /** * Sample UI to update a record. @@ -37,7 +37,7 @@ class DbtngExampleUpdateForm extends FormBase { '#markup' => $this->t('Demonstrates a database update operation.'), ); // Query for items to display. - $entries = DbtngExampleStorage::load(); + $entries = DBTNGExampleStorage::load(); // Tell the user if there is nothing to display. if (empty($entries)) { $form['no_values'] = array( @@ -148,7 +148,7 @@ class DbtngExampleUpdateForm extends FormBase { 'age' => $form_state->getValue('age'), 'uid' => $account->id(), ); - $count = DbtngExampleStorage::update($entry); + $count = DBTNGExampleStorage::update($entry); drupal_set_message(t('Updated entry @entry (@count row updated)', array( '@count' => $count, '@entry' => print_r($entry, TRUE), only in patch2: unchanged: --- a/dbtng_example/src/Tests/DBTNGExampleTest.php +++ b/dbtng_example/src/Tests/DBTNGExampleTest.php @@ -7,7 +7,7 @@ namespace Drupal\dbtng_example\Tests; -use Drupal\dbtng_example\DbtngExampleStorage; +use Drupal\dbtng_example\DBTNGExampleStorage; use Drupal\simpletest\WebTestBase; /** @@ -47,7 +47,7 @@ class DbtngExampleTest extends WebTestBase { */ public function testDbtngExample() { // Assert that two entries were inserted at install. - $result = DbtngExampleStorage::load(); + $result = DBTNGExampleStorage::load(); $this->assertEqual( count($result), 2, 'Found two entries in the table after installing the module.' ); @@ -113,7 +113,7 @@ class DbtngExampleTest extends WebTestBase { $this->assertPattern("/Some[td\/<>\w\s]+Anonymous/", "Text 'Some Anonymous' found in table"); // Try the update tab. // Find out the pid of our "anonymous" guy. - $result = DbtngExampleStorage::load(array('surname' => 'Anonymous')); + $result = DBTNGExampleStorage::load(array('surname' => 'Anonymous')); $this->drupalGet('/examples/dbtng_example'); $this->assertEqual( count($result), 1, 'Found one entry in the table with surname = "Anonymous".' @@ -136,14 +136,14 @@ class DbtngExampleTest extends WebTestBase { /** * Tests several combinations, adding entries, updating and deleting. */ - public function testDbtngExampleStorage() { + public function testDBTNGExampleStorage() { // Create a new entry. $entry = array( 'name' => 'James', 'surname' => 'Doe', 'age' => 23, ); - DbtngExampleStorage::insert($entry); + DBTNGExampleStorage::insert($entry); // Save another entry. $entry = array( @@ -151,22 +151,22 @@ class DbtngExampleTest extends WebTestBase { 'surname' => 'NotDoe', 'age' => 19, ); - DbtngExampleStorage::insert($entry); + DBTNGExampleStorage::insert($entry); // Verify that 4 records are found in the database. - $result = DbtngExampleStorage::load(); + $result = DBTNGExampleStorage::load(); $this->assertEqual( count($result), 4, 'Found a total of four entries in the table after creating two additional entries.' ); // Verify 2 of these records have 'Doe' as surname. - $result = DbtngExampleStorage::load(array('surname' => 'Doe')); + $result = DBTNGExampleStorage::load(array('surname' => 'Doe')); $this->assertEqual( count($result), 2, 'Found two entries in the table with surname = "Doe".' ); // Now find our not-Doe entry. - $result = DbtngExampleStorage::load(array('surname' => 'NotDoe')); + $result = DBTNGExampleStorage::load(array('surname' => 'NotDoe')); $this->assertEqual( count($result), 1, 'Found one entry in the table with surname "NotDoe' ); @@ -174,14 +174,14 @@ class DbtngExampleTest extends WebTestBase { $entry = $result[0]; $entry->surname = "NowDoe"; // update() returns the number of entries updated. - $this->assertNotEqual(DbtngExampleStorage::update((array) $entry), 0, "NotDoe updated to NowDoe."); + $this->assertNotEqual(DBTNGExampleStorage::update((array) $entry), 0, "NotDoe updated to NowDoe."); - $result = DbtngExampleStorage::load(array('surname' => 'NowDoe')); + $result = DBTNGExampleStorage::load(array('surname' => 'NowDoe')); $this->assertEqual( count($result), 1, "Found renamed 'NowDoe' surname"); // Read only John Doe entry. - $result = DbtngExampleStorage::load(array('name' => 'John', 'surname' => 'Doe')); + $result = DBTNGExampleStorage::load(array('name' => 'John', 'surname' => 'Doe')); $this->assertEqual( count($result), 1, 'Found one entry for John Doe.' ); @@ -190,11 +190,11 @@ class DbtngExampleTest extends WebTestBase { // Change age to 45. $entry['age'] = 45; // Update entry in database. - DbtngExampleStorage::update((array) $entry); + DBTNGExampleStorage::update((array) $entry); // Find entries with age = 45. // Read only John Doe entry. - $result = DbtngExampleStorage::load(array('surname' => 'NowDoe')); + $result = DBTNGExampleStorage::load(array('surname' => 'NowDoe')); $this->assertEqual( count($result), 1, 'Found one entry with surname = Nowdoe.' ); @@ -209,10 +209,10 @@ class DbtngExampleTest extends WebTestBase { ); // Delete the entry. - DbtngExampleStorage::delete($entry); + DBTNGExampleStorage::delete($entry); // Verify that now there are only 3 records. - $result = DbtngExampleStorage::load(); + $result = DBTNGExampleStorage::load(); $this->assertEqual( count($result), 3, 'Found only three records, a record was deleted.' );