Skip to content

Commit f48ba02

Browse files
authored
Merge pull request #27 from itosho/v1.2.1
v1.2.1
2 parents 4518680 + 9ef5409 commit f48ba02

File tree

12 files changed

+99
-136
lines changed

12 files changed

+99
-136
lines changed

.semver

Lines changed: 0 additions & 6 deletions
This file was deleted.

.travis.yml

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ services:
44
- mysql
55

66
php:
7-
- 7.0
87
- 7.1
98
- 7.2
109
- 7.3
10+
- 7.4
1111

1212
sudo: false
1313

@@ -22,17 +22,14 @@ matrix:
2222
fast_finish: true
2323

2424
include:
25-
- php: 7.3
25+
- php: 7.4
2626
env: PREFER_LOWEST="" DB=mysql db_user=root db_host=0.0.0.0 db_name=cakephp_test
2727

28-
- php: '7.4snapshot'
29-
env: PREFER_LOWEST="" DB=mysql db_user=root db_host=0.0.0.0 db_name=cakephp_test
30-
31-
- php: 7.3
32-
env: PHPCS=1 DEFAULT=0 PREFER_LOWEST="
28+
- php: 7.4
29+
env: PHPCS=1 DEFAULT=0 PREFER_LOWEST=""
3330

34-
- php: 7.3
35-
env: PHPSTAN=1 DEFAULT=0 PREFER_LOWEST="
31+
- php: 7.4
32+
env: PHPSTAN=1 DEFAULT=0 PREFER_LOWEST=""
3633

3734

3835
cache:
@@ -41,8 +38,6 @@ cache:
4138
- $HOME/.composer/cache
4239

4340
before_script:
44-
- if [[ $TRAVIS_PHP_VERSION != '7.4snapshot' ]]; then phpenv config-rm xdebug.ini; fi
45-
4641
- composer self-update
4742
- composer update --prefer-stable --prefer-dist --no-interaction $PREFER_LOWEST
4843

@@ -51,19 +46,16 @@ before_script:
5146
- if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:"^3.0"; fi
5247
- if [[ $PHPSTAN = 1 ]]; then composer require phpstan/phpstan; fi
5348

54-
# see: https://github.com/cakephp/chronos/issues/106
55-
- if [[ $TRAVIS_PHP_VERSION != 7.0 && $PREFER_LOWEST != "" ]]; then composer require --prefer-stable --prefer-dist --no-interaction $PREFER_LOWEST cakephp/chronos:^1.0.1; fi
56-
5749
script:
58-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.3 ]]; then vendor/bin/phpunit; fi
59-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.3 ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=clover.xml; fi
50+
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.4 ]]; then vendor/bin/phpunit; fi
51+
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.4 ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=clover.xml; fi
6052

6153
- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -n -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi
6254
- if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse -c phpstan.neon -l 7 src; fi
6355

6456
after_success:
6557
- |
66-
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.3 ]]; then
58+
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.4 ]]; then
6759
curl -s https://codecov.io/bash > codecov
6860
sed -i -e 's/TRAVIS_.*_VERSION/^TRAVIS_.*_VERSION=/' codecov
6961
bash codecov

CHANGES.md

Lines changed: 0 additions & 23 deletions
This file was deleted.

README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ CakePHP behavior plugin for easily some complicated queries.
1010

1111
## Requirements
1212

13-
- PHP 7.0+
14-
- CakePHP 3.5+
13+
- PHP 7.1+
14+
- CakePHP 3.6+
1515
- MySQL 5.6+
1616

1717
## Installation
@@ -28,12 +28,12 @@ composer require itosho/easy-query
2828
$this->Tags = TableRegistry::get('Tags');
2929
$this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [
3030
'uniqueColumns' => ['name'],
31-
'updateColumns' => ['description', 'modified']
31+
'updateColumns' => ['description', 'modified'],
3232
]);
3333

