Skip to content

Commit a7cc6fc

Browse files
author
Parsoolak
committed
feat: init
0 parents  commit a7cc6fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+4622
-0
lines changed

.github/workflows/php.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: PHP Composer
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Validate composer.json and composer.lock
21+
run: composer validate --strict
22+
23+
- name: Cache Composer packages
24+
id: composer-cache
25+
uses: actions/cache@v3
26+
with:
27+
path: vendor
28+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
29+
restore-keys: |
30+
${{ runner.os }}-php-
31+
32+
- name: Install dependencies
33+
run: composer install --prefer-dist --no-progress
34+
35+
- name: Run test suite
36+
run: composer run-script test

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.DS_Store
2+
3+
# IDEs
4+
.idea/
5+
.vscode/
6+
7+
# Composer
8+
composer.lock
9+
vendor/
10+
11+
# Cache
12+
*.cache
13+
14+
nodemon.json
15+
package-lock.json
16+
package.json
17+
node_modules
18+
19+
.env

.pre-commit-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
repos:
2+
- repo: https://github.com/digitalpulp/pre-commit-php.git
3+
rev: 1.4.0
4+
hooks:
5+
- id: php-lint
6+
7+
- repo: https://github.com/digitalpulp/pre-commit-php.git
8+
rev: 1.4.0
9+
hooks:
10+
- id: php-cs-fixer
11+
files: \.(php)$
12+
13+
# - repo: https://github.com/digitalpulp/pre-commit-php.git
14+
# rev: 1.4.0
15+
# hooks:
16+
# - id: php-stan
17+
# files: \.(php)$
18+
19+
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
20+
rev: v9.1.0
21+
hooks:
22+
- id: commitlint
23+
stages: [ commit-msg ]
24+
additional_dependencies: [ '@commitlint/config-angular' ]

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Saraf Query Builder
2+
3+
This project is an easy-to-use query builder and mysql connection manager for saraf projects.
4+
5+
6+
### Development
7+
8+
Before anything please ensure that you have `phpcs` `php-cs-fixer` installed on your machine. Then run the following
9+
commands to set up project for yourself.
10+
11+
```console
12+
sudo npm i -g @commitlint/config-conventional
13+
pip install pre-commit
14+
pre-commit install
15+
pre-commit install --hook-type commit-msg
16+
```

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {extends: ['@commitlint/config-conventional']}

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "saraf/qb",
3+
"description": "an easy to use query builder for mysql",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Parsa Dehfuli",
8+
"email": "parsa@saraf.app"
9+
},
10+
{
11+
"name": "Hassan Parasteh",
12+
"email": "hassan@saraf.app"
13+
}
14+
],
15+
"require": {
16+
"php": ">=8.0",
17+
"react/mysql": "^0.5.7",
18+
"vlucas/phpdotenv": "^5.5.0"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Saraf\\QB\\": "src"
23+
}
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "^9",
27+
"friendsofphp/php-cs-fixer": "^3.12",
28+
"phpstan/phpstan": "^1.8"
29+
},
30+
"scripts": {
31+
"test": "./vendor/bin/phpunit tests",
32+
"phpcs": "phpcs --parallel=4 --standard=PSR1 src/",
33+
"phpcs-fixer": "php-cs-fixer fix src/",
34+
"phpstan": "vendor/bin/phpstan analyse src tests"
35+
}
36+
}

