7e8819b6915f58b835d2b5e02a1ff20a533cda83.svn-base 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php namespace Maze\Data\Mysql;
  2. use Maze\Debug\Process as Debug;
  3. class Connect
  4. {
  5. /**
  6. * handle
  7. *
  8. * @var object
  9. */
  10. private $handle;
  11. /**
  12. * instance
  13. *
  14. * @var string
  15. */
  16. static protected $instance;
  17. /**
  18. * getInstance
  19. *
  20. * @return Maze\Data\Mysql\Connect;
  21. */
  22. static public function getInstance($config)
  23. {
  24. $key = $config['host'] . $config['database'];
  25. if(empty(self::$instance[$key]))
  26. {
  27. self::$instance[$key] = new self();
  28. self::$instance[$key]->init($config);
  29. }
  30. return self::$instance[$key];
  31. }
  32. /**
  33. * init
  34. *
  35. * @return mixd
  36. */
  37. private function init($config)
  38. {
  39. if(strpos($config['host'], ':') !== false) list($config['host'], $config['port']) = explode(':', $config['host']);
  40. $dsn['type'] = $config['type'];
  41. $dsn['host'] = $config['host'];
  42. $dsn['port'] = $config['port'];
  43. $dsn['dbname'] = $config['database'];
  44. $dsn['charset'] = $config['charset'];
  45. foreach($dsn as $key => $val)
  46. {
  47. $dsn[$key] = "$key=$val";
  48. }
  49. $dsnList = 'mysql:' . implode(';', $dsn);
  50. try
  51. {
  52. $this->handle = new \PDO($dsnList, $config['username'], $config['password']);
  53. //$this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  54. $this->handle->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_NATURAL);
  55. $this->handle->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
  56. Debug::log('db ' . $config['host'] . ' connected');
  57. }
  58. catch(\PDOException $e)
  59. {
  60. if(strstr($e->getMessage(),'Unknown database'))
  61. {
  62. $link = @mysql_connect($config['host'], $config['username'], $config['password']);
  63. @mysql_query("CREATE DATABASE `".$config['database']."` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;", $link);
  64. @mysql_close($link);
  65. $this->init($config);
  66. }
  67. else
  68. {
  69. echo $e->getMessage();
  70. }
  71. }
  72. //$this->query("set names '".$config['charset']."'");
  73. //$this->_log('connected mysql:' . $config['host']);
  74. }
  75. /**
  76. * __construct
  77. *
  78. * @return mixd
  79. */
  80. public function __destruct()
  81. {
  82. $this->close();
  83. }
  84. /**
  85. * handle
  86. *
  87. * @return object
  88. */
  89. public function handle()
  90. {
  91. return $this->handle;
  92. }
  93. /**
  94. * close
  95. *
  96. * @return mixd
  97. */
  98. public function close()
  99. {
  100. $this->handle = null;
  101. }
  102. /**
  103. * prepare
  104. *
  105. * @return object
  106. */
  107. public function prepare($sql)
  108. {
  109. return $this->handle->prepare($sql);
  110. }
  111. /**
  112. * exec
  113. *
  114. * @return object
  115. */
  116. public function exec($sql)
  117. {
  118. return $this->handle->exec($sql);
  119. }
  120. /**
  121. * query
  122. *
  123. * @return object
  124. */
  125. public function query($sql)
  126. {
  127. return $this->handle->query($sql);
  128. }
  129. /**
  130. * lastid
  131. *
  132. * @return int
  133. */
  134. public function id()
  135. {
  136. return $this->handle->lastInsertId();
  137. }
  138. }