add missing dataimport.php file and add support for importing tab separated data
authorchriskl <chriskl>
Mon, 12 Apr 2004 06:43:15 +0000 (06:43 +0000)
committerchriskl <chriskl>
Mon, 12 Apr 2004 06:43:15 +0000 (06:43 +0000)
HISTORY
dataimport.php [new file with mode: 0644]
tblproperties.php

diff --git a/HISTORY b/HISTORY
index a89089200df7571277cea2784a745ed35144ab8a..3ce4a4c6abc60757209135e27d0154753762c86d 100644 (file)
--- a/HISTORY
+++ b/HISTORY
@@ -11,7 +11,7 @@ Features
 * Allow adding array columns to tables
 * Allow creating domains with type length and arrays
 * Show domain base type and comment in domains list
-* Allow import of CSV data
+* Allow import of CSV and Tabbed data
 * Allow upload and execution of _basic_ SQL scripts
 
 Bugs
diff --git a/dataimport.php b/dataimport.php
new file mode 100644 (file)
index 0000000..84999a6
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+
+       /**
+        * Does an import to a particular table from a text file
+        *
+        * $Id: dataimport.php,v 1.1 2004/04/12 06:43:15 chriskl Exp $
+        */
+
+       // Include application functions
+       include_once('./libraries/lib.inc.php');
+
+       $misc->printHeader($lang['strimport']);         
+       $misc->printTableNav();
+       echo "<h2>", $misc->printVal($_REQUEST['database']), ": ", $misc->printVal($_REQUEST['table']), ": {$lang['strimport']}</h2>\n";
+
+       // Check that file is specified and is an uploaded file
+       if (isset($_FILES['source']) && is_uploaded_file($_FILES['source']['tmp_name']) && is_readable($_FILES['source']['tmp_name'])) {
+               
+               $fd = fopen($_FILES['source']['tmp_name'], 'r');
+               // Check that file was opened successfully
+               if ($fd !== false) {            
+                       $status = $data->beginTransaction();
+                       if ($status != 0) {
+                               $misc->printMsg($lang['strimporterror']);
+                               exit;
+                       }
+                       
+                       switch ($_REQUEST['format']) {
+                               case 'csv':
+                               case 'tab':
+                                       // XXX: Length of CSV lines limited to 100k
+                                       $csv_max_line = 100000;
+                                       // Set delimiter to tabs or commas
+                                       if ($_REQUEST['format'] == 'csv') $csv_delimiter = ',';
+                                       else $csv_delimiter = "\t";
+                                       // Get first line of field names
+                                       $fields = fgetcsv($fd, $csv_max_line, $csv_delimiter);
+                                       $row = 1;
+                                       while ($line = fgetcsv($fd, $csv_max_line, $csv_delimiter)) {
+                                               // Build value map
+                                               $vars = array();
+                                               $nulls = array();
+                                               $format = array();                                              
+                                               $i = 0;
+                                               foreach ($fields as $f) {
+                                                       // Check that there is a column
+                                                       if (!isset($line[$i])) {
+                                                               $misc->printMsg(sprintf($lang['strimporterrorline'], $row));
+                                                               exit;
+                                                       }
+                                                       // Check for nulls
+                                                       if ($line[$i] == '\\N') $nulls[$f] = 'on';
+                                                       // Add to value array
+                                                       $vars[$f] = $line[$i];
+                                                       // Format is always VALUE
+                                                       $format[$f] = 'VALUE';
+                                                       // Type is always text
+                                                       $types[$f] = 'text';
+                                                       $i++;
+                                               }
+                                               $status = $data->insertRow($_REQUEST['table'], $vars, $nulls, $format, $types);
+                                               if ($status != 0) {
+                                                       $data->rollbackTransaction();
+                                                       $misc->printMsg(sprintf($lang['strimporterrorline'], $row));
+                                                       exit;
+                                               }
+                                               $row++;
+                                       }
+                                       break;
+                               default:
+                                       // Unknown type
+                                       $data->rollbackTransaction();
+                                       $misc->printMsg($lang['strinvalidparam']);
+                                       exit;
+                       }
+       
+                       $status = $data->endTransaction();
+                       if ($status != 0) {
+                               $misc->printMsg($lang['strimporterror']);
+                               exit;
+                       }
+                       fclose($fd);
+
+                       $misc->printMsg($lang['strfileimported']);
+               }
+               else {
+                       // File could not be opened
+                       $misc->printMsg($lang['strimporterror']);
+               }
+       }
+       else {
+               // Upload went wrong
+               $misc->printMsg($lang['strimporterror']);
+       }
+       
+       $misc->printFooter();
+
+?>
index b15803b918a508b308c3a59649ea85b0acf8049f..b718a713fa8f9fefc9ca261777734cbc82e230da 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * List tables in a database
         *
-        * $Id: tblproperties.php,v 1.40 2004/04/12 06:30:55 chriskl Exp $
+        * $Id: tblproperties.php,v 1.41 2004/04/12 06:43:15 chriskl Exp $
         */
 
        // Include application functions
                                //echo "<option value=\"copy\">COPY</option>\n";
                                //echo "<option value=\"sql\">SQL</option>\n";
                                echo "<option value=\"csv\">CSV</option>\n";
-                               //echo "<option value=\"tab\">Tabbed</option>\n";
+                               echo "<option value=\"tab\">Tabbed</option>\n";
                                //echo "<option value=\"html\">XHTML</option>\n";
                                //echo "<option value=\"xml\">XML</option>\n";
                                echo "</select>\n</td>\n</tr>\n";