Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
require_once 'XML/Query2XML.php';
3
require_once 'XML/Query2XML/Callback.php';
4
require_once 'MDB2.php';
5
 
6
/**Static class that provides validation and parsing methods for
7
* generating XML.
8
*
9
* It is static so that we can easyly call its methods from inside
10
* Query2XML using eval'd code.
11
*/
12
class Helper
13
{
14
    /**Associative array of US postal state codes*/
15
    public static $statePostalCodes = array(
16
        'ALABAMA' => 'AL', 'ALASKA' => 'AK', 'AMERICAN SAMOA' => 'AS', 'ARIZONA' => 'AZ', 'ARKANSAS' => 'AR', 'CALIFORNIA' => 'CA',
17
        'COLORADO' => 'CO', 'CONNECTICUT' => 'CT', 'DELAWARE' => 'DE', 'DISTRICT OF COLUMBIA' => 'DC', 'FEDERATED STATES OF MICRONESIA' => 'FM',
18
        'FLORIDA' => 'FL', 'GEORGIA' => 'GA', 'GUAM' => 'GU', 'HAWAII' => 'HI', 'IDAHO' => 'ID', 'ILLINOIS' => 'IL', 'INDIANA' => 'IN',
19
        'IOWA' => 'IA', 'KANSAS' => 'KS', 'KENTUCKY' => 'KY', 'LOUISIANA' => 'LA', 'MAINE' => 'ME', 'MARSHALL ISLANDS' => 'MH', 'MARYLAND' => 'MD',
20
        'MASSACHUSETTS' => 'MA', 'MICHIGAN' => 'MI', 'MINNESOTA' => 'MN', 'MISSISSIPPI' => 'MS', 'MISSOURI' => 'MO', 'MONTANA' => 'MT',
21
        'NEBRASKA' => 'NE', 'NEVADA' => 'NV', 'NEW HAMPSHIRE' => 'NH', 'NEW JERSEY' => 'NJ', 'NEW JESEY' => 'NJ', 'NEW MEXICO' => 'NM', 'NEW YORK' => 'NY',
22
        'NORTH CAROLINA' => 'NC', 'NORTH DAKOTA' => 'ND', 'NORTHERN MARIANA ISLANDS' => 'MP', 'OHIO' => 'OH', 'OKLAHOMA' => 'OK', 'OREGON' => 'OR',
23
        'PALAU' => 'PW', 'PENNSYLVANIA' => 'PA', 'PUERTO RICO' => 'PR', 'RHODE ISLAND' => 'RI', 'SOUTH CAROLINA' => 'SC', 'SOUTH DAKOTA' => 'SD',
24
        'TENNESSEE' => 'TN', 'TEXAS' => 'TX', 'UTAH' => 'UT', 'VERMONT' => 'VT', 'VIRGIN ISLANDS' => 'VI', 'VIRGINIA' => 'VA', 'WASHINGTON' => 'WA',
25
        'WEST VIRGINIA' => 'WV', 'WISCONSIN' => 'WI', 'WYOMING' => 'WY'
26
    );
27
 
28
    /**Translates a US state name into its two-letter postal code.
29
    * If the translation fails, $state is returned unchanged
30
    * @param $record The record
31
    */
32
    public static function getStatePostalCode($record)
33
    {
34
        $state = $record["state"];
35
        $s = str_replace("  ", " ", trim(strtoupper($state)));
36
        if (isset(self::$statePostalCodes[$s])) {
37
            return self::$statePostalCodes[$s];
38
        } else {
39
            return $state;
40
        }
41
    }
42
 
43
    function summarize($str, $limit=50, $appendString=' ...')
44
    {
45
        if (strlen($str) > $limit) {
46
            $str = substr($str, 0, $limit - strlen($appendString)) . $appendString;
47
        }
48
        return $str;
49
    }
50
 
51
    function summarizeComment($record, $limit)
52
    {
53
        return self::summarize($record["comment"], $limit);
54
    }
55
}
56
 
57
/**Command class that implements the command pattern.
58
* It implements the XML_Query2XML_Callback interface
59
* and therefore has to provide the public non-static
60
* method execute(array $record).
61
*/
62
class UppercaseColumnCommand implements XML_Query2XML_Callback
63
{
64
    public function __construct($columnName)
65
    {
66
        $this->_columnName = $columnName;
67
    }
68
    public function execute(array $record)
69
    {
70
        return strtoupper($record[$this->_columnName]);
71
    }
72
}
73
 
74
$query2xml = XML_Query2XML::factory(MDB2::factory('mysql://root@localhost/Query2XML_Tests'));
75
 
76
require_once 'Log.php';
77
$debugLogger = Log::factory('file', 'case06.log', 'XML_Query2XML');
78
$query2xml->enableDebugLog($debugLogger);
79
 
80
$query2xml->startProfiling();
81
 
82
 
