XML_Query2XML
[ class tree: XML_Query2XML ] [ index: XML_Query2XML ] [ all elements ]

Source for file case06_debug.php

Documentation is available at case06_debug.php

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

Documentation generated on Mon, 11 Mar 2019 14:49:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.