forked from doofpot/DBFToMySQL
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdbf-import.php
227 lines (202 loc) · 6.52 KB
/
dbf-import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
@include "config.php"; // please copy the config.sample.php and edit the correct fields
@include "classes/XBase/Table.php";
@include "classes/XBase/Column.php";
@include "classes/XBase/Record.php";
@include "classes/XBase/Memo.php";
@include "classes/DBFhandler.php";
use XBase\Table;
// Initializing vars
ini_set( 'memory_limit', '2048M' );
set_time_limit( 0 );
$time_start = time();
$files = scandir($xbase_dir) or die ("Error! Could not open directory '$xbase_dir'.");
$conn = new mysqli($db_host, $db_uname, $db_passwd, $db_name) or die ("Error connecting to mysql $mysqli->connect_error");
mysqli_set_charset($conn, 'utf8');
foreach ($files as $file) {
switch ($file) {
case (preg_match("/dbf$/i", $file) ? true : false):
print_r("DBF: $file\n");
dbftomysql($file);
break;
default:
print_r("Other file: $file\n");
}
}
$time_end = time();
"\n\nImport finished! Time spent: ". round( ( $time_end - $time_start ) / 60, 2 ) ." minutes\n";
function dbftomysql($file) {
// Path to dbase file
global $xbase_dir;
global $conn;
global $die_on_mysql_error;
$db_path = sprintf("%s/%s",$xbase_dir,$file);
// Open dbase file
$table = new Table($db_path);
$tbl = substr($file,0,strlen($file)-4);
print ( "Beware: space of C (Character field) - will be double in MySQL for the special case of a full special-chars-only field\n" );
print_r ("$tbl");
$line = array();
foreach ($table->getColumns() as $column) {
print_r("\t$column->name ($column->type / $column->length)\n");
switch($column->type) {
case 'C': // Character field
$line[]= "`$column->name` VARCHAR(" . $column->length * 2 . ")"; // double space for utf-8 conversion of special chars
break;
case 'F': // Floating Point
$line[]= "`$column->name` FLOAT";
break;
case 'N': // Numeric
$line[]= "`$column->name` VARCHAR(" . $column->length . ")";
break;
case 'L': // Logical - ? Y y N n T t F f (? when not initialized).
$line[]= "`$column->name` TINYINT";
break;
case 'D': // Date
$line[]= "`$column->name` DATE";
break;
case 'T': // DateTime
$line[]= "`$column->name` DATETIME";
break;
case 'M': // Memo type field
default:
$line[]= "`$column->name` TEXT";
break;
}
}
$str = implode(",",$line);
$sql = "CREATE TABLE IF NOT EXISTS `$tbl` ( $str );";
if ($conn->query("$sql") === TRUE) {
echo "Table $tbl successfully created\n";
} else {
echo "Error SQL: ".$conn->error ." >> $sql \n";
if ($die_on_mysql_error) {
die;
}
}
$table->close();
// Import using dbf + fpt files (for MEMO data...)
$fpt_file = str_replace( '.dbf', '.fpt', $db_path );
$fpt_path = ( file_exists( $fpt_file ) ? $fpt_file : '' );
import_dbf_to_mysql( $tbl, $db_path, $fpt_path );
// import_dbf($db_path, $tbl);
}
// Not used (see above)
function import_dbf($db_path, $tbl) {
global $conn;
global $die_on_mysql_error;
// print_r ("$db_path\n");
$table = new Table($db_path);
print_r ("$table->recordCount\n");
print_r (sizeof($table->columns));
$i = 0;
while ($record=$table->nextRecord()) {
$fields = array();
$line = array();
foreach ($record->getColumns() as $column) {
$fields[]=$column->name;
// print_r("$column->name\n");
switch($column->type) {
case 'C': // Character field
case 'M': // Memo type field
$line[]= sprintf("'%s'", $record->getObject($column) );
break;
case 'F': // Floating Point
$line[]=sprintf("%7.2f", $record->getObject($column) );
break;
case 'N': // Numeric
$line[]=sprintf("'%s'", $record->getObject($column) );
break;
case 'L': // Logical - ? Y y N n T t F f (? when not initialized).
// $line[] = sprintf("%d", ($record->getBoolean($column) ? 1 : 0) );
$line[] = sprintf("%d", $record->getString($column->name) );
break;
case 'T': // DateTime
case 'D': // Date
$line[]= sprintf("'%s'", strftime("%Y-%m-%d %H:%M", $record->getObject($column) ) );
break;
}
}
$val = implode(",",$line);
$col = implode(",",$fields);
if($GLOBALS['from_encoding']!="")$val = iconv($GLOBALS['from_encoding'], 'UTF-8', $val);
$sql = "INSERT INTO `$tbl` ($col) VALUES ($val)\n";
// print_r ("$sql");
if ($conn->query("$sql") === TRUE) {
$i++;
if ( $i % 100 == 0 ) {
echo "$i records inserted in $tbl\n";
}
die;
} else {
echo "Error SQL: ".$conn->error ." >> $sql \n";
if ($die_on_mysql_error) {
die;
}
}
}
$table->close();
echo "Table $tbl imported\n";
}
function import_dbf_to_mysql( $table, $dbf_path, $fpt_path ) {
echo "Initializing import: Table $table\n";
global $conn;
global $die_on_mysql_error;
$i = 0;
$Test = new DBFhandler( $dbf_path, $fpt_path );
while ( ($Record = $Test->GetNextRecord( true )) and ! empty( $Record ) ) {
$a = 0;
$sql1 = "INSERT INTO `$table` (";
$sql2 = ") values (";
$sql = "";
foreach ( $Record[ 'type' ] as $key => $type ) {
$val = $Record[ 'value' ][ $key ];
$key = (strpos($key, 0x00) !== false ) ? substr($key, 0, strpos($key, 0x00)) : $key;
if ( $val == '{BINARY_PICTURE}' ) {
continue;
}
switch( $type ) {
case 'C': // Character field
case 'M': // Memo type field
$val = sprintf("'%s'", $val );
break;
case 'F': // Floating Point
$val = sprintf("%7.2f", $val );
break;
case 'N': // Numeric
$val = sprintf("'%s'", $val );
break;
case 'L': // Logical - ? Y y N n T t F f (? when not initialized).
$val = sprintf("%d", ( ( strtolower($val) === 't' || strtolower($val) === 'y' ) ? 1 : 0) );
break;
case 'T': // DateTime
case 'D': // Date
$val = sprintf("'%s'", strftime("%Y-%m-%d %H:%M", strtotime($val) ) );
break;
}
$val = str_replace( "'", "", $val );
if($GLOBALS['from_encoding']!="")$val = iconv($GLOBALS['from_encoding'], 'UTF-8', $val);
$a = $a + 1;
if ( $a == 1 ) {
$sql1 .="`$key`";
$sql2 .="'" . trim( $val ) . "'";
} else {
$sql1 .=",`$key`";
$sql2 .=",'$val'";
}
}
$sql = "$sql1 $sql2)";
if ($conn->query("$sql") === TRUE) {
$i++;
if ( $i % 100 == 0 ) {
echo ".";
}
} else {
echo "Error SQL: ".$conn->error ." >> $sql \n";
if ($die_on_mysql_error) {
die;
}
}
}
echo "Table $table imported\n";
}