Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Please add a getSql() method #953

Open
kakenbok opened this issue Apr 20, 2021 · 0 comments
Open

Feature request: Please add a getSql() method #953

kakenbok opened this issue Apr 20, 2021 · 0 comments

Comments

@kakenbok
Copy link

kakenbok commented Apr 20, 2021

Describe the feature
In order to build an intermediate nested count query, I would like to reuse the built query from a Medoo instance.
Use case: Before returning a paginated list of objects I would like to know the overall number of objects in the database.
I already have a configured Medoo instance which (since complex) I really would like to reuse for the count operation.

The Medoo code for the select is:

$articles = $db->select(
    'articles',
    $fields,
    $where
);

// SELECT (...) FROM articles // where, having, group

A Medoo code for the count could look like this:

$query = $db->getSql( // <-- new method
    'articles',
    $fields,
    $where
);

$count = intval($db->query('SELECT COUNT(*) from (' . $query . ') tmp')->fetchColumn());

// SELECT COUNT(*) FROM (
//    SELECT COUNT(...) FROM articles // where, having, group
// ) AS tmp

Information

  • Version of Medoo: "version": "v1.7.10"
  • Type of Database (MySQL, MSSQL, SQLite...): MySQL
  • System (Liunx\Windows\Mac): Linux

Workaround

I am already able to subclass Medoo to provide such getSql() method like this:

class MedooWithGetSql extends Medoo
{
    public function getSql($table, $join, $columns = null, $where = null)
    {
        $map = [];
        $query = $this->selectContext($table, $map, $join, $columns, $where);
        return $this->generate($query, $map);
    }
}

Alternative feature

Alternatively, it would be great to pass a Medoo or a Medoo::raw or an sql-string to the $db->count() method.

$query = $db->count(
    'articles',
    $fields,
    $where
);

$count = $db->count($query);

// SELECT COUNT(*) FROM (
//    SELECT COUNT(...) FROM articles // where, having, group
// ) AS tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant