<?php /*! * Medoo database framework * http://medoo.in * Version 0.9.6 * * Copyright 2014, Angel Lai * Released under the MIT license */ class medoo { protected $database_type = ‘mysql‘; // For MySQL, MariaDB, MSSQL, Sybase, PostgreSQL, Oracle protected $server = ‘localhost‘; protected $username = ‘username‘; protected $password = ‘password‘; // For SQLite protected $database_file = ‘‘; // Optional protected $port = 3306; protected $charset = ‘utf8‘; protected $database_name = ‘‘; protected $option = array(); // Variable protected $queryString; 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; } $set_charset = "SET NAMES ‘" . $this->charset . "‘"; $type = strtolower($this->database_type); $is_port = isset($port); switch ($type) { case ‘mariadb‘: $type = ‘mysql‘; case ‘mysql‘: // Make MySQL using standard quoted identifier $commands[] = ‘SET SQL_MODE=ANSI_QUOTES‘; case ‘pgsql‘: $dsn = $type . ‘:host=‘ . $this->server . ($is_port ? ‘;port=‘ . $port : ‘‘) . ‘;dbname=‘ . $this->database_name; $commands[] = $set_charset; break; case ‘sybase‘: $dsn = ‘dblib:host=‘ . $this->server . ($is_port ? ‘:‘ . $port : ‘‘) . ‘;dbname=‘ . $this->database_name; $commands[] = $set_charset; break; case ‘oracle‘: $dsn = ‘oci:host=‘ . $this->server . ($is_port ? ‘;port=‘ . $port : ‘‘) . ‘;dbname=‘ . $this->database_name . ‘;charset=‘ . $this->charset; break; case ‘mssql‘: $dsn = strpos(PHP_OS, ‘WIN‘) !== false ? ‘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‘; $commands[] = $set_charset; break; case ‘sqlite‘: $dsn = $type . ‘:‘ . $this->database_file; $this->username = null; $this->password = null; break; } $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) { $this->queryString = $query; return $this->pdo->query($query); } public function exec($query) { $this->queryString = $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])) { if ($match[4] == ‘‘) { $wheres[] = $column . ‘ ‘ . $match[4] . ‘= ‘ . $this->quote($value); } elseif ($match[4] == ‘!‘) { 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; } } else { if ($match[4] == ‘<>‘ || $match[4] == ‘><‘) { if ($type == ‘array‘) { if ($match[4] == ‘><‘) { $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]) . ‘)‘; } } } else { if (is_numeric($value)) { $wheres[] = $column . ‘ ‘ . $match[4] . ‘ ‘ . $value; } else { $datetime = strtotime($value); if ($datetime) { $wheres[] = $column . ‘ ‘ . $match[4] . ‘ ‘ . $this->quote(date(‘Y-m-d H:i:s‘, $datetime)); } } } } } else { if (is_int($key)) { $wheres[] = $this->quote($value); } else { switch ($type) { case ‘NULL‘: $wheres[] = $column . ‘ IS NULL‘; break; case ‘array‘: $wheres[] = $column . ‘ 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; } } } } } return implode($conjunctor . ‘ ‘, $wheres); } protected function where_clause($where) { $where_clause = ‘‘; if (is_array($where)) { $where_keys = array_keys($where); $where_AND = preg_grep("/^AND\s*#?$/i", $where_keys); $where_OR = preg_grep("/^OR\s*#?$/i", $where_keys); $single_condition = array_diff_key($where, array_flip( explode(‘ ‘, ‘AND OR GROUP ORDER HAVING LIMIT LIKE MATCH‘) )); if ($single_condition != array()) { $where_clause = ‘ WHERE ‘ . $this->data_implode($single_condition, ‘‘); } if (!empty($where_AND)) { $value = array_values($where_AND); $where_clause = ‘ WHERE ‘ . $this->data_implode($where[ $value[0] ], ‘ AND‘); } if (!empty($where_OR)) { $value = array_values($where_OR); $where_clause = ‘ WHERE ‘ . $this->data_implode($where[ $value[0] ], ‘ OR‘); } if (isset($where[‘LIKE‘])) { $like_query = $where[‘LIKE‘]; if (is_array($like_query)) { $is_OR = isset($like_query[‘OR‘]); $clause_wrap = array(); if ($is_OR || isset($like_query[‘AND‘])) { $connector = $is_OR ? ‘OR‘ : ‘AND‘; $like_query = $is_OR ? $like_query[‘OR‘] : $like_query[‘AND‘]; } else { $connector = ‘AND‘; } foreach ($like_query as $column => $keyword) { $keyword = is_array($keyword) ? $keyword : array($keyword); foreach ($keyword as $key) { preg_match(‘/(%?)([a-zA-Z0-9_\-\.]*)(%?)((\[!\])?)/‘, $column, $column_match); if ($column_match[1] == ‘‘ && $column_match[3] == ‘‘) { $column_match[1] = ‘%‘; $column_match[3] = ‘%‘; } $clause_wrap[] = $this->lumn_maquotbspt;lumn_match[2]) . spt;lumn_match[4] != ‘‘ ? ‘ NOT‘ : ‘‘) . ‘ LIKE ‘ . $this->quotbspt;lumn_match[1] . $key . $t;lumn_match[3]); } } $where_clause .= ($where_clause != ‘‘ ? ‘ AND ‘ : ‘ WHERE ‘) . ‘(‘ . implode($clause_wrap, ‘ ‘ . $t;nnector . ‘ ‘) . ‘)‘; } } if (isset($where[‘MATCH‘])) { $matchaqusp; = $where[‘MATCH‘]N; i:nbsp;  1;nbsp; nbsp;nbsp;p;  &nbnbsp;&s&nbause  &nbbspp;&n&nbnbsp; if&nb;if&nb;if&nb;if&nb;if (isset($where[‘MATCH‘])) ; bsp; &nb; p;&nb&nb &nb; $where_clause .= ($where_clause&nbclausesp;&nclause(`&nbclause_clausestr_replacesp;!.p;!use !`.`&nbuse&nbs ?&n &nbnbsp;&s&nbause&nbs&nb`use&nbs`&nb)($where_clause&nb`($whereAGAINST$wheresp;!$where_clausep; &nb &nbbspp;&n&nbnbclause_clause&nbimplode($clause_wrap, ‘ ‘ . $t;nnector . ‘ ‘) . ‘)‘; } &n&nbs  &nbGROUP&nbnbsp; if (isset($where[‘MATCH‘])) p bsp; &nb; &&nbnbsp;&GROUPnbsp;&BYnbsp;&&nbnbsp;&_clausep; &nnbsp;&;&np;&nb &nbGROUP&nbnbode($clause_wrap, ‘ ‘ . $t;nnector . ‘ } &n&nbs  &nbORDEsp; if (isset($where[‘MATCH‘])) prsort &&nb/(^ &\s*(DESC|ASC))?bsp; $matchaqusp; = $where[‘MATCH‘]N; i:nbsp; &nbORDEsp; if&nb;if&nb;if&nb;if&nb;if (isset($where[‘MATCH‘])) H‘]N;&np;(isset($where[‘MATCH‘])) bs  &nbORDEsp;&nsp;bclause;nbsp;nbspp;(isset($where[‘MATCH‘])) bspi:nbsp; &nbORDEsp;&nsp;bp;(isset($where[‘MATCH‘])) bp;(isset($where[‘MATCH‘])) sp;(isset($where[‘MATCH‘])) ; bsp; &nb; &&nbnbsp;&ORDEsnbsp;&BYnbsp;&FIELDsp;!$where_clausep; &nnbsp;&;&np;&nb &nbORDEs&nb][0;bclause_clause&nbuse&nbs&nbse&nbs_clausep; &ni:nbs;&np;&nb &nbORDEs&nb][p;bclause_clause&nbimplode($clause_wrap, ‘ ‘ . $t;nnector . ‘ ‘) .时间: 2024-08-23 22:47:39
修改了medoo文件,将SQL语句双引号改为反引号。
修改了medoo文件,将SQL语句双引号改为反引号。的相关文章
Python 操作文件模拟SQL语句功能
Python操作文件模拟SQL语句功能 一.需求 当然此表你在文件存储时可以这样表示 1,Alex Li,22,13651054608,IT,2013-04-01 现需要对这个员工信息文件,实现增删改查操作 1. 可进行模糊查询,语法至少支持下面3种: 1. select name,age from staff_table where age > 22 2. select * from staff_table where dept = "IT" 3. select * from
【Access2007】修改数据库密码与输入SQL语句进行查询
Access是个不入流的数据库,之所以说他不入流是因为其兼容性,大小等都未达到软件运营的要求, 但是某些项目对方要求必须以Access作为数据库,你也是不得不对此了解. Access是个完全图形化操作的数据库,程序猿出来刚到会对此很不适应. 因为你不知道怎么修改数据库密码--这还是其次,你根本找不到哪个部分给你输入SQL语句操作数据. 毕竟对于程序猿来说,能输入代码操作计算机才是最重要的. 一.修改Access2007数据库密码 左上角有打开Access数据库的按钮,就像你在word中打开.do
Python操作文件模拟SQL语句功能
一.要求 当然此表你在文件存储时可以这样表示 1,li,22,18230393840,IT,2013-06-01 现需要对这个员工信息文件,实现增删改查操作 1. 可进行模糊查询,语法至少支持下面3种: 1. select name,age from staff_table where age > 22 2. select * from staff_table where dept = "IT" 3. select * from staff_table
让EFCore更疯狂些的扩展类库(一):通过json文件配置sql语句
前言 EF通过linq和各种扩展方法,再加上实体模型,编写数据库的访问代码确实是优美.舒服,但是生成的sql不尽如意.性能低下,尤其是复杂些的逻辑关系,最终大家还是会回归自然,选择能够友好执行sql语句的ORM,认认真真的编写sql:问题是:EF是否也能够很友好的执行sql语句?EF提供直接执行sql语句的方法并不多,而且也是极其简单的:那是否容易进行扩展?答案是肯定的,在DbContext下提供了Database属性就是为了执行sql用的,然后自己就通过Database下的方法属性进行了扩展(
如何在sqlplus中查看、修改、执行缓存的SQL语句
在sqlplus中,如果没有上下翻页工具,sqlplus是不能向Linux终端一样上下翻历史命令的,但sqlplus会记录最近的一行DML语句到buffer中,我们可以使用一些简单的命令查看.修改.执行记录在buffer中的语句,如下: l[ist] [n] 查看buf中的SQL: del [n] 移除buf中的某行SQL: cl[ear] buff[er] 移除buf中语句: C[hange]/old_value/new_value 表示更改第一个出现的字符 : 此外,还有I和A,分别表示:
mapper.xml文件,sql语句参数为list
<insert id="insertPjCustomAttribute" parameterType="com.devops.server.model.PjCustomAttribute"> INSERT INTO `PJ_CUSTOM_ATTRIBUTE` (PJ_ID,NAME,VALUE,CREATE_TIME) VALUES <foreach collection="list" item="listRelati
shell 脚本中双引号 单引号 反引号 的区别
转自:http://blog.csdn.net/iamlaosong/article/details/54728393 最近要编个shell脚本处理数据,需要检测数据文件是否存在,文件名中包含日期,所以需要生成最近几天的日期,以便检测文件是否存在,看下面的脚本中如何使用双引号.单引号.反引号: OPDATE=`date -d '-1 day' +%Y%m%d` for i in $(seq 10) do FILEDATE=`date -d "-$i day" +%Y%m%d` echo
读取CSV文件生成sql语句
1.TestFilingTextLoadFareClassProcessorForIntegration public class TestFilingTextLoadFareClassProcessorForIntegration { protected static MockDBDataGenerationUtil dbDataGenerationUtil; @BeforeClass public static void setUpBeforeClass() throws Exception
在C#中使用SQL语句什么情况需要使用‘单引号’和“双引号”
例如 string sql = " insert into [Demo].[dbo].[textable] (姓名,身份证号)values ('" + name.Text.ToString() + "'," + number.Text.ToString() + ")";//执行在数据库表中增加一行姓名和身份证号 在表中已经设置成int类型的不需要再次添加'单引号',而字符串类型的数据需要进行添加'单引号'. 原文地址:https://www.c