Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Mojo database routing

by rajaman (Sexton)
on Feb 22, 2020 at 00:19 UTC ( [id://11113325]=note: print w/replies, xml ) Need Help??


in reply to Re: Mojo database routing
in thread Mojo database routing

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!

Replies are listed 'Best First'.
Re^3: Mojo database routing
by haukex (Archbishop) on Feb 23, 2020 at 09:10 UTC
    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: note [id://11113325]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-18 00:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found