Convert a Dart Row Object to a Map. (sqlJocky) - dart

I'd like to get the row data given to me by sqlJocky in my Dart Server application and convert it (with column names) to a Map. Ie. row['email'] == "somename".
The row data is a "_BinaryDataPacket" which is an extension of Row type. Right now the method recommended by the sqlJocky developer to access the data involves either knowing the column name of what your accessing in the database:
row.email == "somename"
or just ignoring the column name all together:
row[0] == "somename"
I've tried a few hacks to get at the column data and even edited the original sqlJockey code to get at _BinaryDataPacket._fieldIndex to made it public. While that did give me access to Symbol instances of the Column titles to build a map with, I would like to avoid modifying the developers stable code if at all possible.
I assume there has to be an easy way to get the column names and put them in a Map with the row data.
TLDR:
I want to Convert alpha.brava == "charle" into alpha["bravo"] == "charle".
Thanks

In your results, you have a fields list attribute.
So you could do this :
.then((Results results) {
results.forEach((Row row) {
Map r = new Map();
for(int i=0; i<results.fields.length; i++) {
r[results.fields[i].name] = row[i];
}
});
});
This will map the data from a row to the map

Related

Lua 5.0 - iterations over tables ignore duplicate keys even though values are different

Reading the injected comments in the Code Snippet should give enough context.
--| Table |--
QuestData = {
["QuestName"]={
["Quest Descrip"]={8,1686192712},
["Quest Descrip"]={32,1686193248},
["Quest Descrip"]={0,2965579272},
},
}
--| Code Snippet |--
--| gets QuestName then does below |--
if QuestName then
-- (K = QuestName) and (V = the 3 entries below it in the table)
for k,v in pairs(QuestData) do
-- Checks to make sure the external function that obtained the QuestName matches what is in the table before cont
if strlower(k) == strlower(QuestName) then
local index = 0
-- Iterates over the first two pairs - Quest Descrip key and values
for kk,vv in pairs(v) do
index = index + 1
end
-- Iterates over the second two pairs of values
if index == 1 then
for kk,vv in pairs(v) do
-- Sends the 10 digit hash number to the function
Quest:Function(vv[2])
end
end
end
end
end
The issue I'm running into is that Lua will only pick up one of the numbers and ignore the rest. I need all the possible hash numbers regardless of duplicates. The QuestData table ("database") has well over 10,000 entries. I'm not going to go through all of them and remove the duplicates. Besides, the duplicates are there because the same quest can be picked up in more than one location in the game. It's not a duplicate quest but it has a different hash number.
Key is always unique. It is the point of the key, that the key is pointing to unique value and you can't have more keys with same name to point different values. It is by definition by Lua tables.
It is like if you would want to have two variables with same name and different content. It does not make sense ...
The table type implements associative arrays. [...]
Like global variables, table fields evaluate to nil if they are not initialized. Also like global variables, you can assign nil to a table field to delete it. That is not a coincidence: Lua stores global variables in ordinary tables.
Quote from Lua Tables
Hashing in Lua
Based on comments, I update the answer to give some idea about hashing.
You are using hashing usually in low-level languages like C. In Lua, the associative arrays are already hashed somehow in the background, so it will be overkill (especially using SHA or so).
Instead of linked lists commonly used in C, you should just construct more levels of tables to handle collisions (there is nothing "better" in Lua).
And if you want to have it fancy set up some metatables to make it somehow transparent. But from your question, it is really not clear how your data look like and what you really want.
Basically you don't need more than this:
QuestData = {
["QuestName"]={
["Quest Descrip"]={
{8,1686192712},
{32,1686193248},
{0,2965579272},
},
},
}
As Jakuje already mentioned table keys are unique.
But you can store both as a table member like:
QuestData = {
-- "QuestName" must be unique! Of course you can put it into a table member as well
["QuestName"]={
{hash = "Quest Descrip", values = {8,1686192712} },
{hash = "Quest Descrip", values = {32,1686193248} },
{hash = "Quest Descrip", values = {0,2965579272} }
}
}
I'm sure you can organize this in a better way. It looks like a rather confusing concept to me.
You've said you can't "rewrite the database", but the problem is the QuestData table doesn't hold what you think it holds.
Here's your table:
QuestData = {
["QuestName"]={
["Quest Descrip"]={8,1686192712},
["Quest Descrip"]={32,1686193248},
["Quest Descrip"]={0,2965579272},
},
}
But, this is actually like writing...
QuestData["Quest Descrip"] = {8,1686192712}
QuestData["Quest Descrip"] = {32,1686193248}
QuestData["Quest Descrip"] = {0,2965579272}
So the second (and then, third) values overwrite the first. The problem is not that you can't access the table, but that the table doesn't contain the values any more.
You need to find a different way of representing your data.

