From d3fdcf53bcaf4b143c316f3379190d0053a6036f Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 16 Sep 2021 22:19:07 +0200 Subject: Clean up, fix and rename db tables. This patch got a bit more involved than what was originally planned, but since we're messing with the tables I decided to do it all right away. - Moves the constraint definition to the CREATE TABLE statement for the concerts table. This replaces the existing KEY definition that it had. - Make sure the venues table is created before the concerts table so that the above mentioned constraint definition works. - Rename the tables. Use the wpdb-prefix and make the name a bit prettier. This caused some changes in the Concert and Venue classes, and for slightly silly reasons some test classes. The code actually turned out better (for the most part), but some refactoring can still be done. The column names remains unchanged for now. --- includes/concert.php | 73 +++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 35 deletions(-) (limited to 'includes/concert.php') diff --git a/includes/concert.php b/includes/concert.php index 102536b..8609694 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -37,22 +37,6 @@ if ( !class_exists('GiglogAdmin_Concert') ) { public const STATUS_ALL_APPROVED = 4; public const STATUS_REJECTED = 5; - // Table to translate from filter keys to db columns used by - // find_Concerts - private const KEY_TRANS_TABLE = [ - 'name' => 'wpgconcert_name', - 'date' => 'wpgconcert_date', - 'venue_id' => 'wpg_venues.id', - 'venue' => 'wpg_venues.wpgvenue_name', - 'city' => 'wpg_venues.wpgvenue_city', - 'currentuser' => 'wpgconcert_roles', - ]; - - private const BASE_QUERY = - 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name wpg_venues_wpgvenue_city ' - . 'FROM wpg_concerts ' - . 'LEFT JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id '; - /* * Constructs a new concert object from an array of attributes. @@ -99,19 +83,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { */ static function get( int $id ) : ?self { - global $wpdb; - - $query = self::BASE_QUERY - . 'WHERE ' . $wpdb->prepare('wpg_concerts.id = %d', $id); - - $res= $wpdb->get_row($query); - - if ( !$res) { - $wpdb->print_error( __METHOD__ ); - return null; - } - - return new GiglogAdmin_Concert($res); + return self::find_concerts(['id' => $id])[0]; } public static function create(string $name, int $venue_id, string $date, string $ticketlink, string $eventlink): ?self @@ -188,6 +160,21 @@ if ( !class_exists('GiglogAdmin_Concert') ) { return $need_update; } + // Table to translate from filter keys to db columns used by + // find_Concerts + private function translate_key($key) : string + { + return [ + 'id' => 'id', + 'name' => 'wpgconcert_name', + 'date' => 'wpgconcert_date', + 'venue_id' => $wpdb->prefix . 'giglogadmin_venues.id', + 'venue' => $wpdb->prefix . 'giglogadmin_venues.wpgvenue_name', + 'city' => $wpdb->prefix . 'giglogadmin_venues.wpgvenue_city', + 'currentuser' => 'wpgconcert_roles', + ][$key]; + } + /** * Return an array of concert objects optionally limited by a specified * filter. @@ -203,7 +190,22 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { global $wpdb; - $query = self::BASE_QUERY; + $ct = "{$wpdb->prefix}giglogadmin_concerts"; + $vt = "{$wpdb->prefix}giglogadmin_venues"; + + $query = "SELECT {$ct}.*, {$vt}.wpgvenue_name, {$vt}.wpgvenue_city " + . "FROM {$ct} " + . "LEFT JOIN {$vt} ON {$ct}.venue = {$vt}.id "; + + $keymap = [ + 'id' => $wpdb->prefix . 'giglogadmin_concerts.id', + 'name' => 'wpgconcert_name', + 'date' => 'wpgconcert_date', + 'venue_id' => $wpdb->prefix . 'giglogadmin_venues.id', + 'venue' => $wpdb->prefix . 'giglogadmin_venues.wpgvenue_name', + 'city' => $wpdb->prefix . 'giglogadmin_venues.wpgvenue_city', + 'currentuser' => 'wpgconcert_roles', + ]; $where = []; foreach( $filter as $key => $value ) { @@ -212,15 +214,16 @@ if ( !class_exists('GiglogAdmin_Concert') ) { case 'date': case 'venue': case 'city': - array_push($where, $wpdb->prepare(self::KEY_TRANS_TABLE[$key] . '=%s', $value)); + array_push($where, $wpdb->prepare($keymap[$key] . '=%s', $value)); break; + case 'id': case 'venue_id': - array_push($where, $wpdb->prepare(self::KEY_TRANS_TABLE[$key] . '=%d', $value)); + array_push($where, $wpdb->prepare($keymap[$key] . '=%d', $value)); break; case 'currentuser': - array_push($where , $wpdb->prepare(self::KEY_TRANS_TABLE[$key] . ' like "%%%s%%"', $value)); + array_push($where , $wpdb->prepare($keymap[$key] . ' like "%%%s%%"', $value)); break; } } @@ -251,10 +254,10 @@ if ( !class_exists('GiglogAdmin_Concert') ) { ]; if ( $this->id !== NULL ) { - $res = $wpdb->update( 'wpg_concerts', $columns, [ 'id' => $this->id ] ); + $res = $wpdb->update( $wpdb->prefix . 'giglogadmin_concerts', $columns, [ 'id' => $this->id ] ); } else { - $res = $wpdb->insert('wpg_concerts', $columns); + $res = $wpdb->insert( $wpdb->prefix . 'giglogadmin_concerts', $columns); } if ( $res === false ) { -- cgit v1.2.3