rdbms.cls.php
上传用户:jiangbw
上传日期:2022-03-16
资源大小:49k
文件大小:18k
源码类别:

MySQL数据库

开发平台:

Unix_Linux

  1. <?
  2. /*======================================================================*
  3. || #################################################################### ||
  4. || # BUILD UNDER PHP SCRIPTNET 3.2.1 FRAMEWORK ||
  5. || # ---------------------------------------------------------------- # ||
  6. || # Code2Art Open Source Software. All Rights Reserved.  ||
  7. || # This file should be redistributed in whole or significant part.  # ||
  8. || # ------------------ SCRIPTNET IS FREE SOFTWARE ------------------ # ||
  9. || # http://www.code2art.com | http://www.code2art.com/scriptnet      # ||
  10. || # Copyleft by Benediktus Ardian Hersanto, SE       # ||
  11. || # Sorry if this framework is still unavailable for public       # ||
  12. || # because I still searching on my own head.       # ||
  13. || #################################################################### ||
  14. *======================================================================*/
  15. /*======================================================================*
  16. || #################################################################### ||
  17. || DBF to MySQL Bulk Converter 2.0 ||
  18. ||______________________________________________________________________||
  19. || This utility was developed on 2006, since this were used to be on    ||
  20. || my own library. But I think I should share it to you, because I      ||
  21. || need to save my work on Internet (I don't believe on my own HD drive)||
  22. || it always crash and had a terrible bad sectors.  ||
  23. || Thanks to all person who download this utility, I hope you enjoy it. ||
  24. || ||
  25. || Regards, ||
  26. || Benediktus Ardian Hersanto,SE (ardie_b@yahoo.com) ||
  27. /*======================================================================*/
  28. // ---------------------------------------------------------------------//
  29. // Free to use for everyone who understand PHP
  30. // ---------------------------------------------------------------------//
  31. // Last Update : 25.08.2008
  32. // LAST CHANGES
  33. // createNavigator : group by data counting have been replaced by this->Count
  34. // createNavigator : You can add link to other pages with JavaScript Command (18.09.2006)
  35. // createNavigator : You can add link to other pages with string manipulation function ex : base64_encode (09.09.2006)
  36. // Module Dependency
  37. //  = EnvSetup Class available on file init.php or config.php
  38. /*
  39. *@author B. Ardian Hersanto
  40. *@copyright GNU as long as you're friends of mine, just kidding
  41. *@package DBComponents
  42. */
  43. // DATABASE CORE OBJECT / ABSTRACT CLASS //
  44. class RDBMS {
  45.     var $hostname;
  46. var $username;
  47. var $password;
  48. var $database;
  49. var $port;
  50. var $server_type;
  51.     var $debug = false;
  52. var $simulate = false;
  53. var $error = false;
  54. var $FILE,$LINE,$connected;
  55. var $HScroll_Limit=10;
  56. var $SQL_LIMIT = '';
  57. var $return_as_array = false;
  58. var $admin;
  59. var $dba_password;
  60. //Library Properties
  61. var $about = "GNU PHP RDBMS Component for PHP";
  62. var $log_errors = true;
  63. var $log_file = "/logs/dbms.log";
  64. var $proc_log = "/logs/process.log";
  65. var $error_msg = '';
  66. var $error_priority;
  67. var $version = "4.2.06-dev.27.01";
  68. var $ErrorMarkup = '<p><hr size="0" noshade color="#FF0000"><b><font size="2" face="Verdana">DATABASE ERROR LIST :</font></b></p>';
  69. // Page Navigator Template Vars
  70. var $start_page = "#";
  71. var $previous_page = "#";
  72. var $next_page = "#";
  73. var $end_of_page = "#";
  74. var $navigator = "<span class="page_nav_inactive">&nbsp;1&nbsp;</span>";
  75. var $nav_template = '<span>&nbsp;<a class="navigator_text" href="$this->start_page">Home</a></span>&nbsp;|<span>&nbsp;<a class="navigator_text" href="$this->previous_page">Previous</a>&nbsp;</span>|$this->navigator|<span>&nbsp;<a class="navigator_text" href="$this->next_page">Next</a></span>&nbsp;|<span>&nbsp;<a class="navigator_text" href="$this->end_of_page">Last</a></span>';
  76. function RDBMS() 
  77. {
  78. if(get_class($this)=='RDBMS'||!is_subclass_of($this)) {
  79. trigger_error('class RDBMS is an abstract class, cannot instantiate directly',E_USER_ERROR);
  80. }
  81. define('RDBMS_LOADED',true);
  82. }
  83. // Base Function
  84. // Constructor Declaration
  85. function _create() 
  86. {
  87. if(defined('DB_HOST')) $this->hostname = DB_HOST;
  88. if(defined('DB_USER')) $this->username = DB_USER;
  89. if(defined('DB_PASS')) $this->password = DB_PASS;
  90. if(defined('DB_NAME')) $this->database = DB_NAME;
  91. if(defined('DB_PORT')) $this->port = DB_PORT;
  92. if(defined('DB_TYPE')) $this->server_type = strtoupper(DB_TYPE);
  93. $this->log_file = ROOT_DIR.DB_ERROR_LOG; // Put on config.ini.php
  94. $this->FILE = (PHPVERSION<4.10)?basename(getenv('REDIRECT_URL')):basename($_SERVER['PHP_SELF']);
  95. $this->start_page = "#"; $this->previous_page = "#"; $this->next_page = "#"; $this->end_of_page = "#";
  96. eval("$this->navigator = "$this->nav_template";");
  97. }
  98. // Destructor Declaration
  99. function _destroy(){
  100. $this->Close();
  101. }
  102. // Common Functional
  103. function Connect(){}
  104. function TestConnection($dbname,$dbhost,$dbuser,$dbpassword){}
  105. function Select($fields,$table,$exp='',$page='',$limit='',$urlquery=''){}
  106. function Insert($table,$fields,$values){}
  107. function Update($table,$value,$exp=''){}
  108. function Delete($table,$exp=''){}
  109. function Query($command,$return_result=0){}
  110. // Error Tracking
  111. function TrackError() 
  112. {
  113. $this->error = true;
  114. $REMOTE_ADDR = getenv("REMOTE_ADDR");
  115. if($this->log_errors) {
  116. $date = getdate(); $command = (isset($this->command))?$this->command:'none'; $FILE=(empty($this->FILE))?0:$this->FILE; $LINE=(empty($this->LINE))?0:$this->LINE;
  117. error_log("[".$date['weekday']." ".$date['month']." ".$date['mday']." ".$date['hours'].":".$date['minutes'].":".$date['seconds']." ".$date['year']."] [client ".$REMOTE_ADDR."] [SQL Strings : ".$command."] [FILE:".$FILE.", LINE:".$LINE."] [".$this->error_msg."]rn", 3, $this->log_file);
  118. }
  119. $this->ErrorMarkup .= "<table border=0 width="100%" cellspacing=1><tr><td><hr color="#000000" size=1><p><font face="Verdana" size=2><b>FILE :</b> <font color="#FF0000">{$FILE}</font>;<br/><b>LINE :</b> <font color="#FF0000">{$LINE}</font><br>";
  120. $this->ErrorMarkup .= '<br/><b>ERROR MSG :<br/></b><font color="#0000FF">'.addslashes($this->error_msg).'<br/></font><br/><b>QUERY :<br></b><font color="#0000FF">'.$command.'</font></font></td></tr></table><br/>';
  121. global $_ERRORS; 
  122. $_ERRORS .= stripslashes($this->ErrorMarkup);
  123. trigger_error($this->error_msg,E_USER_ERROR);
  124. }
  125. // Miscellaneous Function
  126. function Count($fields,$table,$exp='') {}
  127. function Sum($field,$table,$exp='') {}
  128. function Max($field,$table,$exp='') {}
  129. function Min($field,$table,$exp='') {}
  130. function createNavigator($table,$exp='',$page='',$limit='',$urlquery='',$encode_url_function="",$func_type="php") 
  131. {
  132. // In case if there is no filtering but there was an order by * asc command
  133. if(eregi("group by",$table)) {
  134. $space = explode(" ",$table);
  135. if(count($space)>1) $table = $space[0];
  136. }
  137. // In case there were a field filtering with GROUP BY command
  138. /*if (eregi("group by",$exp)) {
  139. $tmp = $this->Query("SELECT * FROM {$table} WHERE {$exp}");
  140. $this->TotalRecord = (isset($this->RecordCount))?$this->RecordCount:0;
  141. } else*/
  142. $this->TotalRecord = $this->Count("*",$table,$exp);
  143. if($page<2) $page = 1;
  144. $pages = $this->TotalRecord/$limit;
  145. $bottom = $page*$limit;
  146. $upper = $bottom - $limit;
  147. $stat = is_float($pages);
  148. if (!$stat) $this->TotalPage = floor($pages);else $this->TotalPage = ceil($pages);
  149. if($this->TotalPage>$this->HScroll_Limit) {
  150. if($func_type!='php') {
  151. $params = explode("&",substr($urlquery,1,strlen($urlquery)));
  152. if(count($params)>0) {
  153. for($i=0;$i<count($params);$i++) {
  154. $param = explode("=",$params[$i]);
  155. $arrays[] = $param[0].":'{$param[1]}'";
  156. }
  157. $jsdata = "result={".implode(",",$arrays)."}";
  158. }
  159. }
  160. $total_group = ceil($this->TotalPage/$this->HScroll_Limit); /* Count Total Group of number */
  161. $group = ceil($page/$this->HScroll_Limit); /* define current page position in group ex : current page = 18; $this->TotalPage = 20; total_group = 20/10(number/pages); group_start = 18/total_group => this page located at 2nd group ( 11...20 ); */
  162. $group_start = (integer)($group*$this->HScroll_Limit)-($this->HScroll_Limit-1);
  163. $group_end = (($group * $this->HScroll_Limit)>$this->TotalPage)?$this->TotalPage:($group * $this->HScroll_Limit);
  164. //$this->previous_group = ($encode_url_function!='')$this->FILE."?".$encode_url_function("page=".($group_start-1).$urlquery):$this->FILE."?page=".($group_start-1).$urlquery; /* Information for the previous group allocated at arrow link ( < ) */
  165. $this->previous_group = ($func_type=='php')?($this->FILE.(((trim($encode_url_function)!='')&&($urlquery!=''))?"?".$encode_url_function("page=".($group_start-1).$urlquery):"?page=".($group_start-1).$urlquery)):((((trim($encode_url_function)!='')&&($urlquery!=''))?"javascript:".$encode_url_function."({$jsdata},".($group_start-1).")":$this->FILE."?page=".($group_start-1).$urlquery));
  166. $this->next_group = ($func_type=='php')?($this->FILE.(((trim($encode_url_function)!='')&&(strlen($urlquery)>0))?"?".$encode_url_function("page=".($group_end+1).$urlquery):"?page=".($group_end+1).$urlquery)):((((trim($encode_url_function)!='')&&(strlen($urlquery)>0))?"javascript:".$encode_url_function."({$jsdata},".($group_end+1).")":$this->FILE."?page=".($group_end+1).$urlquery)); /* Information for the next group allocated at arrow link ( > ) */
  167. $prev = ($page<=1)?$page:($page-1);
  168. $this->start_page = ($func_type=='php')?($this->FILE.(((trim($encode_url_function)!='')&&($urlquery!=''))?"?".$encode_url_function("page=1".$urlquery):"?page=1".$urlquery)):((((trim($encode_url_function)!='')&&($urlquery!=''))?"javascript:".$encode_url_function."({$jsdata},1)":$this->FILE."?page=1".$urlquery));
  169. $this->previous_page = ($func_type=='php')?($this->FILE.(((trim($encode_url_function)!='')&&($urlquery!=''))?"?".$encode_url_function("page=".$prev.$urlquery):"?page=".$prev.$urlquery)):((((trim($encode_url_function)!='')&&($urlquery!=''))?"javascript:".$encode_url_function."({$jsdata},".$prev.")":$this->FILE."?page=".$prev.$urlquery));
  170. $this->next_page = ($func_type=='php')?((($page+1)>$this->TotalPage)?$this->FILE.(((trim($encode_url_function)!='')&&($urlquery!='')?"?".$encode_url_function("page=".$page.$urlquery):"?page=".$page.$urlquery)):$this->FILE."?".((((trim($encode_url_function)!='')&&($urlquery!=''))?$encode_url_function("page=".($page+1).$urlquery):"page=".($page+1).$urlquery))):((($page+1)>$this->TotalPage)?(((trim($encode_url_function)!='')&&($urlquery!='')?"javascript:".$encode_url_function."({$jsdata},".$page.")":$this->FILE."?page=".$page.$urlquery)):"javascript:".(((trim($encode_url_function)!='')?$encode_url_function."({$jsdata},".($page+1).")":$this->FILE."?page=".($page+1).$urlquery)));
  171. $this->end_of_page = ($func_type=='php')?($this->FILE.(((trim($encode_url_function)!='')&&($urlquery!=''))?"?".$encode_url_function("page=".$this->TotalPage.$urlquery):"?page=".$this->TotalPage.$urlquery)):((((trim($encode_url_function)!='')&&($urlquery!=''))?"javascript:".$encode_url_function."({$jsdata},".$this->TotalPage.")":$this->FILE."?page=".$this->TotalPage.$urlquery));
  172. if(($this->TotalPage>1)&&($group_start>1)) $link[] = "<span>&nbsp;<a class="navigator_arrow" href="{$this->previous_group}"><<</a>&nbsp;</span>";
  173. for($i=$group_start;$i<=$group_end;$i++) $link[] = ($func_type=='php')?(($i==$page)?"<span class="navigator_selected">&nbsp;{$i}&nbsp;</span>":"<span>&nbsp;<a class="navigator_active" href="{$this->FILE}?".((trim($encode_url_function)!='')&&($urlquery!='')?$encode_url_function("page=".$i.$urlquery):"page=".$i.$urlquery)."">{$i}</a>&nbsp;</span>"):(($i==$page)?"<span class="navigator_selected">&nbsp;{$i}&nbsp;</span>":"<span>&nbsp;<a class="navigator_active" href="".((trim($encode_url_function)!='')&&($urlquery!='')?"javascript:".$encode_url_function."({$jsdata},".$i.")":$this->FILE."?page=".$i.$urlquery)."">{$i}</a>&nbsp;</span>");
  174. if(($group_end>1)&&($group_end<$this->TotalPage)) $link[] = "<span>&nbsp;<a class="navigator_arrow" href="{$this->next_group}">>></a>&nbsp;</span>";
  175. $this->navigator = implode("",$link);
  176. $this->navNum = $this->navigator;
  177. eval("$this->navigator = "$this->nav_template";");
  178. } else {
  179. if($this->TotalPage>1) {
  180. if($func_type!='php') {
  181. $params = explode("&",substr($urlquery,1,strlen($urlquery)));
  182. if(count($params)>0) {
  183. for($i=0;$i<count($params);$i++) {
  184. $param = explode("=",$params[$i]);
  185. $arrays[] = $param[0].":'{$param[1]}'";
  186. }
  187. $jsdata = "result={".implode(",",$arrays)."}";
  188. }
  189. }
  190. $this->start_page = ($func_type=='php')?($this->FILE.(((trim($encode_url_function)!='')&&($urlquery!=''))?"?".$encode_url_function("page=1".$urlquery):"?page=1".$urlquery)):(((((trim($encode_url_function)!='')&&($urlquery!=''))?"javascript:".$encode_url_function."({$jsdata},1)":$this->FILE."?page=1".$urlquery)));
  191. $this->previous_page = ($func_type=='php')?(($page>1)?$this->FILE.(((trim($encode_url_function)!='')&&($urlquery!=''))?"?".$encode_url_function("page=".($page-1).$urlquery):"?page=".($page-1).$urlquery):$this->FILE.(((trim($encode_url_function)!='')&&($urlquery!=''))?"?".$encode_url_function("page=1".$urlquery):"?page=1".$urlquery)):(($page>1)?(((trim($encode_url_function)!='')&&($urlquery!=''))?"javascript:".$encode_url_function."({$jsdata},".($page-1).")":"?page=".($page-1).$urlquery):(((trim($encode_url_function)!='')&&($urlquery!=''))?"javascript:".$encode_url_function."({$jsdata},1)":$this->FILE."?page=1".$urlquery));
  192. $this->next_page = ($func_type=='php')?($this->FILE."?".(($page==$this->TotalPage)?(((trim($encode_url_function)!='')&&($urlquery!=''))?$encode_url_function("page=".$page.$urlquery):"?page=".$page.$urlquery):(((trim($encode_url_function)!='')&&($urlquery!=''))?$encode_url_function("page=".($page+1).$urlquery):"page=".($page+1).$urlquery))):("javascript:".(($page==$this->TotalPage)?(((trim($encode_url_function)!='')&&($urlquery!=''))?$encode_url_function."({$jsdata},{$page})":"?page=".$page.$urlquery):(((trim($encode_url_function)!='')&&($urlquery!=''))?$encode_url_function."({$jsdata},".($page+1).")":$this->FILE."page=".($page+1).$urlquery)));
  193. $this->end_of_page = ($func_type=='php')?($this->FILE.(((trim($encode_url_function)!='')&&($urlquery!=''))?"?".$encode_url_function("page=".$this->TotalPage.$urlquery):"?page=".$this->TotalPage.$urlquery)):((((trim($encode_url_function)!='')&&($urlquery!=''))?"javascript:".$encode_url_function."({$jsdata},".$this->TotalPage.")":$this->FILE."?page=".$this->TotalPage.$urlquery));
  194. for($i=1;$i<=$this->TotalPage;$i++) $link[] = ($func_type=='php')?(($i==$page)?"<span class="navigator_selected">&nbsp;{$i}&nbsp;</span>":"<span>&nbsp;<a class="navigator_active" href="{$this->FILE}?".(((trim($encode_url_function)!='')&&($urlquery!=''))?$encode_url_function("page=".$i.$urlquery):"page=".$i.$urlquery)."">{$i}</a>&nbsp;</span>"):(($i==$page)?"<span class="navigator_selected">&nbsp;{$i}&nbsp;</span>":"<span>&nbsp;<a class="navigator_active" href="javascript:".(((trim($encode_url_function)!='')&&($urlquery!=''))?$encode_url_function."({$jsdata},".$i.")":$this->FILE."?page=".$i.$urlquery)."">{$i}</a>&nbsp;</span>");
  195. $this->navigator = implode("",$link);
  196. $this->navNum = implode("",$link);
  197. eval("$this->navigator = "$this->nav_template";");
  198. }
  199. }
  200. if(!isset($this->navNum)) $this->navNum = "<span class="navigator_selected">&nbsp;1&nbsp;</span>";
  201. $this->SQL_LIMIT = " LIMIT {$upper},{$limit}"; // Optional on other SQL Server, this commonly used at MySQL
  202. }
  203. function inherited($command) 
  204. {
  205. RDBMS::$command();
  206. }
  207. function isConnected() 
  208. {
  209. if(isset($this->connected)) return $this->connected; else return false;
  210. }
  211. function trace($line,$file='') 
  212. {
  213. $this->LINE = $line;
  214. if(!empty($file)) $this->FILE = $file;
  215. }
  216. function getServerVersion(){}
  217. function getFieldsFromObject($obj) 
  218. {
  219. if(is_array($obj)) {
  220. if(count($obj)>0) {
  221. return array_keys(get_object_vars($obj[0]));
  222. }
  223. } else if(is_object($obj)) {
  224. return array_keys(get_object_vars($obj));
  225. }
  226. }
  227. function getFieldsFromAssoc($obj,$type='integer') 
  228. {
  229. if(is_array($obj)) {
  230. if(count($obj)>0) {
  231. if(is_array($obj[0]))
  232. $fields = array_keys($obj[0]); else
  233. $fields = array_keys($obj);
  234. for($n=0;$n<count($fields);$n++) {
  235. if($type=='integer') {
  236. if(is_int($fields[$n])) $field[] = $fields[$n];
  237. } else if($type=='string') {
  238. if(is_string($fields[$n])) $field[] = $fields[$n];
  239. }
  240. }
  241. return $field;
  242. }
  243. } else if(is_object($obj)) {
  244. return array();
  245. }
  246. }
  247. function getDataFromObject($obj,$quote='') 
  248. {
  249. if(is_array($obj)) {
  250. if(count($obj)>0) {
  251. if(empty($quote)) {
  252. return array_values(get_object_vars($obj[0]));
  253. } else {
  254. $dat = array_values(get_object_vars($obj[0]));
  255. for($i=0;$i<count($dat);$i++) $dat[$i] = $quote.$dat[$i].$quote;
  256. return $dat;
  257. }
  258. }
  259. } else if(is_object($obj)) {
  260. if(empty($quote)) {
  261. return array_values(get_object_vars($obj));
  262. } else {
  263. $dat = array_values(get_object_vars($obj));
  264. for($i=0;$i<count($dat);$i++) $dat[$i] = $quote.$dat[$i].$quote;
  265. return $dat;
  266. }
  267. }
  268. }
  269. function splitSql($sql)
  270. {
  271.     $sql = preg_replace("/r/s", "n", $sql);
  272.     $sql = preg_replace("/[n]{2,}/s", "n", $sql);
  273.     $lines = explode("n", $sql);
  274.     $queries = array();
  275.     $inQuery = 0;
  276.     $i = 0;
  277.     foreach ($lines as $line) {
  278.         $line = trim($line);
  279.         if (!$inQuery) {
  280.             if (preg_match("/^CREATE/i", $line)) {
  281.                 $inQuery = 1;
  282.                 $queries[$i] = $line;
  283.             }
  284.             elseif (!empty($line) && $line[0] != "#") {
  285.                 $queries[$i] = preg_replace("/;$/i", "", $line);
  286.                 $i++;
  287.             }
  288.         }
  289.         elseif ($inQuery) {
  290.             if (preg_match("/^[)]/", $line)) {
  291.                 $inQuery = 0;
  292.                 $queries[$i] .= preg_replace("/;$/i", "", $line);
  293.                 $i++;
  294.             }
  295.             elseif (!empty($line) && $line[0] != "#") {
  296.                 $queries[$i] .= $line;
  297.             }
  298.         }
  299.     }
  300.     return $queries;
  301. }
  302. }
  303. ?>