* Add print errors when zendata insert mysql failed.

This commit is contained in:
zhaoke
2023-05-04 09:40:30 +08:00
parent fa6e45007f
commit a91f0419cc
4 changed files with 62 additions and 37 deletions
@@ -3,8 +3,8 @@
include dirname(__FILE__, 5) . "/test/lib/init.php";
su('admin');
$project = zdTable('project');
$project->gen(5);
$project = zdTable('project')->config('project');
$project->gen(4);
/**
@@ -12,7 +12,7 @@ title=测试 projectModel->getListByCurrentUser();
timeout=0
cid=1
- 执行projectModel模块的getListByCurrentUser方法 @1
- 执行projectModel模块的getListByCurrentUser方法 @0
- 执行projectModel模块的getListByCurrentUser方法 @1
@@ -27,6 +27,9 @@ cid=1
global $tester;
$projectModel = $tester->loadModel('project');
r(count($projectModel->getListByCurrentUser())) && p() && e('1');
$tester->app->user->admin = true;
$tester->app->config->vision = 'lite';
r(count($projectModel->getListByCurrentUser())) && p() && e('0');
$tester->app->config->vision = 'rnd';
r(count($projectModel->getListByCurrentUser())) && p() && e('1');
r($projectModel->getListByCurrentUser()) && p('11:id,name') && e('11,项目1');
@@ -0,0 +1,16 @@
title: zt_project
author: liuhong
version: "1.0"
fields:
- field: type
range: 'project,sprint,project,stage'
- field: status
range: 'wait,doing,closed,suspended'
- field: deleted
range: 0,1,1,0
- field: vision
range: rnd,rnd,rnd,lite
- field: openedDate
range: "20220101 000000:1W" # 生成从2021年1月1日0时0分0秒到当前时间的数据,步长为1周
type: timestamp
format: "YY/MM/DD hh:mm:ss"
+2 -3
View File
@@ -102,9 +102,6 @@ function p($keys = '', $delimiter = ',')
{
global $_result;
$_keys = $keys;
$_delimiter = $delimiter;
if(empty($_result)) return print(implode("\n", array_fill(0, substr_count($keys, $delimiter) + 1, 0)) . "\n");
if(is_array($_result) and isset($_result['code']) and $_result['code'] == 'fail') return print((string) $_result['message'] . "\n");
@@ -117,6 +114,8 @@ function p($keys = '', $delimiter = ',')
foreach($parts as $part)
{
$values = getValues($_result, $part, $delimiter);
if(!is_array($values)) continue;
foreach($values as $value) echo $value . "\n";
}
+37 -30
View File
@@ -341,21 +341,6 @@ class yaml
$this->insertDB($yamlFile, $this->tableName, $rows, $isClear);
}
/**
* 打印zendata生成的sql和导入数据库产生的错误.
* Print sql and error.
*
* @param int $rows
* @param bool $isClear Truncate table if set isClear to true.
* @param bool $debug Print sql and error if set isClear to true.
* @access public
* @return void
*/
public function debug($rows, $isClear = true)
{
$yamlFile = $this->mergeYaml();
$this->insertDB($yamlFile, $this->tableName, $rows, $isClear, true);
}
/**
* Insert the data into database.
*
@@ -363,11 +348,10 @@ class yaml
* @param string $tableName
* @param int $rows
* @param bool $isClear Truncate table if set isClear to true.
* @param bool $debug
* @access public
* @return string
*/
function insertDB($yamlFile, $tableName, $rows, $isClear = true, $debug = false)
function insertDB($yamlFile, $tableName, $rows, $isClear = true)
{
$tableSqlDir = "{$_SERVER['PWD']}/data/sql";
@@ -385,32 +369,55 @@ class yaml
$dbUser = $this->config->db->user;
$dbPWD = $this->config->db->password;
$command = "$zdPath -c %s -d %s -n %d -t %s -dns mysql://%s:%s@%s:%s/%s#utf8";
$command = "mysql -u%s -p%s -h%s -P%s -D%s < %s";
$genSQL = "$zdPath -c %s -d %s -n %d -t %s -o %s";
if($isClear === true)
{
/* Truncate table to reset auto increment number. */
system(sprintf("mysql -u%s -p%s -h%s -P%s %s -e 'truncate %s' 2>/dev/null", $dbUser, $dbPWD, $dbHost, $dbPort, $dbName, $tableName));
$command .= ' --clear';
}
$sqlPath = "{$tableSqlDir}/{$tableName}_zd.sql";
$execYaml = sprintf($command, $configYaml, $yamlFile, $rows, $tableName, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName);
$execYaml = sprintf($command, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName, $sqlPath);
$execDump = sprintf($dumpCommand, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName, $tableName);
$execGenSQL = sprintf($genSQL, $configYaml, $yamlFile, $rows, $tableName, $sqlPath);
system($execGenSQL);
if($debug)
{
echo "\n" . file_get_contents($sqlPath) . "\n";
$tmpl = "mysql -u%s -p%s -h%s -P%s -D%s < %s";
system(sprintf($tmpl, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName, $sqlPath));
return;
}
system($execDump);
system($execYaml);
$stderr = '';
$this->exec_with_stderr($execYaml, $stderr);
if(empty($stderr)) return;
$errors = explode(PHP_EOL, $stderr);
$errors = array_filter($errors, function($error)
{
return !empty($error) && !strpos($error, 'Using a password on the command line interface can be insecure');
});
if(!empty($errors)) echo implode(PHP_EOL, $errors) . PHP_EOL;
}
/**
* 执行系统命令,并捕捉stderror
* exec command and catch stderror.
*
* @param string $cmd
* @param string $stderr
* @access private
* @return string
*/
private function exec_with_stderr($cmd, &$stderr=null)
{
$proc = proc_open($cmd,array(
1 => array('pipe','w'),
2 => array('pipe','w'),
),$pipes);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
return proc_close($proc);
}
/**