public function connect()
{
if (!is_null($this->connection) && $this->connected()) {
- return true;
+ return $this->connected;
}
+ // Reset connected state
+ $this->connected = false;
+
$port = 0;
$serveraddr = trim($this->configCache->get('database', 'hostname'));
$serverdata = explode(':', $serveraddr);
*/
public function disconnect()
{
- if (is_null($this->connection)) {
- return;
+ if (!is_null($this->connection)) {
+ switch ($this->driver) {
+ case 'pdo':
+ $this->connection = null;
+ break;
+ case 'mysqli':
+ $this->connection->close();
+ $this->connection = null;
+ break;
+ }
}
- switch ($this->driver) {
- case 'pdo':
- $this->connection = null;
- break;
- case 'mysqli':
- $this->connection->close();
- $this->connection = null;
- break;
- }
+ $this->driver = null;
+ $this->connected = false;
}
/**
$connected = $this->connection->ping();
break;
}
+
return $connected;
}
*
* @param string|array $table Table name or array [schema => table]
* @param array $condition Array of fields for condition
+ * @param array $params Array of several parameters
*
* @return int
*
* $count = DBA::count($table, $condition);
* @throws \Exception
*/
- public function count($table, array $condition = [])
+ public function count($table, array $condition = [], array $params = [])
{
if (empty($table)) {
return false;
$condition_string = DBA::buildCondition($condition);
- $sql = "SELECT COUNT(*) AS `count` FROM " . $table_string . $condition_string;
+ if (empty($params['expression'])) {
+ $expression = '*';
+ } elseif (!empty($params['distinct'])) {
+ $expression = "DISTINCT " . DBA::quoteIdentifier($params['expression']);
+ } else {
+ $expression = DBA::quoteIdentifier($params['expression']);
+ }
+
+ $sql = "SELECT COUNT(" . $expression . ") AS `count` FROM " . $table_string . $condition_string;
$row = $this->fetchFirst($sql, $condition);