From 33313f5b8b3be8bb81d30012eedec64419aef0c4 Mon Sep 17 00:00:00 2001 From: wlx Date: Sat, 31 Jan 2015 13:15:08 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0serial=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E8=87=AA=E5=8A=A8=E5=A4=84=E7=90=86=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=8F=98=E9=87=8F=E7=B1=BB=E5=9E=8B=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controllers/DataController.php | 50 ++++++++++++++----- .../scripts/data/dataimport-target.phtml | 3 +- .../Westdc/Dataimport/AbstractProcessing.php | 3 +- .../Westdc/Dataimport/Processing/Csv.php | 42 +++++++++++----- 4 files changed, 70 insertions(+), 28 deletions(-) diff --git a/application/admin/controllers/DataController.php b/application/admin/controllers/DataController.php index 19fe71b1..a152161f 100755 --- a/application/admin/controllers/DataController.php +++ b/application/admin/controllers/DataController.php @@ -4162,14 +4162,14 @@ class Admin_DataController extends Zend_Controller_Action } //文件列表 - if($ac == "files"){ + elseif($ac == "files"){ $file = new Dataimport\File; $this->_helper->json($file->getUploadFiles()); return true; } //刪除上傳的文件 - if($ac == "delete"){ + elseif($ac == "delete"){ $file = $this->_getParam('file'); $fileHandle = new Dataimport\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'); $file = $this->_getParam('file'); if(empty($file)) { - view::Post($this,"參數錯誤",-1); + view::Post($this,"参数错误",-1); return true; } @@ -4217,10 +4217,10 @@ class Admin_DataController extends Zend_Controller_Action } //檢查文件是否規則 - if($ac == "check"){ + elseif($ac == "check"){ $file = $this->_getParam('file'); if(empty($file)){ - $this->_helper->json(['error'=>'參數錯誤']); + $this->_helper->json(['error'=>'参数错误']); 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->view->file = $file = $this->_getParam('file'); if(empty($file)){ - view::Post($this,"參數錯誤",-1); + view::Post($this,"参数错误",-1); return true; } @@ -4253,20 +4253,37 @@ class Admin_DataController extends Zend_Controller_Action } //获得数据表 - if($ac == "gettables") + elseif($ac == "gettables") { $schema = $this->_getParam('schema'); if(empty($schema)){ - $this->_helper->json(['error'=>'參數錯誤']); + $this->_helper->json(['error'=>'参数错误']); return true; } $tableControl = new Visual\DataTableControl; $this->_helper->json($tableControl->readTables($schema)); 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->viewRenderer->setNoRender(); @@ -4274,7 +4291,7 @@ class Admin_DataController extends Zend_Controller_Action $schema = $this->_getParam('schema'); $file = $this->_getParam('file'); if(empty($table) || empty($schema) || empty($file)){ - $this->_helper->json(['error'=>'參數錯誤']); + $this->_helper->json(['error'=>'参数错误']); return true; } @@ -4289,6 +4306,7 @@ class Admin_DataController extends Zend_Controller_Action { $tableControl = new Visual\DataTableControl; $fields = $tableControl->readFields($schema . "." .$table); + if ($fields[0]['column_name']=='id') unset($fields[0]); $status = $processing->checkTableField($fields); $this->_helper->json($status); @@ -4296,7 +4314,13 @@ class Admin_DataController extends Zend_Controller_Action } 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); return true; } diff --git a/application/admin/views/scripts/data/dataimport-target.phtml b/application/admin/views/scripts/data/dataimport-target.phtml index 9344d9e6..4562d61e 100644 --- a/application/admin/views/scripts/data/dataimport-target.phtml +++ b/application/admin/views/scripts/data/dataimport-target.phtml @@ -24,7 +24,8 @@ $this->headTitle()->setSeparator(' - ');
- +
+

diff --git a/application/module/Westdc/Dataimport/AbstractProcessing.php b/application/module/Westdc/Dataimport/AbstractProcessing.php index 135a9d37..707778f1 100644 --- a/application/module/Westdc/Dataimport/AbstractProcessing.php +++ b/application/module/Westdc/Dataimport/AbstractProcessing.php @@ -22,13 +22,14 @@ abstract class AbstractProcessing { switch ($pgsql_data_type){ case "integer": case "bigint": - case "int2vector": case "real": case "double precision": return is_int($data) || is_float($data); case "date": + case "timestamp without time zone": case "char": case "character varying": + case "text": return is_string($data); default: diff --git a/application/module/Westdc/Dataimport/Processing/Csv.php b/application/module/Westdc/Dataimport/Processing/Csv.php index b1c65f5c..d6e54e07 100644 --- a/application/module/Westdc/Dataimport/Processing/Csv.php +++ b/application/module/Westdc/Dataimport/Processing/Csv.php @@ -18,8 +18,8 @@ class Csv extends AbstractProcessing implements ProcessingInterface{ private $csv_object; private $csv_line_data; - const ERROR_INDEX_NOT_MATCHED = "值的个数于其它行不匹配"; - const ERROR_INDEX_TYPE_DIFFERENT = "值的类型于上一行不同"; + const ERROR_INDEX_NOT_MATCHED = "值的个数与其它行不匹配"; + const ERROR_INDEX_TYPE_DIFFERENT = "值的类型与上一行不同"; const ERROR_EMPTY_FILE = "文件是空的"; const ERROR_LINE_TITLE = "行:"; const ERROR_COLUMN_TITLE = "列:"; @@ -108,6 +108,19 @@ class Csv extends AbstractProcessing implements ProcessingInterface{ 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 @@ -127,12 +140,12 @@ class Csv extends AbstractProcessing implements ProcessingInterface{ 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; } 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; } @@ -157,15 +170,15 @@ class Csv extends AbstractProcessing implements ProcessingInterface{ $error_stack = []; $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($func) - continue; - else + if (!$this->checkFieldsDataType($field['data_type'],$first_line[$index])) + { $error_stack[] = self::ERROR_COLUMN_TITLE . $index .":" . self::ERROR_DATA_TYPE_NOT_MATCHED; + } + $index++; } 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'); $db = new PDO([ @@ -200,7 +213,10 @@ class Csv extends AbstractProcessing implements ProcessingInterface{ $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) {