-
Notifications
You must be signed in to change notification settings - Fork 4
/
kladr.php
70 lines (63 loc) · 1.76 KB
/
kladr.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
<?php
$ftypes = array(
"character" => 'varchar(600)',
"date" => 'date',
"integer" => 'int',
"number" => 'float',
"boolean" => 'bool',
"float" => 'float',
);
function getSql($fname){
global $ftypes;
$table = str_ireplace(".dbf","",$fname);
//if($table!='DOMA') return;
$fp = fopen(__DIR__.'/'.$table.'.my.sql','w+');
echo "opened $fp\n";
if(!$fp) die("$table cant write");
$db = dbase_open($fname, 0);
fwrite($fp, "set names utf8;");
if ($db) {
$record_numbers = dbase_numrecords($db);
fwrite($fp, "/*NUM: $record_numbers*/\n");
// var_dump(dbase_get_header_info($db));
$headers = dbase_get_header_info($db);
$fields = array();
$names = array();
foreach($headers as $header)
{
$t = $ftypes[$header['type']];
$header['name'] = strtolower($header['name']);
$fields[] = "kladr_{$header['name']} {$t}";
$names[] = "kladr_{$header['name']}";
}
fwrite($fp, "create table $table ( ".implode(", ",$fields)." )default charset=utf8 engine=innodb;\n");
fwrite($fp, " /* DATA START */ \n");
fwrite($fp, "insert into $table (".implode(',',$names).") VALUES ");
for ($i = 1; $i <= $record_numbers; $i++) {
if($i>1) fwrite($fp, ",");
$row = dbase_get_record($db, $i);
$fvals = array();
unset($row['deleted']);
$l = count($row);
fwrite($fp, "(");
for($ii =0 ; $ii<$l ; $ii++)
{
if($ii >0 ) fwrite($fp, " , ");
$val = trim(iconv('cp866','utf-8',$row[$ii]));
fwrite($fp, "'$val'");
}
fwrite($fp, ")\n");
}
fwrite($fp, ";\n");
fclose($fp);
}else{
die("can not open $fname\n");
}
}
$a = glob('*.DBF');
echo "SET NAMES utf8;\n";
foreach($a as $f)
{
echo( " /** DUMPING $f **/\n\n");
getSql($f);
}