PanacheQuery: Operators to allow Case Insensitive queries - quarkus-panache

I am trying to create a query which has the following 2 criteria:
Should be partial string match instead of an exact match
Should be case insensitive
My query is currently like this:
return list("company=?1 and name?=2", company, name);
However this doesn't do partial string match. Is there an operator for this?
Just for clarity's sake, assuming the DB contains peter, Peter, peTeR, a query for name=peter should return all 3 names.

You can partially match strings using the LIKE operator combined with the CONCAT function like this:
company like concat('%', ?1, '%')
You can match strings case-insensitively using the LOWER function like this:
lower(name) = lower(?2)
You can also combine both to match the company and the name partially and case-insensitively:
return list("lower(company) like concat('%', lower(?1), '%') and lower(name) like concat('%', lower(?2), '%')", company, name);

Related

Flutter - How to replace text (containing "id") with title for the id?

I have text like this:
Forecaster Strongly disagree \n\nSomething else is going to happen\n\nExample:-\n\n#:hashtag/424 \n#:hashtag/2818 ",
I need to get 424 and 2818 (which are ids) and find titles for those ids and replace #:hashtag/424 with #Title1
How do I manipulate string for this?
You can use a regular expression like this:
RegExp exp = RegExp(r'#:hashtag\/(\d+)');
String test = "Forecaster Strongly disagree \n\nSomething else is going to happen\n\nExample:-\n\n#:hashtag/424 \n#:hashtag/2818";
final matches = exp.allMatches(test);
if(matches.isNotEmpty){
print (matches.first[0]!.split('/').last);
}
//prints 424
some additional null checks will probably be nececssary depending on what you can expect from the input string
To replace the IDs you could use the replaceAllMapped method like this:
test.replaceAllMapped(exp, (match) => match[0]!.split('/').first + '/#Title1');

Multiple LIKE Conditions in the same column

I am trying to return a query that when returns all the courses associated with a course code for example 'CSC' will give me a tuple of [('CSCA08H3F',), ('CSCA20H3F',), ('CSCA67H3F',)]...etc. I know I have to use the LIKE clause, but I seem to be doing it wrong as well feel like there is a simpler way of doing this lol...
def create_course_table(db, course_file):
'''Courses Table should be ID,Course,Section,Name'''
con = sqlite3.connect(db)
cur = con. cursor()
cur.execute('''DROP TABLE IF EXISTS Courses''')
# create the table
cur.execute('''CREATE TABLE Courses( ID TEXT , Course TEXT ,
Sections TEXT , Name TEXT)''')
# Read CSV File
csv_reader = open(course_file, 'r')
csv_reader.readline()
# Insert the rows
for line in csv_reader:
course = line.strip().split(',')
ID = course[0]
Course = course[1]
Section = course[2]
Name = course[3:]
for names in Name:
cur.execute('''INSERT INTO Courses VALUES (?, ?, ?, ?)''',
(ID, Course, Section, names))
# commit and close the cursor and connection
con.commit()
cur.close()
con.close()
db = 'exams.db'
def find_dept_courses(db, dept):
'''Return the courses from the given department. Use the "LIKE"
clause in your SQL query for the course name.'''
return run_query(db, ''' SELECT Course FROM Courses WHERE LIKE Course 'ACT%'
OR LIKE Course 'AFS%' OR LIKE Course 'ANT%' OR LIKE Course 'AST%'
OR LIKE Course 'BIO%'
OR LIKE Course 'CHM%' OR LIKE Course CIT%' OR LIKE Course 'CLA%'
OR LIKE Course 'CRT%' OR LIKE Course 'CSC%' OR LIKE Course 'CTL%'
OR LIKE Course 'ECT%' OR LIKE Course 'EES%' OR LIKE Course 'ENG%'
OR LIKE Course 'EST%' OR LIKE Course 'FRE%' OR LIKE Course 'FST%'
OR LIKE Course 'GAS%'
OR LIKE Course 'GGR%' OR LIKE Course 'HIS%' OR LIKE Course 'HLT%'
OR LIKE Course 'IDS%' OR LIKE Course 'JOU%' OR LIKE Course 'LGG%'
OR LIKE Course 'LIN%' OR LIKE Course 'MAT%' OR LIKE Course 'MDS%'
OR LIKE Course 'MGA%' OR LIKE Course'MGE%' OR LIKE Course 'MGF%'
OR LIKE Course 'MGH%' OR LIKE Course 'MGI%' OR LIKE Course 'MGM%'
OR LIKE Course 'MGO%' OR LIKE Course 'MGS%' OR LIKE Course 'MGT%'
OR LIKE Course 'NRO%' OR LIKE Course 'PHL%' OR LIKE Course 'PHY%'
OR LIKE Course 'PLI%' OR LIKE Course 'POL%' OR LIKE Course 'PPG%'
OR LIKE Course 'PSC%' OR LIKE Course 'PSY%' OR LIKE Course 'RLG%'
OR LIKE Course 'SOC%' OR LIKE Course 'STA%' OR LIKE Course 'VPA%'
OR LIKE Course 'VPD%' OR LIKE Course 'VPM%' OR LIKE Course 'WST%' AND WHERE Course = ? ''', [dept])
Any help or comments would be appreciated.
Familiarize yourself with the proper syntax of the LIKE clause.
You've put the column name after "LIKE". Column name comes before "LIKE".
The following query will return the Course column from all rows in Courses where the Course column starts with the given string.
SELECT Course FROM Courses WHERE Course LIKE ? || "%";
Substitute ? with the desired prefix.
Since all Course column items are uppercase, then you could use IN instead of LIKE.
Simple test at a prompt that shows that printf used on Course can trim it down to 3 characters and so you can compare to 3 characters of each item to search.
sqlite> .schema
CREATE TABLE test (one text);
sqlite> select * from test;
abcd
efgh
ijkl
abcd
efgh
ijkl
abcd
efgh
ijkl
sqlite> select one from test where printf('%.3s',one) in ('abc');
abcd
abcd
abcd
sqlite>
Thus find_dept_courses() could use:
def find_dept_courses(db, dept):
'''Return the courses from the given department. Use the "IN"
clause in your SQL query for the course name.'''
return run_query(db, ''' SELECT Course FROM Courses WHERE printf('%.3',Course)
IN ('ACT','AFS','ANT','AST','BIO','CHM','CIT','CLA','CRT','CSC','CTL',
'ECT','EES','ENG','EST','FRE','FST','GAS','GGR','HIS','HLT','IDS','JOU',
'LGG','LIN','MAT','MDS','MGA','MGE','MGF','MGH','MGI','MGM','MGO','MGS',
'MGT','NRO','PHL','PHY','PLI','POL','PPG','PSC','PSY','RLG','SOC','STA',
'VPA','VPD','VPM','WST') AND WHERE Course = ? ''', [dept])
Edit: If needed sqlite has a upper() and lower() functions to force the case.
sqlite upper function

