/**
  * db connect by mysqli
  */
 public static function connect()
 {
     //connect to db
     self::$db = new mysqli(\config::$db_server, \config::$db_username, \config::$db_userpass, \config::$db_name);
     // check connect
     if (self::$db->connect_errno) {
         die('MySQLi cann\'t connect with DataBase');
     }
 }
 /**
  * run select query by generated text query
  *
  * @param $query_text
  */
 public function run_select($query_text)
 {
     //$times = (int) \testing_config::$select ['times'];
     $times = $_GET['times'];
     $db = \hlkiller_core::db();
     $start_time = microtime(2);
     for ($i = 0; $i < $times; $i++) {
         try {
             $result = $db->query($query_text);
             if ($db->errno) {
                 throw new \Exceptions\MySQLQuery('Mysqli died. ' . $db->errno . ' : ' . $db->error);
             }
             $result_array = array();
             while ($tmp_array = $result->fetch_assoc()) {
                 $result_array[] = $tmp_array;
             }
         } catch (\Exceptions\MySQLQuery $e) {
             die($e->getMessage());
         }
     }
     $end_time = microtime(2);
     echo $end_time - $start_time, '<br>';
     \annex::showArray($result_array);
 }
 /**
  * clear data in database
  */
 public function clear_db()
 {
     $action = $_GET['action'];
     $_SESSION['exist'] = '0';
     foreach (\config::getTables() as $table) {
         try {
             $sql = "DROP TABLE IF EXISTS `" . $table['TABLE_NAME'] . "`";
             $result = \hlkiller_core::db()->query($sql);
             if ($result === false) {
                 echo $sql . ';';
                 throw new \Exceptions\MySQLQuery('Mysqli died.');
             }
         } catch (\Exceptions\MySQLQuery $e) {
             die($e->getMessage());
         }
     }
     switch ($action) {
         case 1:
             $this->add_structure_dump(PATH . 'test_dump1.sql');
             break;
         case 2:
             $this->add_structure_dump(PATH . 'test_dump2.sql');
             break;
     }
 }
 public static function queries_array($index = null)
 {
     $arr = array(function () {
         $users_q = \hlkiller_core::db()->query("SELECT `users_id` FROM `users` ORDER BY RAND() LIMIT 1");
         $users = $users_q->fetch_array(MYSQLI_ASSOC);
         $posts_q = \hlkiller_core::db()->query("SELECT `posts_id` FROM `posts` ORDER BY RAND() LIMIT 1");
         $posts = $posts_q->fetch_array(MYSQLI_ASSOC);
         $likes_q = \hlkiller_core::db()->query("SELECT COUNT(*) FROM `users_posts_like` WHERE `users_id`='{$users['users_id']}' AND `posts_id`='{$posts['posts_id']}'");
         $likes = $likes_q->fetch_array(MYSQLI_NUM);
         if ($likes[0]) {
             $result = \hlkiller_core::db()->query("DELETE FROM `users_posts_like` WHERE `users_id`='{$users['users_id']}' AND `posts_id`='{$posts['posts_id']}'");
         } else {
             $result = \hlkiller_core::db()->query("INSERT INTO `users_posts_like` (`users_id`,`posts_id`,`rel_users_posts_like_time`) VALUES ('{$users['users_id']}','{$posts['posts_id']}',UNIX_TIMESTAMP())");
         }
         return $result;
     }, function () {
         $users_q = \hlkiller_core::db()->query("SELECT `users_id` FROM `users` ORDER BY RAND() LIMIT 1");
         $users = $users_q->fetch_array(MYSQLI_ASSOC);
         $users2_q = \hlkiller_core::db()->query("SELECT `users_id` FROM `users` ORDER BY RAND() LIMIT 1");
         $users2 = $users2_q->fetch_array(MYSQLI_ASSOC);
         $likes_q = \hlkiller_core::db()->query("SELECT `following_id` FROM `rel_users_following` WHERE `users_follower_id`='{$users['users_id']}' AND `users_supplier_id`='{$users2['users_id']}'");
         $likes = $likes_q->fetch_array(MYSQLI_NUM);
         if ($likes[0]) {
             $result = \hlkiller_core::db()->query("DELETE FROM `rel_users_following` WHERE `following_id`='{$likes['0']}'");
         } else {
             $result = \hlkiller_core::db()->query("INSERT INTO `rel_users_following` (`users_follower_id`,`users_supplier_id`,`following_time`) VALUES ('{$users['users_id']}','{$users2['users_id']}',UNIX_TIMESTAMP())");
         }
         return $result;
     }, function () {
         $users_q = \hlkiller_core::db()->query("SELECT `users_id` FROM `users` ORDER BY RAND() LIMIT 1");
         $users = $users_q->fetch_array(MYSQLI_ASSOC);
         $posts_q = \hlkiller_core::db()->query("SELECT `posts_id` FROM `posts` ORDER BY RAND() LIMIT 1");
         $posts = $posts_q->fetch_array(MYSQLI_ASSOC);
         $text = \annex::gen_rnd_text();
         $result = \hlkiller_core::db()->query("INSERT INTO `rel_users_posts_comments` (`users_id`,`posts_id`,`rel_users_posts_comments_time`,`comments_text`) VALUES ('{$users['users_id']}','{$posts['posts_id']}',UNIX_TIMESTAMP(),'{$text}')");
         return $result;
     }, function () {
         $posts_q = \hlkiller_core::db()->query("SELECT `posts_id` FROM `posts` ORDER BY RAND() LIMIT 1");
         $posts = $posts_q->fetch_array(MYSQLI_ASSOC);
         $categories_q = \hlkiller_core::db()->query("SELECT `categories_id` FROM `categories` ORDER BY RAND() LIMIT 1");
         $categories = $categories_q->fetch_array(MYSQLI_ASSOC);
         $pc_q = \hlkiller_core::db()->query("SELECT `posts_categories_id` FROM `rel_posts_categories` WHERE `posts_id`='{$posts['posts_id']}' AND `categories_id`='{$categories['categories_id']}'");
         if ($pc_q->num_rows) {
             $result = \hlkiller_core::db()->query("DELETE FROM `rel_posts_categories` WHERE `posts_id`='{$posts['posts_id']}' AND `categories_id`='{$categories['categories_id']}'");
         } else {
             $result = \hlkiller_core::db()->query("INSERT INTO `rel_posts_categories` (`posts_id`,`categories_id`) VALUES ('{$posts['posts_id']}','{$categories['categories_id']}')");
         }
         return $result;
     }, function () {
         $name = \annex::gen_rnd_str(20);
         $pass = \annex::gen_rnd_str(25);
         $time = time();
         $result = \hlkiller_core::db()->query("INSERT INTO `users` (`users_username`,`users_password`,`users_created_on`) VALUES ('{$name}','{$pass}','{$time}')");
         return $result;
     });
     if (!is_null($index)) {
         return $arr[$index]();
     } else {
         return count($arr);
     }
 }