<?php
/*!
* Medoo database framework
* http://medoo.in
* Version 0.9.8
*
* Copyright 2015, Angel Lai
* Released under the MIT license
*/
class medoo
{
// General
protected $database_type;
protected $charset;
protected $database_name;
// For MySQL, MariaDB, MSSQL, Sybase, PostgreSQL, Oracle
protected $server;
protected $username;
protected $password;
// For SQLite
protected $database_file;
// For MySQL or MariaDB with unix_socket
protected $socket;
// Optional
protected $port;
protected $option = array();
// Variable
protected $logs = array();
protected $debug_mode = false;
public function __construct($options = null)
{
try {
$commands = array();
if (is_string($options) && !empty($options))
{
if (strtolower($this->database_type) == 'sqlite')
{
$this->database_file = $options;
}
else
{
$this->database_name = $options;
}
}
elseif (is_array($options))
{
foreach ($options as $option => $value)
{
$this->$option = $value;
}
}
if (
isset($this->port) &&
is_int($this->port * 1)
)
{
$port = $this->port;
}
$type = strtolower($this->database_type);
$is_port = isset($port);
switch ($type)
{
case 'mariadb':
$type = 'mysql';
case 'mysql':
if ($this->socket)
{
$dsn = $type . ':unix_socket=' . $this->socket . ';dbname=' . $this->database_name;
}
else
{
$dsn = $type . ':host=' . $this->server . ($is_port ? ';port=' . $port : '') . ';dbname=' . $this->database_name;
}
// Make MySQL using standard quoted identifier
$commands[] = 'SET SQL_MODE=ANSI_QUOTES';
break;
case 'pgsql':
$dsn = $type . ':host=' . $this->server . ($is_port ? ';port=' . $port : '') . ';dbname=' . $this->database_name;
break;
case 'sybase':
$dsn = 'dblib:host=' . $this->server . ($is_port ? ':' . $port : '') . ';dbname=' . $this->database_name;
break;
case 'oracle':
$dbname = $this->server ?
'//' . $this->server . ($is_port ? ':' . $port : ':1521') . '/' . $this->database_name :
$this->database_name;
$dsn = 'oci:dbname=' . $dbname . ($this->charset ? ';charset=' . $this->charset : '');
break;
case 'mssql':
$dsn = strstr(PHP_OS, 'WIN') ?
'sqlsrv:server=' . $this->server . ($is_port ? ',' . $port : '') . ';database=' . $this->database_name :
'dblib:host=' . $this->server . ($is_port ? ':' . $port : '') . ';dbname=' . $this->database_name;
// Keep MSSQL QUOTED_IDENTIFIER is ON for standard quoting
$commands[] = 'SET QUOTED_IDENTIFIER ON';
break;
case 'sqlite':
$dsn = $type . ':' . $this->database_file;
$this->username = null;
$this->password = null;
break;
}
if (
in_array($type, explode(' ', 'mariadb mysql pgsql sybase mssql')) &&
$this->charset
)
{
$commands[] = "SET NAMES '" . $this->charset . "'";
}
$this->pdo = new PDO(
$dsn,
$this->username,
$this->password,
$this->option
);
foreach ($commands as $value)
{
$this->pdo->exec($value);
}
}
catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function query($query)
{
if ($this->debug_mode)
{
echo $query;
$this->debug_mode = false;
return false;
}
array_push($this->logs, $query);
return $this->pdo->query($query);
}
public function exec($query)
{
if ($this->debug_mode)
{
echo $query;
$this->debug_mode = false;
return false;
}
array_push($this->logs, $query);
return $this->pdo->exec($query);
}
public function quote($string)
{
return $this->pdo->quote($string);
}
protected function column_quote($string)
{
return '"' . str_replace('.', '"."', preg_replace('/(^#|\(JSON\))/', '', $string)) . '"';
}
protected function column_push($columns)
{
if ($columns == '*')
{
return $columns;
}
if (is_string($columns))
{
$columns = array($columns);
}
$stack = array();
foreach ($columns as $key => $value)
{
preg_match('/([a-zA-Z0-9_\-\.]*)\s*\(([a-zA-Z0-9_\-]*)\)/i', $value, $match);
if (isset($match[1], $match[2]))
{
array_push($stack, $this->column_quote( $match[1] ) . ' AS ' . $this->column_quote( $match[2] ));
}
else
{
array_push($stack, $this->column_quote( $value ));
}
}
return implode($stack, ',');
}
protected function array_quote($array)
{
$temp = array();
foreach ($array as $value)
{
$temp[] = is_int($value) ? $value : $this->pdo->quote($value);
}
return implode($temp, ',');
}
protected function inner_conjunct($data, $conjunctor, $outer_conjunctor)
{
$haystack = array();
foreach ($data as $value)
{
$haystack[] = '(' . $this->data_implode($value, $conjunctor) . ')';
}
return implode($outer_conjunctor . ' ', $haystack);
}
protected function fn_quote($column, $string)
{
return (strpos($column, '#') === 0 && preg_match('/^[A-Z0-9\_]*\([^)]*\)$/', $string)) ?
$string :
$this->quote($string);
}
protected function data_implode($data, $conjunctor, $outer_conjunctor = null)
{
$wheres = array();
foreach ($data as $key => $value)
{
$type = gettype($value);
if (
preg_match("/^(AND|OR)\s*#?/i", $key, $relation_match) &&
$type == 'array'
)
{
$wheres[] = 0 !== count(array_diff_key($value, array_keys(array_keys($value)))) ?
'(' . $this->data_implode($value, ' ' . $relation_match[1]) . ')' :
'(' . $this->inner_conjunct($value, ' ' . $relation_match[1], $conjunctor) . ')';
}
else
{
preg_match('/(#?)([\w\.]+)(\[(\>|\>\=|\<|\<\=|\!|\<\>|\>\<|\!?~)\])?/i', $key, $match);
$column = $this->column_quote($match[2]);
if (isset($match[4]))
{
$operator = $match[4];
if ($operator == '!')
{
switch ($type)
{
case 'NULL':
$wheres[] = $column . ' IS NOT NULL';
break;
case 'array':
$wheres[] = $column . ' NOT IN (' . $this->array_quote($value) . ')';
break;
case 'integer':
case 'double':
$wheres[] = $column . ' != ' . $value;
break;
case 'boolean':
$wheres[] = $column . ' != ' . ($value ? '1' : '0');
break;
case 'string':
$wheres[] = $column . ' != ' . $this->fn_quote($key, $value);
break;
}
}
if ($operator == '<>' || $operator == '><')
{
if ($type == 'array')
{
if ($operator == '><')
{
$column .= ' NOT';
}
if (is_numeric($value[0]) && is_numeric($value[1]))
{
$wheres[] = '(' . $column . ' BETWEEN ' . $value[0] . ' AND ' . $value[1] . ')';
}
else
{
$wheres[] = '(' . $column . ' BETWEEN ' . $this->quote($value[0]) . ' AND ' . $this->quote($value[1]) . ')';
}
}
}
if ($operator == '~' || $operator == '!~')
{
if ($type == 'string')
{
$value = array($value);
}
if (!empty($value))
{
$like_clauses = array();
foreach ($value as $item)
{
if ($operator == '!~')
{
$column .= ' NOT';
}
if (preg_match('/^(?!%).+(?<!%)$/', $item))
{
$item = '%' . $item . '%';
}
$like_clauses[] = $column . ' LIKE ' . $this->fn_quote($key, $item);
}
$wheres[] = implode(' OR ', $like_clauses);
}
}
if (in_array($operator, array('>', '>=', '<', '<=')))
{
if (is_numeric($value))
{
$wheres[] = $column . ' ' . $operator . ' ' . $value;
}
elseif (strpos($key, '#') === 0)
{
$wheres[] = $column . ' ' . $operator . ' ' . $this->fn_quote($key, $value);
}
else
{
$wheres[] = $column . ' ' . $operator . ' ' . $this->quote($value);
}
}
}
else
{
switch ($type)
{
case 'NULL':
$whe
没有合适的资源?快使用搜索试试~ 我知道了~
PHP数据库开发框架Medoo.7z
资源推荐
资源详情
资源评论
收起资源包目录
PHP数据库开发框架Medoo.7z (1个子文件)
PHP数据库开发框架Medoo
medoo.php 20KB
共 1 条
- 1
资源评论
BryanDing
- 粉丝: 297
- 资源: 5587
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功