1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelUpsert\Query\Grammars; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder; |
6
|
|
|
use Illuminate\Database\Query\Grammars\SqlServerGrammar as Base; |
7
|
|
|
|
8
|
|
|
class SqlServerGrammar extends Base |
9
|
|
|
{ |
10
|
|
|
use CompilesUpsertQueries; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Compile an "upsert" statement into SQL. |
14
|
|
|
* |
15
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
16
|
|
|
* @param array $values |
17
|
|
|
* @param array $target |
18
|
|
|
* @param array $update |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
8 |
|
public function compileUpsert(Builder $query, array $values, array $target, array $update) |
22
|
|
|
{ |
23
|
8 |
|
return $this->compileMerge($query, $values, $target, $update); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Compile an "insert ignore" statement into SQL. |
28
|
|
|
* |
29
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
30
|
|
|
* @param array $values |
31
|
|
|
* @param array $target |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
4 |
|
public function compileInsertIgnore(Builder $query, array $values, array $target) |
35
|
|
|
{ |
36
|
4 |
|
return $this->compileMerge($query, $values, $target); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Compile a "merge" statement into SQL. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
43
|
|
|
* @param array $values |
44
|
|
|
* @param array $target |
45
|
|
|
* @param array|null $update |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
12 |
|
public function compileMerge(Builder $query, array $values, array $target, array $update = null) |
49
|
|
|
{ |
50
|
12 |
|
$columns = $this->columnize(array_keys(reset($values))); |
51
|
|
|
|
52
|
12 |
|
$sql = 'merge '.$this->wrapTable($query->from).' '; |
53
|
|
|
|
54
|
12 |
|
$parameters = collect($values)->map(function ($record) { |
55
|
12 |
|
return '('.$this->parameterize($record).')'; |
56
|
12 |
|
})->implode(', '); |
57
|
|
|
|
58
|
12 |
|
$sql .= 'using (values '.$parameters.') '.$this->wrapTable('laravel_source').' ('.$columns.') '; |
59
|
|
|
|
60
|
12 |
|
$on = collect($target)->map(function ($column) use ($query) { |
61
|
12 |
|
return $this->wrap('laravel_source.'.$column).' = '.$this->wrap($query->from.'.'.$column); |
62
|
12 |
|
})->implode(' and '); |
63
|
|
|
|
64
|
12 |
|
$sql .= 'on '.$on.' '; |
65
|
|
|
|
66
|
12 |
|
if ($update) { |
67
|
8 |
|
$update = collect($update)->map(function ($value, $key) { |
68
|
8 |
|
return is_numeric($key) |
69
|
7 |
|
? $this->wrap($value).' = '.$this->wrap('laravel_source.'.$value) |
70
|
8 |
|
: $this->wrap($key).' = '.$this->parameter($value); |
71
|
8 |
|
})->implode(', '); |
72
|
|
|
|
73
|
8 |
|
$sql .= 'when matched then update set '.$update.' '; |
74
|
|
|
} |
75
|
|
|
|
76
|
12 |
|
$sql .= 'when not matched then insert ('.$columns.') values ('.$columns.');'; |
77
|
|
|
|
78
|
12 |
|
return $sql; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|