Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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