example/delete.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
use Dotenv\Dotenv;
4+
use React\EventLoop\Loop;
5+
use Saraf\QB\QueryBuilder\Core\DBFactory;
6+
use Saraf\QB\QueryBuilder\Enums\OrderDirection;
7+
use Saraf\QB\QueryBuilder\Exceptions\DBFactoryException;
8+
9+
include "vendor/autoload.php";
10+
11+
// Loop
12+
$loop = Loop::get();
13+
14+
// Environments
15+
$env = Dotenv::createImmutable(__DIR__ . "/../");
16+
$env->load();
17+
18+
// Env Loader
19+
$DB_NAME = $_ENV['DB_NAME'];
20+
$DB_USER = $_ENV['DB_USER'];
21+
$DB_PASS = $_ENV['DB_PASS'];
22+
$DB_HOST = $_ENV['DB_HOST'];
23+
$DB_PORT_READ = $_ENV['DB_PORT_READ'];
24+
$DB_PORT_WRITE = $_ENV['DB_PORT_WRITE'];
25+
26+
27+
try {
28+
$dbFactory = new DBFactory(
29+
$loop,
30+
$DB_HOST,
31+
$DB_NAME,
32+
$DB_USER,
33+
$DB_PASS,
34+
$DB_PORT_WRITE,
35+
$DB_PORT_READ,
36+
5,
37+
5,
38+
2,
39+
2
40+
);
41+
} catch (DBFactoryException $e) {
42+
echo $e->getMessage();
43+
exit(1);
44+
}
45+
46+
47+
$loop->run();
48+
49+
// DELETE FROM Customers WHERE CustomerName= 'Alfreds Futterkiste';
50+
$dbFactory->getQueryBuilder()
51+
->delete()
52+
->from("Customers")
53+
->where("CustomerName", "Alfreds Futterkiste")
54+
->compile()
55+
->getQuery()
56+
->then(function ($result) {
57+
echo "Excepted: DELETE FROM Customers WHERE CustomerName= 'Alfreds Futterkiste'" . PHP_EOL;
58+
echo "Actual: " . $result['query'] . PHP_EOL . PHP_EOL;
59+
});
60+
61+
// DELETE FROM Customers WHERE id >= 10 LIMIT 3
62+
$dbFactory->getQueryBuilder()
63+
->delete()
64+
->from("Customers")
65+
->whereGreater("id", 10, true)
66+
->setLimit(3)
67+
->compile()
68+
->getQuery()
69+
->then(function ($result) {
70+
echo "Excepted: DELETE FROM Customers WHERE id >= 10 LIMIT 3" . PHP_EOL;
71+
echo "Actual: " . $result['query'] . PHP_EOL . PHP_EOL;
72+
});

example/insert.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
use Dotenv\Dotenv;
4+
use React\EventLoop\Loop;
5+
use Saraf\QB\QueryBuilder\Core\DBFactory;
6+
use Saraf\QB\QueryBuilder\Enums\OrderDirection;
7+
use Saraf\QB\QueryBuilder\Exceptions\DBFactoryException;
8+
9+
include "vendor/autoload.php";
10+
11+
// Loop
12+
$loop = Loop::get();
13+
14+
// Environments
15+
$env = Dotenv::createImmutable(__DIR__ . "/../");
16+
$env->load();
17+
18+
// Env Loader
19+
$DB_NAME = $_ENV['DB_NAME'];
20+
$DB_USER = $_ENV['DB_USER'];
21+
$DB_PASS = $_ENV['DB_PASS'];
22+
$DB_HOST = $_ENV['DB_HOST'];
23+
$DB_PORT_READ = $_ENV['DB_PORT_READ'];
24+
$DB_PORT_WRITE = $_ENV['DB_PORT_WRITE'];
25+
26+
27+
try {
28+
$dbFactory = new DBFactory(
29+
$loop,
30+
$DB_HOST,
31+
$DB_NAME,
32+
$DB_USER,
33+
$DB_PASS,
34+
$DB_PORT_WRITE,
35+
$DB_PORT_READ,
36+
5,
37+
5,
38+
2,
39+
2
40+
);
41+
} catch (DBFactoryException $e) {
42+
echo $e->getMessage();
43+
exit(1);
44+
}
45+
46+
47+
$loop->run();
48+
49+
// INSERT INTO Customers (CustomerName, ContactName, Address) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21');
50+
$dbFactory->getQueryBuilder()
51+
->insert()
52+
->into("Customers")
53+
->setColumns(["CustomerName", "ContactName", "Address"])
54+
->addRow(['Cardinal', 'Tom B. Erichsen', 'Skagen 21'])
55+
->compile()
56+
->getQuery()
57+
->then(function ($result) {
58+
echo "Excepted: INSERT INTO Customers (CustomerName, ContactName, Address) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21')" . PHP_EOL;
59+
echo "Actual: " . $result['query'] . PHP_EOL . PHP_EOL;
60+
});
61+
62+
63+
// INSERT INTO Customers (CustomerName, ContactName, Address) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21'),('MamadReza', 'Ola Sr', 'Samoel');
64+
$dbFactory->getQueryBuilder()
65+
->insert()
66+
->into("Customers")
67+
->setColumns(["CustomerName", "ContactName", "Address"])
68+
->addRows([
69+
['Cardinal', 'Tom B. Erichsen', 'Skagen 21'],
70+
['MamadReza', 'Ola Sr', 'Samoel'],
71+
])
72+
->compile()
73+
->getQuery()
74+
->then(function ($result) {
75+
echo "Excepted: INSERT INTO Customers (CustomerName, ContactName, Address) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21'),('MamadReza', 'Ola Sr', 'Samoel')" . PHP_EOL;
76+
echo "Actual: " . $result['query'] . PHP_EOL . PHP_EOL;
77+
});

example/insertUpdate.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
use Dotenv\Dotenv;
4+
use React\EventLoop\Loop;
5+
use Saraf\QB\QueryBuilder\Core\DBFactory;
6+
use Saraf\QB\QueryBuilder\Enums\OrderDirection;
7+
use Saraf\QB\QueryBuilder\Exceptions\DBFactoryException;
8+
9+
include "vendor/autoload.php";
10+
11+
// Loop
12+
$loop = Loop::get();
13+
14+
// Environments
15+
$env = Dotenv::createImmutable(__DIR__ . "/../");
16+
$env->load();
17+
18+
// Env Loader
19+
$DB_NAME = $_ENV['DB_NAME'];
20+
$DB_USER = $_ENV['DB_USER'];
21+
$DB_PASS = $_ENV['DB_PASS'];
22+
$DB_HOST = $_ENV['DB_HOST'];
23+
$DB_PORT_READ = $_ENV['DB_PORT_READ'];
24+
$DB_PORT_WRITE = $_ENV['DB_PORT_WRITE'];
25+
26+
27+
try {
28+
$dbFactory = new DBFactory(
29+
$loop,
30+
$DB_HOST,
31+
$DB_NAME,
32+
$DB_USER,
33+
$DB_PASS,
34+
$DB_PORT_WRITE,
35+
$DB_PORT_READ,
36+
5,
37+
5,
38+
2,
39+
2
40+
);
41+
} catch (DBFactoryException $e) {
42+
echo $e->getMessage();
43+
exit(1);
44+
}
45+
46+
47+
$loop->run();
48+
49+
// INSERT INTO Users (id, name, age) VALUES (10, "Roy", 34) ON DUPLICATE KEY UPDATE name="Roy", age=34
50+
$dbFactory->getQueryBuilder()
51+
->insertUpdate()
52+
->into("Users")
53+
->setColumns(["id", "name", "age"])
54+
->setRow([10, 'Ray', 34])
55+
->setUpdates([
56+
'name' => "Ray",
57+
'age' => 34
58+
])
59+
->compile()
60+
->getQuery()
61+
->then(function ($result) {
62+
echo "Excepted: INSERT INTO Users (id, name, age) VALUES (10, \"Roy\", 34) ON DUPLICATE KEY UPDATE name=\"Roy\", age=34" . PHP_EOL;
63+
echo "Actual: " . $result['query'] . PHP_EOL . PHP_EOL;
64+
});

0 commit comments

Comments
 (0)