-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbf2sql.php
More file actions
64 lines (47 loc) · 1.37 KB
/
dbf2sql.php
File metadata and controls
64 lines (47 loc) · 1.37 KB
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
<?php
// Check that data *can* be added to database (rollback all changes)
define('DRY_RUN', false);
// Insert all or nothing
define('TRANSACTIONAL', false);
// Create schema only
define('DISABLE_INSERTS', false);
// Amount of rows to insert per transaction
define('CHUNK_SIZE', 1000);
require __DIR__ . '/vendor/autoload.php';
use XBase\Table;
use Orbital\Util\DBase2PostgreSQL;
if($argc < 2){
echo "Usage " . $argv[0] . " <file.dbf> [encoding] \n";
die;
}
$file = $argv[1];
$encoding = isset($argv[2]) ? $argv[2] : null;
$table = new Table($file, null, $encoding);
if(TRANSACTIONAL || DRY_RUN)
echo "begin work; \n";
echo DBase2PostgreSQL::getCreateTableSentence($table) . "\n";
if(!DISABLE_INSERTS) {
$issueInsert = true;
$insertRowCount = 0;
while ($record = $table->nextRecord()) {
if ($issueInsert) {
echo DBase2PostgreSQL::getInsertSentenceBegin($table) . "\n";
$issueInsert = false;
} else {
echo "\t, ";
}
echo DBase2PostgreSQL::getInsertSentenceValues($record) . "\n";
$insertRowCount++;
if(CHUNK_SIZE>0 && $insertRowCount >= CHUNK_SIZE){
$issueInsert = true;
$insertRowCount = 0;
echo ";";
}
}
if(!$issueInsert)
echo ";";
}
if(TRANSACTIONAL)
echo "commit\n";
elseif(DRY_RUN)
echo "rollback; \n";