Returning multi value in dynamic query using neo4j client

Following the question I asked: Build a dynamic query using neo4j client
I got an answer about how can I return value dynamically using string only.
When I'm trying to use the syntax to return multi values from the query it failed,
I tried the following query:
var resQuery2 = WebApiConfig.GraphClient.Cypher
.Match("(movie:Movie {title:{title}})")
.OptionalMatch("(movie)<-[r]-(person:Person)")
.WithParam("title", title)
.Return(() => Return.As<string>("movie, collect([person.name, head(split(lower(type(r)), '_')), r.roles])"));
I'm getting the following error:
The deserializer is running in single column mode, but the response
included multiple columns which indicates a projection instead. If
using the fluent Cypher interface, use the overload of Return that
takes a lambda or object instead of single string. (The overload with
a single string is for an identity, not raw query text: we can't map
the columns back out if you just supply raw query text.)
Is it possible to return multiple nodes using only strings?
We can't get an output like in the question you asked previously - this is due to the fact that you are asking for a Node (the movie) and a Collection of strings (the collect) and they have no common properties, or even styles of property.
Firstly, let's look at the painful way to do this:
var q = gc.Cypher
.Match("(movie:Movie)")
.OptionalMatch("(movie)<-[r]-(person:Person)")
.Return(() => Return.As<string>("{movie:movie, roles:collect([person.name, head(split(lower(type(r)), '_')), r.roles])}"));
var results = q.Results;
Here we take the query items (movie, r, person) and create a type with them the {} around the results, and cast that to a string.
This will give you a horrible string with the Node data around the movie and then a collection of the roles:
foreach (var m in results)
{
//This is going to be painful to navigate/use
dynamic d = JsonConvert.DeserializeObject<dynamic>(m);
Console.WriteLine(d.movie);
Console.WriteLine(d.roles);
}
You'd be a lot better off doing something like:
var q = gc.Cypher
.Match("(movie:Movie)")
.OptionalMatch("(movie)<-[r]-(person:Person)")
.Return(() => new
{
Movie = Return.As<Node<string>>("movie"),
Roles = Return.As<IEnumerable<string>>("collect([person.name, head(split(lower(type(r)), '_')), r.roles])")
});
var res = q.Results;
You could either JsonConvert.DeserializeObject<dynamic>() the Movie node, at your leisure, or write a strongly typed class.
In terms of a 'dynamic' object, I don't know how you were wanting to interact with the collect part of the return statement, if this doesn't help, you might need to update the question to show a usage expectation.

Looping over IQueryable element don't remember previous results

I'll try to explain this well, as it's quite strange.
I have a .NET MVC 4 app with Linq2SQL where I'm developing a search where users are able to seek elements by one or more different values.
I have a method that receive a string like: "value1&value2|value3&value4"
I pretend to return results where a determinated element contains value1 AND value2 OR that determinated element contains value3 AND value4.
I'm using an algorithm like this:
if (!String.IsNullOrEmpty(figuresSelected)) {
string[] groupsOR = figuresSelected.Split('|');
IQueryable<CENTROSOPERACIONES> sqlAuxOr = null;
foreach (string goupOR in groupsOR) {
string[] groupsAND = groupOR.Split('&');
IQueryable<CENTROSOPERACIONES>[] sqlAuxAnd = sql; //sql contains current query so far
foreach (string group in groupsAND) {
sqlAuxAnd = sqlAuxAnd.Where(o => o.CENTROS.FIGURASCENTROS.Any(f => f.FIGURAS.nombre == group));
}
if (sqlAuxAnd != null)
if (sqlAuxOr == null)
sqlAuxOr = sqlAuxAnd;
else
sqlAuxOr = sqlAuxOr.Union(sqlAuxAnd);
}
}
Well, the problem here is that, on every iteration inside the inner loop, sqlAuxAnd queries from the original data container on sql, not from the subresult obtained on the previous loop. I've followed the loop iteration by iteration and I've checked that sqlAuxAnd contains the expected subresult on the first iteration, but when reaching a second one that value is lost and a full query from the whole data is stored on it.
I've tried to store sqlAuxAnd partial results on different elements (I used an array) and checked it's evolution. On the first iteration sqlAuxAnd[0] contains the expected result and sqlAuxAnd[1] is empty, on the second iteration both elements, sqlAuxAnd[0] and sqlAuxAnd[1] contains the expected result from querying the second value from the whole dataset.
It seems I'm losing something here... probably it's something trivial, but I supose I'm too dizzy to see it. Anyone have any idea to share?
Feel free to ask for explanations on the matter, english is not my mother language and I supose that probably the question is not as well redacted as it should be.
You need to capture the group variable from the foreach:
foreach (string group in groupsAND) {
var captured = group;
sqlAuxAnd = sqlAuxAnd.Where(o => o.CENTROS.FIGURASCENTROS.Any(f => f.FIGURAS.nombre == captured));
}
See Linq query built in foreach loop always takes parameter value from last iteration.

