Browse Source

Create tables with prefix (#38 #214 #196)

Tobias Reich 10 years ago
parent
commit
9f11875e39

+ 1 - 1
php/database/albums_table.sql

@@ -2,7 +2,7 @@
 # Version 2.5
 # ------------------------------------------------------------
 
-CREATE TABLE IF NOT EXISTS `lychee_albums` (
+CREATE TABLE IF NOT EXISTS `?` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `title` varchar(50) NOT NULL,
   `description` varchar(1000) DEFAULT '',

+ 1 - 1
php/database/log_table.sql

@@ -2,7 +2,7 @@
 # Version 2.5
 # ------------------------------------------------------------
 
-CREATE TABLE IF NOT EXISTS `lychee_log` (
+CREATE TABLE IF NOT EXISTS `?` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `time` int(11) NOT NULL,
   `type` varchar(11) NOT NULL,

+ 1 - 1
php/database/photos_table.sql

@@ -2,7 +2,7 @@
 # Version 2.5
 # ------------------------------------------------------------
 
-CREATE TABLE IF NOT EXISTS `lychee_photos` (
+CREATE TABLE IF NOT EXISTS `?` (
   `id` bigint(14) NOT NULL,
   `title` varchar(50) NOT NULL,
   `description` varchar(1000) DEFAULT '',

+ 1 - 1
php/database/settings_content.sql

@@ -2,7 +2,7 @@
 # Version 2.5
 # ------------------------------------------------------------
 
-INSERT INTO `lychee_settings` (`key`, `value`)
+INSERT INTO `?` (`key`, `value`)
 VALUES
   ('version',''),
   ('username',''),

+ 1 - 1
php/database/settings_table.sql

@@ -2,7 +2,7 @@
 # Version 2.5
 # ------------------------------------------------------------
 
-CREATE TABLE IF NOT EXISTS `lychee_settings` (
+CREATE TABLE IF NOT EXISTS `?` (
   `key` varchar(50) NOT NULL DEFAULT '',
   `value` varchar(200) DEFAULT ''
 ) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

+ 25 - 10
php/modules/Database.php

@@ -29,7 +29,8 @@ class Database extends Module {
 			if (!Database::createDatabase($database, $name)) exit('Error: Could not create database!');
 
 		# Check tables
-		if (!$database->query('SELECT * FROM lychee_photos, lychee_albums, lychee_settings, lychee_log LIMIT 0;'))
+		$query = Database::prepare($database, 'SELECT * FROM ?, ?, ?, ? LIMIT 0', [LYCHEE_TABLE_PHOTOS, LYCHEE_TABLE_ALBUMS, LYCHEE_TABLE_SETTINGS, LYCHEE_TABLE_LOG]);
+		if (!$database->query($query))
 			if (!Database::createTables($database)) exit('Error: Could not create tables!');
 
 		return $database;
@@ -140,30 +141,36 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 		Module::dependencies(isset($database));
 
 		# Create log
-		if (!$database->query('SELECT * FROM lychee_log LIMIT 0;')) {
+		$exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', [LYCHEE_TABLE_LOG]);
+		if (!$database->query($exist)) {
 
 			# Read file
 			$file	= __DIR__ . '/../database/log_table.sql';
 			$query	= @file_get_contents($file);
 
-			# Create table
 			if (!isset($query)||$query===false) return false;
+
+			# Create table
+			$query = Database::prepare($database, $query, [LYCHEE_TABLE_LOG]);
 			if (!$database->query($query)) return false;
 
 		}
 
 		# Create settings
-		if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
+		$exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', [LYCHEE_TABLE_SETTINGS]);
+		if (!$database->query($exist)) {
 
 			# Read file
 			$file	= __DIR__ . '/../database/settings_table.sql';
 			$query	= @file_get_contents($file);
 
-			# Create table
 			if (!isset($query)||$query===false) {
 				Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_settings');
 				return false;
 			}
+
+			# Create table
+			$query = Database::prepare($database, $query, [LYCHEE_TABLE_SETTINGS]);
 			if (!$database->query($query)) {
 				Log::error($database, __METHOD__, __LINE__, $database->error);
 				return false;
@@ -173,11 +180,13 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 			$file	= __DIR__ . '/../database/settings_content.sql';
 			$query	= @file_get_contents($file);
 
-			# Add content
 			if (!isset($query)||$query===false) {
 				Log::error($database, __METHOD__, __LINE__, 'Could not load content-query for lychee_settings');
 				return false;
 			}
+
+			# Add content
+			$query = Database::prepare($database, $query, [LYCHEE_TABLE_SETTINGS]);
 			if (!$database->query($query)) {
 				Log::error($database, __METHOD__, __LINE__, $database->error);
 				return false;
@@ -186,17 +195,20 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 		}
 
 		# Create albums
-		if (!$database->query('SELECT * FROM lychee_albums LIMIT 0;')) {
+		$exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', [LYCHEE_TABLE_ALBUMS]);
+		if (!$database->query($exist)) {
 
 			# Read file
 			$file	= __DIR__ . '/../database/albums_table.sql';
 			$query	= @file_get_contents($file);
 
-			# Create table
 			if (!isset($query)||$query===false) {
 				Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_albums');
 				return false;
 			}
+
+			# Create table
+			$query = Database::prepare($database, $query, [LYCHEE_TABLE_ALBUMS]);
 			if (!$database->query($query)) {
 				Log::error($database, __METHOD__, __LINE__, $database->error);
 				return false;
@@ -205,17 +217,20 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 		}
 
 		# Create photos
-		if (!$database->query('SELECT * FROM lychee_photos LIMIT 0;')) {
+		$exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', [LYCHEE_TABLE_PHOTOS]);
+		if (!$database->query($exist)) {
 
 			# Read file
 			$file	= __DIR__ . '/../database/photos_table.sql';
 			$query	= @file_get_contents($file);
 
-			# Create table
 			if (!isset($query)||$query===false) {
 				Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_photos');
 				return false;
 			}
+
+			# Create table
+			$query = Database::prepare($database, $query, [LYCHEE_TABLE_PHOTOS]);
 			if (!$database->query($query)) {
 				Log::error($database, __METHOD__, __LINE__, $database->error);
 				return false;