Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sqlexpression.php
1<?php
9namespace Bitrix\Main\DB;
10
12
19{
21 protected $expression;
22
24 protected $args = array();
25
26 protected $pattern = '/([^\\\\]|^)(\?[#sifv]?)/';
27
28 protected $i;
29
31 protected $connection;
32
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
61 public function compile()
62 {
63 $this->i = -1;
64
65 if (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
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 (array_key_exists($this->i, $this->args))
106 {
107 $value = $this->args[$this->i];
108
109 if ($value === null && $ph !== '?#')
110 {
111 $value = 'NULL';
112 }
113 elseif ($ph == '?' || $ph == '?s')
114 {
115 $value = "'" . $sqlHelper->forSql($value) . "'";
116 }
117 elseif ($ph == '?#')
118 {
119 $value = $sqlHelper->quote($value);
120 }
121 elseif ($ph == '?v')
122 {
123 $value = $sqlHelper->values($value);
124 }
125 elseif ($ph == '?i')
126 {
127 $value = (int) $value;
128 }
129 elseif ($ph == '?f')
130 {
131 $value = (float) $value;
132 }
133
134 return $pre . $value;
135 }
136
137 return $matches[0];
138 }
139
140 public function __toString()
141 {
142 return $this->compile();
143 }
144
148 public function getConnection()
149 {
150 if ($this->connection === null)
151 {
152 $this->connection = Application::getConnection();
153 }
154
155 return $this->connection;
156 }
157
161 public function setConnection($connection)
162 {
163 $this->connection = $connection;
164 }
165}
static getConnection($name="")