3434
$data = [
3535
'name' => 'cakephp',
36-
'description' => 'php web framework'
36+
'description' => 'php web framework',
3737
];
3838
$entity = $this->Tags->newEntity($data);
3939
$this->Tags->upsert($entity);
@@ -44,17 +44,17 @@ $this->Tags->upsert($entity);
4444
```php
4545
$this->Tags = TableRegistry::get('Tags');
4646
$this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [
47-
'updateColumns' => ['description', 'modified']
47+
'updateColumns' => ['description', 'modified'],
4848
]);
4949

5050
$data = [
5151
[
5252
'name' => 'cakephp',
53-
'description' => 'php web framework'
53+
'description' => 'php web framework',
5454
],
5555
[
5656
'name' => 'rubyonrails',
57-
'description' => 'ruby web framework'
57+
'description' => 'ruby web framework',
5858
]
5959
];
6060
$entities = $this->Tags->newEntities($data);
@@ -71,12 +71,12 @@ $data = [
7171
[
7272
'title' => 'First Article',
7373
'body' => 'First Article Body',
74-
'published' => '1'
74+
'published' => '1',
7575
],
7676
[
7777
'title' => 'Second Article',
7878
'body' => 'Second Article Body',
79-
'published' => '0'
79+
'published' => '0',
8080
]
8181
];
8282
$entities = $this->Articles->newEntities($data);
@@ -95,12 +95,10 @@ $this->Articles->addBehavior('Itosho/EasyQuery.Insert');
9595

9696
$data = [
9797
'title' => 'New Article?',
98-
'body' => 'New Article Body?'
98+
'body' => 'New Article Body?',
9999
];
100100
$entity = $this->Articles->newEntity($data);
101-
$condition = [
102-
'title' => 'New Article?'
103-
];
101+
$condition = ['title' => 'New Article?'];
104102

105103
$this->Articles->insertOnce($entities);
106104
```
@@ -123,7 +121,7 @@ $this->Articles->addBehavior('Itosho/EasyQuery.Insert');
123121

124122
$data = [
125123
'title' => 'New Article',
126-
'body' => 'New Article Body'
124+
'body' => 'New Article Body',
127125
];
128126
$entity = $this->Articles->newEntity($data);
129127

