-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
SOQLConnection.php
134 lines (116 loc) · 3.16 KB
/
SOQLConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Lester\EloquentSalesForce\Database;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\MySqlBuilder;
use Illuminate\Database\Query\Processors\MySqlProcessor;
use Illuminate\Database\Query\Grammars\MySqlGrammar as QueryGrammar;
use Illuminate\Database\Schema\Grammars\MySqlGrammar as SchemaGrammar;
use Omniphx\Forrest\Exceptions\MissingResourceException;
use Lester\EloquentSalesForce\Facades\SObjects;
use Closure;
use Carbon\Carbon;
use Illuminate\Support\Str;
class SOQLConnection extends Connection
{
/**
* {@inheritDoc}
*/
public function select($query, $bindings = [], $useReadPdo = true)
{
return $this->run($query, $bindings, function($query, $bindings) {
if ($this->pretending()) {
return [];
}
$statement = $this->prepare($query, $bindings);
/** @scrutinizer ignore-call */
$result = SObjects::query($statement);
SObjects::log('SOQL Query', [
'query' => $statement
]);
$records = $result['records'];
while (isset($result['nextRecordsUrl'])) {
$result = SObjects::next($result['nextRecordsUrl']);
if (isset($result['records'])) {
$records = \array_merge($records, $result['records']);
}
}
return $records;
});
}
/**
* {@inheritDoc}
*/
public function cursor($query, $bindings = [], $useReadPdo = true)
{
$result = $this->run($query, $bindings, function($query, $bindings) {
if ($this->pretending()) {
return [];
}
$statement = $this->prepare($query, $bindings);
/** @scrutinizer ignore-call */
return SObjects::query($statement);
});
while (true) {
foreach ($result['records'] as $record) {
yield $record;
}
if (!isset($result['nextRecordsUrl'])) {
break;
}
$result = SObjects::next($result['nextRecordsUrl']);
}
}
/**
* {@inheritDoc}
*/
protected function run($query, $bindings, Closure $callback)
{
$start = microtime(true);
try {
$result = $this->runQueryCallback($query, $bindings, $callback);
} catch (QueryException $e) {
$result = $this->handleQueryException(
$e, $query, $bindings, $callback
);
}
// Once we have run the query we will calculate the time that it took to run and
// then log the query, bindings, and execution time so we will report them on
// the event that the developer needs them. We'll log time in milliseconds.
$this->logQuery(
$query, $bindings, $this->getElapsedTime($start)
);
return $result;
}
private function prepare($query, $bindings)
{
$query = str_replace('`', '', $query);
$bindings = array_map(function($item) {
try {
if (\Carbon\Carbon::parse($item) !== false && !$this->isSalesForceId($item)) {
return $item;
}
} catch (\Exception $e) {
if (is_int($item) || is_float($item)) {
return $item;
} else {
return "'$item'";
}
}
return "'$item'";
}, $bindings);
$query = Str::replaceArray('?', $bindings, $query);
return $query;
}
/**
* Based on characters and length of $str, determine if it appears to be a
* SalesForce ID.
*
* @param string $str String to test
*
* @return bool
*/
public function isSalesForceId($str)
{
return boolval(\preg_match('/^[0-9a-zA-Z]{15,18}$/', $str));
}
}