connection.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php namespace System\DB;
  2. class Connection {
  3. /**
  4. * The connection name.
  5. *
  6. * @var string
  7. */
  8. public $name;
  9. /**
  10. * The connection configuration.
  11. *
  12. * @var array
  13. */
  14. public $config;
  15. /**
  16. * The Connector instance.
  17. *
  18. * @var Connector
  19. */
  20. public $connector;
  21. /**
  22. * The PDO connection.
  23. *
  24. * @var PDO
  25. */
  26. public $pdo;
  27. /**
  28. * Create a new Connection instance.
  29. *
  30. * @param string $name
  31. * @param object $config
  32. * @param Conector $connector
  33. * @return void
  34. */
  35. public function __construct($name, $config, $connector)
  36. {
  37. $this->name = $name;
  38. $this->config = $config;
  39. $this->connector = $connector;
  40. $this->pdo = $this->connector->connect($this->config);
  41. }
  42. /**
  43. * Execute a SQL query against the connection and return the first result.
  44. *
  45. * @param string $sql
  46. * @param array $bindings
  47. * @return object
  48. */
  49. public function first($sql, $bindings = array())
  50. {
  51. return (count($results = $this->query($sql, $bindings)) > 0) ? $results[0] : null;
  52. }
  53. /**
  54. * Execute a SQL query against the connection.
  55. *
  56. * The method returns the following based on query type:
  57. *
  58. * SELECT -> Array of stdClasses
  59. * UPDATE -> Number of rows affected.
  60. * DELETE -> Number of Rows affected.
  61. * ELSE -> Boolean true / false depending on success.
  62. *
  63. * @param string $sql
  64. * @param array $bindings
  65. * @return array
  66. */
  67. public function query($sql, $bindings = array())
  68. {
  69. $query = $this->pdo->prepare($sql);
  70. $result = $query->execute($bindings);
  71. if (strpos(strtoupper($sql), 'SELECT') === 0)
  72. {
  73. return $query->fetchAll(\PDO::FETCH_CLASS, 'stdClass');
  74. }
  75. elseif (strpos(strtoupper($sql), 'UPDATE') === 0 or strpos(strtoupper($sql), 'DELETE') === 0)
  76. {
  77. return $query->rowCount();
  78. }
  79. else
  80. {
  81. return $result;
  82. }
  83. }
  84. /**
  85. * Begin a fluent query against a table.
  86. *
  87. * @param string $table
  88. * @return Query
  89. */
  90. public function table($table)
  91. {
  92. return new Query($table, $this);
  93. }
  94. /**
  95. * Get the keyword identifier wrapper for the connection.
  96. *
  97. * @return string
  98. */
  99. public function wrapper()
  100. {
  101. if (array_key_exists('wrap', $this->config) and $this->config['wrap'] === false) return '';
  102. return ($this->driver() == 'mysql') ? '`' : '"';
  103. }
  104. /**
  105. * Get the driver name for the database connection.
  106. *
  107. * @return string
  108. */
  109. public function driver()
  110. {
  111. return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
  112. }
  113. /**
  114. * Get the table prefix for the database connection.
  115. *
  116. * @return string
  117. */
  118. public function prefix()
  119. {
  120. return (array_key_exists('prefix', $this->config)) ? $this->config['prefix'] : '';
  121. }
  122. }