@@ -146,7 +144,7 @@ And you can change the action manually by using `event` config like this.
146144
```php
147145
// default value is true
148146
$this->Articles->addBehavior('Itosho/EasyQuery.Insert', [
149-
'event' => ['beforeSave' => false]
147+
'event' => ['beforeSave' => false],
150148
]);
151149
```
152150

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
"source": "https://github.com/itosho/easy-query"
2727
},
2828
"require": {
29-
"cakephp/orm": "^3.5"
29+
"cakephp/orm": "^3.6"
3030
},
3131
"require-dev": {
32-
"cakephp/cakephp": "^3.5",
32+
"cakephp/cakephp": "^3.6",
3333
"phpunit/phpunit": "^5.7.14|^6.0"
3434
},
3535
"autoload": {

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
parameters:
2+
checkMissingIterableValueType: false
23
ignoreErrors: []

src/Model/Behavior/InsertBehavior.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class InsertBehavior extends Behavior
2222
* @var array
2323
*/
2424
protected $_defaultConfig = [
25-
'event' => ['beforeSave' => true]
25+
'event' => ['beforeSave' => true],
2626
];
2727

2828
/**
@@ -78,23 +78,18 @@ public function insertOnce(Entity $entity, array $conditions = null)
7878
$insertData['modified'] = FrozenTime::now()->toDateTimeString();
7979
}
8080

81-
$escape = function ($content) {
82-
return is_null($content) ? 'NULL' : '\'' . addslashes($content) . '\'';
83-
};
84-
85-
$escapedInsertData = array_map($escape, $insertData);
8681
$fields = array_keys($insertData);
8782
$existsConditions = $conditions;
8883
if (is_null($existsConditions)) {
89-
$existsConditions = $this->getExistsConditions($escapedInsertData);
84+
$existsConditions = $this->getExistsConditions($insertData);
9085
}
9186

9287
$query = $this->_table
9388
->query()
9489
->insert($fields)
9590
->epilog(
9691
$this
97-
->buildTmpTableSelectQuery($escapedInsertData)
92+
->buildTmpTableSelectQuery($insertData)
9893
->where(function (QueryExpression $exp) use ($existsConditions) {
9994
$query = $this->_table
10095
->find()
@@ -111,55 +106,61 @@ public function insertOnce(Entity $entity, array $conditions = null)
111106
/**
112107
* build tmp table's select query for insert select query
113108
*
114-
* @param array $escapedData escaped array data
109+
* @param array $insertData insert data
115110
* @throws LogicException select query is invalid
116111
* @return Query tmp table's select query
117112
*/
118-
private function buildTmpTableSelectQuery($escapedData)
113+
private function buildTmpTableSelectQuery($insertData)
119114
{
120115
$driver = $this->_table
121116
->getConnection()
122117
->getDriver();
123118
$schema = [];
124-
foreach ($escapedData as $key => $value) {
119+
$binds = [];
120+
foreach ($insertData as $key => $value) {
125121
$col = $driver->quoteIdentifier($key);
126-
$schema[] = "{$value} AS {$col}";
122+
if (is_null($value)) {
123+
$schema[] = "NULL AS {$col}";
124+
} else {
125+
$bindKey = ':' . strtolower($key);
126+
$binds[$bindKey] = $value;
127+
$schema[] = "{$bindKey} AS {$col}";
128+
}
127129
}
128130

129131
$tmpTable = TableRegistry::getTableLocator()->get('tmp', [
130-
'schema' => $this->_table->getSchema()
132+
'schema' => $this->_table->getSchema(),
131133
]);
132134
$query = $tmpTable
133135
->find()
134-
->select(array_keys($escapedData))
136+
->select(array_keys($insertData))
135137
->from(
136138
sprintf('(SELECT %s) as tmp', implode(',', $schema))
137139
);
138140
/** @var Query $selectQuery */
139141
$selectQuery = $query;
142+
foreach ($binds as $key => $value) {
143+
$selectQuery->bind($key, $value);
144+
}
140145

141146
return $selectQuery;
142147
}
143148

144149
/**
145150
* get conditions for finding a record already exists
146151
*
147-
* @param array $escapedData escaped array data
152+
* @param array $insertData insert data
148153
* @return array conditions
149154
*/
150-
private function getExistsConditions($escapedData)
155+
private function getExistsConditions($insertData)
151156
{
152157
$autoFillFields = ['created', 'modified'];
153158
$existsConditions = [];
154-
foreach ($escapedData as $field => $value) {
159+
foreach ($insertData as $field => $value) {
155160
if (in_array($field, $autoFillFields, true)) {
156161
continue;
157162
}
158-
if ($value === 'NULL') {
159-
$existsConditions[] = "{$field} IS NULL";
160-
} else {
161-
$existsConditions[] = "{$field} = {$value}";
162-
}
163+
$existsConditions[$field . ' IS'] = $value;
163164
}
164165

165166
return $existsConditions;

src/Model/Behavior/UpsertBehavior.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class UpsertBehavior extends Behavior
2121
protected $_defaultConfig = [
2222
'updateColumns' => null,
2323
'uniqueColumns' => null,
24-
'event' => ['beforeSave' => true]
24+
'event' => ['beforeSave' => true],
2525
];
2626

2727
/**

tests/Fixture/ArticlesFixture.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,30 @@ class ArticlesFixture extends TestFixture
1414
'created' => 'datetime',
1515
'modified' => 'datetime',
1616
'_constraints' => [
17-
'primary' => ['type' => 'primary', 'columns' => ['id']]
18-
]
17+
'primary' => ['type' => 'primary', 'columns' => ['id']],
18+
],
1919
];
2020
public $records = [
2121
[
2222
'title' => 'First Article',
2323
'body' => 'First Article Body',
2424
'published' => 1,
2525
'created' => '2017-09-01 00:00:00',
26-
'modified' => '2017-09-01 00:00:00'
26+
'modified' => '2017-09-01 00:00:00',
2727
],
2828
[
2929
'title' => 'Second Article',
3030
'body' => 'Second Article Body',
3131
'published' => 1,
3232
'created' => '2017-09-01 00:00:00',
33-
'modified' => '2017-09-01 00:00:00'
33+
'modified' => '2017-09-01 00:00:00',
3434
],
3535
[
3636
'title' => 'Third Article',
3737
'body' => 'Third Article Body',
3838
'published' => 1,
3939
'created' => '2017-09-01 00:00:00',
40-
'modified' => '2017-09-01 00:00:00'
41-
]
40+
'modified' => '2017-09-01 00:00:00',
41+
],
4242
];
4343
}

0 commit comments

Comments
 (0)