Bitrix-D7  20.5.0
sqlexpression.php
См. документацию.
1 <?php
2 /**
3  * Bitrix Framework
4  * @package bitrix
5  * @subpackage main
6  * @copyright 2001-2013 Bitrix
7  */
8 
9 namespace Bitrix\Main\DB;
10 
12 
13 /**
14  * Class SqlExpression
15  *
16  * @package Bitrix\Main\DB
17  */
19 {
20  /** @var string */
21  protected $expression;
22 
23  /** @var array */
24  protected $args = array();
25 
26  protected $pattern = '/([^\\\\]|^)(\?[#sif]?)/';
27 
28  protected $i;
29 
30  /** @var Connection */
31  protected $connection;
32 
33  /**
34  * @param string $expression Sql expression.
35  * @param string,... $args Substitutes.
36  *
37  * @throws \Bitrix\Main\ArgumentException
38  */
39  public function __construct()
40  {
41  $args = func_get_args();
42 
43  if (!isset($args[0]))
44  {
45  throw new \Bitrix\Main\ArgumentException('No pattern has been found for SqlExpression');
46  }
47 
48  $this->expression = $args[0];
49 
50  for ($i = 1, $n = count($args); $i < $n; $i++)
51  {
52  $this->args[] = $args[$i];
53  }
54  }
55 
56  /**
57  * Returns $expression with replaced placeholders.
58  *
59  * @return string
60  */
61  public function compile()
62  {
63  $this->i = -1;
64 
65  if (mb_strpos($this->expression, '\\') === false)
66  {
67  // regular case
68  return preg_replace_callback($this->pattern, array($this, 'execPlaceholders'), $this->expression);
69  }
70  else
71  {
72  // handle escaping \ and \\
73  $parts = explode('\\\\', $this->expression);
74 
75  foreach ($parts as &$part)
76  {
77  if (!empty($part))
78  {
79  $part = preg_replace_callback($this->pattern, array($this, 'execPlaceholders'), $part);
80  }
81  }
82 
83  $parts = str_replace('\\?', '?', $parts);
84 
85  return implode('\\\\', $parts);
86  }
87  }
88 
89  /**
90  * Used by compile method to replace placeholders with values.
91  *
92  * @param array $matches Matches found by preg_replace.
93  *
94  * @return string
95  */
96  protected function execPlaceholders($matches)
97  {
98  $sqlHelper = $this->getConnection()->getSqlHelper();
99 
100  $this->i++;
101 
102  $pre = $matches[1];
103  $ph = $matches[2];
104 
105  if (isset($this->args[$this->i]))
106  {
107  $value = $this->args[$this->i];
108 
109  if ($ph == '?' || $ph == '?s')
110  {
111  $value = "'" . $sqlHelper->forSql($value) . "'";
112  }
113  elseif ($ph == '?#')
114  {
115  $value = $sqlHelper->quote($value);
116  }
117  elseif ($ph == '?i')
118  {
119  $value = (int) $value;
120  }
121  elseif ($ph == '?f')
122  {
123  $value = (float) $value;
124  }
125 
126  return $pre . $value;
127  }
128 
129  return $matches[0];
130  }
131 
132  public function __toString()
133  {
134  return $this->compile();
135  }
136 
137  /**
138  * @return Connection
139  */
140  public function getConnection()
141  {
142  if ($this->connection === null)
143  {
144  $this->connection = Application::getConnection();
145  }
146 
147  return $this->connection;
148  }
149 
150  /**
151  * @param Connection $connection
152  */
153  public function setConnection($connection)
154  {
155  $this->connection = $connection;
156  }
157 }
Base class for any application.
static getConnection($name="")
Static method returns database connection for the specified name.
compile()
Returns $expression with replaced placeholders.
execPlaceholders($matches)
Used by compile method to replace placeholders with values.