Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c7bb1d1b20
Fetching contributors…

Cannot retrieve contributors at this time

file 206 lines (179 sloc) 5.945 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
<?php
/************************************************************************
* Call of Cthulhu Character Generator
* Copyright (C) 2011 Brian Turchyn, Glen Conolly, Tyler Omichinski
* All references to commercial items copyright their respective owners.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class MySQLQueries extends mysqli {
  // Private instance variables
  private $conn = null; // Connection holder
  private $prefix = ""; // Database table prefixes


  // List of all eras
  const getEras = "SELECT * FROM ?eras ORDER BY name ASC";

  // Count number of first names
  const getFirstNameCount =
    "SELECT COUNT(*) FROM %snames
WHERE isfirst = true
AND gender = ?
AND deleted_dt IS NULL";

  // Count number of last names
  const getLastNameCount =
    "SELECT COUNT(*) FROM %snames
WHERE isfirst = false
AND deleted_dt IS NULL";

// Retrieves a specific first name
const getFirstName =
"SELECT name FROM %snames
WHERE isfirst = true
AND gender = ?
LIMIT ?, 1";

// Retrieves a specific last name
const getLastName =
"SELECT name FROM %snames
WHERE isfirst = false
LIMIT ?, 1";

  public function __construct($host, $user, $pass, $database, $prefix) {

$this->prefix = $prefix;

    parent::__construct($host, $user, $pass, $database);
    // Check for errors
    if(mysqli_connect_error()) {
      die("Connection error (" . mysqli_connect_errno() . ") " . mysqli_connect_error());
    }
  }

  public function __destruct() {
mysqli_close($this);
}

/**
* Retrieves the number of first names of a particular gender in the database
*
* @param string $gender 'M' or 'F' (case-sensitive)
* @return integer >= 0 on success, false on failure
* @author Brian Turchyn
*/
  public function getFirstNameCount($gender) {
    $result = false;
$stmt = null;
    if( $stmt = $this->prepare($this->preparePrefix(self::getFirstNameCount) ) ) {
$both = 'B';
      $stmt->bind_param("s", $gender);
      $stmt->execute();
      // Retrieve the result, and return false on failure.
      $stmt->bind_result($res);
      if ( $stmt->fetch() ) {
        $result = $res;
      }
    } else echo $this->error;

$stmt->close();

    return $result;
  }

/**
* Retrieves the number of last names in the database
*
* @return integer >= 0 on success, false on failure
* @author Brian Turchyn
*/
public function getLastNameCount() {
    $result = false;
$stmt = null;
    if( $stmt = $this->prepare($this->preparePrefix(self::getLastNameCount) ) ) {
      //$stmt->bind_param("ss", $gender, $both);
      $stmt->execute();
      // Retrieve the result, and return false on failure.
      $stmt->bind_result($res);
      if ( $stmt->fetch() ) {
        $result = $res;
      }
    } else echo $this->error;

$stmt->close();

    return $result;
  }

/**
* Retrieves a specific first name from the database
*
* @param char $gender Either 'M' or 'F' (case-sensitive)
* @param integer $index Optional specific index
* @return string on success, boolean false on failure
* @author Brian Turchyn
*/
public function getFirstName($gender, $index = null) {
$result = false;
$stmt = null;

// Check input. If no indexes for first name is given, generate a random value
if ( $index == null ) {
$index = mt_rand(1, $this->getFirstNameCount($gender));
}
    if( $stmt = $this->prepare($this->preparePrefix(self::getFirstName) ) ) {
      $stmt->bind_param("si", $gender, $index);
      $stmt->execute();
      // Retrieve the result, and return false on failure.
      $stmt->bind_result($res);
      if ( $stmt->fetch() ) {
        $result = $res;
      }
    } else echo $this->error;

$stmt->close();

return $result;
}

/**
* Retrieves a specific last name from the database
*
* @param integer $index
* @return string on success, boolean false on failure
* @author Brian Turchyn
*/
public function getLastName($index = null) {
$result = false;
$stmt = null;

// Check input. If no index for last name is given, generate a random value
if ( $index == null ) {
$index = mt_rand(1, $this->getLastNameCount());
}
    if( $stmt = $this->prepare($this->preparePrefix(self::getLastName) ) ) {
      $stmt->bind_param("i", $index);
      $stmt->execute();
      // Retrieve the result, and return false on failure.
      $stmt->bind_result($res);
      if ( $stmt->fetch() ) {
        $result = $res;
      }
    } else echo $this->error;

$stmt->close();

return $result;
}

/**
* Generates a full name, either a specific index or a random one
*
* @param char $gender Either 'M' or 'F' (case-sensitive)
* @param integer $first optional first name index
* @param integer $last optional last name index
* @return string on success, false on failure
* @author Brian Turchyn
*/
public function getFullName($gender, $first = null, $last = null) {
    return $this->getFirstName($gender, $first) . " " . $this->getLastName($last);
  }

/**
* Prepares the query to populate the DB table prefixes
*
* @param string $query
* @return string query with the database prefix
* @author Brian Turchyn
*/
private function preparePrefix($query) {
return preg_replace("/%s/", $this->prefix, $query);
}
}
?>
Something went wrong with that request. Please try again.