紫猫插件手机版V3.1929_紫猫插件_紫猫_手机插件「紫猫插件手机版V3.1929_紫猫插件_紫猫_」
导读:<?php /*! * Medoo database framework * https://medoo.in * Version 1.6.1 * * Copyright 2018, Angel Lai * Released under the MIT license */ namespace Medoo; use PDO; use...
<?php
/*!
* Medoo database framework
* https://medoo.in
* Version 1.6.1
*
* Copyright 2018, Angel Lai
* Released under the MIT license
*/
namespace Medoo;
use PDO;
use Exception;
use PDOException;
use InvalidArgumentException;
class Raw {
public $map;
public $value;
}
class Medoo
{
public $pdo;
protected $type;
protected $prefix;
protected $statement;
protected $dsn;
protected $logs = [];
protected $logging = false;
protected $debug_mode = false;
protected $guid = 0;
public function __construct(array $options)
{
if (isset($options[ 'database_type' ]))
{
$this->type = strtolower($options[ 'database_type' ]);
if ($this->type === 'mariadb')
{
$this->type = 'mysql';
}
}
if (isset($options[ 'prefix' ]))
{
$this->prefix = $options[ 'prefix' ];
}
if (isset($options[ 'logging' ]) && is_bool($options[ 'logging' ]))
{
$this->logging = $options[ 'logging' ];
}
$option = isset($options[ 'option' ]) ? $options[ 'option' ] : [];
$commands = (isset($options[ 'command' ]) && is_array($options[ 'command' ])) ? $options[ 'command' ] : [];
switch ($this->type)
{
case 'mysql':
// Make MySQL using standard quoted identifier
$commands[] = 'SET SQL_MODE=ANSI_QUOTES';
break;
case 'mssql':
// Keep MSSQL QUOTED_IDENTIFIER is ON for standard quoting
$commands[] = 'SET QUOTED_IDENTIFIER ON';
// Make ANSI_NULLS is ON for NULL value
$commands[] = 'SET ANSI_NULLS ON';
break;
}
if (isset($options[ 'pdo' ]))
{
if (!$options[ 'pdo' ] instanceof PDO)
{
throw new InvalidArgumentException('Invalid PDO object supplied');
}
$this->pdo = $options[ 'pdo' ];
foreach ($commands as $value)
{
$this->pdo->exec($value);
}
return;
}
if (isset($options[ 'dsn' ]))
{
if (is_array($options[ 'dsn' ]) && isset($options[ 'dsn' ][ 'driver' ]))
{
$attr = $options[ 'dsn' ];
}
else
{
throw new InvalidArgumentException('Invalid DSN option supplied');
}
}
else
{
if (
isset($options[ 'port' ]) &&
is_int($options[ 'port' ] * 1)
)
{
$port = $options[ 'port' ];
}
$is_port = isset($port);
switch ($this->type)
{
case 'mysql':
$attr = [
'driver' => 'mysql',
'dbname' => $options[ 'database_name' ]
];
if (isset($options[ 'socket' ]))
{
$attr[ 'unix_socket' ] = $options[ 'socket' ];
}
else
{
$attr[ 'host' ] = $options[ 'server' ];
if ($is_port)
{
$attr[ 'port' ] = $port;
}
}
break;
case 'pgsql':
$attr = [
'driver' => 'pgsql',
'host' => $options[ 'server' ],
'dbname' => $options[ 'database_name' ]
];
if ($is_port)
{
$attr[ 'port' ] = $port;
}
break;
case 'sybase':
$attr = [
'driver' => 'dblib',
'host' => $options[ 'server' ],
'dbname' => $options[ 'database_name' ]
];
if ($is_port)
{
$attr[ 'port' ] = $port;
}
break;
case 'oracle':
$attr = [
'driver' => 'oci',
'dbname' => $options[ 'server' ] ?
'//' . $options[ 'server' ] . ($is_port ? ':' . $port : ':1521') . '/' . $options[ 'database_name' ] :
$options[ 'database_name' ]
];
if (isset($options[ 'charset' ]))
{
$attr[ 'charset' ] = $options[ 'charset' ];
}
break;
case 'mssql':
if (isset($options[ 'driver' ]) && $options[ 'driver' ] === 'dblib')
{
$attr = [
'driver' => 'dblib',
'host' => $options[ 'server' ] . ($is_port ? ':' . $port : ''),
'dbname' => $options[ 'database_name' ]
];
if (isset($options[ 'appname' ]))
{
$attr[ 'appname' ] = $options[ 'appname' ];
}
if (isset($options[ 'charset' ]))
{
$attr[ 'charset' ] = $options[ 'charset' ];
}
}
else
{
$attr = [
'driver' => 'sqlsrv',
'Server' => $options[ 'server' ] . ($is_port ? ',' . $port : ''),
'Database' => $options[ 'database_name' ]
];
if (isset($options[ 'appname' ]))
{
$attr[ 'APP' ] = $options[ 'appname' ];
}
$config = [
'ApplicationIntent',
'AttachDBFileName',
'Authentication',
'ColumnEncryption',
'ConnectionPooling',
'Encrypt',
'Failover_Partner',
'KeyStoreAuthentication',
'KeyStorePrincipalId',
'KeyStoreSecret',
'LoginTimeout',
'MultipleActiveResultSets',
'MultiSubnetFailover',
'Scrollable',
'TraceFile',
'TraceOn',
'TransactionIsolation',
'TransparentNetworkIPResolution',
'TrustServerCertificate',
'WSID',
];
foreach ($config as $value)
{
$keyname = strtolower(preg_replace(['/([a-zd])([A-Z])/', '/([^_])([A-Z][a-z])/'], '$1_$2', $value));
if (isset($options[ $keyname ]))
{
$attr[ $value ] = $options[ $keyname ];
}
}
}
break;
case 'sqlite':
$attr = [
'driver' => 'sqlite',
$options[ 'database_file' ]
];
break;
}
}
if (!isset($attr))
{
throw new InvalidArgumentException('Incorrect connection options');
}
$driver = $attr[ 'driver' ];
if (!in_array($driver, PDO::getAvailableDrivers()))
{
throw new InvalidArgumentException("Unsupported PDO driver: {$driver}");
}
unset($attr[ 'driver' ]);
$stack = [];
foreach ($attr as $key => $value)
{
$stack[] = is_int($key) ? $value : $key . '=' . $value;
}
$dsn = $driver . ':' . implode($stack, ';');
if (
in_array($this->type, ['mysql', 'pgsql', 'sybase', 'mssql']) &&
isset($options[ 'charset' ])
)
{
$commands[] = "SET NAMES '{$options[ 'charset' ]}'" . (
$this->type === 'mysql' && isset($options[ 'collation' ]) ?
" COLLATE '{$options[ 'collation' ]}'" : ''
);
}
$this->dsn = $dsn;
try {
$this->pdo = new PDO(
$dsn,
isset($options[ 'username' ]) ? $options[ 'username' ] : null,
isset($options[ 'password' ]) ? $options[ 'password' ] : null,
$option
);
foreach ($commands as $value)
{
$this->pdo->exec($value);
}
}
catch (PDOException $e) {
throw new PDOException($e->getMessage());
}
}
public function query($query, $map = [])
{
$raw = $this->raw($query, $map);
$query = $this->buildRaw($raw, $map);
return $this->exec($query, $map);
}
public function exec($query, $map = [])
{
if ($this->debug_mode)
{
echo $this->generate($query, $map);
$this->debug_mode = false;
return false;
}
if ($this->logging)
{
$this->logs[] = [$query, $map];
}
else
{
$this->logs = [[$query, $map]];
}
$statement = $this->pdo->prepare($query);
if ($statement)
{
foreach ($map as $key => $value)
{
$statement->bindValue($key, $value[ 0 ], $value[ 1 ]);
}
$statement->execute();
$this->statement = $statement;
return $statement;
}
return false;
}
protected function generate($query, $map)
{
$identifier = [
'mysql' => '`$1`',
'mssql' => '[$1]'
];
$query = preg_replace(
'/"([a-zA-Z0-9_]+)"/i',
isset($identifier[ $this->type ]) ? $identifier[ $this->type ] : '"$1"',
$query
);
foreach ($map as $key => $value)
{
if ($value[ 1 ] === PDO::PARAM_STR)
{
$replace = $this->quote($value[ 0 ]);
}
elseif ($value[ 1 ] === PDO::PARAM_NULL)
{
$replace = 'NULL';
}
elseif ($value[ 1 ] === PDO::PARAM_LOB)
{
$replace = '{LOB_DATA}';
}
else
{
$replace = $value[ 0 ];
}
$query = str_replace($key, $replace, $query);
}
return $query;
}
public static function raw($string, $map = [])
{
$raw = new Raw();
$raw->map = $map;
$raw->value = $string;
return $raw;
}
protected function isRaw($object)
{
return $object instanceof Raw;
}
protected function buildRaw($raw, &$map)
{宣威版权声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕,E-mail:xinmeigg88@163.com