Skip to content

Commit e4b7722

Browse files
update
1 parent eae9383 commit e4b7722

File tree

14 files changed

+75
-39
lines changed

14 files changed

+75
-39
lines changed

src/Connectors/ConnectionBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ class ConnectionBuilder
6565
public function __construct($data = null, $name = null, $connection = null)
6666
{
6767
if($data){
68-
$this->timer = new DateTime();
69-
$this->pdo = $connection['pdo'] ?? $connection['message'];
70-
$this->config = array_merge($connection['config'], ['name' => $name]);
71-
$this->database = $this->config['database'];
72-
$this->tablePrefix = $this->getTablePrefixIfAllowed();
68+
$this->timer = new DateTime();
69+
$this->pdo = $connection['pdo'] ?? $connection['message'];
70+
$this->config = array_merge($connection['config'], ['name' => $name]);
71+
$this->database = $this->config['database'];
72+
$this->tablePrefix = $this->getTablePrefixIfAllowed();
7373
}
7474
}
7575

src/Connectors/Connector.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class Connector extends DatabaseManager{
2929
*/
3030
private $connection;
3131

32+
/**
33+
* @var string|null
34+
*/
35+
private $prefix;
36+
3237
/**
3338
* @var array|null
3439
*/
@@ -227,6 +232,12 @@ private function buidTable($table = null)
227232
// set connection
228233
$instance->connection = $this->dbConnection();
229234

235+
// update builder table prefix
236+
// only when the prefix is not empty
237+
if(!is_null($instance->prefix)){
238+
$instance->connection['config']['prefix'] = $instance->prefix;
239+
}
240+
230241
// setup table name
231242
$builder->from = $table;
232243

@@ -247,14 +258,26 @@ private function buidTable($table = null)
247258
return $builder;
248259
}
249260

261+
/**
262+
* Change the prefix of the currently selected database driver
263+
* @param string $prefix
264+
* @return void
265+
*/
266+
public function changeTablePrefix($prefix)
267+
{
268+
$this->prefix = $prefix;
269+
}
270+
250271
/**
251272
* Get the prefix of the currently selected database driver
252273
*
253274
* @return string|null
254275
*/
255276
public function getTablePrefix()
256277
{
257-
return $this->getDataByMode('prefix');
278+
return !is_null($this->prefix)
279+
? $this->prefix
280+
: $this->getDataByMode('prefix');
258281
}
259282

