PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/adapters/BaseAdapter.php

http://github.com/ihumanable/prosper-lib
PHP | 342 lines | 121 code | 49 blank | 172 comment | 6 complexity | 2360dc1d9b93c8bf08b8bcf575b43299 MD5 | raw file
Possible License(s): Unlicense, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Prosper
  4. */
  5. namespace Prosper;
  6. /**
  7. * Base Adapter all SQL adapters are based off of
  8. */
  9. abstract class BaseAdapter {
  10. static protected $connection;
  11. protected $username;
  12. protected $password;
  13. protected $hostname;
  14. protected $schema;
  15. /**
  16. * Creates a new adapter instance
  17. * @param string $username Database username
  18. * @param string $password Database password
  19. * @param string $hostname Database hostname
  20. * @param string $schema Database schema
  21. * @param bool $lazy [optional] Lazy loading, defaults to true
  22. * @return New Adapter Instance
  23. */
  24. function __construct($username, $password, $hostname, $schema, $lazy = true) {
  25. $this->username = $username;
  26. $this->password = $password;
  27. $this->hostname = $hostname;
  28. $this->schema = $schema;
  29. if(!$lazy) {
  30. $this->connection();
  31. }
  32. }
  33. /**
  34. * Destroys an adapter instance
  35. */
  36. function __destruct() {
  37. //TODO: Figure out if we should close the connection here, I don't think so
  38. }
  39. /**
  40. * Get the connection, lazy loads connection if necessary
  41. * @return mixed $connection The connection to the database
  42. */
  43. function connection() {
  44. if(self::$connection == null) {
  45. self::$connection = $this->connect();
  46. }
  47. return self::$connection;
  48. }
  49. /**
  50. * Create a connection to the database backend
  51. */
  52. abstract function connect();
  53. /**
  54. * Destroy the connection to the database backend
  55. */
  56. abstract function disconnect();
  57. /**
  58. * Quotes a database object, uses the backtick by default
  59. * @param string $str string to quote
  60. * @return quoted string
  61. */
  62. function quote($str) {
  63. return "`$str`";
  64. }
  65. /**
  66. * Escapes a value, adds slashes and surrounds with single quotes
  67. * @param string $str string to escape
  68. * @return string escaped string or placeholder for prepared statement
  69. */
  70. function escape($str) {
  71. return "'" . $this->addslashes($str) . "'";
  72. }
  73. /**
  74. * Wrapper for the adapters add slashes function, defaults to using php's
  75. * addslashes built-in
  76. * @param string $str string to add slashes too
  77. * @return string escaped string
  78. */
  79. function addslashes($str) {
  80. return addslashes($str);
  81. }
  82. /**
  83. * Since various databases support a wide variety of limit syntaxes the
  84. * adapter is responsible for writing the limit syntax.
  85. * @param string $sql sql statment
  86. * @param int $limit how many to limit the result to
  87. * @param int $offset where to start at
  88. * @return sql statement with embedded limit statement
  89. */
  90. function limit($sql, $limit, $offset) {
  91. return $sql . " limit $limit" . ($offset !== 0 ? " offset $offset" : "");
  92. }
  93. /**
  94. * Determine if an adapter supports transactions, defaults to false
  95. * @return bool true if supports transactions, false otherwise
  96. */
  97. function has_transactions() {
  98. return false;
  99. }
  100. /**
  101. * Begins a transaction, if the adapter doesn't support transactions, this is a no-op
  102. */
  103. function begin() {
  104. }
  105. /**
  106. * Commits a transaction, if the adapter doesn't support transactions, this is a no-op
  107. */
  108. function commit() {
  109. }
  110. /**
  111. * Rolls back a transaction, if the adapter doesn't support transactions, this is a no-op
  112. */
  113. function rollback() {
  114. }
  115. /**
  116. * Turns transactions off
  117. * If the adapter doesn't support transactions, this is a no-op
  118. * @see BaseAdapter::begin()
  119. * @see BaseAdapeter::commit()
  120. * @see BaseAdapter::rollback()
  121. */
  122. function end() {
  123. }
  124. /**
  125. * Executes a sql statement, the base implementation drives the query results using
  126. * several derivitive functions
  127. * @see BaseAdapter::platform_execute($sql)
  128. * @see BaseAdapter::affected_rows($set)
  129. * @see BaseAdapter::last_id($set)
  130. * @see BaseAdapter::fetch_assoc($set)
  131. * @param string $sql statement to execute
  132. * @param string $mode [optional] statement type, defaults to Query::SELECT_STMT
  133. * @return mixed Number of rows affected if update or delete, insert id for insert statements, result set for select
  134. */
  135. function execute($sql, $mode = Query::SELECT_STMT) {
  136. $set = $this->platform_execute($sql, $mode);
  137. switch($mode) {
  138. case Query::DELETE_STMT:
  139. $result = $this->affected_rows($set);
  140. break;
  141. case Query::INSERT_STMT:
  142. $result = $this->insert_id($set);
  143. break;
  144. case Query::SELECT_STMT:
  145. $result = array();
  146. if($set) {
  147. while($row = $this->fetch_assoc($set)) {
  148. $result[] = $row;
  149. }
  150. $this->free_result($set);
  151. }
  152. break;
  153. case Query::UPDATE_STMT:
  154. $result = $this->affected_rows($set);
  155. break;
  156. }
  157. return $result;
  158. }
  159. /**
  160. * Platform specific execution wrapper
  161. * @param string $sql SQL to execute
  162. * @param string $mode Query type
  163. * @return mixed Platform specific result set
  164. */
  165. function platform_execute($sql, $mode) {
  166. }
  167. /**
  168. * Get the number of affected rows for a platform specific result object
  169. * @param mixed $set Platform specific result set
  170. * @return int number of rows affected
  171. */
  172. function affected_rows($set) {
  173. }
  174. /**
  175. * Used to rebind the query
  176. * @params array Array of new bindings
  177. * @return nothing
  178. */
  179. function rebind($bindings) {
  180. }
  181. /**
  182. * Retrieve the last insert id for a statement
  183. * @param mixed $set Platform specific result set
  184. * @return int inserted id
  185. */
  186. function insert_id($set) {
  187. }
  188. /**
  189. * Retrieve an associative array from a platform specific result set
  190. * @param mixed $set Platform specific result set
  191. * @return array row as associative array
  192. */
  193. function fetch_assoc($set) {
  194. }
  195. /**
  196. * Do optional memory cleanup, not necessary but will allow for long running scripts
  197. * @param mixed $set Platform specific result set
  198. * @return nothing
  199. */
  200. function free_result($set) {
  201. }
  202. /**
  203. * Retrieve table information from the current schema
  204. * @param array $filter [optional] Filter of table names to return
  205. * @return array Table data
  206. */
  207. function tables($filter = null) {
  208. }
  209. /**
  210. * Translates a platform specific type into a cross platform type
  211. * The inverse of this function is the BaseAdapter::platform_type($type, $length) function
  212. * @param array $platform The platform specific type array from BaseAdapter::type_array($raw)
  213. * @return array A cross platform type array created with BaseAdapter::type_array($raw)
  214. * @see BaseAdapter::platform_type($type, $length)
  215. * @see BaseAdapter::type_array($raw)
  216. */
  217. function cross_type($platform) {
  218. }
  219. /**
  220. * Translates a cross platform type into the best possible platform type
  221. * The inverse of this function is the BaseAdapter::cross_type($type, $length) function
  222. * @param array $cross The cross platform type array from BaseAdapter::type_array($raw)
  223. * @return array A platform specific type created with BaseAdapter::type_array($raw)
  224. * @see BaseAdapter::platform_type($type, $length)
  225. * @see BaseAdapter::type_array($raw)
  226. */
  227. function platform_type($cross) {
  228. }
  229. /**
  230. * Translates a raw data representation to an array of components
  231. * Example:
  232. * type_array('int(11)') => array ( raw => 'int(11)',
  233. * type => 'int' ,
  234. * length => 11 )
  235. *
  236. * type_array('timestamp') => array ( raw => 'timestamp',
  237. * type => 'timestamp',
  238. * length => null )
  239. * @param string $raw The raw string with the length
  240. * @param string $split [optional] The character that indicates a length is coming up, defaults to '('
  241. * @return array A type array
  242. */
  243. function type_array($raw, $split = '(') {
  244. $result['raw'] = $raw;
  245. if(strpos($raw, $split) !== false) {
  246. $parts = explode($split, $raw);
  247. $result['type'] = $parts[0];
  248. $result['length'] = substr($parts[1], 0, -1);
  249. } else {
  250. $result['type'] = $raw;
  251. $result['length'] = null;
  252. }
  253. return $result;
  254. }
  255. /**
  256. * Return database specific timestamp from unix timestamp
  257. * Default implementation is the sensible MySQL Timestamp
  258. */
  259. function timestamp($timestamp) {
  260. return date ("Y-m-d H:i:s", $timestamp);
  261. }
  262. /**
  263. * Return unix timestamp from database specific timestamp
  264. * Default implementation is the sensible MySQL Timestamp
  265. */
  266. function mktime($timestamp) {
  267. return strtotime($timestamp);
  268. }
  269. /**
  270. * Platform specific true value
  271. * @return mixed truth value
  272. */
  273. function true_value() {
  274. return "TRUE";
  275. }
  276. /**
  277. * Platform specific false value
  278. * @return mixed false value
  279. */
  280. function false_value() {
  281. return "FALSE";
  282. }
  283. /**
  284. * Platform specific modulus function
  285. * @param string $lhs left hand side snippet
  286. * @param string $rhs right hand side snippet
  287. * @return string modulus snippet
  288. */
  289. function modulus($lhs, $rhs) {
  290. return " ($lhs % $rhs) ";
  291. }
  292. }
  293. ?>