-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQL_OpenDocument_Spreadsheet_Writer.class.php
82 lines (61 loc) · 2.49 KB
/
PostgreSQL_OpenDocument_Spreadsheet_Writer.class.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
<?php
/**
* This is a a library that offers support for handling spreadsheets in OpenDocument format
* Copyright (C) 2008-20112 Alexandru Szasz <[email protected]>
* http://code.google.com/p/open-document-spreadsheet-php/
*/
class PostgreSQL_OpenDocument_Spreadsheet_Writer extends OpenDocument_Spreadsheet_Writer {
protected $hndConnection;
public $blnShowProgress = true;
public function __construct($strFile, $strServer, $strName, $strUsername, $strPassword, $strPort) {
parent::__construct($strFile);
$this->hndConnection = pg_connect(sprintf('host=%s dbname=%s user=%s password=%s port=%s',$strServer, $strName, $strUsername, $strPassword, $strPort));
if (!$this->hndConnection)
throw new Exception('Failed to connect to the database');
$this->startDoc();
}
public function Run($strQuery, $strSheetName = 'Sheet 0') {
$this->startSheet($strSheetName);
$result = pg_query($this->hndConnection, $strQuery);
if (!$result) {
throw new Exception('Failed to execute query');
}
$intRowCount = pg_num_rows($result);
$row = pg_fetch_assoc($result);
// write the column names in the first row
$intColumnId = 0;
foreach($row as $strColumnName=>$strValue) {
$this->addCell($intColumnId, $strColumnName, 'string');
$intColumnId++;
}
$this->saveRow();
do {
$intColumnId = 0;
foreach($row as $mixValue) {
if (is_float($mixValue))
$this->addCell($intColumnId, $mixValue, 'float');
else
$this->addCell($intColumnId, $mixValue, 'string');
$intColumnId++;
}
$this->saveRow();
$intCurrentRow++;
$strProgress = '';
for($i=1;$i<11;$i++) {
if (($intCurrentRow * 10)/$intRowCount <= $i)
$strProgress .= '-';
else
$strProgress .= '+';
}
printf("\rProgress: [%s], %s", $strProgress, sprintf('%d%% done %d exported', intval(($intCurrentRow * 100)/$intRowCount), $intCurrentRow));
@ob_flush();
}
while ($row = pg_fetch_assoc($result));
$this->endSheet();
}
public function Save() {
$this->endDoc();
$this->saveOds();
}
}
?>