SQL Response to JSON for non-trivial query - orientdb

I have this query that works in studio, which for a given worker, returns those other workers that recommend them.
[Worker:V] -> [RecommendedBy:E] -> [Worker:V]
Im constructing SQL to return some selected data from the E and the V that is recommending. So below works
SELECT out('RecommendedBy').firstName as recommendedByFirstName,
out('RecommendedBy').lastName as recommendedByLastName,
out('RecommendedBy').#rid as recommendedByRID,
outE('RecommendedBy').recommendationHeadline as headline
FROM Worker WHERE userName = 'paulw';
How can I convert the response to the above adhoc query to a single JSON object using #this.toJSON? I can get something like below to work Ok:
SELECT #this.toJson('rid,version,fetchPlan:in_RecommendedBy:1') FROM Worker WHERE userName = 'paulw';
but not for the first SQL. Any help appreciated thanks!

I'm not sure I understand what you need, is this useful?
SELECT #this.toJson() FROM (
SELECT out('RecommendedBy').firstName as recommendedByFirstName,
out('RecommendedBy').lastName as recommendedByLastName,
out('RecommendedBy').#rid as recommendedByRID,
outE('RecommendedBy').recommendationHeadline as headline
FROM Worker WHERE userName = 'paulw')

Related

How do you pass variables into Mirth Database Reader Channels for SQL expressions?

I can't find any documentation on how to manager parameters into Database Reader SQL statements?
-> this is a simplified example: I am not looking for scripting a variable to "yesterday" which is easy to express in SQL. That's not the point. I have have more complex variables in the actual SQL statement I'm trying to martial in. I just want to know how to get variables into the SQL form if possible.
-> "you can just do that in JavaScript": the actual queries I need to run are about a hundred lines long, I don't want to maintain and debug a query build by concatenating strings and then deal with escaping 'quoted' things everywhere in the SQL. I really prefer to maintain an actual SQL statement that copy/paste works in a SQL IDE.
How do we pass in parameters into the SQL block at the bottom of the Database Reader form?
SELECT patientsex, visitnumber, samplereceived_dt, sr_d, sr_t, orderpriority, orderrequestcode, orderrequestname
FROM mydata.somedata
WHERE sr_d = (${DateUtil.getCurrentDate('yyyyMMdd')})::integer;
JavaScript is the feasible way to achieve this, with SQL statements defined inside Mirth connect or have the SQL statements bundled in a stored procedure then use SQL server's Exec command within Mirth connect to call the stored procedure while passing the parameters (interestingly using JavaScript).
For example
var dbConn;
try {
dbConn = DatabaseConnectionFactory.createDatabaseConnection('','DB:Port\instance','user','pass');
var paramList = new java.util.ArrayList();
paramList.add($('patientId'));
paramList.add($('lastName'));
paramList.add($('firstName'));
var result = dbConn.executeCachedQuery("SELECT * FROM patients WHERE patientid = ? AND lastname = ? AND firstname = ?) ",paramList);
while (result.next()) {
//you can reference by column index like so...
//result.getString(1);
}
} finally {
if (dbConn) {
dbConn.close();
}
}
Should be noted that the parameters you add to the list MUST be in order.

unable to set db using string while coding in python

I would like to know if there is a way to set db using a variable
For example: I am coding in Python, and I connect using client = MongoClient(uri). All goes fine.
There are 4 dbs: test1,test2,test3,test4.
I am able to list them all.
dblist = client.list_database_names()
print(dblist)
All goes fine.
Now, Instead of connecting/ using
db = client.test1
Is there a way to use a string rather than actual name of the db?
such as str = 'test1', and then db=client.str.
(this doesn't work)
In my program , I display the list of dbs first and then I am taking user input on the db , and proceed with further flow, but unable to do so.
Please help.
You cannot add string as an name when it comes to that. However there is another function that takes string of the name of certain database and gets the database.
db=client.get_database('test')
Here is the documentation: https://api.mongodb.com/python/current/api/pymongo/mongo_client.html

celery: Weird signs in result_backend (postgresql-db)

I am using a celery installation configured with a result_backend postgresql-database.
It's working ok so far, but I have a little problem with the "formatting" of the result in the db.
I am returning several values (a string, an Exception with an error-message, an int) from my task (function-name under #app.task).
But when I take a look into the table "celery_taskmeta"-table which celery writes to when result_backend-option is set, I see some more 'crypted' values next to the expected values (screenshot of the select-result).
Everything circled in red are the expected values. The 273 at the bottom for example is the octal representation of the int I returned.
Of course I can filter out all that unwanted stuff but if it contains some useful information or might be different under some other circumstances...
Does anyone know why there are those strange signs?
Thanks for your help,
Lukas
Don't query the database directly to get the result data - celery provides an API for this - see the docs
In your case you can do this to get the output of your task and any traceback:
cur.execute("SELECT task_id FROM celery_taskmeta WHERE date_done ...")
rows = cur.fetchall()
for row in rows:
task_id = row[0]
# you'll need to do something different if you have GroupResult
result = celery.AsyncResult(task_id)
LOG.info(result.result)
if result.traceback:
LOG.error(result.traceback)

How to export DBUnit result set from query

Someone asked a similar question here: 1
This code works:
IDatabaseConnection connection = getConnection();
ITableFilter filter = new DatabaseSequenceFilter(connection);
IDataSet dataset = new FilteredDataSet(filter, connection.createDataSet());
FlatXmlDataSet.write(dataset, new FileOutputStream(file));
It dumps the entire database to an XML flat file. Good. We know my username/password/drivername/connection string etc.. are good.
Now how do I modify it so I can supply a SQL select statement and export the results of a single query?
I did some google searching and found this code:
System.out.println("begin partial");
IDatabaseConnection connection = getConnection();
QueryDataSet partialDataSet = new QueryDataSet(connection);
partialDataSet.addTable("gov", "SELECT * FROM GOV_UNIT");
partialDataSet.addTable("gov_unit");
FlatXmlDataSet.write(partialDataSet, new FileOutputStream("partial-dataset.xml"));
System.out.println("end partial");
This does not create the file "partial-dataset.xml" like I hoped. It just prints "begin partial" and "end partial".
Can someone help me make this code work? What am I doing wrong? Of course, when get this to work, I'll replace the simple query with a recursive join that works with my postgresql database.
Thanks
siegfried
Your code is OK. This line FlatXmlDataSet.write(partialDataSet, new FileOutputStream("partial-dataset.xml"));, does not create XML file, you have to create it manually before . This is all you need.

simple where clause SSRS 2005 parameter not working

this should be a simple thing but I've spent hours to no avail. Basically, I need to look up a salesrep # in a SQL database using the user's Window's user id. The format of the user id is
"Norstar\kjones" and I need the "kjones" portion of it.
using the split function, I am able to pull just the 'kjones' part out:
split(User!UserID,"\").GetValue(1)
I've created a parameter called SlsmnNum and created a dataset to be used to look up the salesrep # using the user id (the slsm_num field is a varchar, not an integer):
select slsm_num from Salesman_Msid where slsm_msid = ''' + split(User!UserID,"\").GetValue(1) + '''
However, I get no results. How can I get the select to work?
alternatively, I tried the following:
in parameter SlsmnNum, I set the default to an expression using:
split(User!UserID,"\").GetValue(1) and this returns 'kjones', as expected.
I created a SECOND parameter (which is positioned BELOW the SlsmnNum parameter), SlsmnNum2, that has a default (and an available) value using a query, which is a dataset containing the following select statement:
select slsm_num from Salesman_Msid where slsm_msid = (#SlsmnNum)
When I run the query on the Data tab, when I type in 'kjones' into the parameter box, it returns '1366', the salesrep # I'm expecting.
But, when I Preview the report, all I get in SlsmnNum2 box is Select a Value and nothing is there (it should return '1366').
Any help would be greatly appreciated!
Try your first approach with Query Text as
="select slsm_num from Salesman_Msid where slsm_msid = '" & split(User!UserID,"\").GetValue(1) & "'"