How can I modify the SQL generated by ServiceStack.OrmLite?

蹲街弑〆低调 提交于 2020-01-05 10:10:57

问题


I want to use ServiceStack's OrmLite in C# to query AWS RedShift. AWS RedShift uses the postgresql wire protocol as its query interface, and puts some constraints on the queries that can be run, one of which is that it cannot accept parameterised queries (I've been told; I haven't actually been able to find this backed up in documentation yet).

So, I am investigating whether it would be possible to use an ORM to generate parameterised SQL, then find/replace in the parameter values.

How can I get access to the SQL generated by OrmLite to then change it before it is run?


回答1:


From the OrmLite's documentation:

By default OrmLite does not use parameterized SQL. Only API's that use parameterized statements for all SQL operations, these are identified with a Param suffix, e.g:

Parameterized Write operations

db.InsertParam(new Person { FirstName = "Jimi", LastName = "Hendrix", Age = 27})
db.UpdateParam(new Person { FirstName = "Jimi", LastName = "Hendrix", Age = 27})
db.DeleteByIdParam<Person>(1)
Parameterized Read operations

var people = db.SelectParam<Person>(q => q.Age == 27)
var person = db.GetByIdParam<Person>(1)

//Existing parameterized query API's
var people = db.Where<Person>(new { FirstName = "Jimi", Age = 27 })
var people = db.Query<Track>("FirstName = @name and Age = @age", 
    new { name = "Jimi", age = 27 })

In addition Selection methods containing the word Query or Where also use parameterized SQL (other selection methods do not). Anonymous types passed into Where are treated like an AND filter.

var track3 = db.Where<Track>(new { AlbumName = "Throwing Copper", TrackNo = 3 })

Query statements take in parameterized SQL using properties from the supplied anonymous type (if any)

var track3 = db.Query<Track>(
"select * from Track Where AlbumName = @album and TrackNo = @trackNo", 
    new { album = "Throwing Copper", trackNo = 3 })

After it's executed (not before), you can get the Last SQL statement generated with:

db.GetLastSql()



回答2:


OrmLite doesn't (currently) make the SQL that it will generate available until after it has been executed.

I discovered that RedShift does in fact support parameterised queries.



来源:https://stackoverflow.com/questions/16916282/how-can-i-modify-the-sql-generated-by-servicestack-ormlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!