Source for file abstractdb_mysql.php

Documentation is available at abstractdb_mysql.php

  1. <?php
  2. /**
  3. * AbstractDB MySQL Driver
  4. *
  5. * @package AbstractDB
  6. * @author Pacific-Cybersoft
  7. * @copyright (C) 2005 Pacific-Cybersoft. All Rights Reserved.
  8. * @version v 1.0.2
  9. * @license http://www.gnu.org/copyleft/lesser.txt GNU Lesser General Public License
  10. */
  11.  
  12. /**
  13. * MySQL Driver included constant.
  14. *
  15. * Flag that indicates that the MySQL driver has been included.
  16. * @access public
  17. */
  18. define("ABSTRACTDB_MYSQL_INCLUDED", true);
  19.  
  20. /**
  21. * AbstractDB MySQL Driver Class
  22. * @access public
  23. * @package AbstractDB
  24. */
  25. class AbstractDB_MySQL extends AbstractDB_Driver
  26. {
  27. /**
  28. * AbstractDB MySQL Driver Constructor
  29. *
  30. * Initilises an instance of the AbstractDB MySQL Driver class.
  31. * @internal Calls the {@link AbstractDB_Driver} constructor passing the given parameters and sets
  32. * the values of the Support list.
  33. */
  34. function AbstractDB_MySQL($arguments)
  35. {
  36. $this->AbstractDB_Driver($arguments);
  37. $this->Support["AffectedRows"] =
  38. $this->Support["InsertID"] =
  39. $this->Support["FetchObject"] =
  40. $this->Support["FetchAssoc"] =
  41. $this->Support["DataSeek"] = true;
  42. }
  43. /* PUBLIC FUNCTIONS */
  44. function AffectedRows()
  45. {
  46. parent::AffectedRows();
  47. return @mysql_affected_rows($this->_connection);
  48. }
  49. function Close()
  50. {
  51. if($this->_connection)
  52. {
  53. $result = @mysql_close($this->_connection);
  54. if(!$result)
  55. {
  56. $error = mysql_error($this->_connection);
  57. if(strlen($error) == 0 && isset($php_errormsg))
  58. $error = $php_errormsg;
  59. else
  60. $error = "The connection could not be closed, reason unknown.";
  61. $this->SetError("Close", $error);
  62. }
  63. return $result;
  64. }
  65. else
  66. {
  67. $this->SetError("Close", "No connection to close.");
  68. return false;
  69. }
  70. }
  71. function DataSeek(&$rs, $row_num)
  72. {
  73. parent::DataSeek($rs, $row_num);
  74. return @mysql_data_seek($rs, $row_num);
  75. }
  76. function FetchAssoc(&$rs)
  77. {
  78. parent::FetchAssoc($rs);
  79. return @mysql_fetch_assoc($rs);
  80. }
  81. function FetchField(&$rs)
  82. {
  83. parent::FetchField($rs);
  84. $result = false;
  85. if($row = $this->FetchRow($rs))
  86. $result = $row[0];
  87. return $result;
  88. }
  89. function FetchObject(&$rs)
  90. {
  91. parent::FetchObject($rs);
  92. return @mysql_fetch_object($rs);
  93. }
  94. function FetchRow(&$rs)
  95. {
  96. parent::FetchRow($rs);
  97. return @mysql_fetch_row($rs);
  98. }
  99. function FieldCount(&$rs)
  100. {
  101. parent::FieldCount($rs);
  102. return @mysql_num_fields($rs);
  103. }
  104. function FieldNames(&$rs, &$fields)
  105. {
  106. parent::FieldNames($rs, $fields);
  107. for($index = 0; $index < $this->FieldCount($rs); $index++)
  108. {
  109. $fields[] = @mysql_field_name($rs, $index);
  110. if($error = mysql_error($this->_connection))
  111. {
  112. $this->SetError("FieldNames", $error);
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. function FreeResult(&$rs)
  119. {
  120. $result = @mysql_free_result($rs);
  121. if(!$result)
  122. {
  123. $error = mysql_error($this->_connection);
  124. if(strlen($error) == 0 && isset($php_errormsg))
  125. $error = $php_errormsg;
  126. else
  127. $error = "Could not free resource handle, reason unknown.";
  128. $this->SetError("FreeResult", $error);
  129. }
  130. return $result;
  131. }
  132. function InsertID()
  133. {
  134. parent::InsertID();
  135. $result = @mysql_insert_id($this->_connection);
  136. if($result == 0)
  137. return -1;
  138. else
  139. return $result;
  140. }
  141. function Query($sql)
  142. {
  143. if(!$this->Connect())
  144. return false;
  145. if(!(@mysql_select_db($this->_arguments["Database"], $this->_connection) && $rs = @mysql_query($sql, $this->_connection)))
  146. {
  147. $this->SetError("Query", @mysql_error($this->_connection));
  148. return false;
  149. }
  150. return $rs;
  151. }
  152. function Replace($table, $fields)
  153. {
  154. parent::Replace($table, $fields);
  155. for($keys = 0, $query = $values = "", reset($fields), $field = 0; $field < count($fields); next($fields), $field++)
  156. {
  157. $fieldname = key($fields);
  158. if($field > 0)
  159. {
  160. $query .= ", ";
  161. $values .= ", ";
  162. }
  163. $query .= $fieldname;
  164. if(isset($fields[$fieldname]["Null"]) && $fields[$fieldname]["Null"])
  165. $value = "NULL";
  166. else
  167. {
  168. if(!isset($fields[$fieldname]["Value"]))
  169. {
  170. $this->SetError("Replace", "Value was not set for field $fieldname.");
  171. return false;
  172. }
  173. switch(isset($fields[$fieldname]["Type"]) ? $fields[$fieldname]["Type"] : "text")
  174. {
  175. case "text":
  176. $value = "'" . str_replace("'", "\'", $fields[$fieldname]["Value"]) . "'";
  177. break;
  178. case "boolean":
  179. $value = ($fields[$fieldname]["Value"]) ? "1" : "0";
  180. break;
  181. case "numeric":
  182. $value = strval($fields[$fieldname]["Value"]);
  183. break;
  184. default:
  185. $this->SetError("Replace", "Type was not specified for field $fieldname.");
  186. return false;
  187. }
  188. }
  189. $values .= $value;
  190. if(isset($fields[$fieldname]["Key"]) && $fields[$fieldname]["Key"])
  191. {
  192. if($value == "NULL")
  193. {
  194. $this->SetError("Replace", "Key values can not be NULL.");
  195. return false;
  196. }
  197. $keys++;
  198. }
  199. }
  200. if($keys == 0)
  201. {
  202. $this->SetError("Replace", "No Key fields were specified.");
  203. return false;
  204. }
  205. return $this->Query("REPLACE INTO $table ($query) VALUES($values)");
  206. }
  207. function RowCount(&$rs)
  208. {
  209. parent::RowCount($rs);
  210. return @mysql_num_rows($rs);
  211. }
  212. function SetDatabase($dbName)
  213. {
  214. if(!$this->Connect())
  215. return false;
  216. if(!($result = @mysql_select_db($dbName, $this->_connection)))
  217. $this->SetError("SetDatabase", mysql_error($this->_connection));
  218. else
  219. {
  220. $result = $this->_arguments["Database"];
  221. $this->_arguments["Database"] = $dbName;
  222. }
  223. return $result;
  224. }
  225. /* PRIVATE FUNCTIONS */
  226. function Connect()
  227. {
  228. if($this->_connection)
  229. return true;
  230. if(isset($this->_arguments["Options"]["Persistent"]) &&
  231. $this->_arguments["Options"]["Persistent"] === true &&
  232. function_exists("mysql_pconnect"))
  233. $connect = "mysql_pconnect";
  234. else
  235. $connect = "mysql_connect";
  236. $port = isset($this->_arguments["Options"]["Port"]) ? ":" . $this->_arguments["Options"]["Port"] : "";
  237. $args = array();
  238. if(isset($this->_arguments["Hostname"]))
  239. {
  240. $server = $this->_arguments["Hostname"] . $port;
  241. $args[] = $server;
  242. if(isset($this->_arguments["Username"]))
  243. {
  244. $args[] = $this->_arguments["Username"];
  245. if(isset($this->_arguments["Password"]))
  246. $args[] = $this->_arguments["Password"];
  247. }
  248. }
  249. switch(count($args))
  250. {
  251. case 1:
  252. $this->_connection = @$connect($args[0]);
  253. break;
  254. case 2:
  255. $this->_connection = @$connect($args[0], $args[1]);
  256. break;
  257. case 3:
  258. $this->_connection = @$connect($args[0], $args[1], $args[2]);
  259. break;
  260. }
  261. if(!$this->_connection)
  262. {
  263. $this->SetError("Connect", isset($php_errormsg) ? $php_errormsg : "Could not connect to MySQL server");
  264. return false;
  265. }
  266. return true;
  267. }
  268. }
  269. ?>

Documentation generated on Wed, 16 Mar 2005 23:17:19 +1100 by phpDocumentor 1.3.0RC3