Skip to content

Commit

Permalink
Merge pull request #2 from SonyPradana/dev
Browse files Browse the repository at this point in the history
Maintenance code base
  • Loading branch information
SonyPradana authored Apr 6, 2022
2 parents 93e75bc + dc7d44b commit 962c5ee
Show file tree
Hide file tree
Showing 16 changed files with 544 additions and 194 deletions.
5 changes: 5 additions & 0 deletions src/System/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,9 @@ private function flatten_recursing(array $array, $depth = INF): array
return $result;
}

public function immutable(): CollectionImmutable
{
return new CollectionImmutable($this->collection);
}

}
11 changes: 0 additions & 11 deletions src/System/Database/CrudInterface.php

This file was deleted.

5 changes: 2 additions & 3 deletions src/System/Database/MyCRUD.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

use System\Collection\Collection;
use System\Collection\CollectionImmutable;
use System\Database\CrudInterface;
use System\Database\MyPDO;
use System\Database\MyQuery\Join\InnerJoin;

abstract class MyCRUD implements CrudInterface
abstract class MyCRUD
{
/** @var MyPDO|null */
protected $PDO = null;
Expand All @@ -25,7 +24,7 @@ abstract class MyCRUD implements CrudInterface

public function getID()
{
return $this->PRIMERY_KEY ?? null;
return $this->IDENTIFER;
}

public function setID($val)
Expand Down
44 changes: 36 additions & 8 deletions src/System/Database/MyPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class MyPDO
private $user = DB_USER;
private $pass = DB_PASS;

/** @var \PDO PDO */
private $dbh;
/** @var \PDOStatement */
private $stmt;

public function __construct(string $database_name = DB_NAME)
Expand All @@ -29,11 +31,13 @@ public function __construct(string $database_name = DB_NAME)
}
}

/** Create connaction using static */
public static function conn(string $database_name = DB_NAME)
{
return new self($database_name);
}

/** @var self[] */
private static $MySelf = [];

/**
Expand All @@ -42,15 +46,15 @@ public static function conn(string $database_name = DB_NAME)
* @param string $database_name string Database Name *
* @return MyPDO MyPDO with singleton
*/
public static function getInstance(string $database_name = DB_NAME)
public static function getInstance(string $database_name = DB_NAME): self
{
return self::$MySelf[$database_name] = self::$MySelf[$database_name] ?? new self($database_name);
}

/**
* mempersiapkan statement pada query
*/
public function query(string $query)
public function query(string $query): self
{
$this->stmt = $this->dbh->prepare($query);
return $this;
Expand All @@ -59,7 +63,7 @@ public function query(string $query)
/**
* menggantikan paramater input dari user dengan sebuah placeholder
*/
public function bind($param, $value, $type = null)
public function bind($param, $value, $type = null): self
{
if (is_null($type)) {
switch (true){
Expand All @@ -84,7 +88,10 @@ public function bind($param, $value, $type = null)
}

/**
* menjalankan atau mengeksekusi query
* Menjalankan atau mengeksekusi query
*
* @return bool True if success
* @throws \PDOException
*/
public function execute()
{
Expand All @@ -111,6 +118,8 @@ public function single()

/**
* menampilkan jumlah data yang berhasil di simpan, di ubah maupun dihapus
*
* @return int The number of rows.
*/
public function rowCount()
{
Expand All @@ -119,24 +128,43 @@ public function rowCount()

/**
* id dari data yang terakhir disimpan
* @return string last id
* @return string|false last id
*/
public function lastInsertId()
{
return $this->dbh->lastInsertId();
}

public function beginTransaction()
/**
* Initiates a transaction
*
* @return bool True if success
* @throws \PDOException
*/
public function beginTransaction(): bool
{
return $this->dbh->beginTransaction();
}

public function endTransaction()
/**
* Commits a transaction
*
* @return bool True if success
* @throws \PDOException
*/
public function endTransaction(): bool
{
return $this->dbh->commit();
}

public function cancelTransaction(){
/**
* Rolls back a transaction
*
* @return bool True if success
* @throws \PDOException
*/
public function cancelTransaction(): bool
{
return $this->dbh->rollBack();
}
}
7 changes: 7 additions & 0 deletions src/System/Database/MyQuery/Fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace System\Database\MyQuery;

use System\Collection\Collection;

abstract class Fetch extends Query
{
public function get(): ?Collection
{
return new Collection($this->all());
}

public function single(): array
{
$this->builder();
Expand Down
Loading

0 comments on commit 962c5ee

Please sign in to comment.