home *** CD-ROM | disk | FTP | other *** search
Wrap
Notes2SQL by Hans Schou PHP script for converting data from Lotus Notes to SQL statements. <html> <title>Lotus Notes to SQL converter</title> <h1>Lotus Notes to SQL converter</h1> <i>by Hans Schou notesql@schou.dk</i> <p> <?php if (file_exists( "db-oci8.phl")) { $gotsql = 1; include( "db-oci8.phl"); } else $gotsql = 0; function loaddemo() { $d[] = "FirstName: Hans"; $d[] = "Lastname: Schou"; $d[] = "Company: ISS"; $d[] = "Phone: +45 3817 6372"; $d[] = chr(12); $d[] = "FirstName: John"; $d[] = "LastName: Doe"; $d[] = "Email: john.doe@acme.com"; $d[] = "Company: Acme"; $d[] = "Address: Go to gate"; $d[] = "The beach"; // this line demonstrates 'multiple-lines' $d[] = "Christmas Island"; $d[] = chr(12); return $d; } // Select list for SQL types function listtypes( $fname ) { echo "<select name=\"$ftyp_$fname\" size=\"1\">\n"; ?> <option>VARCHAR2</option> <option>NVARCHAR2</option> <option>NUMBER</option> <option>LONG</option> <option>DATE</option> <option>CHAR</option> <option>NCHAR</option> <option>ROWID</option> <option>MLSLABEL</option> <option>CLOB</option> <option>NCLOB</option> <option>BLOB</option> <option>BFILE</option> </select> <?php } // Parse the structured text file and convert to SQL function parseone( $userfile, $userfile_name, $table ) { /* ParseOne: get all field names and there size. Select which fields which should be included and there size */ if (!strlen($userfile_name)) echo "Loading demo...<br>\n"; else echo "Proccesing <b>".$userfile_name. "</b>...<br>\n"; // load file if ($userfile_name) $data = file($userfile); else $data = loaddemo(); echo "<pre>"; // Lastfield is used for 'body text' with several lines $lastfname = ""; // init array $record = array(); while (list($key,$value) = each($data)) { // filter value for CR-LF $v = ereg_replace( "\r", "",ereg_replace( "\n", "",$value)); // check if it looks like a field name, colon-space-space if (strpos($v, ": ")) { $pair = split( ": ",$v); // get field name and filter out 'dollar' $fname = strtolower(ereg_replace( '$', '',$pair[0])); // get field size $fsize = strlen(trim($pair[1])); // add field name and size to the record if (strlen(trim($fname)) && $fsize) { $record[$fname] = ereg_replace( "'", "''",trim($pair[1])); // add up the global field list if ($field[$fname] < $fsize) $field[$fname] = $fsize; } } else if (ord($value) == 12) { // check for form-feed and output record in SQL // output field names echo "INSERT INTO\n $table("; reset($record); $comma = 0; // output field names while (list($fn,$fv) = each($record)) if (strlen($fv)) { if ($comma) echo ","; $comma = 1; echo "'".$fn. "'"; } echo ")\n VALUES("; reset($record); $comma = 0; // output values while (list($fn,$fv) = each($record)) if (strlen($fv)) { if ($comma) echo ","; $comma = 1; // replace one quote with two quotes echo "'".ereg_replace( "'", "''",$fv). "'"; } echo ");\n"; // clear records $record = array(); } else { // it was not a field name or FF, then it must be the 'body' // add up the data to previus field $record[$fname] .= "\n".$v; if ($field[$fname] < strlen($record[$fname])) $field[$fname] = strlen($record[$fname]); } } // while echo "<p><hr><p>"; // sort fields ksort($field); reset($field); $comma = 0; echo "# Suggested table definition\n"; echo "CREATE TABLE $table(\n"; while (list($key,$value) = each($field)) { if ($comma) echo ",\n"; $comma = 1; echo " ".$key. " VARCHAR2(".$value. ")"; } echo ");\n"; echo "<hr>\n"; reset($field); echo "<form action=\"$PHP_SELF\" method=\"post\">\n"; while (list($key,$value) = each($field)) { echo "<input type=\"checkbox\" checked name=\"fuse_$key\">"; echo "<input type=\"text\" name=\"fnam_$key\" value=\"$key\">"; listtypes( $key ); echo "<input type=\"text\" name=\"fsiz_$key\" value=\"$value\" size=\"4\">"; echo "\n"; } // delete input file echo "\n<!-- Deleting: $userfile -->\n"; if (strlen($userfile_name)>1 && unlink($userfile)) echo "<!-- $userfile has been deleted -->\n"; } switch (strtolower($action)) { case "send file": parseone( $userfile, $userfile_name, $table ); break; default: ?> <table border="5"><tr><td> <form enctype="multipart/form-data" action=" <?php echo $PHP_SELF;?>" method="post"> <table border="0" cellpadding="5"> <tr> <td>New table name</td> <td><input type="text" name="table" value="mytable" size="8"></td> </tr> <tr> <td>Upload file</td> <td><input type="file" name="userfile" value="c:\notoes2sql.txt"></td> </tr> <tr> <td colspan="2"><input type="submit" name="action" value="Send file"></td> </tr> </table> </td></tr></table> <p><hr> <h2>Description</h2> This program convert a "structured text" export from Lotus Notes to standard SQL statements. The limitations with this convertion is that you do not get attachments in the export file. Attachments has to be inserted manually afterwards. <ol> <li>Go in to Lotus Notes</li> <li>Select a view</li> <li>Choose File/Export</li> <li>Type a file name and choose "Save as type: Structured Text"</li> <li>Select all documents and "Inter-document delimiter=12"</li> <li>On this page, "browse" to the file and send it</li> <li>Copy and paste the result into a text file and load it in your SQL database</li> </ol> <h3>Example of "structured text"</h3> <pre> <?php $demo = loaddemo(); while (list($key,$value) = each($demo)) echo $value. "\n"; ?> </pre> To try a the example above: copy and paste the fields and values into a new filethen upload it. If you are very impacient just press 'send file' and the demo will be loaded internally. <p> <h3>Error: Maximum execution time exceeded</h3> This server has been set to terminate a script after a certain amount of time to prevent poorly written scripts with end-less loops. <br> If you run your own server with this demo you can edit http.conf and add "php3_max_execution_time 120" to allow a prgram to run up to 120 seconds. Read the PHP Documentation for further details. <p> <?php } if (file_exists($fn = basename($PHP_SELF. "s"))) echo "<i>Source file: <a href=\"".$fn. "\">$fn</a></i>\n"; else echo "<i>Source file $fn is not available</i>\n"; ?> </html>