Posted By

coevolve on 03/05/10


Tagged

mysql database sql php mssql Oracle


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

nerdfiles


LoneDatabaseClass


 / Published in: PHP
 

  1. /*
  2.  
  3. LoneDatabaseClass * PHP SQL class for MySQL, Oracle and MS SQL
  4. ref: lonedb ver: 0.5 date:20100305
  5. Copyright (c) 2009 Milan Stankovic
  6. License: MIT, http://www.opensource.org/licenses/mit-license.php
  7.  
  8. ____________________________________________________________________________
  9. todo:
  10. ora: bind and execute always
  11. ora: insert -add sequence name
  12.  
  13. ____________________________________________________________________________
  14. changes:
  15.  
  16. 20100305
  17. stable mssql support
  18. 20100120
  19. mssql support
  20. 20091210
  21. merged my and ora versions
  22.  
  23. ____________________________________________________________________________
  24. public:
  25.  
  26. $con, $rcount, $keys, $mode
  27. select($table, $cols = '*', $where = null, $order = null, $opt = 0)
  28. insert($table, $values, $cols = null, $filename='', $filefield='')
  29. delete($table, $where = null)
  30. update($table, $where, $values, $filename='', $filefield='')
  31. selectAssociative($table, $cols = '*', $where = null, $order = null, $opt = 0)
  32. getResult()
  33. setConnection($host, $user, $pass, $dbname)
  34. setDatabase($dbname)
  35. disconnect()
  36. cleanup($data, $write = false)
  37. errDetails($rsc=null)
  38.  
  39. ____________________________________________________________________________
  40. private:
  41.  
  42. $db_host, $db_user, $db_pass, $db_name, $result, $oracon, $orast
  43. connect()
  44. tableExists($table)
  45. cleanupSlashes($data, $write = false)
  46. ms_ : select(), update(), insert(), delete(), selectAssociative()
  47. my_ : select(), update(), insert(), delete(), selectAssociative()
  48. ora_ : select(), update(), insert(), delete(), selectAssociative(), updateFile(), insertFile()
  49.  
  50. ____________________________________________________________________________
  51. notes:
  52.  
  53. All methods return true if completed successfully and false if an error occurred, except getResult and cleanup.
  54. On setConnection, setDatabase, insert, delete and update, result is empty array, on disconnect result is intact.
  55. For update and insert, filename and filefield used in oracle mode for file handling (bind mode).
  56.  
  57. ____________________________________________________________________________
  58.  
  59. */
  60.  
  61.  
  62. class LoneDatabaseClass {
  63.  
  64. var $db_host = ''; // use setConnection()
  65. var $db_user = '';
  66. var $db_pass = '';
  67. var $db_name = '';
  68. var $result = array(); // use getResult()
  69. var $con = false; // connection active flag
  70. var $rcount = -1;
  71. var $keys = array();
  72. var $oracon = null;
  73. var $orast = null;
  74. var $mode = '';
  75.  
  76.  
  77. function select($table, $cols = '*', $where = null, $order = null, $opt = 0) {
  78. // required: table: table name
  79. // optional: cols: columns requested, comma separated
  80. // where: expresion as a string
  81. // order: 'column DESC' expresion as a string
  82. // opt: oracle options: OCI_BOTH | ( OCI_ASSOC | OCI_NUM ) & OCI_RETURN_NULLS & OCI_RETURN_LOBS
  83.  
  84. if (!$this->tableExists($table)) { return false; }
  85. $q = 'SELECT '.$cols.' FROM '.$table;
  86. if ($where != null) { $q .= ' WHERE '.$where; }
  87. if ($order != null) { $q .= ' ORDER BY '.$order; }
  88. if ($this->mode=='mysql') {
  89. return $this->my_select($q);
  90. }
  91. else if ($this->mode=='mssql') {
  92. return $this->ms_select($q);
  93. }
  94. else if ($this->mode=='oracle') {
  95. return $this->ora_select($q, $opt);
  96. }
  97. return false;
  98. }
  99.  
  100. function insert($table, $values, $cols = null, $filename='', $filefield='') {
  101. // required: table: table name
  102. // values: values as array
  103. // optional: cols: column names, comma separated
  104. // filename, filefield: oracle file handling
  105.  
  106. if (!$this->tableExists($table)) { return false; }
  107. if ($this->mode=='mysql') {
  108. return $this->my_insert($table, $values, $cols);
  109. }
  110. else if ($this->mode=='mssql') {
  111. return $this->ms_insert($table, $values, $cols);
  112. }
  113. else if ($this->mode=='oracle') {
  114. if ($filename=='') {
  115. return $this->ora_insert($table, $values, $cols);
  116. }
  117. else {
  118. return $this->ora_insertFile($table, $values, $cols, $filename, $filefield);
  119. }
  120. }
  121. return false;
  122. }
  123.  
  124. function update($table, $where, $values, $filename='', $filefield='') {
  125. // required: table: tabel name
  126. // where: expresion as a string
  127. // values: values as array, column names as keys
  128. // filename, filefield: oracle file handling
  129.  
  130. if (!$this->tableExists($table)) { return false; }
  131. if ($where=='' || empty($values)) { return false; }
  132. if ($this->mode=='mysql') {
  133. return $this->my_update($table, $where, $values);
  134. }
  135. else if ($this->mode=='mssql') {
  136. return $this->ms_update($table, $where, $values);
  137. }
  138. if ($this->mode=='oracle') {
  139. if ($filename=='') {
  140. return $this->ora_update($table, $where, $values);
  141. }
  142. else {
  143. return $this->ora_updateFile($table, $where, $values, $filename, $filefield);
  144. }
  145. }
  146. return false;
  147. }
  148.  
  149. function delete($table, $where = null) {
  150.  
  151. if (!$this->tableExists($table)) { return false; }
  152. if ($where == null) { return false; }
  153. if ($this->mode=='mysql') {
  154. return $this->my_delete($table, $where);
  155. }
  156. else if ($this->mode=='mssql') {
  157. return $this->ms_delete($table, $where);
  158. }
  159. else if ($this->mode=='oracle') {
  160. return $this->ora_delete($table, $where);
  161. }
  162. return false;
  163. }
  164.  
  165. function selectAssociative($table, $keycolumn, $cols = '*', $where = null, $order = null, $opt = 0) {
  166.  
  167. if (!$this->tableExists($table)) { return false; }
  168. $q = 'SELECT '.$cols.' FROM '.$table;
  169. if ($where != null) { $q .= ' WHERE '.$where; }
  170. if ($order != null) { $q .= ' ORDER BY '.$order; }
  171. if ($this->mode=='mysql') {
  172. return $this->my_selectAssociative($q, $keycolumn);
  173. }
  174. else if ($this->mode=='oracle') {
  175. return $this->ora_selectAssociative($q, $keycolumn, $opt);
  176. }
  177. else if ($this->mode=='mssql') {
  178. return $this->ms_selectAssociative($q, $keycolumn, $table);
  179. }
  180. return false;
  181. }
  182.  
  183.  
  184. // ____________________________________________________________________________
  185.  
  186. function ms_select($q) { // mssql select
  187.  
  188. //echo $q;
  189.  
  190. $query = mssql_query($q);
  191. if (!$query) { return false; }
  192.  
  193. $this->rcount = mssql_num_rows($query);
  194. if ($this->rcount < 1) { return true; }
  195.  
  196. for ($i = 0; $i < $this->rcount; $i++) {
  197. $r = mssql_fetch_array($query, MSSQL_ASSOC);
  198. $this->keys = array_keys($r);
  199. $kcount = count($this->keys);
  200. for ($x = 0; $x < $kcount; $x++) {
  201.  
  202. //$v= iconv('UCS-2LE', 'UTF-8//IGNORE', $r[$this->keys[$x]]);
  203.  
  204. $v = $r[$this->keys[$x]];
  205.  
  206. if($this->rcount > 1) { $this->result[$i][$this->keys[$x]] = $v; }
  207. else { $this->result[$this->keys[$x]] = $r[$this->keys[$x]]; }
  208. }
  209. }
  210. return true;
  211. }
  212.  
  213. function ms_update($table, $where, $values) { // mssql update
  214.  
  215. $keys = array_keys($values);
  216. $vcount = count($values);
  217. $q = '';
  218. for ($i = 0; $i < $vcount; $i++) {
  219. if ($q!=''){ $q .= ','; }
  220. $q .= $keys[$i].'='.$values[$keys[$i]];
  221. }
  222. $q = 'UPDATE '.$table.' SET '.$q;
  223. $q .= ' WHERE '.$where;
  224. $ok = mssql_query($q);
  225. return $ok;
  226. }
  227.  
  228. function ms_insert($table, $values, $cols) { // mssql insert
  229.  
  230. $q = 'INSERT INTO '.$table;
  231. if ($cols != null) { $q .= ' ('.$cols.')'; }
  232. $values = implode(',',$values);
  233. $q .= ' VALUES ('.$values.')';
  234. $ok = mssql_query($q);
  235. return $ok;
  236. }
  237.  
  238.  
  239. function ms_selectAssociative($q, $keycolumn, $table) { // mssql select associative
  240.  
  241. $query = mssql_query($q);
  242. if (!$query) { return false;}
  243. $this->rcount = mssql_num_rows($query);
  244. if ($this->rcount < 1) { return true; }
  245. $doconv = $this->columnsToConvert($table);
  246. for ($i = 0; $i < $this->rcount; $i++) {
  247. $r = mssql_fetch_array($query, MSSQL_ASSOC);
  248. $this->keys = array_keys($r);
  249. if (!in_array($keycolumn, $this->keys)) { return false; }
  250. $kcount = count($this->keys);
  251. for ($x = 0; $x < $kcount; $x++) {
  252. if ($doconv[$this->keys[$x]]) {
  253. $v= iconv('UCS-2LE', 'UTF-8//IGNORE', $r[$this->keys[$x]]);
  254. }
  255. else {
  256. $v = $r[$this->keys[$x]];
  257. }
  258. $this->result[$r[$keycolumn]][$this->keys[$x]] = $v;
  259. }
  260. }
  261. return true;
  262. }
  263.  
  264. function ms_delete($table, $where) { // mssql delete
  265.  
  266. $q = 'DELETE FROM '.$table.' WHERE '.$where;
  267. $ok = mssql_query($q);
  268. return $ok;
  269. }
  270.  
  271. // ____________________________________________________________________________
  272.  
  273.  
  274. function my_select($q) { // mysql select
  275.  
  276. $query = mysql_query('SET CHARACTER SET utf8;');
  277. $query = mysql_query($q);
  278. if (!$query) { return false;}
  279. $this->rcount = mysql_num_rows($query);
  280. if ($this->rcount < 1) { return true; }
  281. for ($i = 0; $i < $this->rcount; $i++) {
  282. $r = mysql_fetch_array($query);
  283. $this->keys = array_keys($r);
  284. $kcount = count($this->keys);
  285. for ($x = 0; $x < $kcount; $x++) {
  286. if (!is_int($this->keys[$x])) { // Sanitizes keys so only alphavalues are allowed
  287. if($this->rcount > 1) { $this->result[$i][$this->keys[$x]] = $r[$this->keys[$x]]; }
  288. else { $this->result[$this->keys[$x]] = $r[$this->keys[$x]]; }
  289. }
  290. else {
  291. unset($this->keys[$x]);
  292. }
  293. }
  294. }
  295. return true;
  296. }
  297.  
  298. function my_update($table, $where, $values) { // mysql update
  299.  
  300. $keys = array_keys($values);
  301. $vcount = count($values);
  302. $q = '';
  303. for ($i = 0; $i < $vcount; $i++) {
  304. if ($q==''){ $q = $keys[$i].'='.$values[$keys[$i]]; }
  305. else { $q .= ','.$keys[$i].'='.$values[$keys[$i]]; }
  306. }
  307. $q = 'UPDATE '.$table.' SET '.$q;
  308. $q .= ' WHERE '.$where;
  309. $query = mysql_query('SET CHARACTER SET utf8;');
  310. $query = mysql_query($q);
  311. if ($query) { return true; }
  312. else { return false; }
  313. }
  314.  
  315. function my_insert($table, $values, $cols) { // mysql insert
  316.  
  317. $q = 'INSERT INTO '.$table;
  318. if ($cols != null) { $q .= ' ('.$cols.')'; }
  319. $values = implode(',',$values);
  320. $q .= ' VALUES ('.$values.')';
  321. $ok = mysql_query('SET CHARACTER SET utf8;');
  322. if ($ok) { $ok = mysql_query($q); }
  323. return $ok;
  324. }
  325.  
  326. function my_selectAssociative($q, $keycolumn) { // mysql select associative
  327.  
  328. $query = mysql_query('SET CHARACTER SET utf8;');
  329. $query = mysql_query($q);
  330. if (!$query) { return false;}
  331. $this->rcount = mysql_num_rows($query);
  332. if ($this->rcount < 1) { return true; }
  333. for ($i = 0; $i < $this->rcount; $i++) {
  334. $r = mysql_fetch_array($query);
  335. $this->keys = array_keys($r);
  336. if (!in_array($keycolumn, $this->keys)) { return false; }
  337. $kcount = count($this->keys);
  338. for ($x = 0; $x < $kcount; $x++) {
  339. if (!is_int($this->keys[$x])) { // Sanitizes keys so only alphavalues are allowed
  340. $this->result[$r[$keycolumn]][$this->keys[$x]] = $r[$this->keys[$x]];
  341. }
  342. else {
  343. unset($this->keys[$x]);
  344. }
  345. }
  346. }
  347. return true;
  348. }
  349.  
  350. function my_delete($table, $where) { // mysql delete
  351.  
  352. $q = 'DELETE FROM '.$table.' WHERE '.$where;
  353. $ok = mysql_query('SET CHARACTER SET utf8;');
  354. if ($ok) { $ok = mysql_query($q); }
  355. return $ok;
  356. }
  357.  
  358. // ____________________________________________________________________________
  359.  
  360.  
  361. function ora_select($q, $opt) { // oracle select
  362.  
  363. $this->orast = oci_parse($this->oracon, $q);
  364. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  365. if (!oci_execute($this->orast)) { $this->errDetails($this->orast); return false; }
  366. $oramode = OCI_ASSOC+OCI_RETURN_NULLS+OCI_RETURN_LOBS;
  367. if ($opt!=0) { $oramode = $opt; }
  368. $this->rcount = 0;
  369. while ($r = oci_fetch_array($this->orast, $oramode)) {
  370. if (empty($this->keys)) {
  371. $this->keys = array_keys($r);
  372. $kcount = count($this->keys);
  373. }
  374. for ($x = 0; $x < $kcount; $x++) {
  375. if (is_int($this->keys[$x])) { return false; } // only alphavalues are allowed, OCI_ASSOC
  376. $this->result[$this->rcount][$this->keys[$x]] = $r[$this->keys[$x]];
  377. }
  378. $this->rcount++;
  379. }
  380. if($this->rcount == 1) { // columns as keys on one
  381. $t = $this->result[0];
  382. $this->result = array();
  383. for ($x = 0; $x < $kcount; $x++) {
  384. $this->result[$this->keys[$x]]=$t[$this->keys[$x]];
  385. }
  386. }
  387. return true;
  388. }
  389.  
  390. function ora_update($table, $where, $values) { // oracle update
  391.  
  392. $keys = array_keys($values);
  393. $vcount = count($values);
  394. $q = '';
  395. for ($i = 0; $i < $vcount; $i++) {
  396. if ($q==''){ $q = $keys[$i].'='.$values[$keys[$i]]; }
  397. else { $q .= ','.$keys[$i].'='.$values[$keys[$i]]; }
  398. }
  399. $q = 'UPDATE '.$table.' SET '.$q;
  400. $q .= ' WHERE '.$where;
  401. $this->orast = oci_parse($this->oracon, $q);
  402. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  403. if (!oci_execute($this->orast)) { $this->errDetails($this->orast); return false; }
  404. return true;
  405. }
  406.  
  407. function ora_updateFile($table, $where, $values, $filename, $filefield) { // oracle update file
  408.  
  409. $keys = array_keys($values);
  410. $vcount = count($values);
  411. $q = '';
  412. for ($i = 0; $i < $vcount; $i++) {
  413. $pair = $keys[$i].'= '.$values[$keys[$i]];
  414. if ($keys[$i]==$filefield) { $pair=$filefield.'= empty_blob()'; }
  415. if ($q==''){ $q = $pair; }
  416. else { $q .= ','.$pair; }
  417. }
  418. $q = 'UPDATE '.$table.' SET '.$q;
  419. $q .= ' WHERE '.$where;
  420. $q .= ' returning '.$filefield.' into :blobdata';
  421. $this->orast = oci_parse($this->oracon, $q);
  422. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  423. $lob = oci_new_descriptor($this->oracon, OCI_D_LOB);
  424. oci_bind_by_name($this->orast, ':blobdata', $lob, -1, OCI_B_BLOB);
  425. if (!oci_execute($this->orast, OCI_DEFAULT)) { $this->errDetails($this->orast); return false; }
  426. $lob->import($filename);
  427. oci_commit($this->oracon);
  428. $lob->close();
  429. return true;
  430. }
  431.  
  432. function ora_insert($table, $values, $cols) { // oracle insert
  433. global $oraseq;
  434.  
  435. $q = 'INSERT INTO '.$table;
  436. if ($cols != null) { $q .= ' (ID,'.$cols.')'; }
  437. $values = implode(',',$values);
  438. $q .= ' VALUES ('.$oraseq.'.NextVal, '.$values.')';
  439. $this->orast = oci_parse($this->oracon, $q);
  440. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  441. if (!oci_execute($this->orast)) { $this->errDetails($this->orast); return false; }
  442. return true;
  443. }
  444.  
  445. function ora_insertFile($table, $values, $cols, $filename, $filefield) { // oracle insert file
  446. global $oraseq;
  447.  
  448. $keys = array_keys($values);
  449. $vcount = count($values);
  450. $q = 'INSERT INTO '.$table;
  451. if ($cols != null) { $q .= ' (ID,'.$cols.')'; }
  452. $q .= ' VALUES ('.$oraseq.'.NextVal';
  453. for ($i = 0; $i < $vcount; $i++) {
  454. if ($keys[$i]==$filefield) { $q .= ',empty_blob()'; }
  455. else {$q .= ','.$values[$keys[$i]]; }
  456. }
  457. $q .= ') returning '.$filefield.' into :blobdata';
  458. $this->orast = oci_parse($this->oracon, $q);
  459. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  460. $lob = oci_new_descriptor($this->oracon, OCI_D_LOB);
  461. oci_bind_by_name($this->orast, ':blobdata', $lob, -1, OCI_B_BLOB);
  462. if (!oci_execute($this->orast, OCI_DEFAULT)) { $this->errDetails($this->orast); return false; }
  463. $lob->import($filename);
  464. oci_commit($this->oracon);
  465. $lob->close();
  466. return true;
  467. }
  468.  
  469. function ora_selectAssociative($q, $keycolumn, $opt) { // oracle select associative
  470.  
  471. $this->orast = oci_parse($this->oracon, $q);
  472. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  473. if (!oci_execute($this->orast)) { $this->errDetails($this->orast); return false; }
  474. $oramode = OCI_ASSOC+OCI_RETURN_NULLS;
  475. if ($opt!=0) { $oramode = $opt; }
  476. $this->rcount = 0;
  477. while ($r = oci_fetch_array($this->orast, $oramode)) {
  478. if (empty($this->keys)) {
  479. $this->keys = array_keys($r);
  480. $kcount = count($this->keys);
  481. if (!in_array($keycolumn, $this->keys)) { return false; }
  482. }
  483. for ($x = 0; $x < $kcount; $x++) {
  484. if (is_int($this->keys[$x])) { return false; } // only alphavalues are allowed, OCI_ASSOC
  485. $this->result[$r[$keycolumn]][$this->keys[$x]] = $r[$this->keys[$x]];
  486. }
  487. $this->rcount++;
  488. }
  489. return true;
  490. }
  491.  
  492. function ora_delete($table, $where) { // oracle delete
  493.  
  494. $q = 'DELETE FROM '.$table.' WHERE '.$where;
  495. $this->orast = oci_parse($this->oracon, $q);
  496. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  497. if (!oci_execute($this->orast)) { $this->errDetails($this->orast); return false; }
  498. return true;
  499. }
  500.  
  501. // ____________________________________________________________________________
  502.  
  503.  
  504. function getResult() {
  505. return $this->result;
  506. }
  507.  
  508. function setConnection($host, $user, $pass, $dbname) {
  509. global $dbmode;
  510.  
  511. $this->mode = $dbmode;
  512. if ($this->con) {
  513. $ok = $this->disconnect();
  514. if(!$ok) { return false; }
  515. }
  516. $this->oracon = null;
  517. $this->orast = null;
  518. $this->con = false;
  519. $this->rcount = -1;
  520. $this->keys = array();
  521. $this->result = array();
  522. $this->db_host = $host;
  523. $this->db_user = $user;
  524. $this->db_pass = $pass;
  525. $this->db_name = $dbname;
  526. $this->result = array();
  527. $ok = $this->connect(); // $this->con==true on success
  528. return $ok;
  529. }
  530.  
  531.  
  532. function setDatabase($dbname) {
  533. // change database
  534. global $dbmode;
  535.  
  536. $this->mode = $dbmode;
  537. if ($this->con) {
  538. $ok = $this->disconnect();
  539. if(!$ok) return false;
  540. }
  541. $this->con = false;
  542. $this->rcount = -1;
  543. $this->keys = array();
  544. $this->result = array();
  545. $this->db_name = $dbname;
  546. $ok = $this->connect(); // $this->con==true on success
  547. return $ok;
  548. }
  549.  
  550. function disconnect() {
  551.  
  552. $ok = false;
  553. if ($this->mode=='mysql') {
  554. $ok = mysql_close();
  555. }
  556. else if ($this->mode=='mssql') {
  557. $ok = mssql_close();
  558. }
  559. else if ($this->mode=='oracle') {
  560. if ($this->orast) { $ok = oci_free_statement($this->orast); }
  561. if ($ok && $this->oracon) { $ok = oci_close($this->oracon); }
  562. }
  563. $this->con = !$ok;
  564. return $ok;
  565. }
  566.  
  567. function connect() {
  568.  
  569. if ($this->db_host=='' || $this->db_user=='' || $this->db_pass=='' || $this->db_name=='') { return false; }
  570.  
  571. $ok = false;
  572. if ($this->mode=='mysql') {
  573. $dbh = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
  574. if ($dbh) { $ok = mysql_select_db($this->db_name, $dbh); }
  575. }
  576. else if ($this->mode=='oracle') {
  577. $this->oracon = oci_connect($this->db_user,$this->db_pass,$this->db_host.'/'.$this->db_name);
  578. if (!$this->oracon) { $this->errDetails(); }
  579. else { $ok = true; }
  580. }
  581. else if ($this->mode=='mssql') {
  582. $dbh = mssql_connect($this->db_host,$this->db_user,$this->db_pass);
  583. if ($dbh) { $ok = mssql_select_db($this->db_name, $dbh); }
  584. }
  585. $this->con = $ok;
  586. return $ok;
  587. }
  588.  
  589. function tableExists($table) {
  590.  
  591. if (!$this->con || $table=='') { return false; }
  592. $this->result = array(); // resets
  593. $this->rcount = -1;
  594. $this->keys = array();
  595.  
  596. $ok = false;
  597.  
  598. if ($this->mode=='mysql') {
  599. $q = 'SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"';
  600. $q = mysql_query($q);
  601. if ($q) {
  602. if (mysql_num_rows($q)==1) { $ok = true; }
  603. }
  604. }
  605. else if ($this->mode=='oracle') {
  606. $q = "select * from all_objects where object_type in ('TABLE','VIEW') and object_name = '$table'";
  607. $this->orast = oci_parse($this->oracon, $q);
  608. if (!$this->orast) { $this->errDetails($this->oracon); }
  609. else if (!oci_execute($this->orast)) { $this->errDetails($this->orast); }
  610. else { $ok = true; }
  611. }
  612. else if ($this->mode=='mssql') {
  613. if (strpos($table, 'INNER JOIN')!=-1) { return true;}
  614. $table = str_replace(array('[',']'), '', $table);
  615. $q = "SELECT name FROM sys.sysobjects WHERE name='$table' AND OBJECTPROPERTY(id,'IsUserTable')=1";
  616. $q = mssql_query($q);
  617. if ($q) {
  618. if (mssql_num_rows($q)==1) { $ok = true; }
  619. }
  620. }
  621. return $ok;
  622. }
  623.  
  624. // ____________________________________________________________________________
  625.  
  626.  
  627. function cleanup($data, $write = false) {
  628.  
  629. if (is_array($data)) {
  630. foreach ($data as $key => $value) {
  631. $data[$key] = $this->cleanupSlashes($value, $write);
  632. }
  633. } else {
  634. $data = $this->cleanupSlashes($data, $write);
  635. }
  636. return $data;
  637. }
  638.  
  639. function cleanupSlashes($data, $write = false) {
  640.  
  641. if (isset($data)) { // preserve null
  642. $data = stripslashes($data);
  643. }
  644. if ($write && $this->mode=='mysql') {
  645. $data = mysql_real_escape_string($data);
  646. }
  647. }
  648. return $data;
  649. }
  650.  
  651. function errDetails($rsc=null) {
  652. global $debug;
  653.  
  654. if ($debug!==true) { return true; }
  655. if ($rsc==null) { $err = oci_error(); }
  656. else { $err = oci_error($rsc); }
  657. echo '<br /><font color="red"><strong>Oracle Error Details for '. $err['code'];
  658. echo '<br />message = ' . $err['message'];
  659. echo '<br />position = ' . $err['offset'];
  660. echo '<br />statement = ' . $err['sqltext'];
  661. echo '</strong></font><br /><br />';
  662. return true;
  663. }
  664.  
  665. // ____________________________________________________________________________
  666. // MS SQL UCS-2 text read/write,
  667. // declare fields in model: convert(varbinary(2*size), fieldname) as fieldname
  668.  
  669. function columnsToConvert($table) {
  670. // result as [ sColname=>bDoConv ]
  671. global $fields;
  672.  
  673. if (isset($fields[$table])) {
  674. $cols = array_keys($fields[$table]);
  675. }
  676. else {
  677. return array('itemkey'=>false, 'value'=>true);
  678. }
  679. $ret = array();
  680. foreach ($cols as $k=>$f) {
  681. if ($fields[$table][$f]['type']=='ntext') {
  682. $ret[$f] = true;
  683. }
  684. else {
  685. $ret[$f] = false;
  686. }
  687. }
  688. return $ret;
  689. }
  690.  
  691. /* not used */
  692.  
  693. function columnsDeclare($table) {
  694. // result as [ sColname=>declaration ]
  695. global $fields;
  696.  
  697. if (isset($fields[$table])) {
  698. $cols = array_keys($fields[$table]);
  699. }
  700. else {
  701. return array('itemkey'=>'itemkey', 'value'=>'convert(varbinary(200), value) as value');
  702. }
  703. $ret = array();
  704. foreach ($cols as $k=>$f) {
  705. $ret[$f] = $fields[$table][$f]['colname'];
  706. }
  707. return $ret;
  708. }
  709.  
  710. function encUCS2($str) {
  711. $ucs2 = iconv('UTF-8', 'UCS-2LE', $str);
  712. // converting UCS-2 string into "binary" hexadecimal form
  713. $arr = unpack('H*hex', $ucs2);
  714. return "0x{$arr['hex']}"; // include it in sql statement without quotes
  715. }
  716.  
  717. // iconv('UCS-2LE', 'UTF-8', $str) to decode
  718.  
  719. function ucs2html($str) {
  720. $str=trim($str);
  721. $len=strlen($str);
  722. $html='';
  723. for($i=0;$i<$len;$i+=2)
  724. $html.='&#'.hexdec(dechex(ord($str[$i+1])).sprintf("%02s",dechex(ord($str[$i])))).';';
  725. return($html);
  726. }
  727.  
  728. function hex2utf($UtfCharInHex) {
  729. $OutputChar = "";
  730. $UtfCharInDec = hexdec($UtfCharInHex);
  731. if($UtfCharInDec < 128) $OutputChar .= chr($UtfCharInDec);
  732. else if($UtfCharInDec < 2048) $OutputChar .= chr(($UtfCharInDec >> 6) + 192) . chr(($UtfCharInDec & 63) + 128);
  733. else if($UtfCharInDec < 65536) $OutputChar .= chr(($UtfCharInDec >> 12) + 224) . chr((($UtfCharInDec >> 6) & 63) + 128) . chr(($UtfCharInDec & 63) + 128);
  734. else if($UtfCharInDec < 2097152) $OutputChar .= chr($UtfCharInDec >> 18 + 240) . chr((($UtfCharInDec >> 12) & 63) + 128) . chr(($UtfCharInDec >> 6) & 63 + 128) . chr($UtfCharInDec & 63 + 128);
  735. return $OutputChar;
  736. }
  737.  
  738. }

Report this snippet  

You need to login to post a comment.