<?php
/**
* Created by PhpStorm.
* User: QQWWBYC
* Date: 2017/8/30
* Time: 13:44
*/
namespace Org\Net;
class xm
{
private $dba
private $dbname; //数据库名称,顶层元素,与数据库文件名称一致
private $dbtable; //数据表,要取得的节点
private $parser; //剖析器
private $vals; //属性
private $index; //索引
private $dbtable_array;//节点数组
private $array; //下级节点的数组
private $result; //返回的结果
private $querys;
private $xm
/**
* @param $dba
* @param $dbtable xm
* @name 初始化xm
*/
public function __construct($dba
$this->dba
$this->dbname=substr($dba
$this->dbtable=$dbtable;
$data=$this->Readxm
if(!$data){
die("无法读取 $this->dbname.xm
}
$this->parser = xm
xm
xm
$state=xm
xm
if(!$state){
die("无法读取 $this->dbname.xm
}
//遍历索引,筛选出要取值的节点 节点名:$dbtable
foreach ($this->index as $key=>$val) {
if ($key == $this->dbtable) {
//取得节点数组
$this->dbtable_array = $val;
} else {
continue;
}
}
for ($i=0; $i < count($this->dbtable_array); $i+=2) {
$offset = $this->dbtable_array[$i] + 1;
$len = $this->dbtable_array[$i + 1] - $offset;
//array_slice() 返回根据 offset 和 length 参数所指定的 array 数组中的一段序列。
//所取节点下级数组
$value=array_slice($this->vals,$offset,$len);
//取得有效数组,合并为结果数组
$this->array[]=$this->parseEFF($value);
}
return true;
}
/**
* @param $condition 查询条件
* @param $offset 取出数组开始坐标
* @param $limit_num 取出数据条数
* @return array|bool|int 根据查询条件和($offset和$limit_num)取出数据
*/
public function limit($condition,$offset,$limit_num){
if(empty($condition) && !is_array($condition)){
return false;
}
$offset=intval($offset);
$limit_num=intval($limit_num);
if(empty($limit_num)){
return false;
}
$data=$this->xm
if(empty($data)){
return false;
}
$data=array_values($data);
$data=array_slice($data,$offset,$limit_num);
if(empty($data)){
return false;
}
return $data;
}
/**
* @param $file xm
* @return bool|string xm
*/
private function Readxm
{
if(!file_exists($file)){
$this->initialization_xlm($file);
}
return file_get_contents($file);
}
/**
* @param $file xm
* @return bool 当数据库xm
*/
private function initialization_xlm($file){
if(!file_exists($file)){
$this->Writexm
}
return true;
}
/**
* @param $effective 坐标
* @return mixed 取得有效数组
*/
private function parseEFF($effective) {
for ($i=0; $i < count($effective); $i++){
$effect[$effective[$i]["tag"]] = $effective[$i]["value"];
}
return $effect;
}
//xm
/**
* @param $method 操作方式
* @param $condition 操作方式所对应的查询条件
* @param array $array 跟新或插入数据数组
* @return array|int|void 返回操作结果
*/
public function xm
{
if(($method=='select')||($method=='count')){
return $this->xm
} elseif($method=='insert') {
return $this->xm
} elseif($method=='update') {
return $this->xm
}elseif($method=='delete'){
return $this->xm
}else{
die("操作错误");
}
}
/**
* @param $condition 条件数组 组装
EQ 等于(=)
NEQ 不等于(<>)
GT 大于(>)
EGT 大于等于(>=)
LT 小于(<)
ELT 小于等于(<=)
BETWEEN 区间查询
IN 多值查询
*/
private function xm
if(is_array($condition)){
$operator_fields=array("EQ","NEQ","GT","EGT","LT","ELT","BETWEEN","IN");
$num=0;
foreach($condition as $key=>$val){
if(is_array($val)){
$operator=strtoupper($val[0]);//运算符
$field=$key;//条件字段
$match_val=$val[1];//筛选值
if(in_array($operator,$operator_fields) && $match_val){
$this->xm
$this->xm
if(is_string($match_val)){
if($operator=="EQ"){
//运算符 等于(=)
$this->xm
}elseif($operator=="NEQ"){
//运算符 不等于(<>)
$this->xm
}elseif($operator=="GT"){
//运算符 大于(>)
$this->xm
}elseif($operator=="EGT"){
//运算符 大于等于(>=)
$this->xm
}elseif($operator=="LT"){
//运算符 小于(<)
$this->xm
}elseif($operator=="ELT"){
//运算符 小于等于(<=)
$this->xm
}else{
die($operator.":查询条件错误");
}
}elseif(is_array($match_val)){
if($operator=="BETWEEN"){
//运算符 区间查询
$num=count($match_val);
if($num==2){
$this->xm
}else{
die("BETWEEN:查询条件最小值和最大值错误");
}
}elseif($operator=="IN"){
//运算符 IN 多个值查询
if(count($match_val)>=1){
$this->xm
}else{
die("IN:查询条件值错误");
}
}else{
die($operator.":查询条件错误");
}
}else{
die($operator.":查询条件错误");
}
}else{
die("查询条件错误,条件查询说明 :EQ 等于(=)NEQ 不等于(<>)GT 大于(>)EGT 大于等于(>=)LT 小于(<)ELT 小于等于(<=)BETWEEN 区间查询 IN 多值查询");
}
}else{
die("查询条件错误");
}
}
}else{
die("查询条件为数组");
}
}
/**
* @return array 得到当前表名下所有数据
*/
public function get_xm
return $this->xm
}
/**
* @param $data 批量新增数组 必须是二维数组
* @return bool 批量新增多条数据
*/
public function all_xm
if(empty($data)){
return false;
}
$data_Original=$this->get_xm
foreach($data as $val){
if(is_array($val) && !empty($val)){
$data_Original[]=$val;
}else{
break;
}
}
if(empty($data_Original)){
return false;
}
$this->Writexm
return true;
}
/**
* @param null $condition 查询条件
* @return array 获取所有满足条件数据
* @name 筛选满足条件数据
*/
private function xm
{
$row = $this->array; //初始化数据数组
if($condition) {
//是否有条件,如有条件则生成符合条件的数组
//生成条件数组,条件格式 field,operator,match
$this->xm
$cs=count($this->xm
if($cs==0){
die("查询条件为空");
}
if(!empty($row)){
foreach($row as $key=>$val){
$r=$key;
foreach($this->xm
$field=$va['field']; //字段
$operator=$va["operator"];//运算符
$match=$va['match']; //匹配
if(($operator=='=')&&($row[$r][$field]==$match)){
$need_key=$r;
} elseif(($operator=='!=')&&($row[$r][$field]!=$match)){
$need_key=$r;
} elseif(($operator=='>')&&($row[$r][$field]>$match)){
$need_key=$r;
} elseif(($operator=='>=')&&($row[$r][$field]>=$match)){
$need_key=$r;
} elseif(($operator=='<')&&($row[$r][$field]<$match)){
$need_key=$r;
} elseif(($operator=='<=')&&($row[$r][$field]<=$match)){
$need_key=$r;
}elseif(($operator=='BETWEEN_AND')&&($row[$r][$field]>=$match[0] && $row[$r][$field]<=$match[1])){
$need_key=$r;
}elseif(($operator=='IN')&&(in_array($row[$r][$field],$match))){
$need_key=$r;
}
}
if(isset($row[$need_key])){
$result[$need_key]=$row[$need_key];
}
}
$this->dbtable_array=null;
$this->array=null;
}
} else {
$result=$this->array;
}
return $result;
}
/**
* @param $method 查询方式 select 统计数据个数count
* @param $condition 查询条件
* @return array|int 获取所有满足条件数据
* @name 查询满足条件的数据
*/
public function xm
{
$result=$this->xm
if($method=='select'){
return $result;
} else {
return count($result);
}
}
/**
* @param $arr 需要判断数组
* @return mixed 判断数据的维数
*/
public function arrayLevel($arr){
$al = array(0);
$this->aL($arr,$al,$level=0);
return max($al);
}
/**
* @param $arr 需要判断数组
* @param $al 关联数据
* @param int $level 级别
* @name 判断数据的维数的核心方法
*/
private function aL($arr,&$al,$level=0){
if(is_array($arr)){
$level++;
$al[] = $level;
foreach($arr as $v){
$this->aL($v,$al,$level);
}
}
}
/**
* @param $array 要插入的数据
* @name 新增数据
*/
public function xm
{
if(empty($array)){
return false;
}
$num=$this->arrayLevel($array);
if($num==1){
$data=$this->xm
$data=array_filter($data);
$data[]=$array; //插入后的总数据数组
$this->array=$data; //更新总数组
$this->Writexm
return true;
}
return false;
}
/**
* @param $condition 需要删除的数据的条件语句
* @return bool 删除不想要的数据
*/
public function xm
if(empty($condition)){
return false;
}
$datas=$this->get_xm
$del_data=$this->xm
$del_data=array_keys($del_data);
if(empty($del_data)){
return false;
}
foreach($del_data as $val){
$state=isset($datas[$val])?true:false;
if($state){
unset($datas[$val]);
}
}
if(empty($datas)){
$this->initialization_xlm($this->dba
}
$datas=array_values($datas);
$this->Writexm
return true;
}
/**
* @param $condition 更新条件
* @param $array 要更新的数组
* @return bool 跟新数据
*/
public function xm
{
$datas=$this->get_xm
$subtract=$this->xm
if(empty($subtract) || empty($datas)){
return false;
}
$state=1;
$keys_update=array_keys($subtract);
if(empty($keys_update)){
return false;
}
foreach($keys_update as $val){
$new_data_arr=isset($datas[$val])?$datas[$val]:null;
if(empty($new_data_arr)){
$state=0;
break;
}
$new_data_arr=$this->update_data_assemble($new_data_arr,$array);
if($new_data_arr){
$datas[$val]=$new_data_arr;
}else{
$state=0;
break;
}
}
if(empty($state)){
return false;
}
$this->array=$datas;
$this->Writexm
return true;
}
/**
* @param $data_arr 原始数据
* @param $update_arr 需要更新的数据 跟新的数据的值不能null
* @return array|bool 返回当前的要跟新的数据
*/
private function update_data_assemble($data_arr,$update_arr){
if(empty($data_arr) || empty($update_arr)){
return false;
}
$state_update=false;
$need_update_num=0;
if(is_array($data_arr) && is_array($update_arr)){
foreach($data_arr as $key=>$val){
if(is_array($val)){
$state_update=true;
break;
}
$update_val=isset($update_arr[$key])?$update_arr[$key]:null;
if($update_val=='0'){
if($data_arr[$key] != $update_val){
$data_arr[$key]=$update_val;
$need_update_num++;
}
}
if(!empty($update_val)){
if($data_arr[$key] != $update_val){
$data_arr[$key]=$update_val;
$need_update_num++;
}
}
}
if($state_update){
return false;
}
if($need_update_num==0){
return false;
}
return $data_arr;
}
return false;
}
/**
* @param null $array 写入xm
*/
private function Writexm
{
if(is_dir($this->dba
if(!is_writeable($this->dba
die("无法写入".$this->dbname.".xm
}
}
$xm
$xm
$xm
if(empty($array)){
$xm
$xm
}else{
for($i=0;$i<count($array);$i++){
$xm
foreach($array[$i] as $k=>$s){
$xm
}
$xm
}
}
$xm
$fp=@fopen($this->dba
flock($fp, LOCK_EX);
rewind($fp);
fputs($fp,$xm
fclose($fp);
}
}
?>
