includes/dbal.php
changeset 798 ddfc1b554a08
parent 770 62fed244fa1c
child 801 eb8b23f11744
equal deleted inserted replaced
797:8d2a40574bbc 798:ddfc1b554a08
   130     return $internal_text;
   130     return $internal_text;
   131   }
   131   }
   132   
   132   
   133   function connect($manual_credentials = false, $dbhost = false, $dbuser = false, $dbpasswd = false, $dbname = false)
   133   function connect($manual_credentials = false, $dbhost = false, $dbuser = false, $dbpasswd = false, $dbname = false)
   134   {
   134   {
   135     $this->enable_errorhandler();
       
   136     
       
   137     if ( !defined('ENANO_SQL_CONSTANTS') )
   135     if ( !defined('ENANO_SQL_CONSTANTS') )
   138     {
   136     {
   139       define('ENANO_SQL_CONSTANTS', '');
   137       define('ENANO_SQL_CONSTANTS', '');
   140       define('ENANO_DBLAYER', 'MYSQL');
   138       define('ENANO_DBLAYER', 'MYSQL');
   141       define('ENANO_SQLFUNC_LOWERCASE', 'lcase');
   139       define('ENANO_SQLFUNC_LOWERCASE', 'lcase');
   218         return false;
   216         return false;
   219       $this->_die('The database could not be selected.');
   217       $this->_die('The database could not be selected.');
   220     }
   218     }
   221     
   219     
   222     // We're in!
   220     // We're in!
   223     $this->disable_errorhandler();
       
   224     return true;
   221     return true;
   225   }
   222   }
   226   
   223   
   227   function sql_query($q, $log_query = true)
   224   function sql_query($q, $log_query = true)
   228   {
   225   {
   229     if ( $log_query || defined('ENANO_DEBUG') )
       
   230       $this->enable_errorhandler();
       
   231     
       
   232     if ( $this->debug && function_exists('debug_backtrace') )
   226     if ( $this->debug && function_exists('debug_backtrace') )
   233     {
   227     {
   234       $backtrace = @debug_backtrace();
   228       $backtrace = @debug_backtrace();
   235       if ( is_array($backtrace) )
   229       if ( is_array($backtrace) )
   236       {
   230       {
   277     if ( $log_query )
   271     if ( $log_query )
   278       $this->query_times[$q] = microtime_float() - $time_start;
   272       $this->query_times[$q] = microtime_float() - $time_start;
   279     
   273     
   280     $this->latest_result = $r;
   274     $this->latest_result = $r;
   281     
   275     
   282     if ( $log_query )
       
   283       $this->disable_errorhandler();
       
   284     return $r;
   276     return $r;
   285   }
   277   }
   286   
   278   
   287   function sql_unbuffered_query($q, $log_query = true)
   279   function sql_unbuffered_query($q, $log_query = true)
   288   {
   280   {
   289     $this->enable_errorhandler();
       
   290     
       
   291     $this->num_queries++;
   281     $this->num_queries++;
   292     if ( $log_query || defined('ENANO_DEBUG') )
   282     if ( $log_query || defined('ENANO_DEBUG') )
   293       $this->query_backtrace[] = '(UNBUFFERED) ' . $q;
   283       $this->query_backtrace[] = '(UNBUFFERED) ' . $q;
   294     $this->latest_query = $q;
   284     $this->latest_query = $q;
   295     // First make sure we have a connection
   285     // First make sure we have a connection
   307     
   297     
   308     $time_start = microtime_float();
   298     $time_start = microtime_float();
   309     $r = @mysql_unbuffered_query($q, $this->_conn);
   299     $r = @mysql_unbuffered_query($q, $this->_conn);
   310     $this->query_times[$q] = microtime_float() - $time_start;
   300     $this->query_times[$q] = microtime_float() - $time_start;
   311     $this->latest_result = $r;
   301     $this->latest_result = $r;
   312     $this->disable_errorhandler();
       
   313     return $r;
   302     return $r;
   314   }
   303   }
   315   
   304   
   316   /**
   305   /**
   317    * Performs heuristic analysis on a SQL query to check for known attack patterns.
   306    * Performs heuristic analysis on a SQL query to check for known attack patterns.
   364    * @return true on success, false on failure
   353    * @return true on success, false on failure
   365    */
   354    */
   366    
   355    
   367   function sql_data_seek($pos, $result = false)
   356   function sql_data_seek($pos, $result = false)
   368   {
   357   {
   369     $this->enable_errorhandler();
       
   370     if(!$result)
   358     if(!$result)
   371       $result = $this->latest_result;
   359       $result = $this->latest_result;
   372     if(!$result)
   360     if(!$result)
   373     {
   361     {
   374       $this->disable_errorhandler();
       
   375       return false;
   362       return false;
   376     }
   363     }
   377     if(mysql_data_seek($result, $pos))
   364     if(mysql_data_seek($result, $pos))
   378     {
   365     {
   379       $this->disable_errorhandler();
       
   380       return true;
   366       return true;
   381     }
   367     }
   382     else
   368     else
   383     {
   369     {
   384       $this->disable_errorhandler();
       
   385       return false;
   370       return false;
   386     }
   371     }
   387   }
   372   }
   388   
   373   
   389   /**
   374   /**
   413   {
   398   {
   414     return @mysql_insert_id();
   399     return @mysql_insert_id();
   415   }
   400   }
   416   
   401   
   417   function fetchrow($r = false) {
   402   function fetchrow($r = false) {
   418     $this->enable_errorhandler();
       
   419     if(!$this->_conn) return false;
   403     if(!$this->_conn) return false;
   420     if(!$r) $r = $this->latest_result;
   404     if(!$r) $r = $this->latest_result;
   421     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
   405     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
   422     $row = mysql_fetch_assoc($r);
   406     $row = mysql_fetch_assoc($r);
   423     $this->disable_errorhandler();
       
   424     return integerize_array($row);
   407     return integerize_array($row);
   425   }
   408   }
   426   
   409   
   427   function fetchrow_num($r = false) {
   410   function fetchrow_num($r = false) {
   428     $this->enable_errorhandler();
       
   429     if(!$r) $r = $this->latest_result;
   411     if(!$r) $r = $this->latest_result;
   430     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
   412     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
   431     $row = mysql_fetch_row($r);
   413     $row = mysql_fetch_row($r);
   432     $this->disable_errorhandler();
       
   433     return integerize_array($row);
   414     return integerize_array($row);
   434   }
   415   }
   435   
   416   
   436   function numrows($r = false) {
   417   function numrows($r = false) {
   437     $this->enable_errorhandler();
       
   438     if(!$r) $r = $this->latest_result;
   418     if(!$r) $r = $this->latest_result;
   439     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
   419     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
   440     $n = mysql_num_rows($r);
   420     $n = mysql_num_rows($r);
   441     $this->disable_errorhandler();
       
   442     return $n;
   421     return $n;
   443   }
   422   }
   444   
   423   
   445   function escape($str)
   424   function escape($str)
   446   {
   425   {
   447     $this->enable_errorhandler();
       
   448     $str = mysql_real_escape_string($str);
   426     $str = mysql_real_escape_string($str);
   449     $this->disable_errorhandler();
       
   450     return $str;
   427     return $str;
   451   }
   428   }
   452   
   429   
   453   function free_result($result = false)
   430   function free_result($result = false)
   454   {
   431   {
   455     $this->enable_errorhandler();
       
   456     if(!$result)
   432     if(!$result)
   457       $result = $this->latest_result;
   433       $result = $this->latest_result;
   458     if(!$result)
   434     if(!$result)
   459     {
   435     {
   460       $this->disable_errorhandler();
       
   461       return null;
   436       return null;
   462     }
   437     }
   463     @mysql_free_result($result);
   438     @mysql_free_result($result);
   464     $this->disable_errorhandler();
       
   465     return null;
   439     return null;
   466   }
   440   }
   467   
   441   
   468   function close() {
   442   function close() {
   469     @mysql_close($this->_conn);
   443     @mysql_close($this->_conn);
   796   var $num_queries, $query_backtrace, $query_times, $query_sources, $latest_result, $latest_query, $_conn, $sql_stack_fields, $sql_stack_values, $debug;
   770   var $num_queries, $query_backtrace, $query_times, $query_sources, $latest_result, $latest_query, $_conn, $sql_stack_fields, $sql_stack_values, $debug;
   797   var $row = array();
   771   var $row = array();
   798 	var $rowset = array();
   772 	var $rowset = array();
   799   var $errhandler;
   773   var $errhandler;
   800   
   774   
   801   function enable_errorhandler()
       
   802   {
       
   803     // echo "DBAL: enabling error handler<br />";
       
   804     if ( function_exists('debug_backtrace') )
       
   805     {
       
   806       $this->errhandler = set_error_handler('db_error_handler');
       
   807     }
       
   808   }
       
   809   
       
   810   function disable_errorhandler()
       
   811   {
       
   812     // echo "DBAL: disabling error handler<br />";
       
   813     if ( $this->errhandler )
       
   814     {
       
   815       set_error_handler($this->errhandler);
       
   816     }
       
   817     else
       
   818     {
       
   819       restore_error_handler();
       
   820     }
       
   821   }
       
   822   
       
   823   function sql_backtrace()
   775   function sql_backtrace()
   824   {
   776   {
   825     return implode("\n-------------------------------------------------------------------\n", $this->query_backtrace);
   777     return implode("\n-------------------------------------------------------------------\n", $this->query_backtrace);
   826   }
   778   }
   827   
   779   
   879     return $internal_text;
   831     return $internal_text;
   880   }
   832   }
   881   
   833   
   882   function connect($manual_credentials = false, $dbhost = false, $dbuser = false, $dbpasswd = false, $dbname = false)
   834   function connect($manual_credentials = false, $dbhost = false, $dbuser = false, $dbpasswd = false, $dbname = false)
   883   {
   835   {
   884     $this->enable_errorhandler();
       
   885     
       
   886     if ( !defined('ENANO_SQL_CONSTANTS') )
   836     if ( !defined('ENANO_SQL_CONSTANTS') )
   887     {
   837     {
   888       define('ENANO_SQL_CONSTANTS', '');
   838       define('ENANO_SQL_CONSTANTS', '');
   889       define('ENANO_DBLAYER', 'PGSQL');
   839       define('ENANO_DBLAYER', 'PGSQL');
   890       define('ENANO_SQLFUNC_LOWERCASE', 'lower');
   840       define('ENANO_SQLFUNC_LOWERCASE', 'lower');
   952     $this->num_queries = 0;
   902     $this->num_queries = 0;
   953     
   903     
   954     $this->debug = ( defined('ENANO_DEBUG') );
   904     $this->debug = ( defined('ENANO_DEBUG') );
   955     
   905     
   956     // We're in!
   906     // We're in!
   957     $this->disable_errorhandler();
       
   958     return true;
   907     return true;
   959   }
   908   }
   960   
   909   
   961   function sql_query($q)
   910   function sql_query($q)
   962   {
   911   {
   963     $this->enable_errorhandler();
       
   964     
       
   965     if ( $this->debug && function_exists('debug_backtrace') )
   912     if ( $this->debug && function_exists('debug_backtrace') )
   966     {
   913     {
   967       $backtrace = @debug_backtrace();
   914       $backtrace = @debug_backtrace();
   968       if ( is_array($backtrace) )
   915       if ( is_array($backtrace) )
   969       {
   916       {
   997     
   944     
   998     $time_start = microtime_float();
   945     $time_start = microtime_float();
   999     $r = pg_query($q);
   946     $r = pg_query($q);
  1000     $this->query_times[$q] = microtime_float() - $time_start;
   947     $this->query_times[$q] = microtime_float() - $time_start;
  1001     $this->latest_result = $r;
   948     $this->latest_result = $r;
  1002     $this->disable_errorhandler();
       
  1003     return $r;
   949     return $r;
  1004   }
   950   }
  1005   
   951   
  1006   function sql_unbuffered_query($q)
   952   function sql_unbuffered_query($q)
  1007   {
   953   {
  1059    * @return true on success, false on failure
  1005    * @return true on success, false on failure
  1060    */
  1006    */
  1061    
  1007    
  1062   function sql_data_seek($pos, $result = false)
  1008   function sql_data_seek($pos, $result = false)
  1063   {
  1009   {
  1064     $this->enable_errorhandler();
       
  1065     if(!$result)
  1010     if(!$result)
  1066       $result = $this->latest_result;
  1011       $result = $this->latest_result;
  1067     if(!$result)
  1012     if(!$result)
  1068     {
  1013     {
  1069       $this->disable_errorhandler();
       
  1070       return false;
  1014       return false;
  1071     }
  1015     }
  1072     if(pg_result_seek($result, $pos))
  1016     if(pg_result_seek($result, $pos))
  1073     {
  1017     {
  1074       $this->disable_errorhandler();
       
  1075       return true;
  1018       return true;
  1076     }
  1019     }
  1077     else
  1020     else
  1078     {
  1021     {
  1079       $this->disable_errorhandler();
       
  1080       return false;
  1022       return false;
  1081     }
  1023     }
  1082   }
  1024   }
  1083   
  1025   
  1084   /**
  1026   /**
  1157     }
  1099     }
  1158     return false;
  1100     return false;
  1159   }
  1101   }
  1160   
  1102   
  1161   function fetchrow($r = false) {
  1103   function fetchrow($r = false) {
  1162     $this->enable_errorhandler();
       
  1163     if(!$this->_conn) return false;
  1104     if(!$this->_conn) return false;
  1164     if(!$r) $r = $this->latest_result;
  1105     if(!$r) $r = $this->latest_result;
  1165     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
  1106     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
  1166     $row = pg_fetch_assoc($r);
  1107     $row = pg_fetch_assoc($r);
  1167     $this->disable_errorhandler();
       
  1168     return integerize_array($row);
  1108     return integerize_array($row);
  1169   }
  1109   }
  1170   
  1110   
  1171   function fetchrow_num($r = false) {
  1111   function fetchrow_num($r = false) {
  1172     $this->enable_errorhandler();
       
  1173     if(!$r) $r = $this->latest_result;
  1112     if(!$r) $r = $this->latest_result;
  1174     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
  1113     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
  1175     $row = pg_fetch_row($r);
  1114     $row = pg_fetch_row($r);
  1176     $this->disable_errorhandler();
       
  1177     return integerize_array($row);
  1115     return integerize_array($row);
  1178   }
  1116   }
  1179   
  1117   
  1180   function numrows($r = false) {
  1118   function numrows($r = false) {
  1181     $this->enable_errorhandler();
       
  1182     if(!$r) $r = $this->latest_result;
  1119     if(!$r) $r = $this->latest_result;
  1183     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
  1120     if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.');
  1184     $n = pg_num_rows($r);
  1121     $n = pg_num_rows($r);
  1185     $this->disable_errorhandler();
       
  1186     return $n;
  1122     return $n;
  1187   }
  1123   }
  1188   
  1124   
  1189   function escape($str)
  1125   function escape($str)
  1190   {
  1126   {
  1191     $this->enable_errorhandler();
       
  1192     $str = pg_escape_string($str);
  1127     $str = pg_escape_string($str);
  1193     $this->disable_errorhandler();
       
  1194     return $str;
  1128     return $str;
  1195   }
  1129   }
  1196   
  1130   
  1197   function free_result($result = false)
  1131   function free_result($result = false)
  1198   {
  1132   {
  1199     $this->enable_errorhandler();
       
  1200     if(!$result)
  1133     if(!$result)
  1201       $result = $this->latest_result;
  1134       $result = $this->latest_result;
  1202     if(!$result)
  1135     if(!$result)
  1203     {
  1136     {
  1204       $this->disable_errorhandler();
       
  1205       return null;
  1137       return null;
  1206     }
  1138     }
  1207     @pg_free_result($result);
  1139     @pg_free_result($result);
  1208     $this->disable_errorhandler();
       
  1209     return null;
  1140     return null;
  1210   }
  1141   }
  1211   
  1142   
  1212   function close() {
  1143   function close() {
  1213     @pg_close($this->_conn);
  1144     @pg_close($this->_conn);