DB.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace PHPixie;
  3. /**
  4. * Database Module for PHPixie
  5. *
  6. * This module allows you to access the database. Currently
  7. * PDO and Mysqli drivers are supported. PDO drivers can access Mysql,
  8. * SQLite and PostgreSQL databases.
  9. *
  10. * @see \PHPixie\DB\Query
  11. * @package DB
  12. */
  13. class DB {
  14. /**
  15. * Pixie Dependancy Container
  16. * @var \PHPixie\Pixie
  17. */
  18. public $pixie;
  19. /**
  20. * Database connection instances
  21. * @var \PHPixie\DB\Connection
  22. */
  23. protected $db_instances = array();
  24. /**
  25. * Initializes the database module
  26. *
  27. * @param \PHPixie\Pixie $pixie Pixie dependency container
  28. */
  29. public function __construct($pixie) {
  30. $this->pixie = $pixie;
  31. }
  32. /**
  33. * Gets an instance of a connection to the database
  34. *
  35. * @param string $config Configuration name of the connection.
  36. * Defaults to 'default'.
  37. * @return \PHPixie\DB\Connection Driver implementation of the Connection class.
  38. */
  39. public function get($config = 'default'){
  40. if (!isset($this->db_instances[$config])) {
  41. $driver = $this->pixie->config->get("db.{$config}.driver");
  42. $driver = "\\PHPixie\\DB\\".$driver."\\Connection";
  43. $this->db_instances[$config] = new $driver($this->pixie, $config);
  44. }
  45. return $this->db_instances[$config];
  46. }
  47. /**
  48. * Builds a query for specified connection.
  49. *
  50. * @param string $type Query type. Available types: select,update,insert,delete,count
  51. * @param string $config Configuration name of the connection.
  52. * Defaults to 'default'.
  53. * @return \PHPixie\DB\Query Driver implementation of the Query class.
  54. */
  55. public function query($type, $config = 'default')
  56. {
  57. return $this->get($config)->query($type);
  58. }
  59. /**
  60. * Gets the id of the last inserted row
  61. *
  62. * @param string $config Configuration name of the connection.
  63. * Defaults to 'default'.
  64. * @return mixed Id of the last inserted row
  65. */
  66. public function insert_id($config = 'default')
  67. {
  68. return $this->get($config)->insert_id();
  69. }
  70. /**
  71. * Gets column names for the specified table
  72. *
  73. * @param string $table Name of the table to get columns from
  74. * @param string $config Configuration name of the connection.
  75. * Defaults to 'default'.
  76. * @return array Array of column names
  77. */
  78. public function list_columns($table, $config = 'default') {
  79. return $this->get($config)->list_columns($table);
  80. }
  81. /**
  82. * Returns an Expression representation of the value.
  83. * Values wrapped inside Expression are not escaped in queries
  84. *
  85. * @param mixed $value Value to be wrapped
  86. * @return \PHPixie\Db\Expression Raw value that will not be escaped during query building
  87. */
  88. public function expr($value) {
  89. return new \PHPixie\DB\Expression($value);
  90. }
  91. /*
  92. * Creates a new query
  93. *
  94. * @param string $driver Database driver name
  95. * @param \PHPixie\DB\Connection $db Database connection
  96. * @param string $type Query type. Available types: select, update, insert, delete, count
  97. * @return \PHPixie\DB\Query
  98. */
  99. public function query_driver($driver, $db, $type) {
  100. $driver = "\\PHPixie\\DB\\".$driver."\\Query";
  101. return new $driver($db, $type);
  102. }
  103. /*
  104. * Creates a new result
  105. *
  106. * @param string $driver Database driver name
  107. * @param object $cursor Datbase result cursor
  108. * @return \PHPixie\DB\Result
  109. */
  110. public function result_driver($driver, $cursor) {
  111. $driver = "\\PHPixie\\DB\\".$driver."\\Result";
  112. return new $driver($cursor);
  113. }
  114. }