ショコラ
Laravel で XDBをつかってみる
もっさん先輩
プロジェクト名 bedrock で、Laravel の環境を用意します。
curl -s https://laravel.build/bedrock | bash
-dオプション で Laravel をバックグラウンドで実行します。
cd bedrock && ./vendor/bin/sail up -d
↓のコマンドでプロジェクトのホームから sail コマンドを使えるようにします。
echo "alias sail='[ -f sail ] && sh sail || sh vendor/bin/sail'" >> ~/.bashrc
Libsフォルダー を作成します。
mkdir -p app/Libs
app/Libs/EzDB.php を作成します。
vi app/Libs/EzDB.php
<?php
if (!function_exists('connect')) {
function _getenv( $env, $default = null ) {
$val = getenv($env);
return empty($val) ? $default : $val;
}
function connect() {
$HOST = _getenv('POSTGRES_HOST','localhost');
$PORT = _getenv('POSTGRES_PORT','5432');
$DBNAME = _getenv('POSTGRES_DBNAME');
$USER = _getenv('POSTGRES_USER');
$PASSWORD = _getenv('POSTGRES_PASSWORD');
$options = array(
PDO::ATTR_CASE => \PDO::CASE_LOWER,
PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY,
PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
);
return new PDO("pgsql:host={$HOST} port={$PORT} dbname={$DBNAME}",$USER,$PASSWORD,$options);
}
}
function getOne( $pdo, $sql, $params = array() ) {
$stm = $pdo->prepare($sql);
$stm->execute($params);
return $stm->fetchColumn();
}
function getRow( $pdo, $sql, $params = array() ) {
$stm = $pdo->prepare($sql);
$stm->execute($params);
\Log::debug($sql);
\Log::debug($params);
#exit();
return $stm->fetch(\PDO::FETCH_ASSOC);
}
function getAll( $pdo, $sql, $params = array() ) {
$stm = $pdo->prepare($sql);
$stm->execute($params);
return $stm->fetchAll(\PDO::FETCH_ASSOC);
}
function select( $pdo, $sql, $params = array() ) {
return getAll($pdo,$sql,$params);
}
function query( $pdo, $sql, $params = array() ) {
$stm = $pdo->prepare($sql);
$stm->execute($params);
return $stm;
}
function execute( $pdo, $sql, $params = array() ) {
$stm = $pdo->prepare($sql);
return $stm->execute($params);
}
function reg( $pdo, $table, $params, $pkey ) {
$insert_params = array();
$update_columns = array();
$update_condition = array();
$update_params = array();
$placeholders = array();
foreach ($params as $col => $val) {
$mark = $col[0];
if (in_array($mark,array('!',':','?')))
$col = substr($col,1);
$params[$col] = $val;
if (('' == $val) && (('!' == $mark) || (':' != $mark))) {
list($mark,$val) = array('!','NULL');
}
if ('!' == $mark) {
$insert_params[$col] = $val;
$update_columns[] = "{$col}={$val}";
}
else {
$insert_params[$col] = ":{$col}";
$update_columns[] = "{$col}=:{$col}";
$placeholders[":{$col}"] = $val;
}
}
foreach ($pkey as $col) {
$update_condition[] = "{$col}=:PK{$col}";
$update_params[":PK{$col}"] = isset($params[$col]) ? $params[$col] : '';
}
$update_condition = implode(' AND ',$update_condition);
$sql = "UPDATE {$table} SET ".implode(',',$update_columns)." WHERE {$update_condition}";
if (false === getOne($pdo,"SELECT 1 FROM {$table} WHERE {$update_condition}",$update_params)) {
$update_params = [];
$sql = "INSERT INTO {$table} (".implode(",",array_keys($insert_params)).") VALUES (".implode(',',array_values($insert_params)).")";
}
execute($pdo,$sql,array_merge($placeholders,$update_params));
}
function begin( $pdo ) {
execute($pdo,'begin');
}
function commit( $pdo ) {
execute($pdo,'commit');
}
function rollback( $pdo ) {
execute($pdo,'rollback');
}
class EzDB {
public static $EzDB;
public $pdo;
public static function connect() {
if (is_null(self::$EzDB)) {
self::$EzDB = new EzDB;
self::$EzDB->pdo = connect();
self::$EzDB->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
}
return self::$EzDB;
}
public function getOne( $sql, $params = array() ) {
return getOne($this->pdo,$sql,$params);
}
public function getRow( $sql, $params = array() ) {
return getRow($this->pdo,$sql,$params);
}
public function getAll( $sql, $params = array() ) {
return getAll($this->pdo,$sql,$params);
}
public function select( $sql, $params = array() ) {
return select($this->pdo,$sql,$params);
}
public function query( $sql, $params = array() ) {
return query($this->pdo,$sql,$params);
}
public function execute( $sql, $params = array() ) {
return execute($this->pdo,$sql,$params);
}
public function reg( $table, $params, $pkey ) {
reg($this->pdo,$table,$params,$pkey);
return $this;
}
public function begin() {
begin($this->pdo);
return $this;
}
public function commit() {
commit($this->pdo);
return $this;
}
public function rollback() {
rollback($this->pdo);
return $this;
}
}
以上