Skip to content

Commit e4849bb

Browse files
author
Fredrick Peter
committed
Query Method Added
1 parent a2d2300 commit e4849bb

File tree

7 files changed

+70
-30
lines changed

7 files changed

+70
-30
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ was pretty tough. So i decided to create a much more easier way of communicating
6060
* [Get Pagination](#get-pagination)
6161
* [Clause](#clause)
6262
* [Raw](#raw)
63+
* [Query](#query)
6364
* [select](#select)
6465
* [selectRaw](#selectRaw)
6566
* [orderBy](#orderby)
@@ -118,7 +119,7 @@ Prior to installing `php-orm-database` get the [Composer](https://getcomposer.or
118119
**Step 1** — update your `composer.json`:
119120
```composer.json
120121
"require": {
121-
"peterson/database": "^4.3.1"
122+
"peterson/database": "^4.3.2"
122123
}
123124
```
124125

@@ -720,6 +721,15 @@ $db->table("tb_wallet")
720721
->get();
721722
```
722723

724+
### Query
725+
- Allows the use direct sql query `SQL query syntax`
726+
727+
```
728+
$db->query("SHOW COLUMNS FROM users")
729+
->limit(10)
730+
->get();
731+
```
732+
723733
### Select
724734
- Used to select needed columns from database
725735

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"filp/whoops": "^2.15",
2323
"symfony/var-dumper": "^5.4.3"
2424
},
25+
"conflict": {
26+
"symfony/var-dumper": ">=6.0.0"
27+
},
2528
"autoload": {
2629
"files": [
2730
"src/helpers.php"

src/Connectors/Connector.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ public function tableExists(?string $table)
7171
return $this->table('')->tableExists($table);
7272
}
7373

74+
/**
75+
* Direct Query Expression
76+
*
77+
* @param string $query
78+
* @return \builder\Database\Schema\Builder
79+
*/
80+
public function query(string $query)
81+
{
82+
return $this->table('')->query($query);
83+
}
84+
7485
/**
7586
* Configuring pagination settings
7687
* @param array $options
@@ -158,21 +169,6 @@ public function dbConnection()
158169
]);
159170
}
160171

161-
/**
162-
* Query method uses default connection only.
163-
* or you can use [connection('connName')->getPDO()->query()]
164-
*
165-
* @param string $query\Raw sql query string
166-
*
167-
* @return $this
168-
* The PDO instance or null if the connection is not established.
169-
*/
170-
public function query($query)
171-
{
172-
$pdo = $this->getPDO();
173-
return !is_null($pdo) ? $pdo->query($query) : $pdo;
174-
}
175-
176172
/**
177173
* Get the PDO instance for the current database driver.
178174
*

src/Migrations/Schema.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ class Schema{
2222
* @var object\builder\Database\DB
2323
*/
2424
private static $db;
25+
26+
/**
27+
* Instance of Database Object
28+
*
29+
* @var mixed
30+
*/
31+
private static $pdo;
32+
2533

2634
/**
2735
* Creating Instance of Database
@@ -31,6 +39,7 @@ class Schema{
3139
private static function initSchemaDatabase()
3240
{
3341
self::$db = DB::connection();
42+
self::$pdo = self::$db->getPDO();
3443
}
3544

3645
/**
@@ -98,7 +107,7 @@ public static function updateColumnDefaultValue(?string $table, ?string $column,
98107
$formatValue = self::formatDefaultValue($value);
99108

100109
// Get the current column definition
101-
$stmt = self::$db->query("DESCRIBE {$table} {$column}");
110+
$stmt = self::$pdo->query("DESCRIBE {$table} {$column}");
102111
$columnInfo = $stmt->execute()->fetch(PDO::FETCH_ASSOC);
103112

104113
// Extract the column type, nullability, and constraints
@@ -122,7 +131,7 @@ public static function updateColumnDefaultValue(?string $table, ?string $column,
122131
}
123132

124133
// execute query
125-
self::$db->query($query)->execute();
134+
self::$pdo->query($query)->execute();
126135

127136
return [
128137
'status' => Constant::STATUS_200,
@@ -163,12 +172,11 @@ public static function dropTable(?string $tableName, $force = false)
163172
try{
164173
// DROP TABLE IF EXISTS
165174
if($force){
166-
$pdo = self::$db->getPDO();
167-
$pdo->exec("SET FOREIGN_KEY_CHECKS = 0; "); // Disable foreign key checks temporarily
168-
$pdo->exec("DROP TABLE {$tableName} CASCADE;"); // Drop the table with CASCADE option
169-
$pdo->exec("SET FOREIGN_KEY_CHECKS = 1;"); // Enable foreign key checks again
175+
self::$pdo->exec("SET FOREIGN_KEY_CHECKS = 0; "); // Disable foreign key checks temporarily
176+
self::$pdo->exec("DROP TABLE {$tableName} CASCADE;"); // Drop the table with CASCADE option
177+
self::$pdo->exec("SET FOREIGN_KEY_CHECKS = 1;"); // Enable foreign key checks again
170178
} else{
171-
self::$db->query( "DROP TABLE {$tableName};" )->execute();
179+
self::$pdo->query( "DROP TABLE {$tableName};" )->execute();
172180
}
173181

174182
return [
@@ -217,13 +225,13 @@ public static function dropColumn(?string $tableName, ?string $columnName)
217225
// Handle query
218226
try{
219227
// DROP COLUMN IF EXISTS
220-
self::$db->query( "ALTER TABLE {$tableName} DROP COLUMN {$columnName};" )->execute();
228+
self::$pdo->query( "ALTER TABLE {$tableName} DROP COLUMN {$columnName};" )->execute();
221229

222230
// DROP COLUMN TRIGGERS
223-
self::$db->query( "DROP TRIGGER IF EXISTS {$columnName}_created_at;" )->execute();
231+
self::$pdo->query( "DROP TRIGGER IF EXISTS {$columnName}_created_at;" )->execute();
224232

225233
// DROP COLUMN TRIGGERS
226-
self::$db->query( "DROP TRIGGER IF EXISTS {$columnName}_updated_at;" )->execute();
234+
self::$pdo->query( "DROP TRIGGER IF EXISTS {$columnName}_updated_at;" )->execute();
227235

228236
return [
229237
'status' => Constant::STATUS_200,

src/Schema/Builder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ public function from($table, $as = null)
4646
return $this;
4747
}
4848

49+
/**
50+
* Direct Query Expression
51+
*
52+
* @param string $query
53+
* @return $this
54+
*/
55+
public function query(string $query)
56+
{
57+
$this->query = $query;
58+
59+
return $this;
60+
}
61+
4962
/**
5063
* Columns to be selected.
5164
*

src/Schema/Traits/BuilderTrait.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,11 @@ protected function statement($query, $bindings = [], $ignore = false)
885885
$pdo = $this->connection->pdo;
886886

887887
try {
888+
// checking if global query is used
889+
if(!empty($this->query)){
890+
$query = $this->query;
891+
}
892+
888893
$statement = $pdo->prepare($query);
889894
$this->bindValues(
890895
$statement,
@@ -898,6 +903,7 @@ protected function statement($query, $bindings = [], $ignore = false)
898903
}
899904

900905
$this->connection->statement = $statement ?? null;
906+
$this->query = null;
901907

902908
return $this;
903909
}
@@ -1340,6 +1346,7 @@ protected function close($table = true)
13401346
$this->wheres = [];
13411347
$this->columns = null;
13421348
$this->method = null;
1349+
$this->query = null;
13431350
$this->joins = null;
13441351
$this->orders = null;
13451352
$this->havings = null;
@@ -1348,7 +1355,7 @@ protected function close($table = true)
13481355
$this->offset = null;
13491356
$this->runtime = 0.00;
13501357
if($table){
1351-
$this->from = null;
1358+
$this->from = null;
13521359
}
13531360
}
13541361

@@ -1392,7 +1399,5 @@ protected function errorException(mixed $exception)
13921399
exit(1);
13931400
}
13941401

1395-
}
1396-
1397-
1402+
}
13981403

src/Schema/Traits/MySqlProperties.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ trait MySqlProperties{
3030
* @var string
3131
*/
3232
public $method;
33+
34+
/**
35+
* @var string
36+
*/
37+
public $query;
3338

3439
/**
3540
* The current query value bindings.

0 commit comments

Comments
 (0)