Advertisement
TBotNik

EM_DIO

Jul 28th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.82 KB | None | 0 0
  1. Tests of the Enterprise Modules Database IO
  2.  
  3. **********************************************************************************
  4.  
  5. <?php
  6.    /***************************************************************************/
  7.    /*      File: test_dio.php                                                 */
  8.    /*   Purpose: Tests the Enterprise Modules em_dio.php Database IO module.  */
  9.    /***************************************************************************/
  10.     include('em_dio.php');
  11.  
  12.     function set_db() {
  13.        $db_eng = 'mysql';           // Set DB engine
  14.        $db_hst = 'localhost';       // Set DB engine
  15.        $db_nam = 'accounts';        // Set DB name
  16.        $db_prt = '3306';                // Set DB engine
  17.        $db_pwd = 'nomened1497'; // Set DB PWD
  18.        $db_tab = 'pg_menu';         // Set DB table
  19.        $db_uid = 'ndavis';          // Set DB UID
  20.        $db_lnk = new em_dio($db_nam, $db_hst, $db_prt, $db_uid, $db_pwd,
  21.                         $db_tab, $db_eng);
  22.        $db_sql = "SELECT * FROM $db_nam.pg_menu WHERE `ptm_prc`='Accounts';";
  23.         $result = $db_lnk->dbQuery($db_sql);            // Run the Query
  24.        return $result;
  25.    }  // end function set_db
  26.  
  27.     $res = set_db();
  28.     $rw_cnt = 0;
  29.     while ($row = mysql_fetch_assoc($res)) {
  30.         echo "Rw=> $rw_cnt ";
  31.         foreach ($row as $fld => $val) {
  32.             echo "FLD=> $fld VAL=> $val ";
  33.         }  // end foreach
  34.         echo " <br>";
  35.         $rw_cnt++;
  36.     }
  37.  
  38. ?>
  39.  
  40. **********************************************************************************
  41.  
  42. <?php
  43.  
  44.    /***************************************************************************/
  45.    /*      File: em_dio.php                                                   */
  46.    /*   Purpose: The Enterprise Database IO module.                           */
  47.    /***************************************************************************/
  48.    class em_dio {
  49.       var $db_col;      // Data Table Column Name
  50.       var $db_con;      // Database Connection
  51.       var $db_nam;      // Database Name
  52.       var $db_eng;      // Database Engine
  53.       var $db_hst;      // Database Host
  54.       var $db_prt;      // Database Port
  55.       var $db_pwd;      // Database Password
  56.       var $db_rct;      // Data Query Returned Number of Rows
  57.       var $db_res;      // Data Query Returned Results Array
  58.       var $db_row;      // Data Query Returned Row
  59.       var $db_sql;      // Database SQL String
  60.       var $db_tab;      // Database Table
  61.       var $db_uid;      // Database User
  62.       var $db_use;      // Database Used
  63.  
  64.         public   $auto_slashes;
  65.         public   $sql_error;
  66.         public   $linked;
  67.       private  $result = array();  // Results that are returned from the query
  68.  
  69.         public function __construct ($db_use, $db_hst, $db_prt, $db_uid,
  70.                                 $db_pwd, $db_tab, $db_eng) {
  71.            $this->db_con = false;
  72.         $this->db_eng = $db_eng;
  73.         $this->dbhost = $db_hst;
  74.            $this->dbname = $db_use;
  75.            $this->dbport = $db_prt;
  76.            $this->dbpass = $db_pwd;
  77.            $this->dbtabl = $db_tab;
  78.            $this->dbuser = $db_uid;
  79.            $this->linked = self::dbConnect();
  80.             return $this->linked;
  81.         }  // end function __construct
  82.  
  83.         public function dbConnect() {
  84.            /*********************************************************************/
  85.            /*  Function: dbConnect()                                            */
  86.            /*   Purpose: Connects the database & reports errors.  Only one      */
  87.            /*            connection allowed.                                    */
  88.            /*********************************************************************/
  89.             $link = $this->linked;
  90.            if (!$link) {
  91.               switch ($this->db_eng) {
  92.                  case 'mysql':          // Process MySQL Engine
  93.                     $this->db_con = @mysql_connect($this->dbhost.":".$this->dbport,
  94.                                         $this->dbuser, $this->dbpass) or die(mysql_error());
  95.                     if ($this->db_con) {
  96.                         $seldb = @mysql_select_db($this->dbname, $this->db_con)
  97.                                         or die("Error=> ".mysql_error());
  98.                         if ($seldb) {
  99.                            $this->db_con = true;
  100.                         } else {
  101.                            $this->db_con = false;
  102.                         }  // end if $db_con
  103.                     }     // end if $conn
  104.                     break;
  105.                     case 'pgsql':           // Process PostGres Engine
  106.                     $con_str = "host=$db_hst port=$db_prt user=$db_uid ".
  107.                                     "password=$db_pwd dbname=$db_nam";
  108.                     $db_con = pg_connect($con_str)
  109.                         or die("Error: Failed to connect with CONN_STRING");
  110.                     if ($db_con) {
  111.                         $this->db_con = true;
  112.                     } else {
  113.                         $this->db_con = false;
  114.                     }  // end if $db_con
  115.                     break;
  116.                 //case 'oracle':            // Process Oracle Engine
  117.                 //  break;
  118.                 //case 'mssql':         // Process MS-SQL Engine
  119.                 //  break;
  120.                 //case 'sybase':            // Process SyBase Engine
  121.                 //  break;
  122.               }  // end switch
  123.            }     // end if !$this->con
  124.             if ($this->db_con === true) {
  125.                 return array($this->db_con, $this->db_use);
  126.             }  // end if $this->con
  127.         }     // end function dbConnect
  128.  
  129.         public function dbExec() {
  130.             /***********************************************************************/
  131.             /*  Function: dbExec($db_sql)                                          */
  132.             /*   Purpose: Runs the database query string & returns the results or  */
  133.             /*            reports errors.                                          */
  134.             /***********************************************************************/
  135.             if (!$linked) {
  136.                 $linked = self::dbConnect();
  137.             }  // end if !linked
  138.             switch ($this->db_eng) {
  139.           case 'mysql':         // Process MySQL Engine
  140.               $result = mysql_query($this->db_sql) or die(mysql_error());
  141.               break;
  142.           case 'pgsql':         // Process PostGres Engine
  143.               $result = pg_exec($this->db_sql) or die("Error: Query $this->db_sql Failed!");
  144.               break;
  145.           //case 'oracle':      // Process Oracle Engine
  146.           //    break;
  147.           //case 'mssql':       // Process MS-SQL Engine
  148.           //    break;
  149.           //case 'sybase':      // Process SyBase Engine
  150.           //    break;
  151.             }     // end switch
  152.             return $result;
  153.          }       // end function dbExec
  154.  
  155.          public function dbFetch($db_res,$db_row) {
  156.             /***********************************************************************/
  157.             /*  Function: dbFetch()                                                */
  158.             /*   Purpose: Runs the database query string & returns the results in  */
  159.             /*            an array or reports errors.                              */
  160.             /***********************************************************************/
  161.             if (!$linked) {
  162.                 $linked = self::dbConnect();
  163.             }  // end if !linked
  164.             switch ($this->db_eng) {
  165.           case 'mysql':         // Process MySQL Engine
  166.               $result = mysql_result($db_res, $db_row) or die(mysql_error());
  167.               break;
  168.           case 'pgsql':         // Process PostGres Engine
  169.               $result = pg_fetch_row($db_res, $db_row) or die("Error: Fetch Failed!");
  170.               break;
  171.           //case 'oracle':      // Process Oracle Engine
  172.           //    break;
  173.           //case 'mssql':       // Process MS-SQL Engine
  174.           //    break;
  175.           //case 'sybase':      // Process SyBase Engine
  176.           //    break;
  177.             }     // end switch
  178.             return $result;
  179.          }       // end function dbFetch
  180.  
  181.         public function dbNRows($db_res) {
  182.             /***********************************************************************/
  183.             /*  Function: dbRowRes()                                               */
  184.             /*   Purpose: Checks the query result to find the number of rows.      */
  185.             /***********************************************************************/
  186.             if (!$this->linked) {
  187.                 $linked = self::dbConnect();
  188.             }  // end if !linked
  189.             switch ($this->db_eng) {
  190.                 case 'mysql':           // Process MySQL Engine
  191.                     $result = mysql_num_rows($db_res) or die(mysql_error());
  192.                     break;
  193.                 case 'pgsql':           // Process PostGres Engine
  194.                     $result = pg_numrows($db_res) or die("Error: Row Count Failed!");
  195.                     break;
  196.           //case 'oracle':      // Process Oracle Engine
  197.           //    break;
  198.           //case 'mssql':       // Process MS-SQL Engine
  199.           //    break;
  200.           //case 'sybase':      // Process SyBase Engine
  201.           //    break;
  202.             }     // end switch
  203.             $this->db_rct = $result;
  204.             return $this->db_rct;
  205.         }        // end function dbNRows
  206.  
  207.         public function dbQuery($db_sql) {
  208.             /***********************************************************************/
  209.             /*  Function: dbQuery()                                                */
  210.             /*   Purpose: Runs the database query string & returns the results or  */
  211.             /*            reports errors.                                          */
  212.             /***********************************************************************/
  213.             $link = $this->linked;
  214.             //$link = self::$linked;
  215.             if (!$link) {
  216.                 $this->linked = self::dbConnect();
  217.             }  // end if !linked
  218.            switch ($this->db_eng) {
  219.                 case 'mysql':           // Process MySQL Engine
  220.                     $result = mysql_query($db_sql) or die(mysql_error());
  221.                     break;
  222.                 case 'pgsql':           // Process PostGres Engine
  223.                    $result = pg_query($this->db_sql);
  224.                     if ($result == false) {
  225.                         $result = pg_query($newquery) or die("Error: Query failed ".
  226.                                         "with query string:<BR>$this->db_sql");
  227.                     }  // end if $result
  228.                     break;
  229.                 //case 'oracle':        // Process Oracle Engine
  230.                 //  break;
  231.                 //case 'mssql':     // Process MS-SQL Engine
  232.                 //  break;
  233.                 //case 'sybase':        // Process SyBase Engine
  234.                 //  break;
  235.            }     // end switch
  236.            if (self::dbNRows($result)==0) {
  237.                 return;
  238.            } else {
  239.                 return $result;
  240.             }
  241.         }        // end function dbQuery
  242.  
  243.         public function dbRes($db_res,$db_row,$db_col) {
  244.             /***********************************************************************/
  245.             /*  Function: dbRes()                                                  */
  246.             /*   Purpose: Parses the results of a DB query by row and column to    */
  247.             /*            get actual cell results.                                 */
  248.             /***********************************************************************/
  249.             if (!$linked) {
  250.                 $linked = self::dbConnect();
  251.             }  // end if !linked
  252.            switch ($this->db_eng) {
  253.                 case 'mysql':           // Process MySQL Engine
  254.                     $result = mysql_result($db_res,$db_row,$db_col) or die(mysql_error());
  255.                     break;
  256.                 case 'pgsql':           // Process PostGres Engine
  257.                     $result = pg_result($db_res,$db_row,$db_col) or die("Error: Cell ".
  258.                                         "Parse Failed!");
  259.                     break;
  260.                 //case 'oracle':        // Process Oracle Engine
  261.                 //  break;
  262.                 //case 'mssql':     // Process MS-SQL Engine
  263.                 //  break;
  264.                 //case 'sybase':        // Process SyBase Engine
  265.                 //  break;
  266.             }     // end switch
  267.            return $result;
  268.         }        // end function dbRes
  269.  
  270.         public function dbSet($name) {
  271.             /***********************************************************************/
  272.            /*  Function: dbSet()                                                  */
  273.            /*   Purpose: Change to new DB and set all current results to null.    */
  274.            /***********************************************************************/
  275.             if (!$linked) {
  276.                 $linked = self::dbConnect();
  277.             }  // end if !linked
  278.             switch ($this->db_eng) {
  279.           case 'mysql':         // Process MySQL Engine
  280.                   if ($this->db_con) {
  281.                      if (@mysql_close()) {
  282.                         $this->db_con = false;
  283.                         $this->db_res = null;
  284.                         $this->db_nam = $name;
  285.                         $this->dbConnect();
  286.                      }  // end if @mysql_close
  287.                   }     // end if $this->db_con
  288.               break;
  289.           case 'pgsql':         // Process PostGres Engine
  290.                   if ($this->db_con) {
  291.                      if (@pg_close()) {
  292.                         $this->db_con = false;
  293.                         $this->db_res = null;
  294.                         $this->db_nam = $name;
  295.                         $this->dbConnect();
  296.                      }  // end if @pg_close
  297.                   }     // end if $this->db_con
  298.               break;
  299.           //case 'oracle':      // Process Oracle Engine
  300.           //    break;
  301.           //case 'mssql':       // Process MS-SQL Engine
  302.           //    break;
  303.           //case 'sybase':      // Process SyBase Engine
  304.           //    break;
  305.             }     // end switch
  306.             return $result;
  307.          }       // end function dbNRows
  308.  
  309.         public function dbTabChk($table) {
  310.             /***********************************************************************/
  311.            /*  Function: dbTabChk()                                               */
  312.            /*   Purpose: Checks to see if table exists.                           */
  313.            /***********************************************************************/
  314.             if (!$linked) {
  315.                 $linked = self::dbConnect();
  316.             }  // end if !linked
  317.            switch ($this->db_eng) {
  318.                 case 'mysql':           // Process MySQL Engine
  319.                 $tabchkstr  = 'SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"';
  320.                  $tablesInDb = @mysql_query($tabchkstr);
  321.                  if ($tablesInDb) {
  322.                     if (mysql_num_rows($tablesInDb)==1) {
  323.                        return true;
  324.                     } else {
  325.                        return false;
  326.                     }  // end if mysql_num_rows
  327.                  }     // end if $tablesInDb
  328.                     break;
  329.                 case 'pgsql':           // Process PostGres Engine
  330.                     $tabchkstr  = 'SELECT tablename FROM pg_tables LIKE "'.$table.'"';
  331.                    $tablesInDb = @pg_query($tabchkstr);
  332.                    if ($tablesInDb) {
  333.                         if (pg_numrows($tablesInDb)==1) {
  334.                          return true;
  335.                       } else {
  336.                          return false;
  337.                       }  // end if mysql_num_rows
  338.                    }     // end if $tablesInDb
  339.                     break;
  340.                 //case 'oracle':        // Process Oracle Engine
  341.                 //  break;
  342.                 //case 'mssql':     // Process MS-SQL Engine
  343.                 //  break;
  344.                 //case 'sybase':        // Process SyBase Engine
  345.                 //  break;
  346.            }     // end switch
  347.            return $result;
  348.         }        // end function dbNRows
  349.  
  350.         public function dbInsert($table,$values,$rows = null) {
  351.             /***********************************************************************/
  352.            /*  Function: dbInsert()                                               */
  353.            /*   Purpose: Checks to see if table exists.                           */
  354.            /***********************************************************************/
  355.             if (!$linked) {
  356.                 $linked = self::dbConnect();
  357.             }  // end if !linked
  358.             if ($this->dbTabChk($table)) {
  359.                 switch ($this->db_eng) {
  360.                     case 'mysql':           // Process MySQL Engine
  361.                         $insert = 'INSERT INTO '.$table;
  362.                         if ($rows != null)            {
  363.                            $insert .= ' ('.$rows.')';
  364.                         }
  365.                         for ($i = 0; $i < count($values); $i++) {
  366.                            if (is_string($values[$i]))
  367.                               $values[$i] = '"'.$values[$i].'"';
  368.                            }
  369.                            $values = implode(',',$values);
  370.                            $insert .= ' VALUES ('.$values.')';
  371.                            $ins = @mysql_query($insert);
  372.                            if($ins)            {
  373.                                return true;
  374.                            } else {
  375.                                return false;
  376.                            }
  377.                           break;
  378.                     //case 'pgsql':         // Process PostGres Engine
  379.                     //    break;
  380.                     //case 'oracle':        // Process Oracle Engine
  381.                     //  break;
  382.                     //case 'mssql':     // Process MS-SQL Engine
  383.                     //  break;
  384.                     //case 'sybase':        // Process SyBase Engine
  385.                     //  break;
  386.                 }     // end switch
  387.                 return $result;
  388.             }  // end if $this->dbTabChk
  389.         }   // end function dbNRows
  390.  
  391.    }     // end class new_dio
  392.  
  393. ?>
  394.  
  395.  
  396. **********************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement