Guest User

Untitled

a guest
Mar 15th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. namespace framework::stores;
  2.  
  3. class MySQL implements framework::interfaces::iStore{
  4. //configuration options
  5. private $username = NULL;
  6. private $password = NULL;
  7. private $host = NULL;
  8. private $port = NULL;
  9. private $socket = NULL;
  10. private $database = NULL;
  11. private $connection = NULL;
  12.  
  13. //get an instance of this store
  14. public static function getStore(){
  15. return new framework::stores::MySQL();
  16. }
  17.  
  18. //set our configuration options for the store
  19. public function configure($options){
  20. $this->username = $options['username'];
  21. $this->password = $options['password'];
  22. $this->host = $options['host'];
  23. $this->port = $options['port'];
  24. $this->socket = $options['socket'];
  25. $this->database = $options['database'];
  26. }
  27.  
  28. //get our current connection
  29. private function getConnection() {
  30. if($this->connection === NULL) {
  31. $this->createConnection();
  32. }
  33. return $this->connection;
  34. }
  35.  
  36. //create a new connection
  37. private function createConnection() {
  38. $connection_string = ($this->host !== NULL ? $this->host : "") .
  39. ($this->port !== NULL && $this->host !== NULL ? ":" . $this->port : "") .
  40. ($this->host !== NULL && $this->socket !== NULL ? ":" : "") .
  41. ($this->socket !== NULL ? $this->socket : "");
  42.  
  43. $this->connection = mysql_connect(
  44. $connection_string,
  45. $this->username,
  46. $this->password
  47. );
  48. mysql_select_db($this->database, $this->connection);
  49. }
  50.  
  51. //get mysql rows based on a query
  52. public function getData($keyObj){
  53. $results = mysql_query($keyObj, $this->getConnection());
  54. return $results;
  55. }
  56.  
  57. //run a mysql query
  58. public function setData($keyObj, $valueObj, $duration = 0){
  59. mysql_query($keyObj, $this->getConnection());
  60. }
  61.  
  62. //convert a field to formats proper for this obj
  63. public function convertToStore($type, $value){
  64. return $value;
  65. }
  66.  
  67. //convert a field from formats from this obj
  68. public function convertFromStore($type, $value){
  69. switch($type) {
  70. case framework::DataTypes::TINYINT:
  71. case framework::DataTypes::SMALLINT:
  72. case framework::DataTypes::INT:
  73. case framework::DataTypes::BIGINT:
  74. $value = (int)$value;
  75. break;
  76. case framework::DataTypes::DATETIME:
  77. $value = strtotime($value);
  78. }
  79. return $value;
  80. }
  81. }
  82.  
  83. ?>
Add Comment
Please, Sign In to add comment