Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Mojo database routing

by rajaman (Sexton)
on Feb 11, 2020 at 20:15 UTC ( #11112802=perlquestion: print w/replies, xml ) Need Help??

rajaman has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am just starting to learn Mojolicious with a toy example:

1. User inputs query keywords (e.g. 'John Doe') in a search form (at home page '/')
2. The keywords are used to search a sqlite table with three columns (id, name, department)
3. The output page shows the search box above the result from sqlite: 'id, name, department' for John Doe (at page '/John Doe')

My raw pseudo-code with comments is below, but I am getting lost on how to connect different pieces together.

Please guide how such a task could be done efficiently.

Thanks a lot!

#!/usr/bin/env perl use strict; use warnings; #use Mojo::SQLite; use Mojolicious::Lite; use DBI; #hypnotoad app->config( hypnotoad => { listen => [ 'http://127.0.0.1:8083/' ], proxy => 1, }, ); # interact with database my $dbh = DBI->connect("dbi:SQLite:mydb.db","","") or die "Could not c +onnect"; helper db => sub { $dbh }; #get '/' => { text => q{ # <form method="POST"><input name="q"><input type="submit" value +="query"></form> #}}; #my $query=param(q); #how to pass 'John Doe' as a variable from the user input helper select => sub { my $self = shift; my $sth = eval { $self->db->prepare('SELECT * FROM contacts where na +me like '%John AND Doe%'') } || return undef; $sth->execute; return $sth->fetchall_arrayref; }; app->select; # setup route, such that search query gets appended to root in the out +put, e.g., /John Doe for 'John Doe' search query. How to connect 'par +am(q)' and ':query'? get '/:query' => sub { my $self = shift; my $query=param(q); my $rows = $self->select; $self->stash( rows => $rows ); $self->render('index'); }; app->start; __DATA__ @@ index.html.ep <!DOCTYPE html> <html> <head><title>Emp</title></head> <body> <form method="POST"><input name="q"><input type="submit" value="John D +oe"></form> <br><br> <table border="1"> <tr> <th>id</th> <th>name</th> <th>department</th> </tr> % foreach my $row (@$rows) { <tr> % foreach my $text (@$row) { <td><%= $text %></td> % } </tr> % } </table> </body> </html>

Replies are listed 'Best First'.
Re: Mojo database routing
by haukex (Bishop) on Feb 11, 2020 at 21:45 UTC

    How to get form parameters and placeholders is covered in Mojolicious::Guides::Tutorial. I see you've commented out Mojo::SQLite, but I personally like these Mojo-style DB adapters.

    Form parameters don't normally get put in the URL in the way you seem to be hoping. One possible solution would be JavaScript, but here is a solution without it: the HTML form gets submitted to a handler at /search, which then just redirects to the corresponding URL. Note I'm generating the form via Mojolicious::Plugin::TagHelpers.

    #!/usr/bin/env perl use Mojolicious::Lite; use Mojo::SQLite; helper sqlite => sub { state $sql = Mojo::SQLite->new('sqlite:mydb.db') }; # disable template cache when running under morbo app->renderer->cache->max_keys(0) if app->mode eq 'development'; app->sqlite->migrations->from_string(<<'END_MIGR')->migrate; -- 1 up CREATE TABLE contacts ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, department TEXT ); INSERT INTO contacts (name,department) VALUES ('John Doe','Perl'); -- 1 down DROP TABLE IF EXISTS contacts; END_MIGR get '/' => sub { my $c = shift; $c->render('main'); } => 'index'; post '/search' => sub { my $c = shift; $c->redirect_to( $c->url_for('name_query', query => $c->param('name') ) ); } => 'search'; get '/name/:query' => sub { my $c = shift; $c->stash( rows => $c->sqlite->db->select( 'contacts', ['id','name','department'], { name => $c->stash('query') } )->arrays ); $c->render('main'); } => 'name_query'; app->start; __DATA__ @@ main.html.ep <!DOCTYPE html> <html> <head><title>Hello Mojo!</title></head> <body> <div> %= form_for search => ( method => 'post' ) => begin %= label_for name => 'Name' %= text_field 'name', required=>'required'; %= submit_button 'Query' %= end </div> % if ( my $rows = stash 'rows' ) { % if (@$rows) { <div><table border="1"> <tr> <th>id</th> <th>name</th> <th>department</th> </tr> % foreach my $row (@$rows) { <tr> % foreach my $text (@$row) { <td><%= $text %></td> % } </tr> % } </table></div> % } else { <div><b>No records found!</b></div> % } % } </body> </html>
      Thanks @haukex, that's very helpful. Seems Mojo is pretty different from CGI in passing form parameters with GET. I have one other question:

      name => $c->stash('query')

      The above code matches exact full name (e.g. 'John Doe'). How to make make it do partial matching (and possibly case insensitive), such as:

      ..LIKE 'john%'

      ..LIKE 'john AND doe'

      ..LIKE '"john Doe"'

      ..MATCH 'NEAR("john" "doe", 0)'

      Cheers!
        Seems Mojo is pretty different from CGI in passing form parameters with GET.

        Not really, CGI.pm also has an OO interface with a ->param method. Everything else is different, though :-)

        How to make make it do partial matching (and possibly case insensitive)

        That's more of an SQL question; it will depend on the database you're using. The most important thing is that if you build SQL queries yourself, you need to be very careful to quote everything correctly, usually using either placeholders or DBI's ->quote method, which in my code above you can access via $c->sqlite->db->dbh->quote(...) (or app->sqlite->...).

        SQLite and DBD::SQLite supports an extension called FTS5 that might be helpful to you. For example, if in my code you replace the CREATE TABLE with this:

        CREATE VIRTUAL TABLE contacts USING fts5(name, department); INSERT INTO contacts (name,department) VALUES ('John Doe','Perl'); INSERT INTO contacts (name,department) VALUES ('Larry Wall','Perl'); INSERT INTO contacts (name,department) VALUES ('Gloria Wall','Perl');

        And the $c->stash( rows => ... value with this:

        $c->sqlite->db->query( q{ SELECT rowid,name,department FROM contacts WHERE name MATCH ? }, $c->stash('query') )->arrays );

        Then you get the full power of the FTS5 extension, e.g. your query can be john OR larry, g*, etc. and it'll just work.

        Edit: Typo fix.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11112802]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2022-05-23 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?