Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SqlCollector.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
20  private $resourceConnection;
21 
25  private $sql = [];
26 
30  private $profiler;
31 
35  public function __construct(ResourceConnection $resourceConnection)
36  {
37  $this->resourceConnection = $resourceConnection;
38  }
39 
45  private function addSql($sql, $bind)
46  {
47  preg_match('~(?:INSERT|REPLACE)\s+(?:IGNORE)?\s*INTO `(.*)` \((.*)\) VALUES (\(.*\))+~', $sql, $queryMatches);
48  if ($queryMatches) {
49  $table = $queryMatches[1];
50  $fields = preg_replace('~[\s+`]+~', '', $queryMatches[2]);
51  $fields = $fields ? explode(',', $fields) : [];
52  $sqlBindGroupAmount = count(explode('), (', $queryMatches[3]));
53  preg_match(' ~\((.*?)\)~', $queryMatches[3], $sqlBind);
54  $sqlBind = preg_replace(['~,\s*~', '~\'~'], [',', ''], $sqlBind[1]);
55  $sqlBind = $sqlBind ? explode(',', $sqlBind) : [];
56  $binds = [];
57 
58  // process multi queries
59  if ($sqlBindGroupAmount > 1) {
60  $valuesCount = count($bind)/$sqlBindGroupAmount;
61  for ($i = 0; $i < $sqlBindGroupAmount; $i++) {
62  $binds[] = array_combine(
63  $fields,
64  $this->handleBindValues($sqlBind, $bind, $i * $valuesCount)
65  );
66  }
67  } else {
68  $sqlBind = $this->handleBindValues($sqlBind, $bind);
69  $binds[] = array_combine($fields, $sqlBind);
70  }
71  $this->sql[] = [$binds, $table];
72  }
73  }
74 
81  private function handleBindValues(array $sqlBind, array $bind, $bindPosition = 0)
82  {
83  $bind = array_values($bind);
84  foreach ($sqlBind as $i => $fieldValue) {
85  if ($fieldValue === '?') {
86  $sqlBind[$i] = $bind[$bindPosition];
87  $bindPosition++;
88  }
89  }
90 
91  return $sqlBind;
92  }
93 
97  public function getSql()
98  {
99  return $this->sql;
100  }
101 
107  public function enable()
108  {
109  $this->sql = [];
110  $this->getProfiler()->clear();
111  $this->getProfiler()->setEnabled(true);
112  }
113 
119  public function disable()
120  {
121  $this->getProfiler()->setEnabled(false);
122  $queries = $this->getProfiler()->getQueryProfiles() ?: [];
123  foreach ($queries as $query) {
124  if ($query->getQueryType() === Profiler::INSERT || $this->isReplaceQuery($query)) {
125  // For generator we do not care about REPLACE query and can use INSERT instead
126  // due to it's not support parallel execution
127  $this->addSql($query->getQuery(), $query->getQueryParams());
128  }
129  }
130  }
131 
138  private function isReplaceQuery($query)
139  {
140  return $query->getQueryType() === Profiler::QUERY && 0 === stripos(ltrim($query->getQuery()), 'replace');
141  }
142 
146  private function getProfiler()
147  {
148  if ($this->profiler === null) {
149  $this->profiler = $this->resourceConnection->getConnection()->getProfiler();
150  }
151 
152  return $this->profiler;
153  }
154 }
__construct(ResourceConnection $resourceConnection)
$fields
Definition: details.phtml:14
$table
Definition: trigger.php:14
$i
Definition: gallery.phtml:31