PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ser-0.9.6/serweb-0.9.4/phplib/db_oci8.inc

#
PHP | 325 lines | 227 code | 43 blank | 55 comment | 42 complexity | b21728ced061ba901d2225d2c676d928 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /*
  3. * Oracle/OCI8 accessor based on Session Management for PHP3
  4. *
  5. * (C) Copyright 1999-2000 Stefan Sels phplib@sels.com
  6. *
  7. * based on db_oracle.inc by Luis Francisco Gonzalez Hernandez
  8. * contains metadata() from db_oracle.inc 1.10
  9. *
  10. * $Id: db_oci8.inc,v 1.2 2004/08/09 12:59:35 kozlik Exp $
  11. *
  12. */
  13. class DB_Sql {
  14. var $Debug = 0;
  15. var $sqoe = 1; // sqoe= show query on error
  16. var $Database = "";
  17. var $User = "";
  18. var $Password = "";
  19. var $Link_ID = 0;
  20. var $Record = array();
  21. var $Row;
  22. var $Parse;
  23. var $Error = "";
  24. /* public: constructor */
  25. function DB_Sql($query = "") {
  26. $this->query($query);
  27. }
  28. function connect() {
  29. if ( 0 == $this->Link_ID ) {
  30. if($this->Debug) {
  31. printf("<br>Connecting to $this->Database...<br>\n");
  32. }
  33. $this->Link_ID=OCIplogon
  34. ("$this->User","$this->Password","$this->Database");
  35. if (!$this->Link_ID) {
  36. $this->halt("Link-ID == false " .
  37. "($this->Link_ID), OCILogon failed");
  38. }
  39. if($this->Debug) {
  40. printf("<br>Obtained the Link_ID: $this->Link_ID<br>\n");
  41. }
  42. }
  43. }
  44. function query($Query_String) {
  45. /* No empty queries, please, since PHP4 chokes on them. */
  46. if ($Query_String == "")
  47. /* The empty query string is passed on from the constructor,
  48. * when calling the class without a query, e.g. in situations
  49. * like these: '$db = new DB_Sql_Subclass;'
  50. */
  51. return 0;
  52. $this->connect();
  53. $this->Parse=OCIParse($this->Link_ID,$Query_String);
  54. if(!$this->Parse) {
  55. $this->Error=OCIError($this->Parse);
  56. } else { OCIExecute($this->Parse);
  57. $this->Error=OCIError($this->Parse);
  58. }
  59. $this->Row=0;
  60. if($this->Debug) {
  61. printf("Debug: query = %s<br>\n", $Query_String);
  62. }
  63. if ($this->Error["code"]!=1403 && $this->Error["code"]!=0 && $this->sqoe)
  64. echo "<BR><FONT color=red><B>".$this->Error["message"]."<BR>Query :\"$Query_String\"</B></FONT>";
  65. return $this->Parse;
  66. }
  67. function next_record() {
  68. if(0 == OCIFetchInto($this->Parse,$result,OCI_ASSOC+OCI_RETURN_NULLS)) {
  69. if ($this->Debug) {
  70. printf("<br>ID: %d,Rows: %d<br>\n",
  71. $this->Link_ID,$this->num_rows());
  72. }
  73. $this->Row +=1;
  74. $errno=OCIError($this->Parse);
  75. if(1403 == $errno) { # 1043 means no more records found
  76. $this->Error="";
  77. $this->disconnect();
  78. $stat=0;
  79. } else {
  80. $this->Error=OCIError($this->Parse);
  81. if($this->Debug) {
  82. printf("<br>Error: %s",
  83. $this->Error["message"]);
  84. }
  85. $stat=0;
  86. }
  87. } else {
  88. for($ix=1;$ix<=OCINumcols($this->Parse);$ix++) {
  89. $col=strtoupper(OCIColumnname($this->Parse,$ix));
  90. $colreturn=strtolower($col);
  91. $this->Record[ "$colreturn" ] = $result["$col"];
  92. if($this->Debug) echo"<b>[$col]</b>:".$result["$col"]."<br>\n";
  93. }
  94. $stat=1;
  95. }
  96. return $stat;
  97. }
  98. function seek($pos) {
  99. $this->Row=$pos;
  100. }
  101. function metadata($table,$full=false) {
  102. $count = 0;
  103. $id = 0;
  104. $res = array();
  105. /*
  106. * Due to compatibility problems with Table we changed the behavior
  107. * of metadata();
  108. * depending on $full, metadata returns the following values:
  109. *
  110. * - full is false (default):
  111. * $result[]:
  112. * [0]["table"] table name
  113. * [0]["name"] field name
  114. * [0]["type"] field type
  115. * [0]["len"] field length
  116. * [0]["flags"] field flags ("NOT NULL", "INDEX")
  117. * [0]["format"] precision and scale of number (eg. "10,2") or empty
  118. * [0]["index"] name of index (if has one)
  119. * [0]["chars"] number of chars (if any char-type)
  120. *
  121. * - full is true
  122. * $result[]:
  123. * ["num_fields"] number of metadata records
  124. * [0]["table"] table name
  125. * [0]["name"] field name
  126. * [0]["type"] field type
  127. * [0]["len"] field length
  128. * [0]["flags"] field flags ("NOT NULL", "INDEX")
  129. * [0]["format"] precision and scale of number (eg. "10,2") or empty
  130. * [0]["index"] name of index (if has one)
  131. * [0]["chars"] number of chars (if any char-type)
  132. * ["meta"][field name] index of field named "field name"
  133. * The last one is used, if you have a field name, but no index.
  134. * Test: if (isset($result['meta']['myfield'])) {} ...
  135. */
  136. $this->connect();
  137. ## This is a RIGHT OUTER JOIN: "(+)", if you want to see, what
  138. ## this query results try the following:
  139. ## $table = new Table; $db = new my_DB_Sql; # you have to make
  140. ## # your own class
  141. ## $table->show_results($db->query(see query vvvvvv))
  142. ##
  143. $this->query("SELECT T.table_name,T.column_name,T.data_type,".
  144. "T.data_length,T.data_precision,T.data_scale,T.nullable,".
  145. "T.char_col_decl_length,I.index_name".
  146. " FROM ALL_TAB_COLUMNS T,ALL_IND_COLUMNS I".
  147. " WHERE T.column_name=I.column_name (+)".
  148. " AND T.table_name=I.table_name (+)".
  149. " AND T.table_name=UPPER('$table') ORDER BY T.column_id");
  150. $i=0;
  151. while ($this->next_record()) {
  152. $res[$i]["table"] = $this->Record[table_name];
  153. $res[$i]["name"] = strtolower($this->Record[column_name]);
  154. $res[$i]["type"] = $this->Record[data_type];
  155. $res[$i]["len"] = $this->Record[data_length];
  156. if ($this->Record[index_name]) $res[$i]["flags"] = "INDEX ";
  157. $res[$i]["flags"] .= ( $this->Record[nullable] == 'N') ? '' : 'NOT NULL';
  158. $res[$i]["format"]= (int)$this->Record[data_precision].",".
  159. (int)$this->Record[data_scale];
  160. if ("0,0"==$res[$i]["format"]) $res[$i]["format"]='';
  161. $res[$i]["index"] = $this->Record[index_name];
  162. $res[$i]["chars"] = $this->Record[char_col_decl_length];
  163. if ($full) {
  164. $j=$res[$i]["name"];
  165. $res["meta"][$j] = $i;
  166. $res["meta"][strtoupper($j)] = $i;
  167. }
  168. if ($full) $res["meta"][$res[$i]["name"]] = $i;
  169. $i++;
  170. }
  171. if ($full) $res["num_fields"]=$i;
  172. # $this->disconnect();
  173. return $res;
  174. }
  175. function affected_rows() {
  176. return $this->num_rows();
  177. }
  178. function num_rows() {
  179. return OCIrowcount($this->Parse);
  180. }
  181. function num_fields() {
  182. return OCINumcols($this->Parse);
  183. }
  184. function nf() {
  185. return $this->num_rows();
  186. }
  187. function np() {
  188. print $this->num_rows();
  189. }
  190. function f($Name) {
  191. if (is_object($this->Record[$Name]))
  192. {
  193. return $this->Record[$Name]->load();
  194. } else
  195. {
  196. return $this->Record[$Name];
  197. }
  198. }
  199. function p($Name) {
  200. print $this->f($Name);
  201. }
  202. function nextid($seqname)
  203. {
  204. $this->connect();
  205. $Query_ID=@ociparse($this->Link_ID,"SELECT $seqname.NEXTVAL FROM DUAL");
  206. if(!@ociexecute($Query_ID))
  207. {
  208. $this->Error=@OCIError($Query_ID);
  209. if($this->Error["code"]==2289)
  210. {
  211. $Query_ID=ociparse($this->Link_ID,"CREATE SEQUENCE $seqname");
  212. if(!ociexecute($Query_ID))
  213. {
  214. $this->Error=OCIError($Query_ID);
  215. $this->halt("<BR> nextid() function - unable to create sequence<br>".$this->Error["message"]);
  216. } else
  217. {
  218. $Query_ID=ociparse($this->Link_ID,"SELECT $seqname.NEXTVAL FROM DUAL");
  219. ociexecute($Query_ID);
  220. }
  221. }
  222. }
  223. if (ocifetch($Query_ID))
  224. {
  225. $next_id = ociresult($Query_ID,"NEXTVAL");
  226. } else
  227. {
  228. $next_id = 0;
  229. }
  230. ocifreestatement($Query_ID);
  231. return $next_id;
  232. }
  233. function disconnect() {
  234. if($this->Debug) {
  235. printf("Disconnecting...<br>\n");
  236. }
  237. OCILogoff($this->Link_ID);
  238. }
  239. function halt($msg) {
  240. printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
  241. printf("<b>ORACLE Error</b>: %s<br>\n",
  242. $this->Error["message"]);
  243. die("Session halted.");
  244. }
  245. function lock($table, $mode = "write") {
  246. $this->connect();
  247. if ($mode == "write") {
  248. $Parse=OCIParse($this->Link_ID,"lock table $table in row exclusive mode");
  249. OCIExecute($Parse);
  250. } else {
  251. $result = 1;
  252. }
  253. return $result;
  254. }
  255. function unlock() {
  256. return $this->query("commit");
  257. }
  258. function table_names() {
  259. $this->connect();
  260. $this->query("
  261. SELECT table_name,tablespace_name
  262. FROM user_tables");
  263. $i=0;
  264. while ($this->next_record())
  265. {
  266. $info[$i]["table_name"] =$this->Record["table_name"];
  267. $info[$i]["tablespace_name"]=$this->Record["tablespace_name"];
  268. $i++;
  269. }
  270. return $info;
  271. }
  272. function add_specialcharacters($query)
  273. {
  274. return str_replace("'","''",$query);
  275. }
  276. function split_specialcharacters($query)
  277. {
  278. return str_replace("''","'",$query);
  279. }
  280. }
  281. ?>