260283
/**

src/Console/Commands/DBCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function export()
125125
$export = new DBExport(
126126
saveAsFileType: $as,
127127
connection: $connection,
128-
retentionDays: (int) $days ?: 7
128+
retentionDays: (int) $days ?: 5
129129
);
130130

131131
$this->checkConnection($export->conn);

src/DB.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* @method \Tamedevelopers\Database\Schema\Builder avg(Expression|string $column)
5454
*
5555
* @method \Tamedevelopers\Database\Schema\Expression raw(mixed $value)
56-
* @method \Tamedevelopers\Database\Schema\Builder totalQueryDuration()
56+
* @method \Tamedevelopers\Database\Schema\Builder runTime()
5757
* @method \Tamedevelopers\Database\Schema\Builder tableName()
5858
* @method \Tamedevelopers\Database\Schema\Builder query(string $query)
5959
*
@@ -62,6 +62,7 @@
6262
* @method \Tamedevelopers\Database\Connectors\Connector getConfig()
6363
* @method \Tamedevelopers\Database\Connectors\Connector getDatabaseName()
6464
* @method \Tamedevelopers\Database\Connectors\Connector getTablePrefix()
65+
* @method \Tamedevelopers\Database\Connectors\Connector changeTablePrefix(string $prefix)
6566
*
6667
* @see \Tamedevelopers\Database\Schema\Builder
6768
* @see \Tamedevelopers\Database\DatabaseManager

src/DBExport.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class DBExport
9292
* @param string|null $saveAsFileType Save the backup file as [zip|rar]
9393
* @param int $retentionDays Days to keep backups before deletion
9494
*/
95-
public function __construct($connection = null, $saveAsFileType = null, int $retentionDays = 7)
95+
public function __construct($connection = null, $saveAsFileType = null, ?int $retentionDays = 5)
9696
{
9797
$saveAsFileType = Str::lower($saveAsFileType);
9898

@@ -106,6 +106,10 @@ public function __construct($connection = null, $saveAsFileType = null, int $ret
106106

107107
$this->conn = DB::connection($connection);
108108
$this->db = $this->conn->dbConnection();
109+
110+
// since we're exporting from a live/database server
111+
// we need to temporarily disable prefixing, by setting its value to empty string
112+
$this->conn->changeTablePrefix('');
109113
}
110114

111115
/**
@@ -129,10 +133,16 @@ public function run($backupDir = null)
129133
$filename = "{$dbName}_backup_{$timestamp}.sql";
130134
$path = "{$backupDir}/{$filename}";
131135

136+
// make required directory
132137
if (!File::isDirectory($backupDir)) {
133138
File::makeDirectory($backupDir, 0755, true);
134139
}
135140

141+
// Cleanup old backups if expired
142+
if ($this->retentionDays >= 0) {
143+
$this->cleanupOldBackups($backupDir, $this->retentionDays);
144+
}
145+
136146
$fh = fopen($path, 'w');
137147
if (!$fh) {
138148
$this->message = sprintf("Cannot open file for writing: [`%s`]", $path);
@@ -166,11 +176,6 @@ public function run($backupDir = null)
166176
// Compress the backup using
167177
$this->saveFileAs($path);
168178

169-
// Cleanup old backups if expired
170-
if ($this->retentionDays > 0) {
171-
$this->cleanupOldBackups($backupDir, $this->retentionDays);
172-
}
173-
174179
$this->error = Constant::STATUS_200;
175180
$this->message = "- Database has been exported successfully.";
176181
} catch (\Throwable $e) {

src/Schema/Builder.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,12 @@ public function paginate($perPage = 15, $pageParam = 'page')
14421442
$this->setMethod(__FUNCTION__);
14431443

14441444
// paginator data
1445-
$results = $paginator->getPagination($totalCount, $perPage, $this);
1445+
$results = $paginator->getPagination($totalCount, $perPage, $this);
1446+
1447+
1448+
dd(
1449+
$results
1450+
);
14461451

14471452
return new Collection($results['data'], $results['builder']);
14481453
}
@@ -1727,19 +1732,19 @@ public function dump()
17271732
{
17281733
dump($this->toSql(), $this->getBindings());
17291734

1730-
$this->totalQueryDuration();
1735+
$this->runTime();
17311736

17321737
return $this;
17331738
}
17341739

17351740
/**
17361741
* Die and dump the current SQL and bindings.
17371742
*
1738-
* @return never
1743+
* @return void
17391744
*/
17401745
public function dd()
17411746
{
1742-
$this->totalQueryDuration();
1747+
$this->runTime();
17431748

17441749
dd($this->toSql(), $this->getBindings(), $this);
17451750
}

src/Schema/Pagination/Paginator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Tamedevelopers\Database\Schema\Pagination\PaginatorAsset;
1818
use Tamedevelopers\Database\Schema\Pagination\Yidas\PaginationLoader;
1919
use Tamedevelopers\Database\Schema\Pagination\Yidas\PaginationWidget;
20-
20+
use Tamedevelopers\Support\Process\HttpRequest;
2121

2222
class Paginator extends Builder{
2323

@@ -322,7 +322,7 @@ private function resetKeys(?array $options = [], $links = true)
322322
*
323323
* @return $this
324324
*/
325-
protected function getPagination($totalCount, int|string $perPage = 15, Builder $query = null)
325+
protected function getPagination($totalCount, int|string $perPage = 15, Builder $query)
326326
{
327327
try {
328328
// convert to int
@@ -350,8 +350,11 @@ protected function getPagination($totalCount, int|string $perPage = 15, Builder
350350
// and collect that from the var above\$perPage
351351
$this->pagination->setPerPage($perPage);
352352

353+
// get all request input - Both POST and GET
354+
$input = HttpRequest::input();
355+
353356
// auto setup pageParam
354-
$pageParam = $_GET[$this->pageParam] ?? $this->pagination->page;
357+
$pageParam = $input[$this->pageParam] ?? $this->pagination->page;
355358

356359
// To avoi conflicts on multiple pagination
357360
// in same page. We autoset from $_GET request

src/Schema/Pagination/PaginatorAsset.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ class PaginatorAsset
88

99
/**
1010
* Pagination Default Texts
11-
* @param string $mode
11+
* @param string|null $mode
1212
* [optional] get array data
1313
*
1414
* @return array
1515
*/
16-
public static function texts(string $mode = null)
16+
public static function texts($mode = null)
1717
{
1818
$data = [
1919
'first' => 'First',
@@ -34,12 +34,12 @@ public static function texts(string $mode = null)
3434

3535
/**
3636
* Pagination Views Style
37-
* @param string $mode
37+
* @param string|null $mode
3838
* [optional] get array data
3939
*
4040
* @return array|string
4141
*/
42-
public static function views(string $mode = null)
42+
public static function views($mode = null)
4343
{
4444
$data = [
4545
'bootstrap' => 'bootstrap',

src/Schema/Traits/BuilderTrait.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
use DateTime;
1010
use Exception;
1111
use DateTimeInterface;
12-
use Throwable;
1312
use Tamedevelopers\Support\Capsule\Forge;
1413
use Tamedevelopers\Database\Schema\Builder;
15-
use Tamedevelopers\Support\Capsule\Manager;
1614
use Tamedevelopers\Database\Schema\Expression;
1715
use Tamedevelopers\Database\Schema\JoinClause;
18-
use Tamedevelopers\Support\Capsule\DebugManager;
1916
use Tamedevelopers\Support\Collections\Collection;
2017
use Tamedevelopers\Database\Schema\BuilderCompiler;
2118
use Tamedevelopers\Database\Schema\Pagination\Paginator;
@@ -1385,24 +1382,24 @@ protected function close($table = true)
13851382
$this->groups = null;
13861383
$this->limit = null;
13871384
$this->offset = null;
1388-
$this->runtime = 0.00;
13891385
if($table){
13901386
$this->from = null;
1391-
}
1387+
$this->runTime();
1388+
}
13921389
}
13931390

13941391
/**
13951392
* Get total query execution time
13961393
*
13971394
* @return void
13981395
*/
1399-
protected function totalQueryDuration()
1396+
protected function runTime()
14001397
{
14011398
$start = $this->connection->timer;
14021399
$end = new DateTime();
14031400

14041401
// time difference
1405-
if(is_object($start)){
1402+
if(is_object($start) && $start instanceof DateTime){
14061403
$diff = $start->diff($end);
14071404

14081405
// runtime

src/Schema/Traits/MySqlProperties.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ trait MySqlProperties{
147147
];
148148

149149
/**
150-
* @var float|int
150+
* @var int|null
151151
*/
152-
public $runtime = 0.00;
152+
public $runtime;
153153
}
154154

155155

0 commit comments

Comments
 (0)