<?php header("content-type:text/html;charset=utf-8"); define("PerTable",4); if(!defined("PerTable")) exit("程序不能正常运行。 "); /** * 基础分表类,存在当数据删除后,原表容量不足的缺陷, * 建议新增可调整各表容量的方法,修改相应表映射关系。 * @author Jiang QQ:312036773 * @access public * @version 1.0.0 * @copyright .com */ class CarveTable{ private $mysqli; private $table; /** * 构造函数 * @param String $table * @return void */ function __construct($table){ $this->table = $table; $this->mysqli=new mysqli("localhost","root","1006","test"); $Utf8Sql="set names utf8;"; $this->mysqli->query($Utf8Sql); $initTableSql = "create table if not exists $this->table (`id` tinyint not null auto_increment primary key,`name` char(255) not null);"; $this->mysqli->query($initTableSql); $initMapSql = "create table if not exists map_$this->table (`rid` tinyint not null auto_increment primary key, `name` char(255) not null);"; $this->mysqli->query($initMapSql); $initMapInsertSql = "insert into map_$this->table (`name`) select '$this->table' from dual where not exists (select * from map_$this->table where map_$this->table.name = '$this->table');"; $this->mysqli->query($initMapInsertSql); } /** * 区间定位获取数据 * @param Integer $minId 最小ID * @param Integer $maxId 最大ID * @return Array */ function getScopeInfo($minId = 1,$maxId = null){ $mapBeginNum = ceil($minId/PerTable); if($maxId != null) { $mapEndNum = ceil($maxId/PerTable); $targetTablesResult = $this->mysqli->query("select name from map_$this->table where rid between $mapBeginNum and $mapEndNum"); } $targetTablesResult = $this->mysqli->query("select name from map_$this->table where rid >=$mapBeginNum;"); while($row = $targetTablesResult->fetch_assoc()){ if($maxId != null) $realSql = "select * from {$row['name']} where id between $minId and $maxId;"; else $realSql = "select * from {$row['name']} where id>=$minId"; $dataSource=$this->mysqli->query($realSql); while($row = $dataSource->fetch_assoc()){ $result[] = $row; } } return $result; } /** * 根据Map_表快速定位数据 * @param Integer $id * @return Array */ function getInfo($id){ $targetTable = ceil($id / PerTable) ; $findTableSql = "select name from map_$this->table where rid=$targetTable"; $tableRow = $this->mysqli->query($findTableSql)->fetch_row(); $result = $this->mysqli->query("select * from $tableRow[0] where id=$id;")->fetch_assoc(); return $result; } /** * 删除某一指定数据 * @param Integer $id * @return Boolean */ function delInfo($id){ $targetTable = ceil($id / PerTable) ; $findTableSql = "select name from map_$this->table where rid=$targetTable"; $tableRow = $this->mysqli->query($findTableSql)->fetch_row(); $this->mysqli->query("delete from $tableRow[0] where id=$id;"); if($this->mysqli->affected_rows > 0) return true; else return false; } /** * 分表方式增加数据 * @param String $post * @return Boolean */ function addData($post){ $getTableSql="SELECT table_name FROM information_schema.TABLES WHERE table_name like '$this->table%' order by create_time desc limit 0,1"; $target_table=$this->mysqli->query($getTableSql)->fetch_row(); $getCount = $this->mysqli->query("select count(`id`) from $target_table[0];")->fetch_row(); $insertSql = "insert into $target_table[0] (`name`) values ('$post');"; if($getCount[0] == PerTable) { $newTable = $this->table . "_" . time(); $createTableSql = "create table $newTable like $this->table;"; $this->mysqli->query($createTableSql); $startId = $this->mysqli->query("select id from $target_table[0] order by id desc limit 0,1;")->fetch_row(); $this->mysqli->query("insert into $newTable (`id`,`name`) values ($startId[0] + 1,'$post');"); $insertMapSql = "insert into map_$this->table (`name`) values ('$newTable');"; $this->mysqli->query($insertMapSql); } else $this->mysqli->query($insertSql); if($this->mysqli->affected_rows >0) return true; else return false; } }
评论回复