Advertisement
Guest User

Oracle DataTables

a guest
Feb 10th, 2015
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.97 KB | None | 0 0
  1. <?php
  2.     /*
  3.      * Script:    DataTables server-side script for PHP and Oracle 10g (via OCI8)
  4.      * Copyright: 2011 - Allan Jardine (base version) & big-deal (Redeveloping for Oracle 10g using OCI8) && 2015 - Bled (Redeveloping for Oracle 11G and DataTables 1.10 new parameters)
  5.      * License:   GPL v2 or BSD (3-point)
  6.      */
  7.  
  8.     //Comment this line to have the php error
  9.     //If this line is not commented, the json response is not correct
  10.     error_reporting(0);
  11.     /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  12.      * Easy set variables
  13.      */
  14.    
  15.     /* Array of database columns which should be read and sent back to DataTables. Use a space where
  16.      * you want to insert a non-database field (for example a counter or static image)
  17.      */
  18.     $aColumns = array( 'num', 'nom', 'blabla', 'toto');
  19.      
  20.     /* Indexed column (used for fast and accurate table cardinality) */
  21.     $sIndexColumn = "id";
  22.      
  23.     /* DB table to use */
  24.     $sTable = "ajax";
  25.    
  26.     /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  27.      * If you just want to use the basic configuration for DataTables with PHP server-side, there is
  28.      * no need to edit below this line
  29.      */
  30.      
  31.     /*
  32.      * Oracle connection
  33.      */
  34.      
  35.     $conn = oci_connect('user','mdp','dbName');
  36.     if (!$conn)
  37.     {
  38.         $e = oci_error();
  39.         //echo $e['code'];
  40.         trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
  41.     }
  42.      
  43.  
  44.     /*
  45.      * Paging
  46.      */
  47.     $sLimit = "";
  48.     if ( isset( $_GET['start'] ) && $_GET['length'] != '-1' )
  49.     {
  50.         //Ff binding error, it's because :iDisplayStart is probably not in the request
  51.         $sLimit = "WHERE  rowsNumerator BETWEEN :iDisplayStart AND :iDisplayEnd";
  52.     }
  53.      
  54.  
  55.     /*
  56.      * Ordering
  57.      */
  58.     //Put the array in another variable because you can't get them with the $_GET method
  59.     $sOrderArr = $_GET['order'];
  60.     $sColumnArr = $_GET['columns'];
  61.     $sOrder = '';
  62.     if (isset($sOrderArr[0]['column']))
  63.     {
  64.         $sOrder = "ORDER BY ";
  65.         //Go over all sorting cols
  66.         for ( $i=0 ; $i<count($sColumnArr); $i++ )
  67.         {
  68.             //Check if the current col can be orderable
  69.             if ( $sColumnArr[$i]['orderable'] == "true" )
  70.             {
  71.                 //Add the name of the column in the ORDER BY
  72.                 $sOrder .= $aColumns[$sOrderArr[$i]['column']];
  73.                  
  74.                 //Determine if it is sorted asc or desc
  75.                 if ((strcasecmp($sOrderArr[$i]['dir'], "asc") == 0))
  76.                 {
  77.                     $sOrder .=" asc, ";
  78.                 }
  79.                 else if ((strcasecmp($sOrderArr[$i]['dir'], "desc") == 0))
  80.                 {
  81.                     $sOrder .=" desc, ";
  82.                 }
  83.             }
  84.         }
  85.          
  86.         //Remove the last space / comma
  87.         $sOrder = substr_replace( $sOrder, "", -2 );
  88.          
  89.         //Check if there is an order by clause
  90.         if ( $sOrder == "ORDER BY" )
  91.         {
  92.             /*
  93.             * If there is no order by clause - ORDER BY INDEX COLUMN!!! DON'T DELETE IT!
  94.             * If there is no order by clause there might be bugs in table display.
  95.             * No order by clause means that the db is not responsible for the data ordering,
  96.             * which means that the same row can be displayed in two pages - while
  97.             * another row will not be displayed at all.
  98.             */
  99.             $sOrder = "ORDER BY ".$sIndexColumn;
  100.              
  101.         }
  102.     }
  103.  
  104.      
  105.      
  106.     /*
  107.      * Filtering
  108.      * NOTE this does not match the built-in DataTables filtering which does it
  109.      * word by word on any field. It's possible to do here, but concerned about efficiency
  110.      * on very large tables.
  111.      */
  112.     $sWhere = "";
  113.     $nWhereGenearalCount = 0;
  114.     $sSearchArr = $_GET['search'];
  115.  
  116.     if (isset($sSearchArr['value']))
  117.     {
  118.         $sWhereGenearal = $_GET['search'];
  119.     }
  120.     else
  121.     {
  122.         $sWhereGenearal = '';
  123.     }
  124.     if ($sSearchArr['value'] != null)
  125.     {
  126.         //Set a default where clause in order for the where clause not to fail
  127.         //in cases where there are no searchable cols at all.
  128.         $sWhere = "WHERE (";
  129.         for ( $i=0 ; $i<count($aColumns)+1 ; $i++ )
  130.         {
  131.             //If current col has a search param
  132.             if ( $sColumnArr[$i]['searchable'] == "true" )
  133.             {
  134.                 //Add the search to the where clause
  135.                 // $sWhere .= $aColumns[$i]." LIKE '%".$sSearchArr['value']."%' OR ";
  136.                 //Add the search to the where clause without the case sensitive
  137.                 $sWhere .= "regexp_like(".$aColumns[$i].", '".$sSearchArr['value']."', 'i') OR ";
  138.                 $nWhereGenearalCount += 1;
  139.             }
  140.         }
  141.  
  142.         $sWhere = substr_replace( $sWhere, "", -3 );
  143.         $sWhere .= ' )';
  144.     }
  145.    
  146.     /* Individual column filtering */
  147.     $sWhereSpecificArray = array();
  148.     $sWhereSpecificArrayCount = 0;
  149.     for ( $i=0 ; $i<count($aColumns) ; $i++ )
  150.     {
  151.         if ( $sColumnArr[$i]['search']['regex'] == "true" && $sColumnArr[$i]['search']['value'] != '' )
  152.         {
  153.             //If there was no where clause
  154.             if ( $sWhere == "" )
  155.             {
  156.                 $sWhere = "WHERE";
  157.             }
  158.             else
  159.             {
  160.                 $sWhere .= " AND ";
  161.             }
  162.              
  163.             //Add the clause of the specific col to the where clause
  164.             $sWhere .= $aColumns[$i]." LIKE '%' || :whereSpecificParam".$sWhereSpecificArrayCount." || '%' ";
  165.              
  166.             //Inc sWhereSpecificArrayCount. It is needed for the bind var.
  167.             //We could just do count($sWhereSpecificArray) - but that would be less efficient.
  168.             $sWhereSpecificArrayCount++;
  169.              
  170.             //Add current search param to the array for later use (binding).
  171.             $sWhereSpecificArray[] =  $sColumnArr[$i]['search']['value'];  
  172.         }
  173.     }
  174.    
  175.     //If there is still no where clause - set a general - always true where clause
  176.     if ( $sWhere == "" )
  177.     {
  178.         $sWhere = "WHERE 1=1  ";
  179.     }
  180.      
  181.      
  182.     /*
  183.      * SQL queries
  184.      * Get data to display
  185.      */
  186.      //Inner sql - not being fetched by itself.
  187.  
  188.     $sQueryInner = "SELECT ".implode(', ', $aColumns).", row_number() over (".$sOrder.") as rowsNumerator FROM   ".$sTable." ".$sWhere;
  189.     $sQueryFinal = "SELECT ".implode(', ', $aColumns)." FROM (".$sQueryInner.") qry ".$sLimit." ORDER BY rowsNumerator";
  190.      
  191.     /* Data set length after filtering */
  192.     $sQueryFinalCount = "SELECT COUNT(*) as \"totalRowsCount\" FROM (".$sQueryInner.") qry";
  193.      
  194.     $iFilteredTotal = 0;
  195.      
  196.     /* Total data set length */
  197.     $sQueryTotalCount = "SELECT COUNT(".$sIndexColumn.") as \"totalRowsCount\" FROM  ".$sTable;
  198.  
  199.     //Create Statments
  200.     $statmntFinal = oci_parse($conn, $sQueryFinal);
  201.     $statmntFinalCount = oci_parse($conn, $sQueryFinalCount);
  202.     $statmntTotalCount = oci_parse($conn, $sQueryTotalCount);
  203.  
  204.     //Bind variables.
  205.      
  206.     if ( isset( $_GET['start'] ))
  207.     {
  208.         $dsplyStart = $_GET['start'];
  209.     }
  210.     else{
  211.         $dsplyStart = 0;
  212.     }
  213.      
  214.     if ( isset( $_GET['length'] ) && $_GET['length'] != '-1' )
  215.     {
  216.         $dsplyRange = $_GET['length'];
  217.         if ($dsplyRange > (2147483645 - intval($dsplyStart)))
  218.         {
  219.             $dsplyRange = 2147483645;
  220.         }
  221.         else
  222.         {
  223.             $dsplyRange = intval($dsplyStart) +  intval($dsplyRange);
  224.         }
  225.     }
  226.     else
  227.     {
  228.         $dsplyRange = 2147483645;
  229.     }
  230.      
  231.    
  232.    
  233.     //Bind variables of number of rows to fetch.
  234.     oci_bind_by_name($statmntFinal, ':iDisplayStart', $dsplyStart);
  235.     oci_bind_by_name($statmntFinal, ':iDisplayEnd', $dsplyRange);
  236.  
  237.    
  238.     // Bind all variables of general search
  239.     for ( $i = 0 ; $i < $nWhereGenearalCount ; $i++ )
  240.     {
  241.         oci_bind_by_name($statmntFinal, ':whereParam'.$i , $sWhereGenearal);
  242.         oci_bind_by_name($statmntFinalCount, ':whereParam'.$i , $sWhereGenearal);
  243.     }
  244.      
  245.     //Bind all variables of specific search
  246.     for ( $i = 0 ; $i < count($sWhereSpecificArray) ; $i++ )
  247.     {
  248.         oci_bind_by_name($statmntFinal, ':whereSpecificParam '.$i , $sWhereSpecificArray[$i]);
  249.         oci_bind_by_name($statmntFinalCount, ':whereSpecificParam '.$i , $sWhereSpecificArray[$i]);
  250.     }
  251.      
  252.      
  253.     //Execute selects
  254.     oci_execute($statmntTotalCount);
  255.     $iTotal = 0;
  256.     while ($row = oci_fetch_array($statmntTotalCount, OCI_ASSOC))
  257.     {
  258.         $iTotal = $row['totalRowsCount'];
  259.     }
  260.     oci_free_statement($statmntTotalCount);
  261.              
  262.     oci_execute($statmntFinalCount);
  263.     $iFilteredTotal = 0;
  264.     while ($row = oci_fetch_array($statmntFinalCount, OCI_ASSOC))
  265.     {
  266.         $iFilteredTotal = $row['totalRowsCount'];
  267.     }
  268.     oci_free_statement($statmntFinalCount);
  269.      
  270.      
  271.      
  272.     /*
  273.      * Output
  274.      */
  275.     $output = array(
  276.         "sEcho" => intval($_GET['sEcho']),
  277.         "iTotalRecords" => $iTotal,
  278.         "iTotalDisplayRecords" => $iFilteredTotal,
  279.         "aaData" => array()
  280.     );
  281.      
  282.      
  283.     oci_execute($statmntFinal);
  284.      
  285.      
  286.     while ( $aRow = oci_fetch_array($statmntFinal, OCI_ASSOC) )
  287.     {
  288.         $row = array();
  289.         for ( $i=0 ; $i<count($aColumns) ; $i++ )
  290.         {
  291.             if ( $aColumns[$i] == "version" )
  292.             {
  293.                 /* Special output formatting for 'version' column */
  294.                 $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
  295.             }
  296.             else if ( $aColumns[$i] != ' ' )
  297.             {
  298.                 /* General output */
  299.                 $row[] = $aRow[$aColumns[$i]];
  300.             }
  301.         }
  302.         $output['aaData'][] = $row;
  303.     }
  304.  
  305.  
  306.  
  307.     oci_free_statement($statmntFinal);
  308.      
  309.     oci_close($conn);
  310.  
  311.     echo json_encode( $output );
  312. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement