How to select an edge from the two vertices it connects - orientdb

I am trying to figure out the format for selecting an edge from the two vertexes that it connects.
The edge is in class 'Connection' and connects two 'Node' class vertices. The source node is #11:6 (UNIQUE indexed field ip = '10.64.32.100') and the destination is #11:7 (ip = '10.79.215.231'). The Connection class has a UNIQUE index on it's Connection.in and Connection.out properties.
I have tried the following queries that all return an empty set:
SELECT FROM Connection WHERE OUT(Node) = (SELECT FROM Node WHERE ip = '10.64.32.100')
SELECT FROM Connection WHERE (SELECT FROM Node WHERE ip = '10.64.32.100') IN OUT(Node)
SELECT FROM Connection WHERE #11:6 IN OUT(Node)
SELECT FROM Connection WHERE OUT(Node) = #11:6
I have checked to ensure the SELECT Node statement does infact return the correct rid (#11:6)
The larger context is updating the time field in the edge class 'Connection'. Connection has an EMBEDDEDLIST times of timestamps. Using pyorient, the calling code is:
try:
client.command("CREATE EDGE Connection FROM {0} TO {1} CONTENT {2}".format(src_str, dst_str, edge_cont))
except pyorient.exceptions.PyOrientORecordDuplicatedException:
client.command("UPDATE Connection ADD times = {0} WHERE {1}".format(time_list, <<!!QUERY GOES HERE!!>>)
As you can see from above, since I am using the caught exception to trigger the UPDATE, I don't have the #rid of the already existing and uniquely indexed Connection, thus I need a SELECT statement to specify which Connection object I want to add new times to. An alternate solution, would be to figure out how to return the #rid of the already existing Connection if it exists. I don't know how to do that either.
Can anyone help me find the correct SELECT statement to return the desired edge using only the two connecting vertices?

Try this query:
SELECT FROM Connection WHERE OUT = #11:6 AND (SELECT #rid FROM Node WHERE ip = "10.79.215.231") IN IN
Hope it helps.

Related

Graph traversal based on value of property of Edge | OrientDB

I have a graph which save topology of cloud infrastructure of a firm.It consists of Vertex called Object (Machines) and Edge called link (to denote how machines are linked to each other these links keep changing according as one machine can connect to different machines depending on need).
create class Object extends V
create class link extends E
Object vertices denotes Machine have properties to store config of the machine.
create property Object.ram long
create property Object.mem long
create property Object.cpu_core long
"link" edges links machine together to form a topology.It is a outbound edge from one machine to other. It has two time based property startTime which denotes the when this edge was created between two machines and endTime which initially is infinity and is update if we need to close this edge denoting that two machine and now no more linked.
create property link.startTime long
create property link.endTime long
create property link.l_id string
I want traverse from a Object(vertex) to all its connected Objects imposing a condition on link(edge) property while traversing that for a time say t, should be startTime <= t <= endTime.
For example Topology (Please click the link )
I want to traverse down from node rid #21:0 to all the edges using link edges whose startTime > 1488965393 and endTime < 1498965393. I am quite new to OrientDB and not able to figure out possible SQL command for the same.
Pseudo query (looking for something similar if possible):
SELECT * FROM (TRAVERSE out('link') FROM #21:0 while startTime > 1488965393 and endTime < 1498965393)
I was able to form a query that produced the desired output
SELECT id FORM (TRAVERSE out() FROM #21:0 WHILE (creationTime<= 1488965393 AND endTime >= 1488965393 AND #class='link')) WHERE #class= 'Object'

In orientdb what is difference between in('edge_type') and in(edge_type)

Trying this query in Grateful dead database provided in orientdb gives 146 records:
select expand(in('sung_by')) from V where name = 'Garcia'
But when we try the similar version of below query: select expand(in(sung_by)) from V where name = 'Garcia', 150 records are returned
Is it a bug?? Just trying orientdb from past week, followed tutorial from this website and this was second issue found.
By using select expand(in(sung_by)), the value of the field sung_by is resolved at query execution, but there is no field called sung_by, so it's null.
For this reason, it's like executing select expand(in()) in that case. By using 'sung_by', instead, only the edges with label sung_by will be traversed.
So, put always " or ' around edge's class/label to traverse.

How to distinct dupulicate nodes on orientdb?

I executed code below.
select expand(both()) from data
where value = '123456'
There are two nodes (node_A, node_B) whose value equals '123456'
and both of them connected with node_C and node_D.
Finally it returned node_C and node_D for several times
but I just want each of them for one time.
I tried to use distinct() around "expand(both())" but failed.
How can I distinct dupulicate nodes?
select expand(distinct(#rid)) from (select expand(both()) from data
where value = '123456')

Sql case stetement to check existing records and taking one

I have two parameters X and Y
Rules for these are only one of them can be null. They both can be existing, it's ok but they both can't be null.
I'm using this to check if they exist in database so I can assign one and rest of the SP can continue inserting.
SELECT #Id=id FROM Table WHERE (No = #x) OR (No = #y)
What I want to add is if they are both existing I want the Id to be the Id of #x.
I can't get the Case Statement right in my mind. Normally this is a no brainer but somehow I managed to get stuck.
ISNULL() will take the first non null value it finds.
SELECT #Id=id FROM Table WHERE No = ISNULL(#x, #y)

SELECT query in PostgreSQL

I am trying to retrieve values from a PostgreSQL database in a variable using a WHERE clause, but I am getting an error.
The query is:
select age into x from employee where name=name.GetValue()
name is the textcontrol in which I am entering a value from wxpython GUI.
I am getting an error as name schema doesn't exist.
What is the correct method for retrieving values?
"name.GetValue()" is a literal string, you are sending that to your db which knows nothing about wxpython and nothing about the variables in your program. You need to send the value of that data to your db, probably using bound parameters. Something like:
cur.execute("select age from employee where name=%s", [name.GetValue()])
x = cur.fetchone()[0] # returns a row containing [age] from the db
is probably what you're after. This will create a query with a placeholder in the database, then bind the value of name.GetValue() to that placeholder and execute the query. The next line fetches the first row of the result of the query and assigns x to the first item in that row.
I'm not positive what you are trying to do, but I think your issue might be syntax (misuse of INTO instead of AS):
SELECT age AS x FROM employee WHERE name = ....