增加serial类型字段自动处理,增强变量类型处理功能

This commit is contained in:
wlx 2015-01-31 13:15:08 +00:00
parent 61f10ec7b2
commit 33313f5b8b
4 changed files with 70 additions and 28 deletions

View File

@ -4162,14 +4162,14 @@ class Admin_DataController extends Zend_Controller_Action
} }
//文件列表 //文件列表
if($ac == "files"){ elseif($ac == "files"){
$file = new Dataimport\File; $file = new Dataimport\File;
$this->_helper->json($file->getUploadFiles()); $this->_helper->json($file->getUploadFiles());
return true; return true;
} }
//刪除上傳的文件 //刪除上傳的文件
if($ac == "delete"){ elseif($ac == "delete"){
$file = $this->_getParam('file'); $file = $this->_getParam('file');
$fileHandle = new Dataimport\File; $fileHandle = new Dataimport\File;
$this->_helper->json(['success'=>$fileHandle->deleteFile($file)]); $this->_helper->json(['success'=>$fileHandle->deleteFile($file)]);
@ -4178,14 +4178,14 @@ class Admin_DataController extends Zend_Controller_Action
//預處理 //預處理
//判斷文件類型,初步識別文件內容 //判斷文件類型,初步識別文件內容
if($ac == "prepare"){ elseif($ac == "prepare"){
$this->_helper->viewRenderer('dataimport-preprocess'); $this->_helper->viewRenderer('dataimport-preprocess');
$file = $this->_getParam('file'); $file = $this->_getParam('file');
if(empty($file)) if(empty($file))
{ {
view::Post($this,"參數錯誤",-1); view::Post($this,"参数错误",-1);
return true; return true;
} }
@ -4217,10 +4217,10 @@ class Admin_DataController extends Zend_Controller_Action
} }
//檢查文件是否規則 //檢查文件是否規則
if($ac == "check"){ elseif($ac == "check"){
$file = $this->_getParam('file'); $file = $this->_getParam('file');
if(empty($file)){ if(empty($file)){
$this->_helper->json(['error'=>'參數錯誤']); $this->_helper->json(['error'=>'参数错误']);
return true; return true;
} }
@ -4237,14 +4237,14 @@ class Admin_DataController extends Zend_Controller_Action
} }
//選擇導入目標 //選擇導入目標
if($ac == "target") elseif($ac == "target")
{ {
$this->_helper->viewRenderer('dataimport-target'); $this->_helper->viewRenderer('dataimport-target');
$this->view->file = $file = $this->_getParam('file'); $this->view->file = $file = $this->_getParam('file');
if(empty($file)){ if(empty($file)){
view::Post($this,"參數錯誤",-1); view::Post($this,"参数错误",-1);
return true; return true;
} }
@ -4253,20 +4253,37 @@ class Admin_DataController extends Zend_Controller_Action
} }
//获得数据表 //获得数据表
if($ac == "gettables") elseif($ac == "gettables")
{ {
$schema = $this->_getParam('schema'); $schema = $this->_getParam('schema');
if(empty($schema)){ if(empty($schema)){
$this->_helper->json(['error'=>'參數錯誤']); $this->_helper->json(['error'=>'参数错误']);
return true; return true;
} }
$tableControl = new Visual\DataTableControl; $tableControl = new Visual\DataTableControl;
$this->_helper->json($tableControl->readTables($schema)); $this->_helper->json($tableControl->readTables($schema));
return true; return true;
} }
//获取表格字段名称和类型
elseif($ac=="getfields")
{
$schema = $this->_getParam('schema');
$table = $this->_getParam('table');
if(empty($schema) || empty($table)){
$this->_helper->json(['error'=>'参数错误']);
return true;
}
$tableControl = new Visual\DataTableControl;
$fields = $tableControl->readFields($schema . "." .$table);
if ($fields[0]['column_name']=='id') unset($fields[0]);
$this->_helper->json($fields);
return true;
}
//检查能否写入数据表 和 导入数据 //检查能否写入数据表 和 导入数据
if($ac == "checktables" || $ac == "import"){ elseif($ac == "checktables" || $ac == "import")
{
$this->_helper->layout->disableLayout(); $this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(); $this->_helper->viewRenderer->setNoRender();
@ -4274,7 +4291,7 @@ class Admin_DataController extends Zend_Controller_Action
$schema = $this->_getParam('schema'); $schema = $this->_getParam('schema');
$file = $this->_getParam('file'); $file = $this->_getParam('file');
if(empty($table) || empty($schema) || empty($file)){ if(empty($table) || empty($schema) || empty($file)){
$this->_helper->json(['error'=>'參數錯誤']); $this->_helper->json(['error'=>'参数错误']);
return true; return true;
} }
@ -4289,6 +4306,7 @@ class Admin_DataController extends Zend_Controller_Action
{ {
$tableControl = new Visual\DataTableControl; $tableControl = new Visual\DataTableControl;
$fields = $tableControl->readFields($schema . "." .$table); $fields = $tableControl->readFields($schema . "." .$table);
if ($fields[0]['column_name']=='id') unset($fields[0]);
$status = $processing->checkTableField($fields); $status = $processing->checkTableField($fields);
$this->_helper->json($status); $this->_helper->json($status);
@ -4296,7 +4314,13 @@ class Admin_DataController extends Zend_Controller_Action
} }
elseif($ac == "import"){ elseif($ac == "import"){
$status = $processing->import($schema . "." .$table); $tableControl = new Visual\DataTableControl;
$fields = $tableControl->readFields($schema . "." .$table);
$status = $processing->import($schema . "." .$table,$fields[0]['column_name']=='id');
if($status===true)
{
$fileHandle->deleteFile($file);
}
$this->_helper->json($status); $this->_helper->json($status);
return true; return true;
} }

View File

@ -24,7 +24,8 @@ $this->headTitle()->setSeparator(' - ');
<?php } ?> <?php } ?>
</div> </div>
<div class="span3" id="table-list"> <div class="span3" id="table-list">
</div>
<div class="span4" id="field-list">
</div> </div>
</div> </div>
<hr class="clear" /> <hr class="clear" />

View File

@ -22,13 +22,14 @@ abstract class AbstractProcessing {
switch ($pgsql_data_type){ switch ($pgsql_data_type){
case "integer": case "integer":
case "bigint": case "bigint":
case "int2vector":
case "real": case "real":
case "double precision": case "double precision":
return is_int($data) || is_float($data); return is_int($data) || is_float($data);
case "date": case "date":
case "timestamp without time zone":
case "char": case "char":
case "character varying": case "character varying":
case "text":
return is_string($data); return is_string($data);
default: default:

View File

@ -18,8 +18,8 @@ class Csv extends AbstractProcessing implements ProcessingInterface{
private $csv_object; private $csv_object;
private $csv_line_data; private $csv_line_data;
const ERROR_INDEX_NOT_MATCHED = "值的个数其它行不匹配"; const ERROR_INDEX_NOT_MATCHED = "值的个数其它行不匹配";
const ERROR_INDEX_TYPE_DIFFERENT = "值的类型上一行不同"; const ERROR_INDEX_TYPE_DIFFERENT = "值的类型上一行不同";
const ERROR_EMPTY_FILE = "文件是空的"; const ERROR_EMPTY_FILE = "文件是空的";
const ERROR_LINE_TITLE = "行:"; const ERROR_LINE_TITLE = "行:";
const ERROR_COLUMN_TITLE = "列:"; const ERROR_COLUMN_TITLE = "列:";
@ -108,6 +108,19 @@ class Csv extends AbstractProcessing implements ProcessingInterface{
return $this->csv_object; return $this->csv_object;
} }
/**
* 面向数据库字段的类型判断
* @return bool
*/
private function typeCompare($var1,$var2)
{
$t1=gettype($var1);
$t2=gettype($var2);
if ($t1==$t2) return true;
if (($t1=='integer' || $t1=='double') && ($t2=='integer' || $t2=='double'))
return true;
return false;
}
/** /**
* 檢查文件內容是否規則 * 檢查文件內容是否規則
* @return array|bool * @return array|bool
@ -127,12 +140,12 @@ class Csv extends AbstractProcessing implements ProcessingInterface{
if(count($v) != count($lines[$k-1])) if(count($v) != count($lines[$k-1]))
{ {
$error_stack[] = self::ERROR_LINE_TITLE . ($k + 1) .":".self::ERROR_INDEX_NOT_MATCHED; $error_stack[] = self::ERROR_LINE_TITLE . ($k + 1) .":".self::ERROR_INDEX_NOT_MATCHED;
continue; continue;
} }
foreach($v as $vIndex => $item){ foreach($v as $vIndex => $item){
if(gettype($item) != gettype($lines[$k-1][$vIndex])) if(!$this->typeCompare($item,$lines[$k-1][$vIndex]))
{ {
$error_stack[] = self::ERROR_LINE_TITLE . ($k + 1) .":".self::ERROR_INDEX_TYPE_DIFFERENT; $error_stack[] = self::ERROR_LINE_TITLE . ($k + 1) .":".self::ERROR_INDEX_TYPE_DIFFERENT;
} }
@ -157,15 +170,15 @@ class Csv extends AbstractProcessing implements ProcessingInterface{
$error_stack = []; $error_stack = [];
$first_line = $this->getLines()[0]; $first_line = $this->getLines()[0];
foreach($fields as $index => $field) $index=0;
foreach($fields as $field)
{ {
$func = $this->checkFieldsDataType($field['data_type'],$first_line[$index]); if (!$this->checkFieldsDataType($field['data_type'],$first_line[$index]))
{
if($func)
continue;
else
$error_stack[] = self::ERROR_COLUMN_TITLE . $index .":" . self::ERROR_DATA_TYPE_NOT_MATCHED; $error_stack[] = self::ERROR_COLUMN_TITLE . $index .":" . self::ERROR_DATA_TYPE_NOT_MATCHED;
}
$index++;
} }
if(count($error_stack) < 1) if(count($error_stack) < 1)
@ -176,7 +189,7 @@ class Csv extends AbstractProcessing implements ProcessingInterface{
} }
public function import($table) public function import($table,$withid=fasle)
{ {
$config = \Zend_Registry::get('config'); $config = \Zend_Registry::get('config');
$db = new PDO([ $db = new PDO([
@ -200,7 +213,10 @@ class Csv extends AbstractProcessing implements ProcessingInterface{
$place_holder[] = "?"; $place_holder[] = "?";
} }
$sth = $db->prepare("INSERT INTO $table VALUES (".join(",",$place_holder).")"); if ($withid)
$sth = $db->prepare("INSERT INTO $table VALUES (default,".join(",",$place_holder).")");
else
$sth = $db->prepare("INSERT INTO $table VALUES (".join(",",$place_holder).")");
foreach($this->getLines() as $line) foreach($this->getLines() as $line)
{ {