A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
5.0 KiB

  1. <?php
  2. namespace SparkLib;
  3. use \SparkLib\Fail;
  4. /**
  5. * A singleton to wrap up database connections for PDO.
  6. * May also be a place to hang some utility methods and
  7. * shortcuts.
  8. *
  9. * http://php.net/manual/en/book.pdo.php
  10. *
  11. * Sample usage:
  12. *
  13. * <code>
  14. * $dbh = \Spark\db::pdo();
  15. * $sth = $dbh->prepare('SELECT * FROM customers WHERE customers_id = :id');
  16. * $sth->execute(array('id' => 81000));
  17. * // FETCH_ASSOC gives us name => value
  18. * print_r($sth->fetch(PDO::FETCH_ASSOC));
  19. * </code>
  20. */
  21. class DB {
  22. /**
  23. * Getters for DB\Literal implementations for various types/values.
  24. */
  25. public static function True () { return new \SparkLib\DB\True; }
  26. public static function False () { return new \SparkLib\DB\False; }
  27. public static function Null () { return new \SparkLib\DB\Null; }
  28. public static function Now () { return new \SparkLib\DB\Now; }
  29. public static function CurrentDate () { return new \SparkLib\DB\CurrentDate; }
  30. public static function Random () { return new \SparkLib\DB\Random(\DB_SERVER_TYPE); }
  31. // FUCK YOU, PHP
  32. public static function DefaultValue () { return new \SparkLib\DB\DefaultValue; }
  33. public static function Field ($field)
  34. {
  35. return new \SparkLib\DB\Field($field);
  36. }
  37. protected static $_instance = null;
  38. /**
  39. * PDO TODO: this should be called something else and handle selecting
  40. * different servers based on queries and and and...
  41. *
  42. * Get a PDO object.
  43. */
  44. public static function pdo ()
  45. {
  46. if (! self::$_instance)
  47. {
  48. // No instance. Get a new one.
  49. self::$_instance = new \PDO(
  50. \DB_SERVER_TYPE . ':host=' . \DB_SERVER . ';dbname=' . \DB_DATABASE,
  51. \DB_SERVER_USERNAME,
  52. \DB_SERVER_PASSWORD,
  53. [\PDO::ATTR_PERSISTENT => \USE_PCONNECT ]
  54. );
  55. // We want exceptions on failures.
  56. self::$_instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  57. self::$_instance->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_TO_STRING);
  58. if (\DB_SERVER_TYPE === 'mysql')
  59. self::$_instance->query("SET sql_mode='ANSI';");
  60. }
  61. return self::$_instance;
  62. }
  63. /**
  64. * place_holders
  65. *
  66. * gets the supplied number of PDO place holders
  67. * as a string to use with prepared queries
  68. *
  69. * @param int number of place holders to create
  70. * @access public
  71. * @return string
  72. *
  73. */
  74. public static function place_holders ($number = 1)
  75. {
  76. $arguments = array();
  77. for($i=0; $i<$number; $i++) {
  78. $arguments[] = '?';
  79. }
  80. return implode(',', $arguments);
  81. }
  82. public static function getInstance ()
  83. {
  84. return static::pdo();
  85. }
  86. /**
  87. * Convenience method to execute a query and return the results
  88. *
  89. * @param string $sql: sql statement. Can contain parameter placeholders
  90. * @param array $params: optional array of parameters that match the
  91. * placeholders in $sql
  92. * @return array of query results
  93. */
  94. public static function fetchAll ($sql, $params = [], $style = \PDO::FETCH_ASSOC)
  95. {
  96. $dbh = self::pdo();
  97. $sth = $dbh->prepare($sql);
  98. $sth->execute($params);
  99. return $sth->fetchAll($style);
  100. }
  101. /**
  102. * Convenience method to execute a query and return the results
  103. *
  104. * @param string $sql: sql statement. Can contain parameter placeholders
  105. * @param array $params: optional array of parameters that match the
  106. * placeholders in $sql
  107. * @param string $style: optional fetch mode. In the form of PDO::FETCH_*
  108. * @return array of query results
  109. */
  110. public static function fetch ($sql, $params = [], $style = \PDO::FETCH_ASSOC)
  111. {
  112. $dbh = self::pdo();
  113. $sth = $dbh->prepare($sql);
  114. $sth->execute($params);
  115. return $sth->fetch($style);
  116. }
  117. /**
  118. * Executes an array of statements
  119. *
  120. * Will catch and throw PDO errors
  121. *
  122. * All statments are executed with the same database connection.
  123. *
  124. * @param $sql_array Array of sql statements.
  125. */
  126. public static function exec_statements ($sql_array)
  127. {
  128. $dbh = self::pdo();
  129. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  130. foreach ($sql_array as $stmt) {
  131. try {
  132. $dbh->exec($stmt);
  133. }
  134. catch (PDOException $e) {
  135. Fail::log($e);
  136. }
  137. }
  138. }
  139. /**
  140. * Convenience method for PDO::inTransaction().
  141. *
  142. * Indicates whether or not there is an active transaction.
  143. *
  144. * @return true if there is a transaction, false otherwise.
  145. */
  146. public static function inTransaction () {
  147. return self::pdo()->inTransaction();
  148. }
  149. /**
  150. * Convenience method for PDO::beginTransaction().
  151. *
  152. * Starts a database transaction.
  153. *
  154. * @returns nothing
  155. */
  156. public static function begin () {
  157. self::pdo()->beginTransaction();
  158. }
  159. /**
  160. * Convenience method for PDO::commit().
  161. *
  162. * Commit a database transaction.
  163. *
  164. * @returns nothing
  165. */
  166. public static function commit () {
  167. self::pdo()->commit();
  168. }
  169. /**
  170. * Convenience method for PDO::rollBack().
  171. *
  172. * Rollback a database transaction.
  173. *
  174. * @returns nothing
  175. */
  176. public static function rollback () {
  177. self::pdo()->rollBack();
  178. }
  179. }