Skip to content

Commit 4c942ec

Browse files
committed
add file interface
add file helpers add function batch()
1 parent 4279857 commit 4c942ec

File tree

6 files changed

+68
-28
lines changed

6 files changed

+68
-28
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Insert and update batch (bulk) in laravel
66
[![Total Downloads](https://poser.pugx.org/mavinoo/laravel-batch/downloads)](https://packagist.org/packages/mavinoo/laravel-batch)
77
[![Daily Downloads](https://poser.pugx.org/mavinoo/laravel-batch/d/daily)](https://packagist.org/packages/mavinoo/laravel-batch)
88

9+
910
# Install
1011
`composer require mavinoo/laravel-batch`
1112

@@ -19,7 +20,6 @@ file app.php in array aliases :
1920

2021
`'Batch' => Mavinoo\LaravelBatch\LaravelBatchFacade::class,`
2122

22-
2323
# Example Update 1
2424

2525
```php
@@ -115,6 +115,7 @@ $batchSize = 500; // insert 500 (default), 100 minimum rows in one query
115115
$result = Batch::insert($userInstance, $columns, $values, $batchSize);
116116
```
117117

118+
118119
```php
119120
// result : false or array
120121

@@ -126,3 +127,18 @@ Array
126127
[totalQuery] => 1
127128
)
128129
```
130+
131+
132+
133+
# Helper batch()
134+
135+
```php
136+
// ex: update
137+
138+
$result = batch()->update($userInstance, $value, $index);
139+
140+
141+
// ex: insert
142+
143+
$result = batch()->insert($userInstance, $columns, $values, $batchSize);
144+
```

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"autoload": {
1414
"psr-4": {
1515
"Mavinoo\\LaravelBatch\\": "src/"
16-
}
16+
},
17+
"files": [
18+
"src/Common/Helpers.php"
19+
]
1720
},
1821
"extra": {
1922
"laravel": {

src/Common/Common.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Mavinoo\LaravelBatch\Common;
4+
5+
class Common
6+
{
7+
public static function mysql_escape($inp)
8+
{
9+
if(is_array($inp)) return array_map(__METHOD__, $inp);
10+
11+
if(!empty($inp) && is_string($inp))
12+
{
13+
return str_replace(
14+
['\\', "\0", "\n", "\r", "'", '"', "\x1a"],
15+
['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'],
16+
$inp);
17+
}
18+
19+
return $inp;
20+
}
21+
}

src/Common/Helpers.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
<?php
2-
3-
namespace Mavinoo\LaravelBatch\Common;
4-
5-
class Helpers
6-
{
7-
public static function mysql_escape($inp)
8-
{
9-
if(is_array($inp)) return array_map(__METHOD__, $inp);
10-
11-
if(!empty($inp) && is_string($inp))
12-
{
13-
return str_replace(
14-
['\\', "\0", "\n", "\r", "'", '"', "\x1a"],
15-
['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'],
16-
$inp);
17-
}
18-
19-
return $inp;
20-
}
21-
}
1+
<?php
2+
3+
if (! function_exists('batch'))
4+
{
5+
function batch()
6+
{
7+
return app('Mavinoo\LaravelBatch\Batch');
8+
}
9+
}

src/InterfaceBatch.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Mavinoo\LaravelBatch;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
interface InterfaceBatch
8+
{
9+
public function update(Model $table, array $values, string $index = null);
10+
11+
public function insert(Model $table, array $columns, array $values, int $batchSize = 500);
12+
}

src/LaravelBatch.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use Illuminate\Database\DatabaseManager;
66
use Illuminate\Database\Eloquent\Model;
7-
use Mavinoo\LaravelBatch\Common\Helpers;
7+
use Mavinoo\LaravelBatch\Common\Common;
88

9-
class Batch
9+
class Batch implements InterfaceBatch
1010
{
1111
/**
1212
* @var DatabaseManager
@@ -61,7 +61,7 @@ public function update(Model $table, array $values, string $index = null)
6161
$ids[] = $val[$index];
6262
foreach (array_keys($val) as $field) {
6363
if ($field !== $index) {
64-
$value = (is_null($val[$field]) ? 'NULL' : '"' . Helpers::mysql_escape($val[$field]) . '"');
64+
$value = (is_null($val[$field]) ? 'NULL' : '"' . Common::mysql_escape($val[$field]) . '"');
6565
$final[$field][] = 'WHEN `' . $index . '` = "' . $val[$index] . '" THEN ' . $value . ' ';
6666
}
6767
}
@@ -143,14 +143,14 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
143143
$values = array_chunk($values, $totalChunk, true);
144144

145145
foreach ($columns as $key => $column) {
146-
$columns[$key] = "`" . Helpers::mysql_escape($column) . "`";
146+
$columns[$key] = "`" . Common::mysql_escape($column) . "`";
147147
}
148148

149149
foreach ($values as $value) {
150150
$valueArray = [];
151151
foreach ($value as $data) {
152152
foreach ($data as $key => $item) {
153-
$item = is_null($item) ? 'NULL' : "'" . Helpers::mysql_escape($item) . "'";
153+
$item = is_null($item) ? 'NULL' : "'" . Common::mysql_escape($item) . "'";
154154
$data[$key] = $item;
155155
}
156156

0 commit comments

Comments
 (0)