1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Core\SqlAction; |
4
|
|
|
|
5
|
|
|
use Db3v4l\API\Interfaces\SqlAction\CommandAction; |
6
|
|
|
|
7
|
|
|
/** |
|
|
|
|
8
|
|
|
* @todo allow extraction of the statements as an array, in case we have some executors that can only work on statements... |
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
class Command implements CommandAction |
11
|
|
|
{ |
12
|
|
|
protected $sql; |
13
|
|
|
protected $callable; |
14
|
|
|
protected $isSingleStatement; |
15
|
|
|
protected $statementSeparator = "\n"; |
16
|
|
|
//protected $statementTerminator = ';'; |
17
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @param string|string[]|null $sql for single statements, pass in either a string or an array with a single string element |
|
|
|
|
20
|
|
|
* for multiple statements, pass in an array of strings |
21
|
|
|
* pass in null when all you need to execute is the callable |
22
|
|
|
* @param callable|null $callable |
|
|
|
|
23
|
|
|
* @param bool $isSingleStatement if left null, it will be inferred |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function __construct($sql, $callable = null, $isSingleStatement = null) |
26
|
|
|
{ |
27
|
|
|
if (is_array($sql)) { |
28
|
|
|
foreach ($sql as &$statement) { |
29
|
|
|
$statement = trim($statement); |
30
|
|
|
// Disabled as it messes up with mssql GO statements, which take no terminator... |
31
|
|
|
// @todo find a simple way to reintroduce this, while allowing support for sql comments as part of statements... |
32
|
|
|
//if (substr($statement, -1) !== $this->statementTerminator) { |
33
|
|
|
// $statement .= $this->statementTerminator; |
34
|
|
|
//} |
35
|
|
|
} |
36
|
|
|
$this->sql = implode($this->statementSeparator, $sql); |
37
|
|
|
$isSingleStatement = ($isSingleStatement === null) ? (count($sql) < 2) : $isSingleStatement; |
38
|
|
|
} else { |
39
|
|
|
$this->sql = ($sql === null) ? $sql : trim($sql); |
40
|
|
|
$isSingleStatement = ($isSingleStatement === null) ? true : $isSingleStatement; |
41
|
|
|
} |
42
|
|
|
$this->callable = $callable; |
43
|
|
|
$this->isSingleStatement = $isSingleStatement; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getCommand() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return $this->sql; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function isSingleStatement() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
return $this->isSingleStatement; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
|
|
|
|
57
|
|
|
* @todo should we unwrap PDOStatement into a string ? |
|
|
|
|
58
|
|
|
* @return Callable|null |
59
|
|
|
*/ |
60
|
|
|
public function getResultsFilterCallable() |
61
|
|
|
{ |
62
|
|
|
return $this->callable; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|