Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace framework::stores;
- class MySQL implements framework::interfaces::iStore{
- //configuration options
- private $username = NULL;
- private $password = NULL;
- private $host = NULL;
- private $port = NULL;
- private $socket = NULL;
- private $database = NULL;
- private $connection = NULL;
- //get an instance of this store
- public static function getStore(){
- return new framework::stores::MySQL();
- }
- //set our configuration options for the store
- public function configure($options){
- $this->username = $options['username'];
- $this->password = $options['password'];
- $this->host = $options['host'];
- $this->port = $options['port'];
- $this->socket = $options['socket'];
- $this->database = $options['database'];
- }
- //get our current connection
- private function getConnection() {
- if($this->connection === NULL) {
- $this->createConnection();
- }
- return $this->connection;
- }
- //create a new connection
- private function createConnection() {
- $connection_string = ($this->host !== NULL ? $this->host : "") .
- ($this->port !== NULL && $this->host !== NULL ? ":" . $this->port : "") .
- ($this->host !== NULL && $this->socket !== NULL ? ":" : "") .
- ($this->socket !== NULL ? $this->socket : "");
- $this->connection = mysql_connect(
- $connection_string,
- $this->username,
- $this->password
- );
- mysql_select_db($this->database, $this->connection);
- }
- //get mysql rows based on a query
- public function getData($keyObj){
- $results = mysql_query($keyObj, $this->getConnection());
- return $results;
- }
- //run a mysql query
- public function setData($keyObj, $valueObj, $duration = 0){
- mysql_query($keyObj, $this->getConnection());
- }
- //convert a field to formats proper for this obj
- public function convertToStore($type, $value){
- return $value;
- }
- //convert a field from formats from this obj
- public function convertFromStore($type, $value){
- switch($type) {
- case framework::DataTypes::TINYINT:
- case framework::DataTypes::SMALLINT:
- case framework::DataTypes::INT:
- case framework::DataTypes::BIGINT:
- $value = (int)$value;
- break;
- case framework::DataTypes::DATETIME:
- $value = strtotime($value);
- }
- return $value;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment