public function count($query = null, array $options = array())
    {
        //Count using the identity column
        if (is_scalar($query))
        {
            $key = $this->getIdentityColumn();
            $query = array($key => $query);
        }

        //Create query object
        if (is_array($query) && !is_numeric(key($query)))
        {
            $columns = $this->mapColumns($query);
            $query = $this->getObject('lib:database.query.select');

            foreach ($columns as $column => $value)
            {
                $query->where($column . ' ' . (is_array($value) ? 'IN' : '=') . ' :' . $column)
                    ->bind(array($column => $value));
            }
        }

        if ($query instanceof KDatabaseQuerySelect)
        {
            if (!$query->columns) {
                $query->columns('COUNT(*)');
            }

            if (!$query->table) {
                $query->table(array('tbl' => $this->getName()));
            }
        }

        $result = (int)$this->select($query, KDatabase::FETCH_FIELD, $options);
        return $result;
    }