Guest User

Untitled

a guest
Jul 24th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Обертка над БД
  5. *
  6. * @author Fragster
  7. */
  8. class DB extends PDO {
  9.  
  10. /**
  11. *
  12. * @var DB
  13. */
  14. private static $singleton;
  15. private $engine;
  16. private $host;
  17. private $database;
  18. private $user;
  19. private $pass;
  20.  
  21. public function __construct() {
  22. $this->engine = 'mysql';
  23. $this->host = Config::$db_host;
  24. $this->database = Config::$db_database;
  25. $this->user = Config::$db_user;
  26. $this->pass = Config::$db_password;
  27. $dns = $this->engine . ':dbname=' . $this->database . ";host=" . $this->host;
  28. parent::__construct($dns, $this->user, $this->pass);
  29. $this->exec("SET NAMES utf8");
  30. $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  31. }
  32.  
  33. /**
  34. *
  35. * @return DB
  36. */
  37. static function Init() {
  38. if (!self::$singleton) {
  39. self::$singleton = new DB;
  40. }
  41. return self::$singleton;
  42. }
  43.  
  44.  
  45. public function arrayPrepare($query, array $input_parameters) {
  46. $query = str_replace("\n", "", $query);
  47.  
  48. $params = array();
  49. foreach ($input_parameters as $key => $value) {
  50. if (is_array($value)) {
  51. $implode = '';
  52. $delim = '';
  53. foreach ($value as $valueKey => $valueValue) {
  54. $params[$key . '_' . $valueKey] = $valueValue;
  55. $implode.= $delim . $key . '_' . $valueKey;
  56. $delim = ', ';
  57. }
  58. $query = str_replace($key, $implode, $query);
  59. } else {
  60. $params[$key] = $value;
  61. }
  62. }
  63.  
  64. try {
  65. $statement = $this->prepare($query);
  66. } catch (Exception $exc) {
  67. throw new Exception($query."\n".$this->errorInfo());
  68. }
  69.  
  70. //echo "$query\n";
  71. foreach ($params as $key => $value) {
  72. //echo "bind $key => $value\n";
  73. $statement->bindValue($key, $value)."\n";
  74. }
  75. return $statement;
  76. }
  77.  
  78. public function multiInsert($query, array $insert_array) {
  79. $query = str_replace("\n", "", $query);
  80.  
  81. $query1 = array();
  82. preg_match('/.*values\s*/im', $query, $query1);
  83. $query1 = $query1[0];
  84.  
  85. $placeholders = array();
  86. preg_match_all('/:\w+/im', $query, $placeholders);
  87. $placeholders = $placeholders[0];
  88. $rowDelim = '';
  89. foreach ($insert_array as $key => $value) {
  90. $query1.= $rowDelim . '(';
  91. $rowDelim = ', ';
  92. $fieldDelim = '';
  93. foreach ($placeholders as $placeholder) {
  94. $query1.= $fieldDelim . $placeholder . '_' . $key;
  95. $fieldDelim = ', ';
  96. }
  97. $query1.= ')';
  98. }
  99.  
  100. try {
  101. $statement = $this->prepare($query1);
  102. } catch (Exception $exc) {
  103. throw new Exception($query1."\n".$this->errorInfo());
  104. }
  105.  
  106. foreach ($insert_array as $key => $value) {
  107. foreach ($placeholders as $placeholder) {
  108. $var = $value[$placeholder];
  109. $statement->bindValue($placeholder . '_' . $key, $var);
  110. }
  111. }
  112.  
  113. $guids = array();
  114. foreach ($insert_array as $key => $value) {
  115. $guids[]=$value[':guid'];
  116. }
  117.  
  118. //throw new Exception(print_r($guids, true));
  119. $statement->execute();
  120. }
  121.  
  122. }
  123.  
  124. ?>
Add Comment
Please, Sign In to add comment