How do I get web2py to insert the current time as understood by the DB server into a datetime field. If I simple use datetime.now() it will insert the client time. In mySQL I would use "NOW()" to get the server time: is there a DAL shortcut for this?
It's for using inside an update_or_insert() statement, if that makes any difference.
I don't think you can directly pass a SQL function to the DAL .insert or .update_or_insert methods, as whatever values are passed will end up being quoted. Instead, you will have to use the .executesql() method and pass it the SQL to be executed. If you want help generating the SQL, you can use the ._insert method to generate a string, and then remove the quotes around the "NOW()" function:
query = db.mytable._insert(mytime='NOW()').replace("'NOW()'", "NOW()")
db.executesql(query)
Of course, you won't be able to use the .update_or_insert method with this approach, but its logic is not complicated, so you could easily implement your own helper to handle the .update_or_insert logic as well as the above logic.
Related
In the new fts5 virtual tables in SQLite there is a ranking function bm25. What is the best way to call it from jOOQ? Is there any way to introduce this function as Java/Kotlin function or should it be just a string literal?
What I do currently is
.orderBy(field("bm25(books_fts)"))
but indeed this approach is error-prone. I can make a typo in any part and will have an error only in runtime.
Is there any better way?
Instead of using the plain SQL template directly in your query, better create a custom library for these purposes, e.g.
fun bm25(t: Table<*>): Field<Double> = field("bm25({0})", SQLDataType.DOUBLE, t)
Now you can pretend this is a natively supported function in jOOQ and use it in a type safe way everywhere.
How can I track events on adding, updating or deleting entity rows with Linq2Db?
I need to recalculate some data in db on this operations, what is the best way?
On Entity Framework I use my custom Repository class with custom Add operations. Mabe this is only way in Linq2Db, but I am interesing, mabe there is some catcher to notify this events?
It is not so easy as in EF, because linq2db manipulates with SQL and you can easily update thousands records by single update statement.
For example
db.SomeEntities.Where(e => e.IsExpired)
.Set(e => e.ExpirationDate, e => Sql.CurrentTimestamp)
.Update();
The same technique can be used with insert from, update from, delete.
But there is possibility to catch SQL AST before executing. You have to override ProcessQuery method in your DataConnection class.
Sample is here: ConcurrencyCheckTests.cs
You should return the same statement that is passed to method but with changed property statement.IsParameterDependent = true to prevent query caching.
SqlStatement analysis is not trivial task but possible. You have to handle SqlUpdateStatement, SqlInsertStatement, SqlDeleteStatement, SqlInsertOrUpdateStatement.
Second option is to write your own extensions which manipulates with single objects, for example, UpdateTracked(), DeleteTracked(), etc. But as you can see from first example it will not help in complex queries.
I am trying to call stored procedure so i wrote like this
Execute Sql String EXEC PREPAID_ACT_ON_FIRSTCALL_IRAN('9900001451');
But I'm getting this error:
DatabaseError: ORA-01756: quoted string not properly terminated
Remove the semicolon at the end of your query and it should fix it. When using this library, you should only put the semicolon when you want to chain multiple queries together. See the examples in the documentation of the library. I remember it took me a while to figure out this problem.
Execute Sql String EXEC PREPAID_ACT_ON_FIRSTCALL_IRAN('9900001451')
FYI, this library also has a keyword specifically meant to call stored procedures, you may want to look into using the keyword Call Stored Procedure instead.
I have a simple stored procedure that takes no parameters and returns zero for success.
How do I wire that up using Code First so that I can call it from the Context?
When using an edmx (Database First) you can use the wizard to import a function and then call it on the Context:
Context.MyStoredProcedure();
Is there a way to do this with Code First?
I have found many examples but they are all CRUD type of stored procedures.
Greg
While I thought that there would be a specific mapping for a stored procedure on the context, the following is the only way I could find for Code First Development:
Context.Database.ExecuteSqlCommand("EXEC ClearLog");
Today is my first day using ASP.NET MVC, and I'm finding it very intriguing. I only just started learning asp.net.
So basically I'm trying to call a procedure from an MSSQL database, and with it I need to send a paramater "PlaceID", which is an integer. This procedure basically just picks out a number of columns from different tables in the database. Here is the Linq to SQL code
The ultimate goal is to be able to retrieve all the information and return it as JSON with a function that will be available for a javascript.
I'm wondering what is the best way to proceed from here. I know I have to create a view, but I'm still unclear exactly on how I can call the procedure and make it store all the information. I could really use some help on this, some code examples would be excellent. I already wrote a C#.net function to convert a datatable to JSON using a stringbuilder, but I get the feeling there is a smarter way to do things.
Any help is very appreciated.
Might I suggest going through the NerdDinner.com example first. I really think that you may have started down the wrong road and it would be worth backing up and doing some review first. For one thing, you probably don't want to be mixing DataTables and LINQ, typically you'd work with strongly-typed models and have your data context/repository return IQueryable/IEnumerables of the model instead of a DataTable. For another, there is a controller method that is able to turn a model into JSON for you. Generally, you should need to write your own JSON serialization.
Declare a list and serialze it into json.
Here Lstcustomeris a genric list of class type customer
I Assign values to class varable and then insert this class in list of Lstcustomer
e.g.
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Return js.Serialize(Lstcustomer)