how to use like and substring in where clause in sql

Hope one can help me and explain this query for me,
why the first query return result but the second does not:
EDIT:
first query:
select name from Items where name like '%abc%'
second Query:
select name from Items where name like substring('''%abc%''',1,10)
why the first return result but the second return nothing while
substring('''%abc%''',1,10)='%abc%'
If there are a logic behind that, Is there another approach to do something like the second query,
my porpuse is to transform a string like '''abc''' to 'abc' in order to use like statement,
You can concatenate strings to form your LIKE string. To trim the first 3 and last 3 characters from a string use the SUBSTRING and LEN functions. The following example assumes your match string is called #input and starts and ends with 3 quote marks that need to be removed to find a match:
select name from Items where name like '%' + SUBSTRING(#input, 3, LEN(#input) - 4) + '%'

How to create a search query for partial string matches in Mongoose?

I'm new to Mongoose.js and I'm wondering how to create a simple Mongoose query that returns values containing the characters in the order that they were submitted.
This will be for an autocomplete form which needs to return cities with names that contain characters input into the search field. Should I start with a .where query?
You could find by regexp, which should allow you to search in a flexible (although not extremely fast) way. The code would be something similar to;
var input = 'ln'; // the input from your auto-complete box
cities.find({name: new RegExp(input, "i")}, function(err, docs) {
...
});
Of course, you could preprocess the string to make it match from the start (prepend by ^), from the end (append by $) etc. Just note that matching against arbitrary parts of long strings may be slow.

MYSQL: Using GROUP BY with string literals

I have the following table with these columns:
shortName, fullName, ChangelistCount
Is there a way to group them by a string literal within their fullName? The fullname represents file directories, so I would like to display results for certain parent folders instead of the individual files.
I tried something along the lines of:
GROUP BY fullName like "%/testFolder/%" AND fullName like "%/testFolder2/%"
However it only really groups by the first match....
Thanks!
Perhaps you want something like:
GROUP BY IF(fullName LIKE '%/testfolder/%', 1, IF(fullName LIKE '%/testfolder2/%', 2, 3))
The key idea to understand is that an expression like fullName LIKE foo AND fullName LIKE bar is that the entire expression will necessarily evaluate to either TRUE or FALSE, so you can only get two total groups out of that.
Using an IF expression to return one of several different values will let you get more groups.
Keep in mind that this will not be particularly fast. If you have a very large dataset, you should explore other ways of storing the data that will not require LIKE comparisons to do the grouping.
You'd have to use a subquery to derive the column values you'd like to ultimately group on:
FROM (SELECT SUBSTR(fullname, ?)AS derived_column
FROM YOUR_TABLE ) x
GROUP BY x.derived_column
Either use when/then conditions or Have another temporary table containing all the matches you wish to find and group. Sample from my database.
Here I wanted to group all users based on their cities which was inside address field.
SELECT ut.* , c.city, ua.*
FROM `user_tracking` AS ut
LEFT JOIN cities AS c ON ut.place_name LIKE CONCAT( "%", c.city, "%" )
LEFT JOIN users_auth AS ua ON ua.id = ut.user_id

Resources