home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phpnuke / PHP-Nuke-7.5.exe / html / db / db2.php < prev    next >
PHP Script  |  2004-01-29  |  8KB  |  421 lines

  1. <?php
  2. /***************************************************************************
  3.  *                                 db2.php
  4.  *                            -------------------
  5.  *   begin                : Saturday, Feb 13, 2001
  6.  *   copyright            : (C) 2001 The phpBB Group
  7.  *   email                : support@phpbb.com
  8.  *
  9.  *   $Id: db2.php,v 1.2 2002/01/28 17:24:45 psotfx Exp $
  10.  *
  11.  ***************************************************************************/
  12.  
  13. /***************************************************************************
  14.  *
  15.  *   This program is free software; you can redistribute it and/or modify
  16.  *   it under the terms of the GNU General Public License as published by
  17.  *   the Free Software Foundation; either version 2 of the License, or
  18.  *   (at your option) any later version.
  19.  *
  20.  ***************************************************************************/
  21.  
  22. if(!defined("SQL_LAYER"))
  23. {
  24.  
  25. define("SQL_LAYER","db2");
  26.  
  27. class sql_db
  28. {
  29.  
  30.     var $db_connect_id;
  31.     var $query_result;
  32.     var $query_resultset;
  33.     var $query_numrows;
  34.     var $next_id;
  35.     var $row = array();
  36.     var $rowset = array();
  37.     var $row_index;
  38.     var $num_queries = 0;
  39.  
  40.     //
  41.     // Constructor
  42.     //
  43.     function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
  44.     {
  45.         $this->persistency = $persistency;
  46.         $this->user = $sqluser;
  47.         $this->password = $sqlpassword;
  48.         $this->dbname = $database;
  49.  
  50.         $this->server = $sqlserver;
  51.  
  52.         if($this->persistency)
  53.         {
  54.             $this->db_connect_id = odbc_pconnect($this->server, "", "");
  55.         }
  56.         else
  57.         {
  58.             $this->db_connect_id = odbc_connect($this->server, "", "");
  59.         }
  60.  
  61.         if($this->db_connect_id)
  62.         {
  63.             @odbc_autocommit($this->db_connect_id, off);
  64.  
  65.             return $this->db_connect_id;
  66.         }
  67.         else
  68.         {
  69.             return false;
  70.         }
  71.     }
  72.     //
  73.     // Other base methods
  74.     //
  75.     function sql_close()
  76.     {
  77.         if($this->db_connect_id)
  78.         {
  79.             if($this->query_result)
  80.             {
  81.                 @odbc_free_result($this->query_result);
  82.             }
  83.             $result = @odbc_close($this->db_connect_id);
  84.             return $result;
  85.         }
  86.         else
  87.         {
  88.             return false;
  89.         }
  90.     }
  91.  
  92.  
  93.     //
  94.     // Query method
  95.     //
  96.     function sql_query($query = "", $transaction = FALSE)
  97.     {
  98.         //
  99.         // Remove any pre-existing queries
  100.         //
  101.         unset($this->query_result);
  102.         unset($this->row);
  103.         if($query != "")
  104.         {
  105.             $this->num_queries++;
  106.  
  107.             if(!eregi("^INSERT ",$query))
  108.             {
  109.                 if(eregi("LIMIT", $query))
  110.                 {
  111.                     preg_match("/^(.*)LIMIT ([0-9]+)[, ]*([0-9]+)*/s", $query, $limits);
  112.  
  113.                     $query = $limits[1];
  114.                     if($limits[3])
  115.                     {
  116.                         $row_offset = $limits[2];
  117.                         $num_rows = $limits[3];
  118.                     }
  119.                     else
  120.                     {
  121.                         $row_offset = 0;
  122.                         $num_rows = $limits[2];
  123.                     }
  124.  
  125.                     $query .= " FETCH FIRST ".($row_offset+$num_rows)." ROWS ONLY OPTIMIZE FOR ".($row_offset+$num_rows)." ROWS";
  126.  
  127.                     $this->query_result = odbc_exec($this->db_connect_id, $query);
  128.  
  129.                     $query_limit_offset = $row_offset;
  130.                     $this->result_numrows[$this->query_result] = $num_rows;
  131.                 }
  132.                 else
  133.                 {
  134.                     $this->query_result = odbc_exec($this->db_connect_id, $query);
  135.  
  136.                     $row_offset = 0;
  137.                     $this->result_numrows[$this->query_result] = 5E6;
  138.                 }
  139.  
  140.                 $result_id = $this->query_result;
  141.                 if($this->query_result && eregi("^SELECT", $query))
  142.                 {
  143.  
  144.                     for($i = 1; $i < odbc_num_fields($result_id)+1; $i++)
  145.                     {
  146.                         $this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);
  147.                     }
  148.  
  149.                     $i =  $row_offset + 1;
  150.                     $k = 0;
  151.                     while(odbc_fetch_row($result_id, $i) && $k < $this->result_numrows[$result_id])
  152.                     {
  153.  
  154.                         for($j = 1; $j < count($this->result_field_names[$result_id])+1; $j++)
  155.                         {
  156.                             $this->result_rowset[$result_id][$k][$this->result_field_names[$result_id][$j-1]] = odbc_result($result_id, $j);
  157.                         }
  158.                         $i++;
  159.                         $k++;
  160.                     }
  161.  
  162.                     $this->result_numrows[$result_id] = $k;
  163.                     $this->row_index[$result_id] = 0;
  164.                 }
  165.                 else
  166.                 {
  167.                     $this->result_numrows[$result_id] = @odbc_num_rows($result_id);
  168.                     $this->row_index[$result_id] = 0;
  169.                 }
  170.             }
  171.             else
  172.             {
  173.                 if(eregi("^(INSERT|UPDATE) ", $query))
  174.                 {
  175.                     $query = preg_replace("/\\\'/s", "''", $query);
  176.                 }
  177.  
  178.                 $this->query_result = odbc_exec($this->db_connect_id, $query);
  179.  
  180.                 if($this->query_result)
  181.                 {
  182.                     $sql_id = "VALUES(IDENTITY_VAL_LOCAL())";
  183.  
  184.                     $id_result = odbc_exec($this->db_connect_id, $sql_id);
  185.                     if($id_result)
  186.                     {
  187.                         $row_result = odbc_fetch_row($id_result);
  188.                         if($row_result)
  189.                         {
  190.                             $this->next_id[$this->query_result] = odbc_result($id_result, 1);
  191.                         }
  192.                     }
  193.                 }
  194.  
  195.                 odbc_commit($this->db_connect_id);
  196.  
  197.                 $this->query_limit_offset[$this->query_result] = 0;
  198.                 $this->result_numrows[$this->query_result] = 0;
  199.             }
  200.  
  201.             return $this->query_result;
  202.         }
  203.         else
  204.         {
  205.             return false;
  206.         }
  207.     }
  208.  
  209.     //
  210.     // Other query methods
  211.     //
  212.     function sql_numrows($query_id = 0)
  213.     {
  214.         if(!$query_id)
  215.         {
  216.             $query_id = $this->query_result;
  217.         }
  218.         if($query_id)
  219.         {
  220.             return $this->result_numrows[$query_id];
  221.         }
  222.         else
  223.         {
  224.             return false;
  225.         }
  226.     }
  227.     function sql_affectedrows($query_id = 0)
  228.     {
  229.         if(!$query_id)
  230.         {
  231.             $query_id = $this->query_result;
  232.         }
  233.         if($query_id)
  234.         {
  235.             return $this->result_numrows[$query_id];
  236.         }
  237.         else
  238.         {
  239.             return false;
  240.         }
  241.     }
  242.     function sql_numfields($query_id = 0)
  243.     {
  244.         if(!$query_id)
  245.         {
  246.             $query_id = $this->query_result;
  247.         }
  248.         if($query_id)
  249.         {
  250.             $result = count($this->result_field_names[$query_id]);
  251.             return $result;
  252.         }
  253.         else
  254.         {
  255.             return false;
  256.         }
  257.     }
  258.     function sql_fieldname($offset, $query_id = 0)
  259.     {
  260.         if(!$query_id)
  261.         {
  262.             $query_id = $this->query_result;
  263.         }
  264.         if($query_id)
  265.         {
  266.             $result = $this->result_field_names[$query_id][$offset];
  267.             return $result;
  268.         }
  269.         else
  270.         {
  271.             return false;
  272.         }
  273.     }
  274.     function sql_fieldtype($offset, $query_id = 0)
  275.     {
  276.         if(!$query_id)
  277.         {
  278.             $query_id = $this->query_result;
  279.         }
  280.         if($query_id)
  281.         {
  282.             $result = @odbc_field_type($query_id, $offset);
  283.             return $result;
  284.         }
  285.         else
  286.         {
  287.             return false;
  288.         }
  289.     }
  290.     function sql_fetchrow($query_id = 0)
  291.     {
  292.         if(!$query_id)
  293.         {
  294.             $query_id = $this->query_result;
  295.         }
  296.         if($query_id)
  297.         {
  298.             if($this->row_index[$query_id] < $this->result_numrows[$query_id])
  299.             {
  300.                 $result = $this->result_rowset[$query_id][$this->row_index[$query_id]];
  301.                 $this->row_index[$query_id]++;
  302.                 return $result;
  303.             }
  304.             else
  305.             {
  306.                 return false;
  307.             }
  308.         }
  309.         else
  310.         {
  311.             return false;
  312.         }
  313.     }
  314.     function sql_fetchrowset($query_id = 0)
  315.     {
  316.         if(!$query_id)
  317.         {
  318.             $query_id = $this->query_result;
  319.         }
  320.         if($query_id)
  321.         {
  322.             $this->row_index[$query_id] = $this->result_numrows[$query_id];
  323.             return $this->result_rowset[$query_id];
  324.         }
  325.         else
  326.         {
  327.             return false;
  328.         }
  329.     }
  330.     function sql_fetchfield($field, $row = -1, $query_id = 0)
  331.     {
  332.         if(!$query_id)
  333.         {
  334.             $query_id = $this->query_result;
  335.         }
  336.         if($query_id)
  337.         {
  338.             if($row < $this->result_numrows[$query_id])
  339.             {
  340.                 if($row == -1)
  341.                 {
  342.                     $getrow = $this->row_index[$query_id]-1;
  343.                 }
  344.                 else
  345.                 {
  346.                     $getrow = $row;
  347.                 }
  348.  
  349.                 return $this->result_rowset[$query_id][$getrow][$this->result_field_names[$query_id][$field]];
  350.  
  351.             }
  352.             else
  353.             {
  354.                 return false;
  355.             }
  356.         }
  357.         else
  358.         {
  359.             return false;
  360.         }
  361.     }
  362.     function sql_rowseek($offset, $query_id = 0)
  363.     {
  364.         if(!$query_id)
  365.         {
  366.             $query_id = $this->query_result;
  367.         }
  368.         if($query_id)
  369.         {
  370.             $this->row_index[$query_id] = 0;
  371.             return true;
  372.         }
  373.         else
  374.         {
  375.             return false;
  376.         }
  377.     }
  378.     function sql_nextid($query_id = 0)
  379.     {
  380.         if(!$query_id)
  381.         {
  382.             $query_id = $this->query_result;
  383.         }
  384.         if($query_id)
  385.         {
  386.             return $this->next_id[$query_id];
  387.         }
  388.         else
  389.         {
  390.             return false;
  391.         }
  392.     }
  393.     function sql_freeresult($query_id = 0)
  394.     {
  395.         if(!$query_id)
  396.         {
  397.             $query_id = $this->query_result;
  398.         }
  399.         if($query_id)
  400.         {
  401.             $result = @odbc_free_result($query_id);
  402.             return $result;
  403.         }
  404.         else
  405.         {
  406.             return false;
  407.         }
  408.     }
  409.     function sql_error($query_id = 0)
  410.     {
  411. //        $result['code'] = @odbc_error($this->db_connect_id);
  412. //        $result['message'] = @odbc_errormsg($this->db_connect_id);
  413.  
  414.         return "";
  415.     }
  416.  
  417. } // class sql_db
  418.  
  419. } // if ... define
  420.  
  421. ?>