mirror of
https://codeberg.org/vlw/scaffold.git
synced 2026-02-26 03:21:57 +01:00
Compare commits
3 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ffd809f76d | |||
| 71a9f12b02 | |||
| 2d55d954a1 |
2 changed files with 20 additions and 5 deletions
|
|
@ -19,6 +19,8 @@
|
||||||
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
|
||||||
*
|
*
|
||||||
|
|
@ -44,6 +46,16 @@
|
||||||
$_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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -56,6 +68,8 @@
|
||||||
$_ENV["mariadb"]["pass"],
|
$_ENV["mariadb"]["pass"],
|
||||||
$_ENV["mariadb"]["db"],
|
$_ENV["mariadb"]["db"],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
self::$instance = $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,8 @@
|
||||||
|
|
||||||
abstract public int|string $id { get; }
|
abstract public int|string $id { get; }
|
||||||
|
|
||||||
protected readonly Database $db;
|
public readonly Database $db;
|
||||||
|
|
||||||
private bool $_resolved = false;
|
private bool $_resolved = false;
|
||||||
private bool $_isset;
|
private bool $_isset;
|
||||||
private ?array $_row;
|
private ?array $_row;
|
||||||
|
|
@ -26,7 +27,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 new Database()->from($table)->insert($values);
|
return Database::instance()->from($table)->insert($values);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -41,7 +42,7 @@
|
||||||
public readonly array $columns,
|
public readonly array $columns,
|
||||||
public readonly array $where
|
public readonly array $where
|
||||||
) {
|
) {
|
||||||
$this->db = new Database();
|
$this->db = Database::instance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -74,7 +75,7 @@
|
||||||
* @return bool Entity exists
|
* @return bool Entity exists
|
||||||
*/
|
*/
|
||||||
public function isset(): bool {
|
public function isset(): bool {
|
||||||
return $this->_isset ??= new Database()
|
return $this->_isset ??= Database::instance()
|
||||||
->from($this->table)
|
->from($this->table)
|
||||||
->where($this->where)
|
->where($this->where)
|
||||||
->limit(1)
|
->limit(1)
|
||||||
|
|
@ -108,6 +109,6 @@
|
||||||
->where($this->where)
|
->where($this->where)
|
||||||
->update([$key => $value]);
|
->update([$key => $value]);
|
||||||
|
|
||||||
return $return ?? $value;
|
return func_num_args() === 2 ? $value : $return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue