Skip to content
Browse files

Added first test query to the SQL class. Still requires test cases to…

… be written.
  • Loading branch information...
1 parent 1bab7ab commit e6a60b1b9460884eafe127e8dbe26fef46663048 @b-turchyn committed May 24, 2011
Showing with 57 additions and 17 deletions.
  1. +57 −17 src/classes/sql.php
View
74 src/classes/sql.php
@@ -19,29 +19,69 @@
************************************************************************/
class MySQLQueries {
+ // Private instance variables
+ private $conn = null; // Connection holder
+ private $prefix = ""; // Database table prefixes
+
+
// List of all eras
- private const $getEras =
- "SELECT * FROM ?eras ORDER BY name ASC";
+ const getEras = "SELECT * FROM ?eras ORDER BY name ASC";
// Count number of first names
- private const $getFirstNameCount =
- "SELECT COUNT(*) FROM ?names " +
- "WHERE isfirstname_sw = true " +
- "AND deleted_dt IS NOT NULL";
+ const getFirstNameCount = <<<'EOT'
+ SELECT COUNT(*) FROM ?names
+ WHERE isfirstname_sw = true
+ AND era = ?
+ AND ( gender = ? OR gender = ? )
+ AND deleted_dt IS NOT NULL
+EOT;
// Count number of last names
- private const $getLastNameCount =
- "SELECT COUNT(*) FROM ?names " +
- "WHERE isfirstname_sw = false " +
- "AND deleted_dt IS NOT NULL";
+ const getLastNameCount = <<<'EOT'
+ SELECT COUNT(*) FROM ?names
+ WHERE isfirstname_sw = false
+ AND era = ?
+ AND ( gender = ? OR gender = ? )
+ AND deleted_dt IS NOT NULL
+EOT;
// Retrieve first and last name from a random index
- private const $getFullName =
- "SELECT name FROM ?names " +
- "WHERE isfirstname_sw = true " +
- "LIMIT ?, 1 " +
- "UNION SELECT name FROM ?names " +
- "WHERE isfirstname_sw = false " +
- "LIMIT ?, 1 "
+ const getFullName = <<<'EOT'
+ SELECT name FROM ?names
+ WHERE isfirstname_sw = true
+ LIMIT ?, 1
+ UNION SELECT name FROM ?names
+ WHERE isfirstname_sw = false
+ LIMIT ?, 1
+EOT;
+
+ function __construct($server, $user, $password, $database) {
+ $this->conn = new mysqli($server, $user, $password, $database);
+
+ // Check for error
+ if(mysqli_connect_errno()) {
+ printf("Connect failed: %s\n", mysqli_connect_error());
+ }
+ }
+
+ function __destruct() {
+ if($this->conn != null)
+ $this->conn->close();
+ }
+
+ public function getFirstNameCount($era, $gender) {
+ $result = false;
+ if( $stmt = $this->conn->prepare(self::getFirstNameCount) ) {
+ $stmt->bind_param("siss", $this->prefix, $era, $gender, 'B');
+ $stmt->execute();
+ // Retrieve the result, and return false on failure.
+ $stmt->bind_result($res);
+ if ( $stmt->fetch() ) {
+ $result = $res;
+ }
+ }
+
+ return $result;
+ }
}
?>

0 comments on commit e6a60b1

Please sign in to comment.
Something went wrong with that request. Please try again.