Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.17 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * ADOdb Lite is a PHP class to encapsulate multiple database APIs and is compatible with
  5.  * a subset of the ADODB Command Syntax.
  6.  * Currently supports Frontbase, MaxDB, miniSQL, MSSQL, MSSQL Pro, MySQLi, MySQLt, MySQL, PostgresSQL,
  7.  * PostgresSQL64, PostgresSQL7, SqLite and Sybase.
  8.  *
  9.  */
  10.  
  11. class mysql_driver_ADOConnection extends ADOConnection
  12. {
  13.     var $nameQuote = '`';
  14.     var $sysDate = 'CURDATE()';
  15.     var $sysTimeStamp = 'NOW()';
  16.  
  17.     function mysql_driver_ADOConnection()
  18.     {
  19.         $this->dbtype = 'mysql';
  20.         $this->dataProvider = 'mysql';
  21.     }
  22.  
  23.     /**
  24.     * Connection to database server and selected database
  25.     *
  26.     * @access private
  27.     */
  28.  
  29.     function _connect($host = "", $username = "", $password = "", $database = "", $persistent, $forcenew)
  30.     {
  31.         if (!function_exists('mysql_connect')) {
  32.             echo "ERROR: Function 'mysql_connect' not found! Check if MySQL is properly set up. And that PHP has the MySQL extension installed.";
  33.             return false;
  34.         }
  35.  
  36.         $this->host = $host;
  37.         if (!empty($this->port)) $this->host .= ":" . $this->port;
  38.         $this->username = $username;
  39.         $this->password = $password;
  40.         $this->database = $database;
  41.         $this->persistent = $persistent;
  42.         $this->forcenewconnection = $forcenew;
  43.  
  44.         if($this->persistent == 1)
  45.         {
  46.             if (strnatcmp(PHP_VERSION, '4.3.0') >= 0)
  47.                 $this->connectionId = @mysql_pconnect( $this->host, $this->username, $this->password, $this->clientFlags );
  48.             else
  49.                 $this->connectionId = @mysql_pconnect( $this->host, $this->username, $this->password );
  50.         }
  51.         else
  52.         {
  53.             if (strnatcmp(PHP_VERSION, '4.3.0') >= 0)
  54.                 $this->connectionId = @mysql_connect( $this->host, $this->username, $this->password, $this->forcenewconnection, $this->clientFlags );
  55.             else if (strnatcmp(PHP_VERSION, '4.2.0') >= 0)
  56.                 $this->connectionId = @mysql_connect( $this->host, $this->username, $this->password, $this->forcenewconnection );
  57.             else
  58.                 $this->connectionId = @mysql_connect( $this->host, $this->username, $this->password );
  59.         }
  60.  
  61.         if ($this->connectionId === false)
  62.         {
  63.             if ($fn = $this->raiseErrorFn)
  64.                 $fn($this->dbtype, 'CONNECT', $this->ErrorNo(), $this->ErrorMsg(), $this->host, $this->database, $this);
  65.             return false;
  66.         }
  67.  
  68.         if (!empty($this->database))
  69.         {
  70.             if($this->SelectDB( $this->database ) == false)
  71.             {
  72.                 $this->connectionId = false;
  73.                 return false;
  74.             }
  75.         }
  76.  
  77.         return true;
  78.     }
  79.  
  80.     /**
  81.     * Choose a database to connect.
  82.     *
  83.     * @param dbname is the name of the database to select
  84.     * @return       true or false
  85.     * @access public
  86.     */
  87.  
  88.     function SelectDB($dbname)
  89.     {
  90.         $this->database = $dbname;
  91.  
  92.         if ($this->connectionId === false)
  93.         {
  94.             $this->connectionId = false;
  95.             return false;
  96.         }
  97.         else
  98.         {
  99.             $result = @mysql_select_db( $this->database, $this->connectionId );
  100.  
  101.             if($result === false)
  102.             {
  103.                 if($this->createdatabase == true)
  104.                 {
  105.                     $result = @mysql_query( "CREATE DATABASE IF NOT EXISTS " . $this->database, $this->connectionId );
  106.                     if ($result === false) { // error handling if query fails
  107.                         return false;
  108.                     }
  109.                     $result = @mysql_select_db( $this->database, $this->connectionId );
  110.                     if($result === false)
  111.                     {
  112.                         return false;
  113.                     }
  114.                 }
  115.                 else
  116.                 {
  117.                     return false;
  118.                 }
  119.             }
  120.             return true;
  121.         }
  122.     }
  123.  
  124.     /**
  125.     * Return database error message
  126.     * Usage: $errormessage =& $db->ErrorMsg();
  127.     *
  128.     * @access public
  129.     */
  130.  
  131.     function ErrorMsg()
  132.     {
  133.         if ($this->connectionId === false)
  134.         {
  135.             return @mysql_error();
  136.         }
  137.         else
  138.         {
  139.             return @mysql_error($this->connectionId);
  140.         }
  141.     }
  142.  
  143.     /**
  144.     * Return database error number
  145.     * Usage: $errorbo =& $db->ErrorNo();
  146.     *
  147.     * @access public
  148.     */
  149.  
  150.     function ErrorNo()
  151.     {
  152.         if ($this->connectionId === false)
  153.         {
  154.             return @mysql_errno();
  155.         }
  156.         else
  157.         {
  158.             return @mysql_errno($this->connectionId);
  159.         }
  160.     }
  161.  
  162.     /**
  163.     * Returns # of affected rows from insert/delete/update query
  164.     *
  165.     * @access public
  166.     * @return integer Affected rows
  167.     */
  168.  
  169.     function Affected_Rows()
  170.     {
  171.         return @mysql_affected_rows($this->connectionId);
  172.     }
  173.  
  174.     /**
  175.     * Returns the last record id of an inserted item
  176.     * Usage: $db->Insert_ID();
  177.     *
  178.     * @access public
  179.     */
  180.  
  181.     function Insert_ID()
  182.     {
  183.         return @mysql_insert_id($this->connectionId);
  184.     }
  185.  
  186.     /**
  187.     * Correctly quotes a string so that all strings are escape coded.
  188.     * An example is $db->qstr("Haven't a clue.");
  189.     *
  190.     * @param string         the string to quote
  191.     * @param [magic_quotes] if $s is GET/POST var, set to get_magic_quotes_gpc().
  192.     *
  193.     * @return single-quoted string IE: 'Haven\'t a clue.'
  194.     */
  195.  
  196.     function qstr($string, $magic_quotes=false)
  197.     {
  198.         if (!$magic_quotes) {
  199.             if (strnatcmp(PHP_VERSION, '4.3.0') >= 0) {
  200.                 return "'" . mysql_real_escape_string($string, $this->connectionId) . "'";
  201.             }
  202.             $string = str_replace("'", "\\'" , str_replace('\\', '\\\\', str_replace("\0", "\\\0", $string)));
  203.             return "'" . $string . "'";
  204.         }
  205.         return "'" . str_replace('\\"', '"', $string) . "'";
  206.     }
  207.  
  208.     function QMagic($string)
  209.     {
  210.         return $this->qstr($string, get_magic_quotes_gpc());
  211.     }
  212.  
  213.     /**
  214.     * Returns concatenated string
  215.     * Usage: $db->Concat($str1,$str2);
  216.     *
  217.     * @return concatenated string
  218.     */
  219.     function Concat()
  220.     {
  221.         $arr = func_get_args();
  222.         $list = implode(', ', $arr);
  223.  
  224.         if (strlen($list) > 0) return "CONCAT($list)";
  225.         else return '';
  226.     }
  227.  
  228.     function IfNull( $field, $ifNull )
  229.     {
  230.         return " IFNULL($field, $ifNull) ";
  231.     }
  232.  
  233.     /**
  234.     * Closes database connection
  235.     * Usage: $db->close();
  236.     *
  237.     * @access public
  238.     */
  239.  
  240.     function Close()
  241.     {
  242.         @mysql_close( $this->connectionId );
  243.         $this->connectionId = false;
  244.     }
  245.  
  246.     /**
  247.     * Returns All Records in an array
  248.     *
  249.     * Usage: $db->GetAll($sql);
  250.     * @access public
  251.     */
  252.  
  253.     function &GetAll($sql, $inputarr = false)
  254.     {
  255.         $data =& $this->GetArray($sql, $inputarr);
  256.         return $data;
  257.     }
  258.  
  259.     /**
  260.     * Returns All Records in an array
  261.     *
  262.     * Usage: $db->GetArray($sql);
  263.     * @access public
  264.     */
  265.  
  266.     function &GetArray($sql, $inputarr = false)
  267.     {
  268.         $data = false;
  269.         $result =& $this->Execute($sql, $inputarr);
  270.         if ($result)
  271.         {
  272.             $data =& $result->GetArray();
  273.             $result->Close();
  274.         }
  275.         return $data;
  276.     }
  277.  
  278.     /**
  279.     * Executes SQL query and instantiates resultset methods
  280.     *
  281.     * @access private
  282.     * @return mixed Resultset methods
  283.     */
  284.  
  285.     function &do_query( $sql, $offset, $nrows, $inputarr=false )
  286.     {
  287.         global $ADODB_FETCH_MODE;
  288.  
  289.         $false = false;
  290.  
  291.         $limit = '';
  292.         if ($offset >= 0 || $nrows >= 0)
  293.         {
  294.             $offset = ($offset >= 0) ? $offset . "," : '';
  295.             $nrows = ($nrows >= 0) ? $nrows : '18446744073709551615';
  296.             $limit = ' LIMIT ' . $offset . ' ' . $nrows;
  297.         }
  298.  
  299.         if ($inputarr && is_array($inputarr)) {
  300.             $sqlarr = explode('?', $sql);
  301.             if (!is_array(reset($inputarr))) $inputarr = array($inputarr);
  302.             foreach($inputarr as $arr) {
  303.                 $sql = ''; $i = 0;
  304.                 foreach($arr as $v) {
  305.                     $sql .= $sqlarr[$i];
  306.                     switch(gettype($v)){
  307.                         case 'string':
  308.                             $sql .= $this->qstr($v);
  309.                             break;
  310.                         case 'double':
  311.                             $sql .= str_replace(',', '.', $v);
  312.                             break;
  313.                         case 'boolean':
  314.                             $sql .= $v ? 1 : 0;
  315.                             break;
  316.                         default:
  317.                             if ($v === null)
  318.                                 $sql .= 'NULL';
  319.                             else $sql .= $v;
  320.                     }
  321.                     $i += 1;
  322.                 }
  323.                 $sql .= $sqlarr[$i];
  324.                 if ($i+1 != sizeof($sqlarr))
  325.                     return $false;
  326.                 $this->sql = $sql . $limit;
  327.                 $time_start = array_sum(explode(' ', microtime()));
  328.                 $this->query_count++;
  329.                 $resultId = @mysql_query( $this->sql, $this->connectionId );
  330.                 $time_total = (array_sum(explode(' ', microtime())) - $time_start);
  331.                 $this->query_time_total += $time_total;
  332.                 if($this->debug_console)
  333.                 {
  334.                     $this->query_list[] = $this->sql;
  335.                     $this->query_list_time[] = $time_total;
  336.                     $this->query_list_errors[] = $this->ErrorMsg();
  337.                 }
  338.                 if($this->debug)
  339.                 {
  340.                     $this->outp($sql . $limit);
  341.                 }
  342.                 if ($resultId === false) { // error handling if query fails
  343.                     if ($fn = $this->raiseErrorFn)
  344.                         $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this);
  345.                     return $false;
  346.                 }
  347.             }
  348.         }
  349.         else
  350.         {
  351.                 $this->sql = $sql . $limit;
  352.                 $this->sql = $sql . $limit;
  353.                 $time_start = array_sum(explode(' ', microtime()));
  354.                 $this->query_count++;
  355.                 $resultId = @mysql_query( $this->sql, $this->connectionId );
  356.                 $time_total = (array_sum(explode(' ', microtime())) - $time_start);
  357.                 $this->query_time_total += $time_total;
  358.                 if($this->debug_console)
  359.                 {
  360.                     $this->query_list[] = $this->sql;
  361.                     $this->query_list_time[] = $time_total;
  362.                     $this->query_list_errors[] = $this->ErrorMsg();
  363.                 }
  364.                 if($this->debug)
  365.                 {
  366.                     $this->outp($sql . $limit);
  367.                 }
  368.         }
  369.  
  370.         if ($resultId === false) { // error handling if query fails
  371.             if ($fn = $this->raiseErrorFn)
  372.                 $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this);
  373.             return $false;
  374.         }
  375.  
  376.         if ($resultId === true) { // return simplified recordset for inserts/updates/deletes with lower overhead
  377.             $recordset = new ADORecordSet_empty();
  378.             return $recordset;
  379.         }
  380.  
  381.         $resultset_name = $this->last_module_name . "_ResultSet";
  382.         $recordset = new $resultset_name( $resultId, $this->connectionId );
  383.  
  384.         $recordset->_currentRow = 0;
  385.  
  386.         switch ($ADODB_FETCH_MODE)
  387.         {
  388.             case ADODB_FETCH_NUM: $recordset->fetchMode = MYSQL_NUM; break;
  389.             case ADODB_FETCH_ASSOC: $recordset->fetchMode = MYSQL_ASSOC; break;
  390.             default:
  391.             case ADODB_FETCH_DEFAULT:
  392.             case ADODB_FETCH_BOTH: $recordset->fetchMode = MYSQL_BOTH; break;
  393.         }
  394.  
  395.         $recordset->_numOfRows = @mysql_num_rows( $resultId );
  396.         if( $recordset->_numOfRows == 0)
  397.         {
  398.             $recordset->EOF = true;
  399.         }
  400.         $recordset->_numOfFields = @mysql_num_fields( $resultId );
  401.         $recordset->_fetch();
  402.  
  403.         return $recordset;
  404.     }
  405. }
  406.  
  407. class mysql_driver_ResultSet
  408. {
  409.     var $connectionId;
  410.     var $fields;
  411.     var $resultId;
  412.     var $_currentRow = 0;
  413.     var $_numOfRows = -1;
  414.     var $_numOfFields = -1;
  415.     var $fetchMode;
  416.     var $EOF;
  417.  
  418.     /**
  419.     * mysqlResultSet Constructor
  420.     *
  421.     * @access private
  422.     * @param string $record
  423.     * @param string $resultId
  424.     */
  425.  
  426.     function mysql_driver_ResultSet( $resultId, $connectionId )
  427.     {
  428.         $this->fields = array();
  429.         $this->connectionId = $connectionId;
  430.         $this->record = array();
  431.         $this->resultId = $resultId;
  432.         $this->EOF = false;
  433.     }
  434.  
  435.     /**
  436.     * Frees resultset
  437.     *
  438.     * @access public
  439.     */
  440.  
  441.     function Close()
  442.     {
  443.         @mysql_free_result( $this->resultId );
  444.         $this->fields = array();
  445.         $this->resultId = false;
  446.     }
  447.  
  448.     /**
  449.     * Returns field name from select query
  450.     *
  451.     * @access public
  452.     * @param string $field
  453.     * @return string Field name
  454.     */
  455.  
  456.     function fields( $field )
  457.     {
  458.         if(empty($field))
  459.         {
  460.             return $this->fields;
  461.         }
  462.         else
  463.         {
  464.             return $this->fields[$field];
  465.         }
  466.     }
  467.  
  468.     /**
  469.     * Returns numrows from select query
  470.     *
  471.     * @access public
  472.     * @return integer Numrows
  473.     */
  474.  
  475.     function RecordCount()
  476.     {
  477.         return $this->_numOfRows;
  478.     }
  479.  
  480.     /**
  481.     * Returns num of fields from select query
  482.     *
  483.     * @access public
  484.     * @return integer numfields
  485.     */
  486.  
  487.     function FieldCount()
  488.     {
  489.         return $this->_numOfFields;
  490.     }
  491.  
  492.     /**
  493.     * Returns next record
  494.     *
  495.     * @access public
  496.     */
  497.  
  498.     function MoveNext()
  499.     {
  500.         if (@$this->fields = mysql_fetch_array($this->resultId,$this->fetchMode)) {
  501.             $this->_currentRow += 1;
  502.             return true;
  503.         }
  504.         if (!$this->EOF) {
  505.             $this->_currentRow += 1;
  506.             $this->EOF = true;
  507.         }
  508.         return false;
  509.     }
  510.  
  511.     /**
  512.     * Move to the first row in the recordset. Many databases do NOT support this.
  513.     *
  514.     * @return true or false
  515.     */
  516.  
  517.     function MoveFirst()
  518.     {
  519.         if ($this->_currentRow == 0) return true;
  520.         return $this->Move(0);
  521.     }
  522.  
  523.     /**
  524.     * Returns the Last Record
  525.     *
  526.     * @access public
  527.     */
  528.  
  529.     function MoveLast()
  530.     {
  531.         if ($this->EOF) return false;
  532.         return $this->Move($this->_numOfRows - 1);
  533.     }
  534.  
  535.     /**
  536.     * Random access to a specific row in the recordset. Some databases do not support
  537.     * access to previous rows in the databases (no scrolling backwards).
  538.     *
  539.     * @param rowNumber is the row to move to (0-based)
  540.     *
  541.     * @return true if there still rows available, or false if there are no more rows (EOF).
  542.     */
  543.  
  544.     function Move($rowNumber = 0)
  545.     {
  546.         if ($rowNumber == $this->_currentRow) return true;
  547.         $this->EOF = false;
  548.         if ($this->_numOfRows > 0){
  549.             if ($rowNumber >= $this->_numOfRows - 1){
  550.                 $rowNumber = $this->_numOfRows - 1;
  551.             }
  552.         }
  553.  
  554.         if ($this->_seek($rowNumber)) {
  555.             $this->_currentRow = $rowNumber;
  556.             if ($this->_fetch()) {
  557.                 return true;
  558.             }
  559.             $this->fields = false;
  560.         }
  561.         $this->EOF = true;
  562.         return false;
  563.     }
  564.  
  565.     /**
  566.     * Perform Seek to specific row
  567.     *
  568.     * @access private
  569.     */
  570.  
  571.     function _seek($row)
  572.     {
  573.         if ($this->_numOfRows == 0) return false;
  574.         return @mysql_data_seek($this->resultId,$row);
  575.     }
  576.  
  577.     /**
  578.     * Fills field array with first database element when query initially executed
  579.     *
  580.     * @access private
  581.     */
  582.  
  583.     function _fetch()
  584.     {
  585.         $this->fields = @mysql_fetch_array($this->resultId,$this->fetchMode);
  586.         return is_array($this->fields);
  587.     }
  588.  
  589.     /**
  590.     * Check to see if last record reached
  591.     *
  592.     * @access public
  593.     */
  594.  
  595.     function EOF()
  596.     {
  597.         if( $this->_currentRow < $this->_numOfRows)
  598.         {
  599.             return false;
  600.         }
  601.         else
  602.         {
  603.             $this->EOF = true;
  604.             return true;
  605.         }
  606.     }
  607.  
  608.     /**
  609.     * Returns All Records in an array
  610.     *
  611.     * @access public
  612.     * @param [nRows] is the number of rows to return. -1 means every row.
  613.     */
  614.  
  615.     function &GetArray($nRows = -1)
  616.     {
  617.         $results = array();
  618.         $cnt = 0;
  619.         while (!$this->EOF && $nRows != $cnt) {
  620.             $results[] = $this->fields;
  621.             $this->MoveNext();
  622.             $cnt++;
  623.         }
  624.         return $results;
  625.     }
  626.  
  627.     function &GetRows($nRows = -1)
  628.     {
  629.         $arr =& $this->GetArray($nRows);
  630.         return $arr;
  631.     }
  632.  
  633.     function &GetAll($nRows = -1)
  634.     {
  635.         $arr =& $this->GetArray($nRows);
  636.         return $arr;
  637.     }
  638.  
  639.     /**
  640.     * Fetch field information for a table.
  641.     *
  642.     * @return object containing the name, type and max_length
  643.     */
  644.     function FetchField($fieldOffset = -1)
  645.     {
  646.         if ($fieldOffset != -1) {
  647.             $fieldObject = @mysql_fetch_field($this->resultId, $fieldOffset);
  648.             $fieldObject->max_length = @mysql_field_len($this->resultId,$fieldOffset);
  649.         }
  650.         else
  651.         {
  652.             $fieldObject = @mysql_fetch_field($this->resultId);
  653.             $fieldObject->max_length = @mysql_field_len($this->resultId);
  654.         }
  655.         return $fieldObject;
  656.     }
  657.  
  658. }
  659.  
  660. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement