How can locking hints be added to a select query using an interceptor? Would using a regex to change the commandtext be appropriate or is there a better way?
And how can I not register the interceptor globally but rather make it specific to a context, or even a query?
Related
I noticed that Spring Data JDBC doesn't seem to implemented Optimistic Locking (something like a JPA's #Version annotation).
I was thinking on creating a #Modifying query which considers the version field and returns boolean to check manually if the update was successful or not. But I'm afraid this approach is limited to simple entities, not aggregates implying multiple tables.
What's the best way to implement optimistic locking for aggregates?
It depends on your situation. If you just have 7 aggregates of which 5 are single entity aggregates go for the #Modifying solution for the single aggregates and write custom methods for the other 2.
If you have more aggregates consisting of more then one class consider properly implementing it and submitting a PR. The issue is already there: https://jira.spring.io/projects/DATAJDBC/issues/DATAJDBC-219
The main code changes will be in SqlGenerator which would need to add a where clause for aggregate roots if they have a version attribute.
If you are interested in doing a PR and need more assistance, please leave comment on the issue.
When using waterline as orm (default) in sails or as a standalone, is it possible to have access to the native query (according to the used adapter) generated by calling model methods?
Lets assume i have a User model, and that i call .find on it with no criteria.
User.find().then(function(users){
// Is there a something like this?
console.log(users._query); // If using mysql it logs... SELECT * from `user`
})
Of course that this is a trivial example, but when we have complex chainings, it'll be really helpful to have access to it, so we can be sure our queries are constructed in a similar fashion to what we are expecting, and if not refactor them or use the method .query to manually construct our queries.
So, is this possible? Even in a hacky ugly way?
I'd love to have this functionality too, but it does not seem available.
However if you are using MySQL/MariaDB, there is a hacky ugly way indeed. Try this in your shell before launching your app:
export LOG_QUERIES=true
From what I have looked at so far, I am guessing the answer to this question is "no" but I thought I would ask in the event that I am missing something.
I have looked at the new RLS (row-level security) feature of Azure SQL. One of the things that needs to be done is to set the (user) context before executing a SQL statement. Since I am also looking at entity framework, my question is whether or not I can embed or inject something like "EXECUTE AS USER = 'User1'" into the SQL that is generated by entity framework.
Is this something that is possible? I know I can execute custom SQL but I was looking to set the code up in one place and have it run the statement for all generated SQL.
may be this EF6 feature can help, and more particularly the interception interface by the IDbCommandInterceptor.
Not clear about the hability to change the generated sql but may be.
Otherwise, have you tried a context.Database.ExecuteSqlCommand("EXECUTE AS...") ?
Create a new connectionstring using the user if you need to, or, you can modify the connectionstring used by DbContext.
I'm considering using the Code First approach with Entity Framework.
I like adding triggers to my SQL database on fields like DateAdded and DateModified so that they automatically update with a getdate() as required.
From what I hear this is difficult with EF: Code First, so is there an alternative?
It doesn't have to be difficult (e.g. getdate() default), you just have to know where to inject...
You can include a custom SQL into the 'chain' - via using Seed-ing
or custom Initializer - to do any kind of custom work.
You may have problems with migrations, if you want to do that 'post-creation' - but if you limit all such similar init work while the Db is still empty you should be ok.
Another thing to worry about is various Db providers - as any custom SQL may vary, or support - but if you know what you're targeting you should be ok.
Check this link - Possible to default DateTime field to GETDATE() with Entity Framework Migrations?
Or this one generally about Triggers https://stackoverflow.com/a/5913581/417747
...it might be close to what you need - and get you ideas about how to do some other things.
I have a linq query
Context.Set<Entity>().where(x=>x.condition == true).select(x=> new ViewModel{Property = x.Property});
I would like to be able to make a change to the linq query through something like this
Context.Set<Entity>().ChangeLinqQuery("String").where(x=>x.condition == true).select(x=> new ViewModel{Property = x.Property});
So that when I capture the DbCommandTree in my EFProviderWrapper I will be able to spot the change and capture the String. I also wish to be sure that the expression is applied to that particular reference to the Entity so that if I join the Entity on itself it will still be able to tell that that particular reference to Entity is the one I mean to alter.
The goal is to be able to alter the SQL generated by EF, so if you have a better means of achieving this goal please feel free to provide it.
I don't think this is possible. Your ChangeLinqQuery will have to add some custom expression into expression tree created on behind. The problem is that this expression tree is translated to ESQL - that is what DbCommandTree describes (it is not SQL). As I know ESQL is not extensible and so you cannot add any custom expressions to this process. Even if you could it would also most probably mean that you will have to rewrite much of SQL generation to satisfy your needs = not developing provider wrapper but provider itself.
Your best choice with EF is simply replace table names in generated SQL which will be complex, slow and unless you build strong SQL parser following its syntax it will also be error prone.
Your best choice is simply not using EF as I already recommended in your previous question.