<?php
namespace esclass;
use PDO;
use PDOStatement;
use PDOException;
use RuntimeException;
use InvalidArgumentException;
class database {
const VERSION = '1.0.1';
private static $_instance;
/**
* @var PDO
*/
protected $pdo;
protected $pk;
/**
* @var PDOStatement
*/
protected $statement;
/**
* Database config
*
* @var array
*/
protected $dbConfig = array();
/**
* SQL statement options
*
* @var array
*/
protected $options = array();
/**
* SQL binding data
*
* @var array
*/
protected $bindData = array();
protected $model = array();
/**
* Fetch the SQL statement?
*
* @var boolean
*/
protected $fetchSql = false;
public $render = '';
/**
* Is escape identifiers?
*
* @var boolean
*/
protected $escape = true;
/**
* SQL query logs
*
* @var array
*/
protected $queryLog = array();
/**
* Constructor
* Connect to the database server
*
* @param array $config
* @throws PDOException
*/
private function __construct(array $config=[]) {
if(empty($config)){
$config=config('database');
}
if (!isset($config['options'])) {
$config['options'] = array();
}
if (isset($config['socket']) && $config['socket']) {
$dsn = 'mysql:unix_socket=' . trim($config['socket']);
} else {
$dsn = 'mysql:host=' . trim($config['host']);
if (isset($config['port']) && is_int($config['port'] * 1)) {
$dsn .= ';port=' . $config['port'];
}
}
if (isset($config['dbname']) && $config['dbname']) {
$dsn .= ';dbname=' . trim($config['dbname']);
}
if (isset($config['charset']) && $config['charset']) {
$dsn .= ';charset=' . trim($config['charset']);
}
try {
$this->pdo = new PDO($dsn, trim($config['username']), $config['password'], $config['options']);
} catch (PDOException $e) {
throw $e;
}
$this->dbConfig = $config;
}
private function __clone()
{
}//覆盖__clone()方法,禁止克隆
public static function getInstance()
{
if(! (self::$_instance instanceof self) )
{
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Config database
*
* @param mixed $config
* @param string $value
* @return mixed
*/
public function dbConfig($config = null, $value = null) {
if (!$config) {
return $this->dbConfig;
} elseif (is_array($config)) {
$this->dbConfig = array_merge($this->dbConfig, $config);
} elseif (is_string($config)) {
if (is_null($value)) {
return isset($this->dbConfig[$config]) ? $this->dbConfig[$config] : null;
} else {
$this->dbConfig[$config] = $value;
return true;
}
}
return null;
}
/**
* Sets the database name
*
* @param string $dbname
* @return self
*/
public function dbname($dbname) {
$this->dbConfig['dbname'] = $dbname;
return $this;
}
public function render($render='') {
if(!empty($render)){
$this->render = $render;
}else{
return $this->render;
}
}
/**
* Sets whether the distinct
*
* @param boolean $distinct
* @return self
*/
public function distinct($distinct) {
$this->options['distinct'] = (bool) $distinct;
return $this;
}
/**
* Parse the `DISTINCT` statement
*
* @access protected
* @param boolean $distinct
* @return string
*/
protected function parseDistinct($distinct) {
return $distinct ? ' DISTINCT' : '';
}
/**
* Sets the select expression
*
* @param mixed $field The select expressions
* @return self
*/
public function field($field) {
if (is_string($field)) {
$this->options['field'] = trim($field);
} elseif (is_array($field)) {
if (isset($this->options['field']) && is_array($this->options['field'])) {
$this->options['field'] = array_merge($this->options['field'], $field);
} else {
$this->options['field'] = $field;
}
} elseif (is_scalar($field)) {
$this->options['field'] = $field;
} else {
$this->options['field'] = '*';
}
return $this;
}
/**
* Parse the `SELECT EXPR`
*
* @access protected
* @param mixed $fields Select expressions
* @return string
*/
protected function parseField($fields) {
$fieldsStr = '*';
if ('*' == $fields || !$fields) {
$fieldsStr = '*';
} elseif (is_string($fields)) {
// Ignore the special characters
$chars = array('(', ')', ' AS ', '=');
foreach ($chars as $char) {
if (stripos($fields, $char) !== false) {
return $fields;
}
}
$fieldsStr = $this->quoteKey($fields);
} elseif (is_array($fields)) {
$array = array();
foreach ($fields as $key => $field) {
if (!is_numeric($key) && $key) {
$array[] = (strpos($field, '*') !== false ? $field : $this->quoteKey($field)) . ' AS ' . $this->quoteKey($key);
} else {
$array[] = $this->quoteKey($field);
}
}
$fieldsStr = implode(', ', $array);
} elseif (is_scalar($fields)) {
$fieldsStr = $fields;
}
return $fieldsStr;
}
/**
* Sets the table name for current statement
*
* @param mixed $table Table name
* @param string $alias Table alias
* @param boolean $prefix The prefix of table name
* @return self
*/
public function table($table, $alias = null, $prefix = true) {
if (is_string($table) && $table) {
$tableName = trim($table);
} elseif (is_array($table)) {
$tableName = array_shift($table);
if (!$alias && count($table) >= 1) {
$alias = array_shift($table);
}
if (empty($tableName)) {
throw new InvalidArgumentException('Unexpected empty value for "table"');
}
} else {
throw new InvalidArgumentException('Invalid argument for "table"');
}
// Global prefix
if (is_bool($prefix) && $prefix && !empty($this->dbConfig['prefix'])) {
$tableName = $this->dbConfig['prefix'] . $tableName;
} elseif (is_string($prefix) && $prefix) {
// Custom prefix
$tableName = trim($prefix) . $tableName;
}
// Filter alias
if ($alias) {
$alias = trim($alias);
}
if (!preg_match('/^([a-zA-Z0-9_]+)$/i', $alias)) {
$alias = null;
}
$this->options['table'] = array($tableName, $alias);
return $this;
}
/**
* Parse the real table name that contains database
*
* @access protected
* @param array $table
* @return string
*/
protected function parseTable($table) {
list($tableName, $alias) = $table;
$tableName =camelcase2underline($tableName);
if (!empty($this->dbConfig['dbname']) && strpos($tableName, '.') !== false) {
$tableName = $this->dbConfig['dbname'] . '.' . $tableName;
}
$tabl
没有合适的资源?快使用搜索试试~ 我知道了~
PHP实例开发源码—EasySNS 极简社区源码(小组版) PHP版.zip
共837个文件
gif:234个
php:190个
html:138个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 159 浏览量
2022-11-11
21:38:37
上传
评论
收藏 6.26MB ZIP 举报
温馨提示
PHP实例开发源码—EasySNS 极简社区源码(小组版) PHP版.zip
资源推荐
资源详情
资源评论
收起资源包目录
PHP实例开发源码—EasySNS 极简社区源码(小组版) PHP版.zip (837个子文件)
light7.css 127KB
bootstrap.min.css 118KB
light7.min.css 105KB
common.css 80KB
aui.css 60KB
animate.min.css 60KB
layui.css 52KB
fox.css 47KB
font-awesome.css 37KB
font-awesome.min.css 30KB
iconfont.css 25KB
default.css 25KB
light7-swiper.css 22KB
swiper.css 21KB
swiper.min.css 19KB
iconfont.css 18KB
light7-swiper.min.css 15KB
layer.css 14KB
layer.css 14KB
user.css 11KB
main.css 10KB
layui.mobile.css 10KB
style.css 9KB
laydate.css 7KB
style.css 7KB
light7-swipeout.css 7KB
setting.css 6KB
layer.css 5KB
admin.css 4KB
style.css 3KB
jquery.edittable.css 2KB
jquery.edittable.min.css 2KB
frame.css 2KB
code.css 1KB
prettify.css 960B
user.css 949B
jquery.autocompleter.css 897B
images.css 735B
message.css 606B
webuploader.css 515B
webuploader.css 515B
login.css 436B
news.css 171B
fontawesome-webfont.eot 162KB
fontawesome-webfont.eot 162KB
iconfont.eot 37KB
iconfont.eot 24KB
iconfont.eot 17KB
static.gif 35KB
35.gif 13KB
42.gif 13KB
59.gif 10KB
kuaiwww.gif 10KB
40.gif 10KB
22.gif 10KB
18.gif 8KB
19.gif 8KB
24.gif 8KB
11.gif 8KB
13.gif 7KB
32.gif 7KB
16.gif 7KB
39.gif 6KB
64.gif 6KB
49.gif 6KB
63.gif 6KB
50.gif 6KB
29.gif 6KB
loading-0.gif 6KB
loading-0.gif 6KB
4.gif 6KB
1.gif 5KB
42.gif 5KB
71.gif 5KB
97.gif 5KB
65.gif 5KB
21.gif 5KB
31.gif 5KB
46.gif 5KB
20.gif 5KB
57.gif 5KB
loding.gif 5KB
29.gif 5KB
45.gif 5KB
8.gif 5KB
70.gif 4KB
5.gif 4KB
17.gif 4KB
27.gif 4KB
43.gif 4KB
33.gif 4KB
9.gif 4KB
68.gif 4KB
44.gif 4KB
11.gif 4KB
8.gif 4KB
3.gif 4KB
26.gif 4KB
14.gif 4KB
7.gif 4KB
共 837 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
毕业_设计
- 粉丝: 1930
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功