PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/classes/softenDbs.class.php

https://github.com/legendar/soften
PHP | 328 lines | 250 code | 65 blank | 13 comment | 23 complexity | 5b1d666f5cd741fa9ffb98cb5dafd39d MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. class softenDbs {
  3. private $db;
  4. private $conf;
  5. private $dsn;
  6. private $order;
  7. private $data;
  8. function __construct() {
  9. require_once('DB.php');
  10. $this->emptyData();
  11. }
  12. function setConf($conf) {
  13. if(!isset($conf["type"])) $conf["type"] = "mysql";
  14. if(!isset($conf["host"])) $conf["host"] = "localhost";
  15. if(!isset($conf["user"])) $conf["user"] = "ODBC";
  16. if(!isset($conf["pass"])) $conf["pass"] = "";
  17. if(!isset($conf["base"])) $conf["base"] = "";
  18. if(!isset($conf["port"])) $conf["port"] = "3306";
  19. if(!isset($conf["pref"])) $conf["pref"] = "";
  20. if(!isset($conf["autocommit"])) $conf["autocommit"] = true;
  21. if(!isset($conf["client_flags"])) $conf["client_flags"] = false;
  22. if($conf["type"] == "mysql" && extension_loaded("mysqli")) {
  23. $conf["type"] = "mysqli";
  24. }
  25. $this->conf = $conf;
  26. }
  27. function connect() {
  28. $conf = $this->conf;
  29. //if(!empty($conf["port"])) $conf["port"] = ':'.$conf["port"];
  30. //if(!empty($conf["pass"])) $conf["pass"] = ':'.$conf["pass"];
  31. //$this->dsn = $conf["type"].'://'.$conf["user"].$conf["pass"].'@'.$conf["host"].$conf["port"].'/'.$conf["base"];
  32. $this->dsn = array(
  33. 'phptype' => $conf['type'],
  34. //'dbsyntax' => false,
  35. 'username' => $conf['user'],
  36. 'password' => $conf['pass'],
  37. //'protocol' => false,
  38. /* we get stupid error with connection to mysqli. connection to 'localhost' works? but connection to 'localhost:3396' doesn't %) */
  39. //'hostspec' => $conf['host'] . (($conf['type'] == 'mysqli' && $conf['port'] == ':3306') ? '' : $conf['port']),
  40. 'hostspec' => $conf['host'],
  41. 'port' => $conf['port'],
  42. //'socket' => false,
  43. 'database' => $conf['base'],
  44. 'client_flags' => $conf['client_flags'],
  45. );
  46. $this->db = &DB::connect($this->dsn, false);
  47. if(DB::isError($this->db)) $this->error($this->db,false);
  48. $this->db->setErrorHandling(PEAR_ERROR_CALLBACK, Array($this,"error"));
  49. //Column data indexed by column names
  50. $this->db->setFetchMode(DB_FETCHMODE_ASSOC);
  51. // autocommit set
  52. $this->autoCommit($conf["autocommit"]);
  53. }
  54. function error($obj,$rollback = true) {
  55. if(is_object($this->db) && $rollback) $this->db->rollback();
  56. //error_log($obj->getMessage().": ".$obj->getDebugInfo(), 0);
  57. //die("<pre>".$obj->getMessage()."\n".$obj->getDebugInfo()."</pre>");
  58. throw new Exception($obj->getMessage()."\n".$obj->getDebugInfo());
  59. }
  60. function emptyData() {
  61. $this->order = array();
  62. $this->data = array();
  63. }
  64. function data($name, $str) {
  65. $this->order[] = $name;
  66. $this->data[$name] = $str;
  67. }
  68. function buildQuery() {
  69. $query = '';
  70. foreach($this->order as $v) {
  71. $query = $query . ' ' . $v . ' ' . $this->data[$v];
  72. }
  73. $this->emptyData();
  74. return trim($query);
  75. }
  76. function select($what) {
  77. $this->data('select', is_array($what) ? implode(', ', $what) : $what);
  78. return $this;
  79. }
  80. function from($w) {
  81. $this->data('from', is_array($w) ? implode(', ', $w) : $w);
  82. return $this;
  83. }
  84. function where($what, $d = 'AND') {
  85. if(!is_array($what)) {
  86. $this->data('where', $what);
  87. } else {
  88. $where = array();
  89. foreach($what as $k => $v) {
  90. $where[] = (is_int($k) ? '' : ($k . ' = ')) . $v;
  91. }
  92. $this->data('where', implode(' ' . $d . ' ', $where));
  93. }
  94. return $this;
  95. }
  96. function limit($n) {
  97. $this->data('limit', $n);
  98. return $this;
  99. }
  100. function one() {
  101. return $this->exec("getOne", array($this->buildQuery()));
  102. }
  103. function all() {
  104. return $this->exec("getAll", array($this->buildQuery()));
  105. }
  106. function row() {
  107. return $this->exec("getRow", array($this->buildQuery()));
  108. }
  109. function col() {
  110. return $this->exec("getCol", array($this->buildQuery()));
  111. }
  112. function pref($args) {
  113. $retArr = true;
  114. if(!is_array($args)) {
  115. $args = Array($args);
  116. $retArr = false;
  117. }
  118. foreach($args as &$arg) {
  119. $arg = preg_replace("/\{([^\}]*)\}/is","{$this->conf["pref"]}$1",$arg);
  120. }
  121. if($retArr) return $args;
  122. else return $args[0];
  123. }
  124. function exec($func,$args) {
  125. $args = $this->pref($args);
  126. return call_user_func_array(array($this->db,$func),$args);
  127. }
  128. function affectedRows() {
  129. $args = func_get_args();
  130. return $this->exec("affectedRows",$args);
  131. }
  132. function autoCommit() {
  133. $args = func_get_args();
  134. return $this->exec("autoCommit",$args);
  135. }
  136. function autoExecute() {
  137. $args = func_get_args();
  138. return $this->exec("autoExecute",$args);
  139. }
  140. function autoPrepare() {
  141. $args = func_get_args();
  142. return $this->exec("autoPrepare",$args);
  143. }
  144. function commit() {
  145. $args = func_get_args();
  146. return $this->exec("commit",$args);
  147. }
  148. function createSequence() {
  149. $args = func_get_args();
  150. return $this->exec("createSequence",$args);
  151. }
  152. function disconnect() {
  153. $args = func_get_args();
  154. return $this->db->disconnect();
  155. }
  156. function dropSequence() {
  157. $args = func_get_args();
  158. return $this->exec("dropSequence",$args);
  159. }
  160. function escapeSimple() {
  161. $args = func_get_args();
  162. return $this->exec("escapeSimple",$args);
  163. }
  164. function execute() {
  165. $args = func_get_args();
  166. return $this->exec("execute",$args);
  167. }
  168. function executeMultiple() {
  169. $args = func_get_args();
  170. return $this->exec("executeMultiple",$args);
  171. }
  172. function freePrepared() {
  173. $args = func_get_args();
  174. return $this->exec("freePrepared",$args);
  175. }
  176. function getAll() {
  177. $args = func_get_args();
  178. return $this->exec("getAll",$args);
  179. }
  180. function getAssoc() {
  181. $args = func_get_args();
  182. return $this->exec("getAssoc",$args);
  183. }
  184. function getCol() {
  185. $args = func_get_args();
  186. return $this->exec("getCol",$args);
  187. }
  188. function getListOf() {
  189. $args = func_get_args();
  190. return $this->exec("getListOf",$args);
  191. }
  192. function getOne() {
  193. $args = func_get_args();
  194. return $this->exec("getOne",$args);
  195. }
  196. function getOption() {
  197. $args = func_get_args();
  198. return $this->exec("getOption",$args);
  199. }
  200. function getRow() {
  201. $args = func_get_args();
  202. return $this->exec("getRow",$args);
  203. }
  204. function limitQuery() {
  205. $args = func_get_args();
  206. return $this->exec("limitQuery",$args);
  207. }
  208. function nextId() {
  209. $args = func_get_args();
  210. return $this->exec("nextId",$args);
  211. }
  212. function nextQueryIsManip() {
  213. $args = func_get_args();
  214. $ver = intval(str_replace(".","",$this->db->apiVersion()));
  215. if($ver < 178) return $this->db->raiseError(DB_ERROR_UNSUPPORTED);
  216. return $this->exec("nextQueryIsManip",$args);
  217. }
  218. function prepare() {
  219. $args = func_get_args();
  220. return $this->exec("prepare",$args);
  221. }
  222. function provides() {
  223. $args = func_get_args();
  224. return $this->exec("provides",$args);
  225. }
  226. function query() {
  227. $args = func_get_args();
  228. return $this->exec("query",$args);
  229. }
  230. function quote() {
  231. $args = func_get_args();
  232. // bug in PEAR::DB mysqli driver
  233. if ($this->conf["type"] == "mysqli") {
  234. return $this->exec("quoteSmart",$args);
  235. }
  236. return $this->exec("quote",$args);
  237. }
  238. function quoteIdentifier() {
  239. $args = func_get_args();
  240. return $this->exec("quoteIdentifier",$args);
  241. }
  242. function quoteSmart() {
  243. $args = func_get_args();
  244. return $this->exec("quoteSmart",$args);
  245. }
  246. function rollback() {
  247. $args = func_get_args();
  248. return $this->exec("rollback",$args);
  249. }
  250. function setFetchMode() {
  251. $args = func_get_args();
  252. return $this->exec("setFetchMode",$args);
  253. }
  254. function setOption() {
  255. $args = func_get_args();
  256. return $this->exec("setOption",$args);
  257. }
  258. function tableInfo() {
  259. $args = func_get_args();
  260. return $this->exec("tableInfo",$args);
  261. }
  262. }
  263. ?>