* Add zdata to the database script automatically.

This commit is contained in:
leiyong
2020-09-16 14:13:59 +08:00
parent d624251ab6
commit 484490eaa5
17 changed files with 218 additions and 34 deletions
+2 -2
View File
@@ -1,9 +1,9 @@
title: date
title: moreDate
desc: 系统可用日期类型
author: ly
version: 1.0
field: date
field: moreDate
instances:
- instance: dateA
fields:
+74
View File
@@ -0,0 +1,74 @@
<?php
$dbms = 'mysql'; // 数据库类型。
$host = 'localhost'; // 数据库主机名。
$port = '3306'; // 数据库主机端口。
$dbName = 'zdata'; // 使用的数据库。
$user = 'root'; // 数据库连接用户名。
$password = 'root'; // 对应的密码。
$dsn = "$dbms:host=$host:$port;dbname=$dbName";
$dirPath = 'demo/story'; // 要生成SQL数据的zdata文件路径。
/* 定义每个yaml文件生成SQL数量。 */
$zdataCount = array();
$zdataCount['zt_product'] = 100;
$zdataCount['zt_productplan'] = 500;
$zdataCount['zt_project'] = 100;
$zdataCount['zt_module'] = 1000;
$zdataCount['zt_story'] = 10000;
$zdataCount['zt_storyspec'] = 10000;
$zdataCount['zt_action'] = 10000;
$zdataExec = "zd -c $dirPath/%s -d %s -n %s -o $dirPath/sql/%s.sql -table %s";
set_time_limit(0);
try
{
$dbh = new PDO($dsn, $user, $password); // 初始化一个PDO对象。
echo $dbms . '连接成功' . PHP_EOL;
$yamlFilePath = scandir($dirPath);
foreach($yamlFilePath as $file)
{
if(strpos($file, '.yaml'))
{
$fileName = basename($file, '.yaml');
$count = isset($zdataCount[$fileName]) ? $zdataCount[$fileName] : 10;
$execYaml = sprintf($zdataExec, $file, $file, $count, $fileName, $fileName);
exec($execYaml); // 执行zdata命令调用yaml文件生成SQL数据。
echo '执行命令:' . $execYaml . PHP_EOL;
}
}
/* 读取生成的SQL文件,导入数据到数据库。*/
$sqlDir = $dirPath . '/sql/';
$sqlFilePath = scandir($sqlDir);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 设置报错处理方式。
$dbh->exec("SET GLOBAL sql_mode = '';");
$dbh->beginTransaction();
foreach($sqlFilePath as $file)
{
if(strpos($file, '.sql'))
{
$tableName = basename($file, '.sql');
$truncate = 'truncate table ' . $tableName;
$dbh->exec($truncate);
$sql = file_get_contents($sqlDir . $file);
$count = $dbh->exec($sql);
echo $tableName . '表影响行数:' . $count . PHP_EOL;
}
}
$dbh->commit();
$dbh = null; // 关闭连接。
}
catch (PDOException $e)
{
die('Error!: ' . $e->getMessage() . PHP_EOL);
}
+4 -4
View File
@@ -1,14 +1,14 @@
title: date
title: moreDate
desc: 系统可用日期类型
author: ly
version: 1.0
field: date
field: moreDate
instances:
- instance: dateA
fields:
- field: year
range: 2019-2020
range: 2019-2022
postfix: "-"
- field: month
range: 1-12
@@ -33,7 +33,7 @@ instances:
- instance: dateB
fields:
- field: year
range: 2019-2020
range: 2019-2022
postfix: "-"
- field: month
range: 1-12
+4 -11
View File
@@ -20,15 +20,15 @@ fields:
- field: product
note: "产品"
range: 1-100
prefix: ""
postfix: ""
prefix: ","
postfix: ","
loop: 0
format: ""
- field: project
note: "项目"
range: 1-100
prefix: ","
postfix: ","
prefix: ""
postfix: ""
loop: 0
format: ""
- field: actor
@@ -44,13 +44,6 @@ fields:
postfix: ""
loop: 0
format: ""
- field: date
note: "日期"
range: 10-22
prefix: "2020-09-09 "
postfix: ":11:24"
loop: 0
format: ""
- field: comment
note: "备注"
range: "1-10000"
View File
View File
+8 -8
View File
@@ -12,23 +12,23 @@ fields:
format: ""
- field: objectID
note: "对象ID"
range: "1-10000"
range: "1-100000"
prefix: ""
postfix: ""
loop: 0
format: ""
- field: product
note: "产品"
range: "1-10000"
prefix: ""
postfix: ""
range: "1-100000"
prefix: ","
postfix: ","
loop: 0
format: ""
- field: project
note: "项目"
range: "1-10000"
prefix: ","
postfix: ","
range: "1-100000"
prefix: ""
postfix: ""
loop: 0
format: ""
- field: actor
@@ -55,7 +55,7 @@ fields:
format: ""
- field: comment
note: "备注"
range: "1-10000"
range: "1-100000"
prefix: "这是一个系统日志测试备注"
postfix: ""
loop: 0