Call db-specific function from jOOQ - sqlite

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.

Related

Web2py: insert server concept of datetime

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.

Can I declare a global variable in xquery in Marklogic Server?

I want a global variable which I can use in my different .xqy pages. Can I declare such a variable in xquery in Marklogic Server ?
You can declare a variable in any module. For instance, it is config.xqy.
declare variable $PRECISION as xs:integer := 4;
For using this variable you need to import this module in your work module.
import module namespace config = "http://your-namespace" at "config.xqy";
And refer to this variable:
$config:PRECISION
If your application is running on a single E-node, you can use server fields , which are sort of designed for this use case as well.
If you need values accessible across the server, there is a library in the Marklogic XQuery Commons for storing persistent key/value pairs:
https://github.com/marklogic/commons/blob/master/properties/properties.xqy
And you may have already considered this, but you could also just simply store the global data in a document on the database and access with doc() - or eval() if you need to get to it from a different database.
You have a few options. If you need a global constant variable, the config.xqy method mentions in #Andrew Orlov's answer is great because you avoid any locking from concurrent access to a properties.xml file.
If you need a variable that can be mutated across a cluster of nodes, the property.xqy example linked by #wst appears to use globally assigned namespaces to embed a retrievable key and value. Pretty clever. However, I'm not sure how much this is meant for heavy levels of change.
The E-node specific variable from #Eric Bloch is good, but please also be aware that it will not survive a system restart.
I'd be interested to know how these all compare performance-wise.

Entity Framework for Multi-tenant architecture - filterings single table by tenant ID

We are looking for a way of automatically filtering all CRUD operations by a tenant ID in Entity Framework.
The ideas we thought of were:
Using table valued user defined functions
Using stored procedures (but we don't really want to, as we're using an ORM to avoid doing so)
Some how modifying the templates used to generate the SQL to add a where clause on each statement.
Some how modifying the templates used to generate the LINQ in the controllers (we may use MVC).
Any tips?
-thanks
Alex.
Using table valued user defined functions
Table valued function are only available in .NET 4.5 Beta (and not available in code first). Using them will still not help you because you will have to use the function in every LINQ query so it is the same as using where clause.
Using stored procedures (but we don't really want to, as we're using an ORM to avoid doing so)
It can be useful for some special complex queries but generally it is not what you want.
Some how modifying the templates used to generate the SQL to add a where clause on each statement.
Too complex and on completely different level of abstraction.
Some how modifying the templates used to generate the LINQ in the controllers (we may use MVC).
Close to ideal solution. You simply need to wrap access to your entity set into some code which will look like:
public class MultiTenantAccess<T> where T : IMultitenant
{
private IDbSet<T> set;
...
public IQueryable<T> GetQuery(int tenantID)
{
return set.Where(e => e.TenantID == tenantID);
}
}
Sometimes this is core for something called Generic repository but it is really just a wrapper around EF set. You will always use GetQuery to query your data store instead of using DbSet directly.
you may also separate the tenants data into different databases
or into same database, but with different schemas? You can read more about this in an old MSDN article called "Multi-Tenant Data Architecture"

SQLAlchemy, reflection, different backends and table/column case insensitivity

Intro: I'm writing web interface with SQLAlchemy reflection that supports multiple databases. It turns out that authors of application defined postgresql with lowercase tables/columns, eg. job.jobstatus while sqlite has mixed case, eg Job.JobStatus. I'm using DeclarativeReflectedBase from examples to combine reflection and declarative style.
The issue: Configure SQLAlchemy to work with tables/columns case insensitive with reflection
I have done so far:
I have changed DeclarativeReflectedBase.prepare() method to pass quote=False into Table.__init__
What is left to be solved:
relationship definitions still has to obey case when configuring joins, like primaryjoin="Job.JobStatus==Status.JobStatus".
configure __tablename__ based on engine type
The question: Are my assumptions correct or is there more straightforward way? Maybe I could tell reflection to reflect everything lowercase and all problems are gone.
you'd probably want to look into defining a ".key" on each Column that's in lower case, that way you can refer to columns as lower case within application code. You should use the column_reflect event (See http://docs.sqlalchemy.org/en/latest/core/events.html#schema-events) to define this key as a lower case version of the .name.
then, when you reflect the table, I'd just do something like this:
def reflect_table(name, engine):
if engine.dialect.name == 'postgresql':
name = name.lower()
return Table(name, autoload=True, autoload_with=engine)
my_table = reflect_table("MyTable", engine)
I think that might cover it.

ASP.NET MVC - getting started with Linq and SQL procedure

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)

Resources