-
Notifications
You must be signed in to change notification settings - Fork 1
/
DBF.php
218 lines (183 loc) · 4.76 KB
/
DBF.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
<?php
/**
*
* Class to copy DBF tables to SQL database (tested on MySQL)
* @author Jaroslav
*
*/
class Helper_DBF{
/**
* File from which to read data
* @var string
*/
public $databaseFile = '';
//
/**
* Table name where we will save retrieved data.
* Will be automatically generated if left empty.
* @var string
*/
public $table = '';
/**
* Table name prefix
* @var string
*/
public $tablePrefix = 'dbf_';
/**
* Function to call on every row.
* Please read more about it here: http://www.php.net/manual/en/function.call-user-func.php
* @var mixed
*/
public $callbackInsert = '';
/**
* Function quote data to be inserted in database.
* Please read more about it here: http://www.php.net/manual/en/function.call-user-func.php
* @var mixed
*/
public $callbackQuote = 'mysql_real_escape_string';
/**
* Encoding of data it DBF table
* @var string
*/
public $encodingIn = 'Windows-1257';
/**
* Encoding of output data
* @var string
*/
public $encodingOut = 'UTF-8//TRANSLIT';
private $db = false;
/**
* Class constructor to set all variables and open database file.
*
* @param string $databaseFile
* @param string $table
* @param string $tablePrefix
*/
public function __construct($databaseFile, $table = ''){
$this->databaseFile = $databaseFile;
$this->table = $table;
if (empty($this->table)){
$this->table = $this->defaultTableName();
}
$this->openDB();
}
/**
* Class destructor to close the database file
*/
public function __destruct(){
$this->closeDB();
}
/**
* Returns the name of database file without extension, to be used as default name for table
*/
public function defaultTableName(){
$file = explode('/', $this->databaseFile);
$fileName = explode('.', $file[count($file)-1]);
return $fileName[0];
}
/**
* Prepares queries to create new table for import
*/
public function createTable(){
$db = &$this->db;
$record_numbers = dbase_numrecords($db);
if ($record_numbers < 1){
echo 'No entries found. Skipping'; // You can delete this line safely if you don't need it
return false;
}
$column_info = dbase_get_header_info($db);
$i = 0 ;
$createTable = 'CREATE TABLE `'.$this->tablePrefix.$this->table.'` (';
foreach($column_info as $col){
if (++$i > 1) $createTable .= ', ';
$createTable .= '`'.$col['name'].'` ';
switch($col['type']){
case 'character':
$createTable .= ' VARCHAR('.$col['length'].') ';
break;
case 'boolean':
$createTable .= ' INT(1) ';
break;
case 'number':
$createTable .= ' DECIMAL('.$col['length'].', '.$col['precision'].') ';
break;
default:
$createTable .= ' TEXT ';
}
}
$createTable .= ');';
return $createTable;
}
/**
* Delete table to create new one
*/
public function deleteTable(){
$deleteTable = 'DROP TABLE IF EXISTS `'.$this->tablePrefix.$this->table.'` ';
return $deleteTable;
}
/**
* Read data from DBF file and give it back to callbackInsert function (where you can write it to your database)
*/
public function readData(){
$db = &$this->db;
$record_numbers = dbase_numrecords($db);
for ($i = 1; $i <= $record_numbers; $i++) {
$row = dbase_get_record_with_names($db, $i);
if($row['deleted']) continue; // Skip deleted rows
unset($row['deleted']); // Don't save this field to our new table
foreach ($row as $k => $v){ // Change encoding of table to something more useful
$row[$k] = iconv($this->encodingIn, $this->encodingOut, $row[$k]);
}
$query = $this->insertStatement($this->tablePrefix.$this->table, $row);
call_user_func($this->callbackInsert, $query);
}
}
/**
* Returns insert statement for a given row
*
* @param string $table
* @param array $what
*/
protected function insertStatement($table, array $what){
$vals = $this->prepareInsert($what);
$sql = "INSERT INTO ".$table." (".$vals['a'].") VALUES (".$vals['b'].") ";
return $sql;
}
/**
* Creates string for INSERT statement
*
* @param array $what
*/
protected function prepareInsert(array $what){
$a = '';
$b = '';
$i = 0;
foreach($what as $k => $v){
$a .= (($i>0)?', ':'').'`'.$k."`";
$b .= (($i>0)?', ':'');
if ($v === null){
$b .= 'NULL';
$update .= 'NULL';
}else{
$b .= "'".call_user_func($this->callbackQuote, $v)."'";
}
$i++;
}
return array('a' => $a, 'b' => $b);
}
/**
* Open DBase file
*/
public function openDB(){
$this->db = dbase_open($this->databaseFile, 0); // Open database with read-only permissions
if($this->db === false){
throw new Exception("Can't open database file: ".$this->databaseFile, 1);
}
}
/**
* Close DBase file
*/
public function closeDB(){
$this->db = dbase_close($this->db);
}
}