Search code examples
solrdataimporthandler

How do I escape single quotes in variables in a Solr DataImportHandler?


I have a DataImportHandler for importing data from a SQL database. The root entity, Foo has many Bars. The Bar table uses Foo.Name as its foreign key. Some Foo names have single quotes in them--such as STW's.

The import query for the Bar entity is something like:

select name from Bar where Foo_Name = ${Foo.Name} 

However, when Foo.Name contains a single quote the import fails with a SQL exception of Incorrect syntax near 's.

I've tried wrapping the parameter with escapeSql(${Foo.Name}) but it doesn't appear to be called--the sql being executed is where Foo_Name = 'escapeSql(STW's)'

How do I properly escape the Foo.Name to avoid issues when they contain single-quotes?


Solution

  • I think the correct syntax is:

    Foo_Name = '${escapeSql(Foo.Name)}'
    

    since escapeSql is a solr function and not a normal sql function.