special characters in HQL - hql

hi i'm trying to get from a mySql DB a query using Hibernates HQL
*
public String getDetailsByUserAndFullPath(String user,String fullpath) {
String temp2 = "%" + user + "%";
String temp3 = "%" + fullpath + "%";
// Query query =
Query query_bd = session
.createQuery("from Files files where user like ? and full_path like ? escape '\'");
query_bd.setString(0, temp2);
query_bd.setText(1, temp3);
List<Files> list = query_bd.list();
the problem is the fullpath contains \ such as
c:\temp\example.docx
and i get e query error using the escape
i'm shure it's a small thing but i'm killing myself for two hours already!!
please help
tnx

The \ is an escape also in your language (that seems to be Java)
so the query looks like this.
from Files files where user like ? and full_path like ? escape ''
Try to add another backslash
from Files files where user like ? and full_path like ? escape '\\'
If you are using binded parameter you don't need to escape characters, also i don't recall that this is special character in some DBMS. Also the escape parameter is provided to support more likely the % or _ characters.

Related

Buffer gets get reduced when escaping dot with back slash

I have the below query
SELECT
categorymap.id,
categorytype.name,
categorytype.value
FROM
categorymap,
categorytype
WHERE
( categorymap.logfilename = '**hello\.log**' )
AND ( categorymap.categorytypeid = categorytype.id )
Index is available for column logfilename of categorymap table.
I noticed the buffer gets was more when not adding "\" before "." in where clause. Both cases, before and after adding "\", index range scan was used on logfilename column as per explain plan.
Could someone please explain what role does '.' play in here in increasing buffer gets?
TIA
If you are talking about this:
= '**hello\.log**'
(maybe it is just = 'hello\.log'; double asterisks for bold?), then: you didn't escape anything. This query will search the logfilename column for exactly such a string: hello followed by a backslash \ followed by a dot . followed by log.
You'd escape a dot in e.g. regular expression, but there's none here, so ...

FoxPro Deleting after specific

I am trying to replace text after "/" on foxpro. Shelly Jones/Foundation Director, How to delete everything after the "/"?
A common way of doing it is to combine the left() and atc() functions, like so:
lcStr = "Shelly Jones/Foundation Director"
lcNewStr = LEFT(lcStr, ATC('/', lcStr) - 1)
The -1 is needed to get the portion of the string ending before the / character.
Assuming this is VFP 7 or later, use the StrExtract function:
cResult = STREXTRACT(cOriginalString, '', '/')

VB6 Getting Error when inputting apostrophe on Filter, using ADODB

I'm trying to filter datagrid from a textbox it works but not if apostrophe or ' was typed on the textbox, I'm using ADODB and VB6
Public Sub pGetCustomer(Optional vSearch As String)
If vSearch = "'" Then
xRSTree.Filter = adFilterNone
xRSTree.Requery
Else
xRSTree.Filter = "description like '%" & vSearch & "%' or customercode like '%" & vSearch & "%'"
End If
Private Sub txtSearch_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
As it says in the ADO documentation (when did people fall into this weird habit of calling ADO "ADODB" anyway???):
Note To include single quotation marks (') in the filter Value, use two single quotation marks to represent one. For example, to filter on O'Malley, the criteria string should be "col1 = 'O''Malley'". To include single quotation marks at both the beginning and the end of the filter value, enclose the string with pound signs (#). For example, to filter on '1', the criteria string should be "col1 = #'1'#".
You must also consider wildcard rules here:
If Operator is LIKE, Value can use wildcards. Only the asterisk (*) and percent sign (%) wild cards are allowed, and they must be the last character in the string. Value cannot be null.
But a little confusingly:
In a LIKE clause, you can use a wildcard at the beginning and end of the pattern (for example, LastName Like '*mit*'), or only at the end of the pattern (for example, LastName Like 'Smit*').
You need to "escape" your qoutes or single qoutes. Simple way would be to replace in vSearch all ' with '' and all " with "".

using gsub in ruby strings correctly

I have this expression:
channelName = rhash["Channel"].gsub("'", " ")
it works fine. However, I can only substitute 1 character with it. I want to add a few more characters to substitue. So I tried the following:
channelName = rhash["Channel"].gsub(/[':;] /, " ")
This did not work, that is there was no substitution done on strings and no error message. I also tried this:
channelName = rhash["Channel"].gsub!("'", " ")
This lead to a string that was blank. So absolutely not what I desired.
I would like to have a gsub method to substitute the following characters with a space in my string:
' ; :
My questions:
How can I structure my gsub method so that all instances of the above characters are replaced with a space?
What is happening with gsub! above as its returning a blank.
Your second attempt was very close. The problem is that you left a space after the closing bracket, meaning it was only looking for one of those symbols followed by a space.
Try this:
channelName = rhash["Channel"].gsub(/[':;]/, " ")
This does not answer your question, but is a better way to do it.
channelName = rhash["Channel"].tr("':;", " ")

Issue dealing with white space with Ruby regular expressions

I'm trying to write a simple script expression that allows me to identify the java files in a directory that have a private constructor. I have had some luck but I want my script to acknowledge there is white space between the access modifier and the constructor name but not care if it is a space or n spaces or a tab or n tabs etc.
I am trying to use...
"private\s+"+object_name
but the + (1 or more) is not finding a constructor with 2 spaces between the modifier and the constructor name.
I know I am missing something. Any help would be greatly appreciated.
Thanks.
Here is the full code if it helps...
!#/usr/bin/ruby
path = ARGV[0]
if path.nil?
puts "missing path argument"
exit
end
entries = Dir.entries( path )
entries.each do |file_name|
file_name = file_name.rstrip
if ( file_name.end_with? "java" )
text = File.read( path+file_name )
object_name = file_name.chomp( ".java" )
search_str = "private\s+"+object_name
matches = text.match( Regexp.escape( search_str ) )
if ( !matches.nil? && matches.length > 0 )
puts matches
end
end
end
I think you want to escape the \ in your Ruby string and also Regexp.escape your object name and not the whole regex including the whitespace matcher, e.g.,
[...]
search_regex = Regexp.new("private\\s+" + Regexp.escape(object_name))
matches = text.match(search_regex)
As #LBg also points out, if you want to use + concatenation, better to use single quotes that won't require escaping the \. Or use doubles with substitution as in:
search_regex = Regexp.new("private\\s+#{Regexp.escape(object_name)}")
A double-quoted string reads "\s" as " ", no problems with that, but prefer use single-quoted in this case. Regexp.escape removes the funcionality of the regex's symbols of the string. private + ("\s" is " ") is converted to private\ \+ and, with match, will try to find the string private +object_name, what is not what you want. Remove the Regexp.escape and it should work well.

Resources