Просмотр файла includes/db/db2.php

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