Permalink
Browse files

Add MySQL port config. Closes #37.

  • Loading branch information...
1 parent 7192df2 commit 6e7f4604b14fbea058ebdb4cc6da2c331d7f510e @NSinopoli NSinopoli committed Mar 1, 2012
Showing with 43 additions and 42 deletions.
  1. +1 −0 config.php
  2. +2 −0 index.php
  3. +40 −42 lib/PDO_MySQL.php
View
1 config.php
@@ -76,6 +76,7 @@
// The database settings
define('DATABASE_NAME', 'vpu');
define('DATABASE_HOST', 'localhost');
+define('DATABASE_PORT', '3306');
define('DATABASE_USER', 'root');
define('DATABASE_PASS', 'admin');
View
2 index.php
@@ -110,6 +110,7 @@ function get_snapshots() {
$config = array(
'database' => DATABASE_NAME,
'host' => DATABASE_HOST,
+ 'port' => DATABASE_PORT,
'username' => DATABASE_USER,
'password' => DATABASE_PASS
);
@@ -152,6 +153,7 @@ function get_snapshots() {
$config = array(
'database' => DATABASE_NAME,
'host' => DATABASE_HOST,
+ 'port' => DATABASE_PORT,
'username' => DATABASE_USER,
'password' => DATABASE_PASS
);
View
82 lib/PDO_MySQL.php
@@ -38,7 +38,7 @@
class PDO_MySQL {
/**
- * The db handle.
+ * The db handle.
*
* @var object
* @access protected
@@ -65,40 +65,38 @@ class PDO_MySQL {
* Loads the configuration settings for a MySQL connection and connects
* to the database.
*
- * @param array $config The configuration settings, which can take four options:
- * `database` - The name of the database.
- * `host` - The database host.
- * `username` - The database username.
- * `password` - The database password.
- * (By default, instances are destroyed at the end of the request.)
+ * @param array $config The configuration settings, which takes
+ * five options:
+ * `database` - The name of the database.
+ * `host` - The database host.
+ * `port` - The database port.
+ * `username` - The database username.
+ * `password` - The database password.
+ * (By default, instances are destroyed at the
+ * end of the request.)
* @access public
* @return void
*/
- public function __construct(array $config = array()) {
- $defaults = array(
- 'database' => DATABASE_NAME,
- 'host' => DATABASE_HOST,
- 'username' => DATABASE_USER,
- 'password' => DATABASE_PASS
- );
- $config += $defaults;
+ public function __construct($config) {
$this->connect($config);
}
/**
* Connects and selects database.
*
- * @param array $options Contains the connection information.
+ * @param array $options Contains the connection information.
* Takes the following options:
- * `database` - The name of the database.
- * `host` - The database host.
- * `username` - The database username.
- * `password` - The database password.
+ * `database` - The name of the database.
+ * `host` - The database host.
+ * `port` - The database port.
+ * `username` - The database username.
+ * `password` - The database password.
* @access public
* @return bool
*/
public function connect($options) {
- $dsn = 'mysql:host=' . $options['host'] . ';dbname=' . $options['database'];
+ $dsn = 'mysql:host=' . $options['host'] . ';port=' . $options['port']
+ . ';dbname=' . $options['database'];
try {
$this->_dbh = new \PDO($dsn, $options['username'], $options['password']);
$this->_dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
@@ -131,15 +129,15 @@ public function close() {
$this->_dbh = null;
return true;
}
-
+
/**
* Deletes a record from the database.
*
* @see PDO_MySQL->_format_where()
* @param string $table The table containing the record to be deleted.
* @param string|array $where The WHERE clause to be included in the DELETE query.
* @access public
- * @return bool
+ * @return bool
*/
public function delete($table, $where = null) {
$sql = 'DELETE FROM `' . $table . '`';
@@ -151,7 +149,7 @@ public function delete($table, $where = null) {
return $this->query($sql, $where);
}
}
-
+
/**
* Fetches the next row from the result set in memory (i.e., the one
* that was created after running query()).
@@ -164,7 +162,7 @@ public function fetch($fetch_style = null) {
$this->_set_fetch_mode($fetch_style);
$row = $this->_statement->fetch();
$this->_statement->closeCursor();
- return $row;
+ return $row;
}
/**
@@ -178,7 +176,7 @@ public function fetch_all($fetch_style = null) {
$this->_set_fetch_mode($fetch_style);
$rows = $this->_statement->fetchAll();
$this->_statement->closeCursor();
- return $rows;
+ return $rows;
}
/**
@@ -191,14 +189,14 @@ public function fetch_all($fetch_style = null) {
public function fetch_column($column_number = 0) {
$column = $this->_statement->fetchColumn($column_number);
$this->_statement->closeCursor();
- return $column;
+ return $column;
}
/**
* Performs a `SELECT FROM` query.
*
* @see PDO_MySQL->query()
- * @see PDO_MySQL->fetch()
+ * @see PDO_MySQL->fetch()
* @see PDO_MySQL->_format_where()
* @param string|array $fields The fields to be retrieved.
* @param string $table The table to SELECT from.
@@ -220,7 +218,7 @@ public function find($fields, $table, $where = null, $additional = null) {
if ( !is_null($additional) ) {
$sql .= ' ' . $additional;
}
- $this->query($sql, $where);
+ $this->query($sql, $where);
}
/**
@@ -297,7 +295,7 @@ protected function _format_where(&$where = null) {
}
/**
- * Inserts a record into the database.
+ * Inserts a record into the database.
*
* @param string $table The table containing the record to be inserted.
* @param array $data An array containing the data to be inserted. Format
@@ -308,11 +306,11 @@ protected function _format_where(&$where = null) {
*/
public function insert($table, $data) {
$sql = 'INSERT INTO `' . $table . '` ';
-
+
$key_names = array_keys($data);
$fields = '`' . implode('`, `', $key_names) . '`';
$values = ':' . implode(', :', $key_names);
-
+
$sql .= '(' . $fields . ') VALUES (' . $values . ')';
$statement = $this->_dbh->prepare($sql);
@@ -344,7 +342,7 @@ public function insert_id() {
* Returns the number of rows affected by the last SELECT query.
*
* @access public
- * @return int
+ * @return int
*/
public function num_rows() {
$this->query('SELECT FOUND_ROWS()');
@@ -358,7 +356,7 @@ public function num_rows() {
* @param string $sql The SQL query to be executed.
* @param array $parameters An array containing the parameters to be bound.
* @access public
- * @return bool
+ * @return bool
*/
public function query($sql, $parameters = null) {
$statement = $this->_dbh->prepare($sql);
@@ -376,7 +374,7 @@ public function query($sql, $parameters = null) {
echo var_dump($e->getTrace());
die('</pre>');
}
-
+
$this->_affected_rows = $statement->rowCount();
$this->_statement = $statement;
return true;
@@ -388,7 +386,7 @@ public function query($sql, $parameters = null) {
* @param string $sql The SQL query to be executed.
* @param array $parameters An array containing the parameters to be bound.
* @access public
- * @return mixed
+ * @return mixed
*/
public function query_first($sql, $parameters = null) {
$this->query($sql . ' LIMIT 1', $parameters);
@@ -399,7 +397,7 @@ public function query_first($sql, $parameters = null) {
*
* @param string $fetch_style Controls how the rows will be returned.
* @access protected
- * @return int
+ * @return int
*/
protected function _set_fetch_mode($fetch_style) {
switch ( $fetch_style ) {
@@ -435,7 +433,7 @@ protected function _set_fetch_mode($fetch_style) {
* be as follows:
* array('column_name' => 'column_value');
* @access public
- * @return bool
+ * @return bool
*/
public function update($table, $data, $where = null) {
$sql = 'UPDATE `' . $table . '` SET ';
@@ -464,7 +462,7 @@ public function update($table, $data, $where = null) {
echo var_dump($e->getTrace());
die('</pre>');
}
-
+
$this->_affected_rows = $statement->rowCount();
return true;
}
@@ -477,7 +475,7 @@ public function update($table, $data, $where = null) {
* should be as follows:
* array('column_name' => 'column_value');
* @access public
- * @return bool
+ * @return bool
*/
public function upsert($table, $data) {
$sql = 'INSERT INTO `' . $table . '` ';
@@ -486,7 +484,7 @@ public function upsert($table, $data) {
$fields = '`' . implode('`, `', $key_names) . '`';
$values = ':' . implode(', :', $key_names);
-
+
$sql .= '(' . $fields . ') VALUES (' . $values . ') ON DUPLICATE KEY UPDATE ';
foreach ( $key_names as $name ) {
@@ -506,7 +504,7 @@ public function upsert($table, $data) {
}
$this->_affected_rows = $statement->rowCount();
- return true;
+ return true;
}
}

0 comments on commit 6e7f460

Please sign in to comment.