public function testTypeDetection() { $null = null; $int = 0; $bool = true; $string = 'string'; $this->assertEquals(Database::TYPE_NULL, Database::typeOfValue($null)); $this->assertEquals(Database::TYPE_INT, Database::typeOfValue($int)); $this->assertEquals(Database::TYPE_BOOL, Database::typeOfValue($bool)); $this->assertEquals(Database::TYPE_STR, Database::typeOfValue($string)); }
/** * Delete record with matching conditions in the $where. * * @param string $table Table. * @param array $where Assoc key(column) conditions. * * @throws DatabaseException * * @return bool Successful deleted? */ public function delete($table, array $where) { $query = "DELETE FROM `{$table}` WHERE "; $bind = array(); foreach ($where as $column => $value) { $query .= "`{$column}` = ? AND "; $bind[] = $value; } $query = substr($query, 0, -5); // Prepare, bind and execute $statement = $this->prepare($query); foreach ($bind as $idx => $value) { $statement->bindValue($idx + 1, $value, Database::typeOfValue($value)); } return $statement->execute(); }
/** * Execute the query, fetch if provided details to do so. * * @param bool $fetch Fetch on or off? * @param null $fetchMode Fetch mode (null for default) * @param null $fetchClass Fetch class (null for none) * * @throws QueryBuilderParseException * @throws QueryBuilderException * @throws QueryException * @throws DatabaseException * * @throws \Exception * * @return bool Boolean when fetching is off. * @return array|object|null Result when fetching is on. */ private function doExecute($fetch = false, $fetchMode = null, $fetchClass = null) { if ($fetch) { $fetchMode = Database::normalizeFetchType($fetchMode); } // Parse SQL and Bind $sql = $this->query->getSQL(); $bind = $this->query->getBind(); // Connection and execute $statement = $this->connection->prepare($sql); foreach ($bind as $idx => $value) { $statement->bindValue($idx + 1, $value, Database::typeOfValue($value)); } // Fetch if ($fetch) { if ($fetchMode === Database::FETCH_CLASS) { // Verify class new \ReflectionClass($fetchClass); $statement->setFetchMode($fetchMode, $fetchClass); } else { $statement->setFetchMode($fetchMode); } } // Execute $success = $statement->execute(); if (!$success) { return false; } if ($fetch && $fetch === 'single') { return $statement->fetch(); } if ($fetch) { return $statement->fetchAll(); } return true; }