83
$dom = $query2xml->getXML(
84
    "SELECT
85
         s.*,
86
         manager.employeeid AS manager_employeeid,
87
         manager.employeename AS manager_employeename,
88
         d.*,
89
         department_head.employeeid AS department_head_employeeid,
90
         department_head.employeename AS department_head_employeename,
91
         e.*,
92
         sa.*,
93
         c.*,
94
         al.*,
95
         ar.*,
96
         (SELECT COUNT(*) FROM sale WHERE sale.store_id = s.storeid) AS store_sales,
97
         (SELECT
98
            COUNT(*)
99
          FROM
100
            sale, employee, employee_department
101
          WHERE
102
            sale.employee_id = employee.employeeid
103
            AND
104
            employee_department.employee_id = employee.employeeid
105
            AND
106
            employee_department.department_id = d.departmentid
107
         ) AS department_sales,
108
         (SELECT
109
            COUNT(*)
110
          FROM
111
            employee, employee_department, department
112
          WHERE
113
            employee_department.employee_id = employee.employeeid
114
            AND
115
            employee_department.department_id = department.departmentid
116
            AND
117
            department.store_id = s.storeid
118
         ) AS store_employees,
119
         (SELECT
120
            COUNT(*)
121
          FROM
122
            employee, employee_department
123
          WHERE
124
            employee_department.employee_id = employee.employeeid
125
            AND
126
            employee_department.department_id = d.departmentid
127
         ) AS department_employees
128
     FROM
129
         store s
130
          LEFT JOIN employee manager ON s.manager = manager.employeeid
131
         LEFT JOIN department d ON d.store_id = s.storeid
132
          LEFT JOIN employee department_head ON department_head.employeeid = d.department_head
133
          LEFT JOIN employee_department ed ON ed.department_id = d.departmentid
134
           LEFT JOIN employee e ON e.employeeid = ed.employee_id
135
            LEFT JOIN sale sa ON sa.employee_id = e.employeeid
136
             LEFT JOIN customer c ON c.customerid = sa.customer_id
137
             LEFT JOIN album al ON al.albumid = sa.album_id
138
              LEFT JOIN artist ar ON ar.artistid = al.artist_id
139
     ORDER BY
140
        s.storeid,
141
        manager.employeeid,
142
        d.departmentid,
143
        department_head.employeeid,
144
        ed.employee_id,
145
        ed.department_id,
146
        e.employeeid,
147
        sa.saleid,
148
        c.customerid,
149
        al.albumid,
150
        ar.artistid",
151
    array(
152
        'rootTag' => 'music_company',
153
        'rowTag' => 'store',
154
        'idColumn' => 'storeid',
155
        'attributes' => array(
156
            'storeid'
157
        ),
158
        'elements' => array(
159
            'store_sales',
160
            'store_employees',
161
            'manager' => array(
162
                'idColumn' => 'manager_employeeid',
163
                'attributes' => array(
164
                    'manager_employeeid'
165
                ),
166
                'elements' => array(
167
                    'manager_employeename'
168
                )
169
            ),
170
            'address' => array(
171
                'elements' => array(
172
                    'country',
173
                    'state' => '#Helper::getStatePostalCode()',
174
                    'city' => new UppercaseColumnCommand('city'),
175
                    'street',
176
                    'phone'
177
                )
178
            ),
179
            'department' => array(
180
                'idColumn' => 'departmentid',
181
                'attributes' => array(
182
                    'departmentid'
183
                ),
184
                'elements' => array(
185
                    'department_sales',
186
                    'department_employees',
187
                    'departmentname',
188
                    'department_head' => array(
189
                        'idColumn' => 'department_head_employeeid',
190
                        'attributes' => array(
191
                            'department_head_employeeid'
192
                        ),
193
                        'elements' => array(
194
                            'department_head_employeename'
195
                        )
196
                    ),
197
                    'employees' => array(
198
                        'rootTag' => 'employees',
199
                        'rowTag' => 'employee',
200
                        'idColumn' => 'employeeid',
201
                        'attributes' => array(
202
                            'employeeid'
203
                        ),
204
                        'elements' => array(
205
                            'employeename',
206
                            'sales' => array(
207
                                'rootTag' => 'sales',
208
                                'rowTag' => 'sale',
209
                                'idColumn' => 'saleid',
210
                                'attributes' => array(
211
                                    'saleid'
212
                                ),
213
                                'elements' => array(
214
                                    'timestamp',
215
                                    'customer' => array(
216
                                        'idColumn' => 'customerid',
217
                                        'attributes' => array(
218
                                            'customerid'
219
                                        ),
220
                                        'elements' => array(
221
                                            'first_name',
222
                                            'last_name',
223
                                            'email'
224
                                        )
225
                                    ),
226
                                    'album' => array(
227
                                        'idColumn' => 'albumid',
228
                                        'attributes' => array(
229
                                            'albumid'
230
                                        ),
231
                                        'elements' => array(
232
                                            'title',
233
                                            'published_year',
234
                                            'comment' => '?#Helper::summarizeComment(12)',
235
                                            'artist' => array(
236
                                                'idColumn' => 'artistid',
237
                                                'attributes' => array(
238
                                                    'artistid'
239
                                                ),
240
                                                'elements' => array(
241
                                                    'name',
242
                                                    'birth_year',
243
                                                    'birth_place',
244
                                                    'genre'
245
                                                )
246
                                            )
247
                                        ) // album elements
248
                                    ) //album array
249
                                ) //sales elements
250
                            ) //sales array
251
                        ) //employees elements
252
                    ) //employees array
253
                ) //department elements
254
            ) // department array
255
        ) //root elements
256
    ) //root
257
); //getXML method call
258
 
259
$root = $dom->firstChild;
260
$root->setAttribute('date_generated', '2005-08-23T14:52:50');
261
 
262
header('Content-Type: application/xml');
263
 
264
$dom->formatOutput = true;
265
print $dom->saveXML();
266
 
267
require_once 'File.php';
268
$fp = new File();
269
$fp->write('case06.profile', $query2xml->getProfile(), FILE_MODE_WRITE);
270
?>