mirror of
https://codeberg.org/vlw/scaffold.git
synced 2026-02-26 11:31:57 +01:00
Compare commits
No commits in common. "master" and "1.7.1" have entirely different histories.
2 changed files with 9 additions and 25 deletions
|
|
@ -19,8 +19,6 @@
|
||||||
private const DEFAULT_USERNAME = "www-data";
|
private const DEFAULT_USERNAME = "www-data";
|
||||||
private const DEFAULT_PASSWORD = "";
|
private const DEFAULT_PASSWORD = "";
|
||||||
|
|
||||||
private static ?Database $instance = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Database instance from credentials
|
* Create a new Database instance from credentials
|
||||||
*
|
*
|
||||||
|
|
@ -34,10 +32,10 @@
|
||||||
?string $host = self::DEFAULT_HOSTNAME,
|
?string $host = self::DEFAULT_HOSTNAME,
|
||||||
?string $username = self::DEFAULT_USERNAME,
|
?string $username = self::DEFAULT_USERNAME,
|
||||||
?string $password = self::DEFAULT_PASSWORD,
|
?string $password = self::DEFAULT_PASSWORD,
|
||||||
?string $database = ""
|
string $database
|
||||||
) {
|
) {
|
||||||
// Create key if it does not exist
|
// Create key if it does not exist
|
||||||
if (!array_key_exists("mariadb", $_ENV)) {
|
if (!$_ENV["mariadb"]) {
|
||||||
$_ENV["mariadb"] = [];
|
$_ENV["mariadb"] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,16 +44,6 @@
|
||||||
$_ENV["mariadb"]["user"] = $username;
|
$_ENV["mariadb"]["user"] = $username;
|
||||||
$_ENV["mariadb"]["pass"] = $password;
|
$_ENV["mariadb"]["pass"] = $password;
|
||||||
$_ENV["mariadb"]["db"] = $database;
|
$_ENV["mariadb"]["db"] = $database;
|
||||||
|
|
||||||
self::$instance = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function instance(): static {
|
|
||||||
if (self::$instance) {
|
|
||||||
return self::$instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new static();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -68,8 +56,6 @@
|
||||||
$_ENV["mariadb"]["pass"],
|
$_ENV["mariadb"]["pass"],
|
||||||
$_ENV["mariadb"]["db"],
|
$_ENV["mariadb"]["db"],
|
||||||
);
|
);
|
||||||
|
|
||||||
self::$instance = $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,9 @@
|
||||||
public const DATE_FORMAT = Database::DATE_FORMAT;
|
public const DATE_FORMAT = Database::DATE_FORMAT;
|
||||||
public const DATETIME_FORMAT = Database::DATETIME_FORMAT;
|
public const DATETIME_FORMAT = Database::DATETIME_FORMAT;
|
||||||
|
|
||||||
abstract public int|string $id { get; }
|
abstract public string $id { get; }
|
||||||
|
|
||||||
public readonly Database $db;
|
|
||||||
|
|
||||||
|
protected readonly Database $db;
|
||||||
private bool $_resolved = false;
|
private bool $_resolved = false;
|
||||||
private bool $_isset;
|
private bool $_isset;
|
||||||
private ?array $_row;
|
private ?array $_row;
|
||||||
|
|
@ -27,7 +26,7 @@
|
||||||
* @param string $table The target database table
|
* @param string $table The target database table
|
||||||
*/
|
*/
|
||||||
protected static function create(string $table, array $values): bool {
|
protected static function create(string $table, array $values): bool {
|
||||||
return Database::instance()->from($table)->insert($values);
|
return new Database()->from($table)->insert($values);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -42,7 +41,7 @@
|
||||||
public readonly array $columns,
|
public readonly array $columns,
|
||||||
public readonly array $where
|
public readonly array $where
|
||||||
) {
|
) {
|
||||||
$this->db = Database::instance();
|
$this->db = new Database();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -75,7 +74,7 @@
|
||||||
* @return bool Entity exists
|
* @return bool Entity exists
|
||||||
*/
|
*/
|
||||||
public function isset(): bool {
|
public function isset(): bool {
|
||||||
return $this->_isset ??= Database::instance()
|
return $this->_isset ??= new Database()
|
||||||
->from($this->table)
|
->from($this->table)
|
||||||
->where($this->where)
|
->where($this->where)
|
||||||
->limit(1)
|
->limit(1)
|
||||||
|
|
@ -98,10 +97,9 @@
|
||||||
*
|
*
|
||||||
* @param string $key Target column to update
|
* @param string $key Target column to update
|
||||||
* @param mixed $value New value of target column
|
* @param mixed $value New value of target column
|
||||||
* @param return $return (optional) Return this instead of $value when set
|
|
||||||
* @return mixed The value that was sent to $value
|
* @return mixed The value that was sent to $value
|
||||||
*/
|
*/
|
||||||
public function set(string $key, mixed $value, mixed $return = null): mixed {
|
public function set(string $key, mixed $value): mixed {
|
||||||
$this->_row[$key] = $value;
|
$this->_row[$key] = $value;
|
||||||
|
|
||||||
$this->db
|
$this->db
|
||||||
|
|
@ -109,6 +107,6 @@
|
||||||
->where($this->where)
|
->where($this->where)
|
||||||
->update([$key => $value]);
|
->update([$key => $value]);
|
||||||
|
|
||||||
return func_num_args() === 2 ? $value : $return;
|
return $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue