$dsn, 'username' => $username, 'password' => $password, 'charset' => $charset, 'tablePrefix' => $data['tablepre'], ]); }else{ $connection = yii::$app->db; } return $connection; } /* * 取PDO */ function fmFunc_pdo_yii($dbconn=null) { static $pdo; if(!$dbconn){ $dbconn = yii::$app->db; } $dbconn->open(); $pdo = $dbconn->getMasterPdo(); return $pdo; } /* * 根据条件包装自有类FMDB的PDO的sql操作 */ /* * query查询操作,执行而不一定返回数据 */ function fmFunc_pdo_yii_query($connection,$sql, $params = array()) { $command = $connection->createCommand($sql); if(!empty($params) && is_array($params)){ foreach($params as $p=>$v){ $command->bindValue($p, $v); } } $result = $command->query(); return $result; } /* * 查找单行数据 */ function fmFunc_pdo_yii_fetch($connection,$sql, $params = array()) { $command = $connection->createCommand($sql); if(!empty($params) && is_array($params)){ foreach($params as $p=>$v){ $command->bindValue($p, $v); } } $result = $command->queryOne(); return $result; } /* * 查找全部匹配行 */ function fmFunc_pdo_yii_fetchall($connection,$sql, $params = array()) { $command = $connection->createCommand($sql); if(!empty($params) && is_array($params)){ foreach($params as $p=>$v){ $command->bindValue($p, $v); } } return $command->queryAll(); } /* * 查找第一列数据 @table, 使用绝对表名(有前缀的情况下需要直接写在表名上) @column 要查询的字段列 @conditions 附加条件(用于条件筛选,sql中where的条件部分) @params 要绑定到附加条件的参数 */ function fmFunc_pdo_yii_fetchcolumn($connection,$table, $column, $conditions = '', $params = array()) { $fields = "`".$column."`"; $sql = " SELECT ".$fields." FROM ".$table." WHERE ".$conditions; $command = $connection->createCommand($sql); if(!empty($params) && is_array($params)){ foreach($params as $p=>$v){ $command->bindValue($p, $v); } } $result = $command->queryColumn(); if($result) { return $result[0]; }else{ return $result; } } /* * 取指定表单行数据 @table, 使用绝对表名(有前缀的情况下需要直接写在表名上) @columns 要取的列(字段名组合的数组) @conditions 附加条件(用于条件筛选,sql中where的条件部分) @params 要绑定到附加条件的参数 @limits 截断操作(数组)(开始位置,结束位置)(如:start=>0,end=>3) */ function fmFunc_pdo_yii_get($connection, $table, $columns = array(), $conditions = '', $params = array(), $limits = array()) { $fields = '*'; if(!empty($columns) && is_array($columns)) { $fields = ''; $i = count($columns); foreach($columns as $k=>$v){ $i --; if($i==0){ $fields .="`".$v."`"; }else{ $fields .="`".$v."`,"; } } } $sql = " SELECT ".$fields." FROM ".$table." WHERE ".$conditions; $_limit = ""; if(!empty($limits) && is_array($limits)){ $limits['start'] = isset($limits['start']) ? intval($limits['start']) : 0; $limits['end'] = $limits['start']+1; $_limit = " LIMIT ".$limits['start'].", ".$limits['end']; } $_order = ""; $orders = array( 'displayorder' => 'DESC', 'id' => 'DESC', ); if(!empty($orders) && is_array($orders)){ $order = ""; $i = count($orders); foreach($orders as $p=>$v){ $i--; if($i==0){ $order .= " `".$p."` ". strtoupper($v) .""; }else{ $order .= " `".$p."` ". strtoupper($v) .","; } } $_order = " ORDER BY ".$order; } $sql = $sql.$_order.$_limit; $command = $connection->createCommand($sql); if(!empty($params) && is_array($params)){ foreach($params as $p=>$v){ $command->bindValue($p, $v); } } $result = $command->queryOne(); return $result; } /* * 取指定表多行数据 @table, 使用绝对表名(有前缀的情况下需要直接写在表名上) @columns 要取的列(字段名组合的数组) @conditions 附加条件(用于条件筛选,sql中where的条件部分) @params 要绑定到附加条件的参数 @limits 截断操作(数组)(开始位置,结束位置)(如:start=>0,end=>3) @orders 排序操作(数组)(字段名=>排序方法) */ function fmFunc_pdo_yii_getall($connection, $table, $columns = array(), $conditions = '', $params = array(), $limits = array(), $orders=array()) { $fields = '*'; if(!empty($columns) && is_array($columns)){ $fields = ''; $i = count($columns); foreach($columns as $k=>$v){ $i--; if($i==0){ $fields .="`".$v."`"; }else{ $fields .="`".$v."`,"; } } } $sql = " SELECT ".$fields." FROM ".$table." WHERE ".$conditions; $_limit = ""; if(!empty($limits) && is_array($limits)){ $limits['start'] = isset($limits['start']) ? intval($limits['start']) : 0; $limits['end'] = isset($limits['end']) ? intval($limits['end']) : 0; $limits['end'] = ($limits['end'] > $limits['start']) ? $limits['end'] : $limits['start']+1; $_limit = " LIMIT ".$limits['start'].", ".$limits['end']; } $_order = ""; if(empty($orders)){ $orders = array( 'displayorder' => 'DESC', 'id' => 'DESC', ); } if(is_array($orders)){ $order = ""; $i = count($orders); foreach($orders as $p=>$v){ $i--; if($i==0){ $order .= " `".$p."` ". strtoupper($v) .""; }else{ $order .= " `".$p."` ". strtoupper($v) .","; } } $_order = " ORDER BY ".$order; } $sql = $sql.$_order.$_limit; $command = $connection->createCommand($sql); if(!empty($params) && is_array($params)){ foreach($params as $p=>$v){ $command->bindValue($p, $v); } } $result = $command->queryAll(); return $result; } /* * 更新指定表数据 @table, 要更新的表,使用绝对表名(有前缀的情况下需要直接写在表名上) @columns 要更新的列(字段名=》新值) @condition 附加条件(用于条件筛选,sql中where的条件部分) @params 要绑定到附加条件的参数 */ function fmFunc_pdo_yii_update($connection, $table, $columns, $conditions = '', $params = array()) { $command = $connection->createCommand(); $result = $command->update($table, $columns, $conditions, $params)->execute(); return $result; } /* * 向指定表插入数据 @table, 要插入的表,使用绝对表名(有前缀的情况下需要直接写在表名上) @data 指定数据(字段名=》值) */ function fmFunc_pdo_yii_insert($connection, $table, $data = array()) { $command = $connection->createCommand(); $result = $command->insert($table, $data)->execute(); return $result; } /* * 取刚刚插入值的id * 必须确保在insert之后执行 */ function fmFunc_pdo_yii_insertid($connection,$table) { $result = $connection->getLastInsertedID(); return $result; } /* * 删除指定表指定条件数据 @table, 要插入的表,使用绝对表名(有前缀的情况下需要直接写在表名上) @condition 指定条件 @params 条件附加参数 array() */ function fmFunc_pdo_yii_delete($connection, $table, $condition = '', $params = []) { $command = $connection->createCommand()->delete($table, $condition); if(!empty($params) && is_array($params)){ foreach($params as $p=>$v){ $command->bindValue($p, $v); } } $result = $command->execute(); return $result; } /* * 批量运行sql脚本 run 高危操作,不开放 @sqls 要运行的脚本 脚本中的各sql命令必须明确指定表名 */ function fmFunc_pdo_yii_run($connection,$sqls) { return; if(!isset($sqls) || empty($sqls)) return; $ret = array(); $num = 0; $sqls = preg_replace("/\;[ \f\t\v]+/", ';', $sqls); foreach(explode(";\n", trim($sqls)) as $query) { $ret[$num] = ''; $queries = explode("\n", trim($query)); foreach($queries as $query) { $ret[$num] .= (isset($query[0]) && $query[0] == '#') || (isset($query[1]) && isset($query[1]) && $query[0].$query[1] == '--') ? '' : $query; } $num++; } unset($sqls); $result = []; foreach($ret as $query) { $query = trim($query); if($query) { $command = $connection->createCommand($query); $result[] = $command->query(); } } return $result; } /* * 判断表字段是否存在 @table 指定表 @filedname 字段名 */ function fmFunc_pdo_yii_fieldexists($connection,$table, $fieldname) { $sql = "DESCRIBE " . $table . " `{$fieldname}`"; $isexists = fmFunc_pdo_yii_fetch($connection,$sql, $params = array()); return !empty($isexists) ? true : false; } /* * 判断表索引是否存在 @table 指定表 @indexname 索引名 */ function fmFunc_pdo_yii_indexexists($connection,$table, $indexname) { $sql = "SHOW INDEX FROM " . $table; $indexes = fmFunc_pdo_yii_fetchall($connection,$sql, $params = array()); if (!empty($indexs) && is_array($indexs)) { foreach ($indexs as $row) { if ($row['Key_name'] == $indexname) { return true; } } } return false; } /* * 判断表是否存在 @table 指定表 */ function fmFunc_pdo_yii_tableexists($connection,$table) { $sql = "SHOW TABLES LIKE '{$table}'"; $isexists = fmFunc_pdo_yii_fetch($connection,$sql, $params = array()); return !empty($isexists) ? true : false; } /* * 取表字段 @table 指定表 TBD:设定fetchall的排序方法 */ function fmFunc_pdo_yii_tablefields($connection,$table) { $sql = "DESCRIBE {$table}"; $fields = fmFunc_pdo_yii_fetchall($connection,$sql, $params = array()); $fields = array_keys($fields); return $fields; }