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

SQL_CALC_FOUND_ROWS don't work? #94

Open
leandrocombr opened this issue Nov 1, 2024 · 3 comments
Open

SQL_CALC_FOUND_ROWS don't work? #94

leandrocombr opened this issue Nov 1, 2024 · 3 comments

Comments

@leandrocombr
Copy link

Is there a way to use

SELECT SQL_CALC_FOUND_ROWS
column ORDER BY ASC LIMIT 5 OFFSET 0;
SELECT FOUND_ROWS();

no Mojo::mysql?

I already searched and couldn't find it, chatGPT just gives wrong codes

@Grinnz
Copy link
Contributor

Grinnz commented Nov 1, 2024

You can pass any custom query to ->query or ->query_p.

@leandrocombr
Copy link
Author

->query_p

@leandrocombr
Copy link
Author

    # Consulta SQL
    my $query = 'SELECT SQL_CALC_FOUND_ROWS column FROM `table` ORDER BY `column` ASC LIMIT 5 OFFSET 0';

    # Executa a consulta assíncrona com promessas
    my $dataPromise = $mysql->db->query_p($query)->then(sub {
        my $results = shift;
        my $resultado = $results->hashes->to_array;
        my $totalFiltrado = scalar @$resultado;

        # Consulta para obter o total de registros encontrados
        return $mysql->db->query_p('SELECT FOUND_ROWS()')->then(sub {
            my $found_rows = shift;
            my $totalRegistros = $found_rows->array->[0];

            # Renderiza a resposta JSON
            return $sel->render(json => { 
                data => $resultado, 
                totalRegistros => $totalRegistros, 
                totalFiltrado => $totalFiltrado 
            });
        });
    })->catch(sub {
        # Tratamento de erro
        my $error = shift;
        return $sel->render(json => { error => 'Erro na consulta: ' . $error });
    });

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

2 participants