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/venue.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'includes/venue.php') diff --git a/includes/venue.php b/includes/venue.php index 47788d1..177ba2d 100644 --- a/includes/venue.php +++ b/includes/venue.php @@ -54,7 +54,7 @@ if ( !class_exists('GiglogAdmin_Venue') ) { { global $wpdb; - $query = $wpdb->prepare('SELECT * from wpg_venues WHERE id = %d', $id); + $query = $wpdb->prepare("SELECT * from {$wpdb->prefix}giglogadmin_venues WHERE id = %d", $id); $results = $wpdb->get_results($query); return $results ? new GiglogAdmin_Venue($results[0]) : NULL; @@ -74,7 +74,9 @@ if ( !class_exists('GiglogAdmin_Venue') ) { static function find_or_create(string $name, string $city = 'Oslo'): self { global $wpdb; - $venuesql = 'SELECT * FROM wpg_venues WHERE upper(wpgvenue_name)=upper("' . $name . '")'.' and wpgvenue_city="'.$city.'"'; + $venuesql = "SELECT * FROM {$wpdb->prefix}giglogadmin_venues " + . $wpdb->prepare("WHERE upper(wpgvenue_name)=upper(%s) and wpgvenue_city=%s", $name, $city); + $results = $wpdb->get_results($venuesql); if ($results) { @@ -88,7 +90,8 @@ if ( !class_exists('GiglogAdmin_Venue') ) { static function all_cities(): array { global $wpdb; - $results = $wpdb->get_results('select distinct wpgvenue_city from wpg_venues'); + $results = $wpdb->get_results( + "select distinct wpgvenue_city from {$wpdb->prefix}giglogadmin_venues"); return array_map(function ($r) { return $r->wpgvenue_city; }, $results); } @@ -102,7 +105,8 @@ if ( !class_exists('GiglogAdmin_Venue') ) { { global $wpdb; - $results = $wpdb->get_results("select * from wpg_venues ORDER BY wpgvenue_name"); + $results = $wpdb->get_results( + "select * from {$wpdb->prefix}giglogadmin_venues ORDER BY wpgvenue_name"); return array_map(function ($r) { return new GiglogAdmin_Venue($r); }, $results); } @@ -116,8 +120,8 @@ if ( !class_exists('GiglogAdmin_Venue') ) { static function venues_in_city(string $city): array { global $wpdb; - $q = $wpdb->prepare("select * from wpg_venues where wpgvenue_city=%s", $city); - $q .=" ORDER BY wpgvenue_name"; + $q = $wpdb->prepare("select * from {$wpdb->prefix}giglogadmin_venues where wpgvenue_city=%s", $city) + . " ORDER BY wpgvenue_name"; $results = $wpdb->get_results($q); return array_map(function ($r) { return new GiglogAdmin_Venue($r); }, $results); @@ -127,7 +131,7 @@ if ( !class_exists('GiglogAdmin_Venue') ) { { global $wpdb; - $wpdb->insert('wpg_venues', array( + $wpdb->insert($wpdb->prefix . 'giglogadmin_venues', array( 'id' => '', 'wpgvenue_name' => $this->name, 'wpgvenue_city' => $this->city, -- cgit v1.2.3