diff --git a/dev_mvc/controller/db/DBTables.php b/dev_mvc/controller/db/DBTables.php new file mode 100644 index 0000000..9ba7206 --- /dev/null +++ b/dev_mvc/controller/db/DBTables.php @@ -0,0 +1,94 @@ +query( + " CREATE TABLE `users` ( + `ID` int(11) NOT NULL AUTO_INCREMENT, + `username` varchar(256) NOT NULL, + `email` varchar(256) NOT NULL, + `password` varchar(256) NOT NULL, + `reg_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `login_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `reg_ip` varchar(256) NOT NULL DEFAULT '127.0.0.1', + `permissions` int(11) NOT NULL DEFAULT '-1', + `active` tinyint(1) DEFAULT '0', + PRIMARY KEY (`ID`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + } + } + static function createEmailActivationKeyTable($con){ + $table = 'email_activation_keys'; + if(!self::checkTableExists($table, $con)){ + $query = $con->query( + " CREATE TABLE `email_activation_keys` ( + `id` int(16) NOT NULL AUTO_INCREMENT, + `users_id` int(16) NOT NULL, + `activationkey` varchar(256) NOT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + } + } + static function createBoardTable($con){ + $table = 'board'; + if(!self::checkTableExists($table, $con)){ + $query = $con->query( + " CREATE TABLE `board` ( + `ID` int(16) NOT NULL AUTO_INCREMENT, + `name` varchar(256) NOT NULL, + `description` text NOT NULL, + `permLevel` int(16) NOT NULL DEFAULT '0', + PRIMARY KEY (`ID`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + } + } + static function createThreadTable($con){ + $table = 'thread'; + if(!self::checkTableExists($table, $con)){ + $query = $con->query( + " CREATE TABLE `thread` ( + `ID` int(16) NOT NULL AUTO_INCREMENT, + `users_ID` int(16) NOT NULL, + `board_ID` int(16) NOT NULL, + `title` varchar(256) NOT NULL, + `text` text NOT NULL, + `date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`ID`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + } + } + static function createReplyTable($con){ + $table = 'reply'; + if(!self::checkTableExists($table, $con)){ + $query = $con->query( + " CREATE TABLE `reply` ( + `ID` int(16) NOT NULL AUTO_INCREMENT, + `thread_ID` int(16) NOT NULL, + `users_ID` int(16) NOT NULL, + `content` text NOT NULL, + `date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`ID`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + } + } + static function checkTableExists($table, $con){ + $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); + //table doesn't exist + if($query->fetchColumn() != 1){ + return false; + }else{ + return true; + } + } +} \ No newline at end of file diff --git a/dev_mvc/controller/db/Database.php b/dev_mvc/controller/db/Database.php index 99b0d06..d453b25 100644 --- a/dev_mvc/controller/db/Database.php +++ b/dev_mvc/controller/db/Database.php @@ -1,5 +1,8 @@ query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = $dbName"); + $result = (bool) $query; + if($result == 1){ + echo('db exists'); + } + else{ + $query = $con->query("CREATE DATABASE $dbName"); + DBTables::createAllTables(); + } + } /*** * ______ __ __ _____ _ _____ _______ _______ __ _______ _____ ____ _ _ * | ____| \/ | /\ |_ _| | /\ / ____|__ __|_ _\ \ / /\|__ __|_ _/ __ \| \ | |