You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
5.9 KiB
224 lines
5.9 KiB
<?php
|
|
error_reporting(E_ALL);
|
|
ini_set("display_errors", "");
|
|
date_default_timezone_set("Asia/Taipei");
|
|
$envFile = __DIR__ . '/../.env'; // .env 文件的路径
|
|
|
|
if (file_exists($envFile)) {
|
|
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
if ($lines !== false) {
|
|
foreach ($lines as $line) {
|
|
list($key, $value) = explode('=', $line, 2);
|
|
$key = trim($key);
|
|
$value = trim($value);
|
|
// 设置环境变量
|
|
putenv("$key=$value");
|
|
}
|
|
}
|
|
}
|
|
class DB
|
|
{
|
|
private static $ds = array(
|
|
'default' => array('host' => '127.0.0.1', 'db_name' => 'appwms', 'user' => 'masadaroot', 'pwd' => '')
|
|
);
|
|
public static $instance;
|
|
/**
|
|
* ?
|
|
*
|
|
* @param string $db
|
|
* @return object
|
|
*/
|
|
public static function instance($db = 'default')
|
|
{
|
|
|
|
$dbh = new PDO("mysql:host=" . self::$ds[$db]['host'] . ";dbname=appwms" . ";charset=utf8", self::$ds[$db]['user'], self::$ds[$db]['pwd']);
|
|
$dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
|
|
return $dbh;
|
|
}
|
|
/**
|
|
* ?insert
|
|
*
|
|
* @param [String] $table_name
|
|
* @param [Array] $cols
|
|
* @param [Array] $values
|
|
* @param string $db
|
|
* @return void
|
|
*/
|
|
public static function insert_batch($table_name, $cols, $values, $db = 'default')
|
|
{
|
|
$_sql = 'insert into ' . $table_name . '(' . implode(",", $cols) . ')';
|
|
$_vsql = array();
|
|
foreach ($values as $value) {
|
|
$_vsql[] = 'select ' . implode(",", $value) . 'from dual';
|
|
}
|
|
$_sql .= implode(' Union All ', $_vsql);
|
|
// self::query($_sql, $db);
|
|
self::instance($db)->exec($_sql);
|
|
}
|
|
/**
|
|
* insert
|
|
*
|
|
* @param [type] $table_name
|
|
* @param [type] $data
|
|
* @param string $db
|
|
* @return void
|
|
*/
|
|
public static function insert_table($table_name, $data, $db = 'default')
|
|
{
|
|
|
|
|
|
// $stmt->debugDumpParams();
|
|
|
|
$fields = array_keys($data);
|
|
|
|
$meta = self::metadata($table_name, array_keys($data), $db);
|
|
// var_dump( $meta);
|
|
|
|
foreach ($fields as $f) {
|
|
|
|
if ($meta[$f] == 'LONG' || $meta[$f] == 'NEWDECIMAL') {
|
|
$data[$f] = (int) $data[$f];
|
|
} elseif ($meta[$f] == 'DATETIME' && empty($data[$f])) {
|
|
$data[$f] = 'NULL';
|
|
} else {
|
|
// var_dump( $data[$f]);
|
|
// if( is_array($data[$f])){ echo "$table_name " ; echo "$f ";};
|
|
|
|
$data[$f] = "'" . $data[$f] . "'";
|
|
$data[$f] = $data[$f];
|
|
}
|
|
}
|
|
|
|
//foreach ($fields as $f) {
|
|
|
|
$_sql = "insert into " . $table_name . "(" . implode(",", array_keys($data)) . ") values (" . implode(",", array_values($data)) . " )";
|
|
//echo $_sql;
|
|
self::instance($db)->exec($_sql);
|
|
// $stmt->debugDumpParams();
|
|
|
|
|
|
|
|
}
|
|
/**
|
|
*
|
|
*
|
|
* @param [String] $table_name
|
|
* @param [Array] $data
|
|
* @param [String] $data
|
|
* @param string $db
|
|
* @return void
|
|
*/
|
|
public static function update_table($table_name, $data, $where, $db = 'default')
|
|
{
|
|
$fields = array_keys($data);
|
|
|
|
$meta = self::metadata($table_name, array_keys($data), $db);
|
|
// var_dump( $meta);
|
|
|
|
foreach ($fields as $f) {
|
|
|
|
if ($meta[$f] == 'LONG') {
|
|
$data[$f] = (int) $data[$f];
|
|
} else if ($meta[$f] == 'NEWDECIMAL') {
|
|
$data[$f] = (float) $data[$f];
|
|
} elseif ($meta[$f] == 'DATETIME' && empty($data[$f])) {
|
|
$data[$f] = NULL;
|
|
} else {
|
|
$data[$f] = $data[$f];
|
|
$data[$f] = $data[$f];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (empty($where)) {
|
|
return false;
|
|
}
|
|
$sql = "update $table_name set ";
|
|
$i = 0;
|
|
foreach ($data as $key => $val) {
|
|
$sql .= $i++ > 0 ? " , $key=:$key " : " $key=:$key ";
|
|
$value[$key] = $val;
|
|
}
|
|
// var_dump($value);
|
|
$sql .= " where " . $where;
|
|
$stmt = self::instance($db)->prepare($sql);
|
|
|
|
$stmt->execute($value);
|
|
// $stmt->debugDumpParams();
|
|
}
|
|
|
|
/**
|
|
* sql
|
|
*
|
|
* @param [String] $sql
|
|
* @param string $db
|
|
* @return void
|
|
*/
|
|
public static function fields($sql, $db = 'default')
|
|
{
|
|
return self::query($sql)->fetch(PDO::FETCH_BOTH);
|
|
}
|
|
/**
|
|
* ?,as_array array?
|
|
*
|
|
* @param [String] $sql
|
|
* @param boolean $as_array
|
|
* @param string $db
|
|
* @return mixed
|
|
*/
|
|
public static function result($sql, $as_array = true, $db = 'default')
|
|
{
|
|
// echo $sql;
|
|
$resource = self::query($sql, $db);
|
|
|
|
return $as_array == false ? $resource->fetchAll(PDO::FETCH_OBJ) : $resource->fetchAll(PDO::FETCH_BOTH);
|
|
}
|
|
/**
|
|
*sql?
|
|
*
|
|
* @param [String] $sql
|
|
* @return [int]
|
|
*/
|
|
public static function effect_num($sql, $db = 'default')
|
|
{
|
|
return count(self::query($sql, $db)->fetchAll());
|
|
}
|
|
public static function query($sql, $db = 'default')
|
|
{
|
|
|
|
// echo $sql;
|
|
$res = self::instance($db)->prepare($sql);
|
|
|
|
if (!$res) {
|
|
// var_dump($res->errorInfo());
|
|
} else {
|
|
$res->execute();
|
|
|
|
return $res;
|
|
}
|
|
}
|
|
public function escape($filed)
|
|
{
|
|
}
|
|
|
|
/*
|
|
* 获取列的元数据
|
|
*
|
|
* @param [type] $tablename
|
|
* @return void
|
|
*/
|
|
public static function metadata($tablename = null, $col = array(), $db = 'default')
|
|
{
|
|
$column_types = array();
|
|
|
|
$stmt = self::query("SELECT " . implode(",", array_values($col)) . " FROM " . $tablename . " limit 0,1");
|
|
for ($i = 0; $i < $stmt->columnCount(); $i++) {
|
|
$meta[$i] = $stmt->getColumnMeta($i);
|
|
|
|
$column_types[$meta[$i]['name']] = $meta[$i]['native_type'];
|
|
}
|
|
|
|
return $column_types;
|
|
}
|
|
}
|
|
|