Vaadin table rows

I'm using vaadin for UI,I have a entity class which has #OneToMany relationship, so Entity class has a list. so I want to display those values in a vaadin table,Could anyone explain me how to do that? Thank you, here is my class details
#OneToMany
private List<Driver> driverList = new ArrayList<Driver>()
This field in TempLocation class.
so when I query from the data I get one city and list of drivers. In vaadin table I could display query result in a single row(assume I have only one record in a database with more than one Driver)
Lets say my table has two columns "Location" and "Driver", so I want to display the results in multiple rows,I want to go through the List and take one drive at time display along with the City
Currently how it display in the table.
|Driver_______________| Location | (Table head)
[Driver1,Driver2,Driver3] | Kolonnanwa (row)
I want display this
|Driver______________| Location | (Table head)
Driver1______________|Kolonnawa (row 1)
Driver2______________| Kolonnawa (row 2)
Driver3______________| Kolonnanwa (row 3)
"_" used to display spaces ,
Hope you got what I want to express and can anyone tell me how to do this?
Thankxx in Advance
Thank you,
Cheers
If you only want a different way of filling the table , I recommend just iterating over the List of your drivers and create one row for everyone (assuming every driver knows his city)
Check this example:
/* Create the table with a caption. */
Table table = new Table("This is my Table");
/* Define the names and data types of columns.
* The "default value" parameter is meaningless here. */
table.addContainerProperty("Driver", String.class, null);
table.addContainerProperty("City", String.class, null);
/* Add a few items in the table. */
int i = 1;
for(Driver driver : driverslist){
table.addItem(new Object[] {
driver.getName(),driver.getCity(), new Integer(i));
i++
}
The Integer in the addItem()-method is for the rownumber.
Please check the articles given in the vaadin book about the use of the table component.

New to Lua - Table operation

Can someone explain this to me? I have figured it out through this tutorial that this is known as a table. Coming from a C/C++ background, can someone explain how this works (I am trying to understand some existing Lua code)?
config = {
devices = {
C56 = "/dev/ttyS2",
ELTRA = "/dev/ttyS3",
-- MICORE = "/dev/ttyS4",
HID = "/dev/ttyS1",
KEYCARD = {
-- [6] = { tty="/dev/ttyS1", speed=9600 },
[7] = { tty="/dev/ttyS4", speed=9600 },
},
},
}
Is it a config table, consisting of a device table but then there is a KEYCARD table? What are C56 and ELTRA called in Lua? Are they fields?
A table in Lua is just an untyped map, like Javascript objects or Python dictionaries. The table associates one value (like "devices" or 6) with another value (like "/dev/ttyS2"). The value could be another table. Tables are used to create objects, maps, and arrays.
In your example, the config variable references a table. That table has one element, "devices", whose value is another table. That table has 5 elements. Four of those elements ("C56", "ELTRA", "MICORE", and "HID") have strings as their values. The fifth element ("KEYCARD") has a table as its value. That table has two elements (6, 7) whose values are other tables (each of two elements).
You have a config table two subtables within it, devices and Keycard, which is a subtable of devices. It's been a while since I used Lua, but to access, for example ELTRA, you'd type Config.devices.ELTRA and to access the 7 keycard you type Config.devices.KEYCARD[7]
To get at the speed of the keycard, you could do
speed = Config.devices.KEYCARD[7].speed

Resources