Skip to content

Commit 0e0b370

Browse files
committed
wip
1 parent fa4853b commit 0e0b370

26 files changed

+1222
-152
lines changed

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
v1.1.0
2-
---------
3-
* Some features.
4-
51
v1.0.0
62
---------
73
* First release

README.md

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,39 @@
11
![logo](assets/logo.png)
22

3-
Design a simple and expressive logo with the same model you see above,
4-
the logo design helps to unify the visual identity and further develop the optimal packages to increase the quality of the software we develop
3+
# Laravel Query Helper
54

6-
#NOTES:
7-
* You should replace all your dependencies into your custom names and files including:
8-
* vendor namespace.
9-
* composer information.
10-
* logo
11-
* credit and contributors links
12-
* security email
13-
* To use this template, please click on button above "Use this template".
14-
* Delete this section after you have finished developing your package
15-
# Package Name
16-
Your package description,Try to be briefly describing the features of the package,
17-
which versions of Laravel and php it require,
18-
and if there are external resources for learning or explanation
5+
Laravel Query Helper was developed for [laravel 7.2+](http://laravel.com/) to help you optimize
6+
sql queries, this package will contain all advanced SQL queries to Help us write better and faster queries and clean code.
197

8+
Installation
9+
------------
2010
##### 1 - Dependency
2111
The first step is using composer to install the package and automatically update your composer.json file, you can do this by running:
22-
2312
```shell
24-
composer require vendor_name/package_name
13+
composer require kmlaravel/laravel-query-helper
2514
```
2615
##### 2 - Copy the package providers to your local config with the publish command, this will publish the config:
27-
28-
Replace this command with your install command,
29-
The package must contain an installation command even if it does not do anything,
30-
this helps with maintenance in the future.
31-
3216
```shell
33-
php artisan package:install
17+
php artisan query-helper:install
3418
```
3519

3620
Features
3721
-----------
38-
Put the name of the feature and a link to this feature
39-
- [Any feature](https://github.com/syrian-open-source/laravel-package-template/blob/main/docs/feature.md#usage)
40-
22+
- [Update Helper](https://github.com/Syrian-Open-Source/laravel-query-helper/blob/main/docs/update.md)
23+
- [Delete Helper](https://github.com/Syrian-Open-Source/laravel-query-helper/blob/main/docs/delete.md)
24+
- [Insert Helper](https://github.com/Syrian-Open-Source/laravel-query-helper/blob/main/docs/insert.md)
25+
- [Join Helper](https://github.com/Syrian-Open-Source/laravel-query-helper/blob/main/docs/join.md)
26+
- [Optimizing Helper](https://github.com/Syrian-Open-Source/laravel-query-helper/blob/main/docs/optimizing.md)
4127

4228
Changelog
4329
---------
44-
Please see the [CHANGELOG](https://github.com/syrian-open-source/laravel-package-template/blob/master/CHANGELOG.md) for more information about what has changed or updated or added recently.
30+
Please see the [CHANGELOG](https://github.com/Syrian-Open-Source/laravel-query-helper/blob/main/CHANGELOG.md) for more information about what has changed or updated or added recently.
4531

4632
Security
4733
--------
48-
If you discover any security related issues, please email them first to "your email",
49-
if we do not fix it within a short period of time please open a new issue describing your problem.
34+
If you discover any security related issues, please email them first to [email protected],
35+
if we do not fix it within a short period of time please open a new issue describe your problem.
5036

5137
Credits
5238
-------
53-
* [your email](https://github.com/syrian-open-source/laravel-package-template/graphs/contributors)
54-
* [All contributors](https://github.com/syrian-open-source/laravel-package-template/graphs/contributors)
39+
[karam mustafa](https://www.linkedin.com/in/karam2mustafa)

assets/logo.png

-5.15 KB
Loading

composer.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
2-
"name": "syrian-open-source/laravel-package-template",
2+
"name": "syrian-open-source/laravel-query-helper",
33
"description": "Package description",
44
"license": "MIT",
55
"keywords": [
66
"laravel",
7-
"syrian open source"
7+
"syrian open source",
8+
"laravel query helper",
9+
"laravel-query-helper"
810
],
911
"authors": [
1012
{
@@ -20,8 +22,8 @@
2022
},
2123
"autoload" : {
2224
"psr-4": {
23-
"SOS\\LaravelPackageTemplate\\" : "src",
24-
"SOS\\LaravelPackageTemplate\\Tests\\": "src/tests"
25+
"SOS\\QueryHelper\\" : "src",
26+
"SOS\\QueryHelper\\Tests\\": "src/tests"
2527
}
2628
},
2729
"scripts": {
@@ -32,7 +34,7 @@
3234
"extra" : {
3335
"laravel" : {
3436
"providers" : [
35-
"SOS\\LaravelPackageTemplate\\Providers\\LaravelPackageTemplateServiceProviders"
37+
"SOS\\QueryHelper\\Providers\\QueryHelperServiceProviders"
3638
]
3739
}
3840
},

docs/delete.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Delete helper
2+
-----------
3+
Suppose you want to drop multiple tables by their names in the database, you can do it with the following implementation.
4+
```php
5+
6+
QueryHelperFacade::deleteInstance()
7+
->setTables(['posts', 'users' , 'comments']) // tables names.
8+
->dropMultiTables()
9+
->executeAll();
10+
11+
```
12+
If you have a table that contains a large number of data (maybe millions of records)
13+
and you want to delete everything contained in this table,
14+
if you execute the command with one query,
15+
this query will take a lot of time,
16+
so this function divides the large query into more queries with an easy-to-use structure.
17+
```php
18+
19+
QueryHelperFacade::deleteInstance()
20+
->setField('id') // Set the field that we will query on it.
21+
->setAllowedWhereInQueryNumber(10) // Set the number that the query will delete each time
22+
->setTableName('tests')
23+
->deleteLargeData();
24+
25+
// and you can implement your custom query by a callback.
26+
QueryHelperFacade::deleteInstance()
27+
->setField('id') // Set the field that we will query on it.
28+
->setAllowedWhereInQueryNumber(10) // Set the number that the query will delete each time.
29+
->setTableName('tests')
30+
->deleteLargeData(function ($table) {
31+
return $table->where('id', '<', 100)->pluck('id')->toArray();
32+
}); // this will implement the delete process only on the result of this callback.
33+
```
34+
If you want to drop all tables from the database.
35+
```php
36+
37+
QueryHelperFacade::deleteInstance()
38+
->prepareDataBaseTablesToDrop()
39+
->executeAll();
40+
```

docs/feature.md

Lines changed: 0 additions & 6 deletions
This file was deleted.
File renamed without changes.

docs/join.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Join helper
2+
-----------
3+
We provide some function we think that we can implement joins more faster and easier.
4+
5+
```php
6+
// if you need to fast join you can use fastJoin function
7+
QueryHelperFacade::joinInstance()
8+
// first parameter is the table will fetch the data from it.
9+
// second parameter is the selection you want from the database.
10+
// third parameter is table to join with.
11+
->fastJoin('users', ['users.id'], 'posts')
12+
// you can add more joins.
13+
// ->fastJoin('other_main_table', ['id'], 'table_to_join')
14+
// ->fastJoin('other_main_table', ['id'], 'table_to_join')
15+
// retrieve the query result
16+
->done();
17+
18+
// if you want to specify all the options and functions,
19+
// join helper class has a strong declarative process,
20+
// and this process make you chose what you want to build.
21+
QueryHelperFacade::joinInstance()
22+
// set the main table that we will select.
23+
->setTableName('users')
24+
// set the join type, the default value is 'JOIN'.
25+
->setJoinType('LEFT JOIN')
26+
// set the select statement, the default value is ['id'].
27+
->setSelection(['users.id'])
28+
// set the table that you want to join with.
29+
->addjoin('tests')
30+
// build this join.
31+
->buildJoin()
32+
// if you want to get the built query.
33+
->getQuery()
34+
// return the result
35+
->done();
36+
37+
```

docs/optimizing.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Optimizing
2+
-----------
3+
In some databases, you can't do any process that require more than 65K of parameters,
4+
so we have to chunk your large query to smaller pieces, and we can do that for you ar effective way.
5+
```php
6+
// Suppose we have a group of users, let say's we have 100k items to insert.
7+
$users = [
8+
['name' => 'example 1'],
9+
['name' => 'example 2'],
10+
['name' => 'example 3'],
11+
['name' => 'example 4'],
12+
...
13+
];
14+
QueryHelperFacade::updateInstance()
15+
->setAllowedWhereInQueryNumber(2000) // chunk size and you can update the default value from query_helper.php config file.
16+
->checkIfQueryAllowed($users , function ($data){
17+
User::insert($data);
18+
});
19+
```

docs/update.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
Update
2+
-----------
3+
Suppose we have a group of users who have an id and a name and we have an array to update each user with a new name
4+
as in the following example
5+
```shell
6+
$usersDataBeforeUpdate = [
7+
['id' => 1, 'name' => 'example before update 1'],
8+
['id' => 2, 'name' => 'example before update 2'],
9+
['id' => 3, 'name' => 'example before update 2'],
10+
];
11+
$usersDataToUpdate = [
12+
['id' => 1, 'name' => 'example after update 1'],
13+
['id' => 2, 'name' => 'example after update 2'],
14+
['id' => 3, 'name' => 'example after update 2'],
15+
];
16+
17+
// so the traditional way to that,
18+
// loop through $usersDataBeforeUpdate and fetch each user with specific (id, name)
19+
// and update his name with new name
20+
// and this will execute a query for each record, for the above array, the request will execute 3 queries
21+
// what if we want to update 1000 records previously?
22+
23+
foreah($usersDataToUpdate as $user){
24+
User::find($user['id'])->update(['name' => $user['name']]);
25+
}
26+
27+
```
28+
So the query helper will help you optimize this process, see the following explanation:
29+
```php
30+
$usersDataBeforeUpdate = [
31+
['id' => 1, 'name' => 'example before update 1'],
32+
['id' => 2, 'name' => 'example before update 2'],
33+
['id' => 3, 'name' => 'example before update 2'],
34+
];
35+
36+
$usersDataToUpdate = [
37+
['id' => 1, 'name' => 'example after update 1'],
38+
['id' => 2, 'name' => 'example after update 2'],
39+
['id' => 3, 'name' => 'example after update 2'],
40+
];
41+
42+
$ids = [];
43+
$values = [];
44+
$cases = [];
45+
$tableName = 'users';
46+
$columnToUpdate = 'name';
47+
foreach($usersDataToUpdate as $user){
48+
49+
array_push($ids , $user['id']);
50+
51+
array_push($values , $user['name']);
52+
53+
// if you want to set your cases in query.
54+
$cases[] = "WHEN id = {$user['id']} then ".$user['name'];
55+
}
56+
57+
// if you want to set your cases in query.
58+
$cases = implode(' ', $cases);
59+
60+
QueryHelperFacade::updateInstance()
61+
->setIds($ids)
62+
->setValues($values)
63+
->setTableName($tableName) // change this parameter value to your database table name.
64+
->setField($columnToUpdate) // change this parameter value to your database column name.
65+
->bindIdsWithValues()
66+
->executeUpdateMultiRows();
67+
68+
// this will execute only one query.
69+
```
70+
What if you want to put your own Cases ? **okay we support that**.
71+
```php
72+
73+
$query = QueryHelperFacade::updateInstance()
74+
->setIds($ids)
75+
->setCasues($cases)
76+
->setTableName($tableName) // change this parameter value to your database table name.
77+
->setField($columnToUpdate) // change this parameter value to your database column name.
78+
->executeUpdateMultiRows();
79+
```
80+
What if you want dump the query which will execute ? **okay we support that**.
81+
```php
82+
83+
$query = QueryHelperFacade::updateInstance()
84+
->setIds($ids)
85+
->setValues($values)
86+
->setTableName($tableName) // change this parameter value to your database table name.
87+
->setField($columnToUpdate) // change this parameter value to your database column name.
88+
->buildStatement()
89+
->getQuery();
90+
dd($query);
91+
92+
```
93+
What if you want to reduce these lines in one line ? **okay we support that**.
94+
```php
95+
96+
$query = QueryHelperFacade::updateInstance()
97+
->fastUpdate($tableName , $ids , $values , $columnToUpdate);
98+
dd($query);
99+
100+
```

0 commit comments

Comments
 (0)