Repository: wintercoder/datamaker Branch: master Commit: c42e12258dc1 Files: 9 Total size: 78.7 KB Directory structure: gitextract_0vul5z6j/ ├── LICENSE ├── README.MD ├── README_EN.MD ├── conf/ │ ├── common.ini │ └── local.ini ├── gensql.php ├── index.php ├── index_en.php └── sqlparse.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2018 wintercoder Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.MD ================================================ # 在线MYSQL测试数据生成工具 根据SQL表结构生成Mysql测试数据,根据字段名猜测类型,生成默认值 [English Readme](./README_EN.MD) ## 生成规则 普通规则见[官网](http://www.datamaker.online/),此处讲个规则的组合使用 **自增日期(组自增) + 常量列表(组模式)** 使用场景: 一个肯德基总店下有一堆子店,希望生成[每个子店每天]的数据,一行记录包含一个店一天的汇总数据 使用方法: 在子店ID 字段上选择 `常量列表(组模式)` 用逗号分隔写上各店的ID,日期选择 `自增日期(组自增)` 即可 **Unique Key** 有 `UNIQUE KEY (user_id,platform) ` 的情况 方案一: 这种platform只有1和2的可以考虑使用 给 `platform` 选 `常量列表(组模式)` 填 `1,2`,然后user_id选自增之类保证不重就OK。 方案二: 选择重复Key选项,测试数据一般跳过就好,注意选 `REPLACE INTO` 可能出现 `AUTO_INCREMENT` + 2 的情况 ## 个性化 你可以通过部署到自己的服务器上,对一些字段进行默认值配置,非常**适合公司内网** 部署也方便,代码里无任何框架依赖,能跑PHP就行 ### 配置 `/conf/common.ini` 为通用配置 `/conf/local.ini` 为个性化配置,适合不宜暴露的默认值,如测试账号ID等,该文件已加入 `.gitignore` 里,如果发现不生效则跑下命令 `git update-index --assume-unchanged conf/local.ini` 配置样例: ``` [0] key = index_day method = INCR_DAY value = 20180301 way = match [1] key = url method = RAND_PIC_URL value = 300,400 way = search ``` `key` : 字段名 `method` : 生成规则,如下表 `value` : 给前端的输入框值 `way` : 输入的key跟配置文件的key匹配规则,目前支持 `match` 完全相等、`search` 字符串包含,默认为`match` | 规则英文 | 中文 | 参数 | |----------|------|------| | INCR_INT | 自增int | from : 从from开始自增,步长1 | RAND_INT | 随机int | from,to : 生成[from,to]闭区间整数 | RAND_FLOAT | 随机浮点 | from,to,round : 生成[from,to]闭区间浮点,保留round位小数,这三个参数都是整数 | INCR_DAY | 日期自增 | from : 从from开始自增,步长1天,格式20180304 | INCR\_DAY\_GROUPLY | 日期自增(组模式) | 同上,但每组值里的这个日期不变,下条SQL才变 | RAND_TIMESTAMP | 随机时间戳 | from,to : 生成[from,to]这些天里的秒级时间戳,如 20180304,20180305 | RAND\_TIMESTAMP\_MYSQL | 随机时间(Mysql格式) | from,to : 生成[from,to]这些天里的秒级时间,格式:2018-04-07 18:08:34 | IGNORE | 不生成该列 | | CONST_STR | 常量 | 常量值 | CONST\_STR\_LIST | 常量列表(组模式) | 输入 a,b,每条SQL都输出 a,b,此时组数固定 | RAND_STR | 随机串 | length : 英文串长度 | RAND\_STR\_LIST | 随机串(指定列表) | a,b,c : 该字段只出现a,b,c之一,可重复出现 | INCR\_STR\_SUFFIX | 前缀+自增数字 | 输入 小王,输出 小王1,小王2,用于人名等 | RAND\_PIC\_URL | 图片地址 | width,height : 图片的宽高 ## 添加新生成规则流程 假设叫 rand_what 1. 前端 `index.php` `getDefaultValueByMethod()` 增加该规则的默认值 `getHoverContent()` 增加该规则的hover文案 `fillTabelWithData()` 增加对应代码 2. 后端 `gensql.php` `ValueGenerator` 类里新增 `randWhat()` 函数就行, 即下划线转驼峰 ## License **The MIT License** ================================================ FILE: README_EN.MD ================================================ # Online Sql Test Data Generator Generate sql test data by Mysql table structure Input: ``` CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `name` varchar(20) NOT NULL , PRIMARY KEY (`id`), ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ; ``` Output: ``` INSERT INTO `student (user_id,name) VALUES ('10001','John1'),('10002','John2'); INSERT INTO `student (user_id,name) VALUES ('10003','John3'),('10004','John4'); ``` **[Try It Now ](http://datamaker.online/index_en.php)** ## Individuation You can deploy this project on your server to set default value for frequently used fields, good for local area network like company Easy to deploy: It only require PHP (without any framework) and webserver ### Config `/conf/common.ini` is common config `/conf/local.ini` is personal config, good for same private value like test account. For safe, you should run `git update-index --assume-unchanged conf/local.ini` to avoid traced to git Sample: ``` [0] key = index_day method = INCR_DAY value = 20180301 way = match [1] key = url method = RAND_PIC_LIST value = 300,400 way = search ``` `key` : field name `method` : generate rule in the following table `value` : default value for front-end `way` : `match`:match this config if the input key is equals to key ; `search` match this config if the input key contains this key; the defalut value is `match` | Rule Method | Description | Parameter | |----------|------|------| | INCR_INT | auto increment integer | `from` : increase from `from` | RAND_INT | random int | `from,to` : random in interval `[from,to]` | RAND_FLOAT | random float | `from,to,round` : Random in interval `[from,to]` , retains `round` digits after the decimal point | INCR_DAY | increment day | `from` : increase from `from` with format 20180304 | INCR\_DAY\_GROUPLY | increment day (grouply same) | same as above,but the value is same in the same group | RAND_TIMESTAMP | random timestamp | `from,to` : timestamp in day `[from,to]`, input format is 20180304,20180305 | RAND\_TIMESTAMP\_MYSQL | random time (Mysql) | `from,to` : same as above,but the result format is 2018-04-07 18:08:34 | IGNORE | ignore this column | | CONST_STR | const value | | CONST\_STR\_LIST | const (group list) | `a,b`: every group in sql will output `a,b` | RAND_STR | random string | `length` : the length of string with alphabet | RAND\_STR\_LIST | random string in list | `a,b,c` : random pick one in `a,b,c` | INCR\_STR\_SUFFIX | Prefix + Number (Incr) | `test` : it will ouput test1,test2,test3 ... | RAND\_PIC\_URL | picture url | `width,height` : the width and height of url ## How to add new generator When you add a method call `rand_what` 1. Frontend: `index.php` `getDefaultValueByMethod()` add the default value `getHoverContent()` add the hover content `fillTabelWithData()` modify it 2. Backend`gensql.php` Add the function call `randWhat()` In class `ValueGenerator` ( Camel-Case of `rand_what` ) ## License **The MIT License** ================================================ FILE: conf/common.ini ================================================ [0] key = avatar method = RAND_PIC_URL value = 300,400 way = search [1] key = photo method = RAND_PIC_URL value = 300,400 way = search [2] key = picture method = RAND_PIC_URL value = 300,400 way = search [3] key = url method = RAND_PIC_URL value = 300,400 way = search [4] key = time method = RAND_TIMESTAMP value = 20180407,20180408 way = search [5] key = phone method = RAND_INT value = 15602000001,15602009999 way = search [6] key = insert_day method = INCR_DAY_GROUPLY value = 20180301 way = match [7] key = index_day method = INCR_DAY value = 20180301 way = match [8] key = status method = RAND_STR_LIST value = 0,1 way = search [9] key = data_day method = INCR_DAY_GROUPLY value = 20180301 way = match [10] key = is_ method = RAND_INT value = 0,1 way = search [11] key = flag method = RAND_INT value = 0,1 way = search [12] key = score method = RAND_INT value = 0,100 way = search ================================================ FILE: conf/local.ini ================================================ ================================================ FILE: gensql.php ================================================ randTimestamp($input,$groupSize); foreach ($ret as &$item){ $item = date("Y-m-d H:i:s",$item); } return $ret; } /** * 常量 * @param $input * value 常量值 * @param $groupSize int 每组多少条合并成一个数组,也是该次函数返回的数组大小 * @return array */ public function constStr($input,$groupSize){ return explode('$#$',str_repeat($input.'$#$',$groupSize)); } /** * 常量列表 * @param $input * value 常量值 * @param $groupSize int 每组多少条合并成一个数组,也是该次函数返回的数组大小 * @return array */ public function constStrList($input,$groupSize){ $input = !empty($input) ? $input : '百度$#$阿里$#$腾讯'; return explode('$#$',$input); } /** * 随机字符串(字符集:大小写字母) * @param $input array * length 长度 * @param $groupSize int 每组多少条合并成一个数组,也是该次函数返回的数组大小 * @return array */ public function randStr($length,$groupSize) { $ret = []; $count = 0; while ($count++ < $groupSize) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $ranStr = ''; for ($i = 0; $i < $length; $i++) { $ranStr .= $characters[rand(0, strlen($characters) - 1)]; } $ret []= $ranStr; } return $ret; } /** * 在字符串列表里 随机,全局可重复 * @param $input array * length 长度 * @param $groupSize int 每组多少条合并成一个数组,也是该次函数返回的数组大小 * @return array */ public function randStrList($input,$groupSize) { $input = !empty($input) ? $input : 'ofo,mobike'; $value = explode(',',$input); $ret = []; $count = 0; while ($count++ < $groupSize) { $ranStr = $value[rand(0, count($value) - 1)]; $ret []= $ranStr; } return $ret; } /** * 自增中文,根据 前缀+1 这种,如程序员1,程序员2 * @param $input array * pre_str 前缀 * @param $groupSize int 每组多少条合并成一个数组,也是该次函数返回的数组大小 * @return array */ public function incrStrPrefix($input,$groupSize,$sqlCounter) { $pre = !empty($input) ? $input : '测试店'; $inputArr['from'] = 1; $intArr = $this->incrInt($inputArr,$groupSize,$sqlCounter); $ret = []; foreach ($intArr as $intVal){ $str = $pre . $intVal; $ret []= $str; } return $ret; } /** * 随机图片,目前是 http://lorempixel.com/ * 也可以考虑用百度的 http://image.baidu.com/channel/listjson?pn=0&rn=30&tag1=%E7%BE%8E%E5%A5%B3&tag2=%E5%85%A8%E9%83%A8&ie=utf8 * @param $input * @param $groupSize int 每组多少条合并成一个数组,也是该次函数返回的数组大小 * @return array */ public function randPicUrl($input,$groupSize){ $exp = explode(',',$input); $width = !empty($exp[0]) ? intval($exp[0]) : 300; $height = !empty($exp[1]) ? intval($exp[1]) : 300; $url = "http://lorempixel.com/{$width}/{$height}/"; return explode(',',str_repeat($url.',',$groupSize)); } } class WorkHandler{ // /** // * 同步的数据导出,格式为.sql // */ // public function syncExportStr2File($fileName, $content){ // header_remove(); // ini_set('memory_limit', '128M'); // set_time_limit(1800); // header("Content-type:text/html;charset=utf-8"); // // header("Content-Transfer-Encoding: binary"); // // header("Content-Type: application/force-download;"); // header("Content-type: application/octet-stream"); // header("Cache-control: no-cache"); // header("Content-Transfer-Encoding: binary"); // header("Content-Disposition: attachment; filename=$fileName.sql"); // header("Expires: 0"); // header("Cache-control: private"); // header("Pragma: no-cache"); // header('Content-Length: ' . strlen($content)); // // $content = iconv('UTF-8', 'GBK//IGNORE', $content); // echo $content.PHP_EOL; // exit(); // } public function checkParams($input){ $input = json_decode($input,true); //json转数组 if(empty($input) || empty($input['list'])){ echo "参数不是JSON格式";exit(); } $input['count'] = intval($input['count']); $input['group_size'] = intval($input['group_size']); if( $input['count'] <= 0 || $input['group_size'] <= 0 || $input['count'] >= 5000 || $input['group_size'] >= 5000) { echo "条数、组数只能是 1 到 5000 以内";exit(); } } public function execute($input){ $this->checkParams($input); $input = json_decode($input,true); //json转数组 $inputList = $input['list']; //对于自增ID等忽略的类型,删除它并重排下标,不能用 array_splice ,删除多个时会乱 foreach ($inputList as $key => $item){ if($item['method'] == 'ignore'){ unset($inputList[$key]); } } $inputList = array_values($inputList); //从0开始 重建下标 $genCount = !empty($input['count']) ? $input['count'] : 5 ; $tableName = !empty($input['table_name']) ? $input['table_name'] : 'test'; $groupSize = $input['group_size']; //每个SQL有多少value组 $insertWay = !empty($input['insert_way']) ? $input['insert_way'] : 'INSERT INTO '; $keyArr = array_column($inputList,'key'); $generator = new ValueGenerator(); $sql = ''; //insert的SQL条数 for($genI = 1; $genI <= $genCount; $genI++) { $genResult = []; //生成结果数组: key => 生成方法+下标 防止同样的方法覆盖数据,value => 按该方法生成的数据数组 foreach ($inputList as $itemIndex => $item){ $genService = $this->camelize($item['method']); //方法名转驼峰 $genResult [ $item['method'].$itemIndex ] = $generator->$genService($item['value'],$groupSize,$genI ); } $valueStr = ''; //每个SQ有多少value组 for($cnt = 0; $cnt < $groupSize; $cnt++){ $valueStr .= '('; //取每个字段的值,单引号引起来 foreach ($genResult as $result) { $valueStr .= "'{$result[$cnt]}',"; } $valueStr = rtrim($valueStr,','); $valueStr .= '),'; } $valueStr = rtrim($valueStr,','); $valueStr .= ';'; $sql .= $insertWay . "{$tableName} (" . implode(",",$keyArr) .") VALUES $valueStr"; $sql .= "\n"; $sql .= "\n"; } //一直没成功,还怀疑是jq的post方法不是超链导致,最终用前端去做了 // $this->syncExportStr2File('datamake_'.date('YmdHis', time()).'sql' ,$sql); echo $sql; } /** * 下划线转驼峰 * @param $str * @param string $separator * @return string */ private function camelize($str,$separator='_') { $str = $separator. str_replace($separator, " ", strtolower($str)); return ltrim(str_replace(" ", "", ucwords($str)), $separator ); } } date_default_timezone_set("Asia/Shanghai"); $postData = file_get_contents('php://input'); //提交的是JSON,不能直接$post获取 if(empty($postData)){ echo "我是空白"; exit(); } //echo ($postData);exit(); $handler = new WorkHandler(); $handler->execute($postData); exit(); /* $parser = new CreateSqlParser(); $ret = $parser->execute($sql); if(0 == ($ret['error'])){ $input = json_encode($ret['data']); $handler = new WorkHandler(); $handler->execute($input); } else{ echo json_encode($ret);exit(); } */ ================================================ FILE: index.php ================================================
| 列名 | 生成规则 | 参数 |
|---|
Copyright © 小光 2017. 你的备案号
| Column | Rule | Parameter |
|---|