Creating nodes and relationships - graph-databases

I want to populate a graph with vertices and edges yet I’ve found in the docs multiple ways of doing this and it is unclear to me what the pros and cons of each one are. There’s:
create_vertex(), 2) .save(), 3) .create()...execute()

Query modules are extensions of the Cypher query language. It is useful if you if you need to expand the Cypher language with custom procedures. Memgraph provides public APIs for writing custom query modules in Python, C/C++ and Rust. Maybe this is not the best first step you take to populate a graph with nodes and relationships.
Using OGM or Object Graph Mapper you are able to create classes representing the nodes and relationships. It is useful if you have a certain database structure as you are able to define node properties. After creating classes, saving nodes and creating realtionships between them, OGM makes fetching and further using node properties easy. On instance of defined graph object with the help of the GQLAlchemy (OGM), you can call save(db) method to save nodes or relationships to a database. It is not necessary to use OGM if you don’t have a strict schema.
Query builder is useful if you are not that familiar with Cypher query language, since it helps you build and execute a Cypher query. With Cypher, you can create a node by running
CREATE (n:Label {name: 'Leslie'})
directly from you Python code with
`memgraph.execute(“CREATE (n:Label {name: 'Leslie'});”)`
or by using the query builder:
`from gqlalchemy import Create
query = Create().node(labels="Person", name="Leslie").execute()`
If you want to create a relationship between two nodes, you can do it like this:
`CREATE (:Person {name: 'Leslie'})-[:FRIENDS_WITH]->(:Person {name: 'Ron'});`
and execute it with
`memgraph.execute`
or by using query builder:
`from gqlalchemy import Create
query = Create()
.node(labels="Person", name="Leslie")
.to(relationship_type="FRIENDS_WITH")
.node(labels="Person", name="Ron")
.execute()`

Related

WordPress creating wp_rewrite rules for state and city through a database

I am attempting to create a wp_rewrite plugin or a function file (which I'm unaware of the location) to query a database to produce post results.
I want the rewrite to look like the following
/state_name/city_name/
Is it possible to point me to the right direction on how to either create a plugin or what files to modify to create the rewrite rules based on the criteria above.
Is it possible to query a database for the results, as there will be many cities and states?

How to use Difference function in EntityFrameworkCore?

I have this query to be executed :
Select * From Products WHERE Title like '%search text%'
ORDER BY Difference(Title, 'search text') DESC
Now I want to implement the above query using EntityFrameworkCore and linq.
So how can I call difference function to order products by closest match in Title column?
Every IQueryable holds an Expression and a Provider. The Expression holds the query that must be performed. The Provider knows who has to execute the query, usually a database management system. It is the task of the Provider to translate the Expression into the language that the database understands (something SQL-like) and to execute the query. The Provider will fetch the results in an efficient way and return the queried data as an enumerable object.
The IQueryable implements IEnumerable.
When you use LINQ functions like ToList(), FirstOrDefault(), Any(), or use the query in a foreach, then internally IEnumerable.GetEnumerator() is called and Enumerator.MoveNext()
This will order the Provider to translate the Expression into SQL and execute the query. The returned enumerable is used to enumerate over the returned items.
It is the task of the programmer of the class that implements the IQueryable to implement the translation of the Expression into SQL. This is not easy, and I think the people who created entity framework did a great job.
However, some items known in SQL are very difficult to implement. Among those are the notions of SoundEx and Difference. I'm not sure, but I think that one of the reasons that made this difficult is that they are typically something used in SQL, and not in any other kind of IQueryable systems.
In fact, there are a several functions that are not supported by entity framework. See Supported and unsupported LINQ methods (LINQ to entities).
Your DbContext is an abstract representation of your database model. Users of it should not care whether it uses Microsoft SQL, MySQL, or whether it is a data collection that doesn't use anything similar to SQL.
But if you are absolutely certain that it is okay to limit your DbContext to only a certain kind of databases, one that knows the concepts of SoundEx and Difference, consider creating a stored procedure for your query. See How to call a Stored Procedure in Entity Framework
SQL can not understand the Difference function written in c#. To make it work you will have to fetch values from Products table in a c# collection like List
Then do ordering on that list using Difference function

Multimapping in Dapper Without Custom SQL

Is there a way to use multimapping in Dapper in a generic way, without using custom SQL embedded in C# code?
See for example
Correct use of Multimapping in Dapper
Is there a generic way to query the data from 2 related entities, where common fields are determined automatically for join?
Don't do this. Don't even think this way! Databases are long lasting and normalized. Objects are perishable and frequently denormalized, and transitioning between the two is something to do thoughtfully, when you're writing your SQL. This is really not a step to automate. Long, painful experience has convinced many of us that database abstractions (tables and joins) should not just be sucked into (or generated out of) code. If you're not yet convinced, then use an established ORM.
If, on the other hand, you absolutely want to be in control of your SQL, but its the "embedding" in string literals in C# that bugs you, then I couldn't agree more. Can I suggest QueryFirst, a visual studio extension that generates the C# wrapper for your queries. Your SQL stays in a real SQL file, syntax validated, DB references checked, and at each save, QueryFirst generates a wrapper class with Execute() methods, and a POCO for the results.
By multi-mapping, I presume you want to fill a graph of nested objects. A nice way to do this is to use one QueryFirst .sql per class in your graph, then in the partial class of the parent, add a List of children. (QueryFirst generated POCOs are split across 2 partial classes, you control one of them, the tool generates the other.)
So, for a graph of Customers and their orders...
In the parent sql
select * from customers where name like #custName
The child sql
select * from orders where customerId = #customerId
In the parent partial class, for eager loading...
public List<Orders> orders;
public void OnLoad()
{
orders = new getOrders().Execute(customerId); // property of the parent POCO
}
or for lazy loading...
private List<Orders> _orders;
public List<Orders> orders
{
get
{
return _orders ?? _orders = new GetOrders().Execute(customerId);
}
}
5 lines of code, not counting brackets, and you have a nested graph, lazy loaded or eager loaded as you prefer, the interface discoverable in code (intellisense for the input parameter and result). Their might be hundreds of columns in those tables, whose names you will never need to re-type, and whose datatypes are going to flow transparently into your C#.
Clean separation of responsibilities. Total control. Disclaimer : I wrote QueryFirst :-)
Multimapping with Dapper is a method of running multiple SQL queries at once and then return each result mapped to a specific object.
In the context of this question, Multimapping is not even relevant, re: you're asking for a way to automatically generate a SQL query from the given objects and creating the correct joins which would result in a single SQL query which is not related to Multimapping.
I suspect what you're looking for is something along the lines of the Entity Framework. There are a couple of Dapper extension projects you may want to look into which will generate some of your SQL. See: Dapper.Rainbow VS Dapper.Contrib

Is it possible to import a database table to Drools Guvnor as a decision table?

We will be having data fed in this database table regularly, and I was wondering if it was possible to import this data on a timely basis into Drools Guvnor?
If you want to maintain rules in a database table, then you should be looking at rule templates:
http://docs.jboss.org/drools/release/6.0.1.Final/drools-docs/html_single/index.html#d0e4969
Rule templates provide a relatively simple mechanism for merging DRL with data.
FWIW - The documentation for this in the manual is poor, so here is a hint on the kind of thing you need to do:
To generate rules from a combination of database data and a template, you will need to import org.drools.template.jdbc.ResultSetGenerator. This class can be used to generate DRL code from a database query result set and a template.
// Get results from your DB query...
resultSet = preparedStmt.executeQuery();
// Generate the DRL...
resultSetGenerator = new ResultSetGenerator();
String drl = resultSetGenerator.compile(resultSet,
new FileInputStream("path/to/template.drt"));
Then you create a package through the API and add that generated DRL to it.

Is there any overhead with LINQ or the Entity Framework when getting large columns as part of an entity?

Let's say you have a table containing articles and you want want to display a list of them, excluding the actual article text. When you get a list of the article objects using LINQ or the Entity Framework, is there a LOT of overhead associated with getting that text column too? I assume that when you start enumerating the list, the article text will be stored in memory until the objects are disposed of.
So would it make sense to create an intermediary object that doesn't contain the text column? If so, how would you do this? Make a class inside your DAL, allow the ORM to automatically create one by setting up a stored procedure, or some other process?
The overhead isn't huge (just the cost of sending the data over the wire), but if you don't need the data sure, don't return it. I find the easiest way is to use anonymous types:
from a in Context.Articles
select new {Name = a.Name, Author = a.Author};
Since you're not actually materializing any Article instances, the Entity Framework won't need to fill out all the properties of an instance.
If you don't need the data you should definitely create a different type. By convention I typically name this sort of class "nnnInfo" or "nnnListItem". To create ArticleListItem, in L2S, simply drag the table to your DataContext designer a second time. Then rename it from 'Article1' to 'ArticleListItem' and remove the unneeded properties (rt click, delete). In EF, the process would be similar. As Craig notes, you could use anonymous types, but by creating a concrete type, you can reuse throughout your app, expose via services, etc.
A second way to do this would be to create the class manually and write an extension method to return ArticleListItem:
public static IQueryable<ArticleListItem> ToListItems(this IQueryable<Article> articles)
{
return from a in articles select new ArticleListItem{ Title = a.Title, ...}
}
This would allow you to "cast" any queries against Article as ArticleListItem...

Resources