How to convert a PGresult to custom data type with libpq (PostgreSQL) - c

I'm using the libpq library in C to accessing my PostgreSQL database. So, when I do res = PQexec(conn, "SELECT point FROM test_point3d"); I don't know how to convert the PGresult I got to my custom data type.
I know I can use the PQgetValue function, but again I don't know how to convert the returning string to my custom data type.

The best way to think about this is that data types interact with applications over a textual interfaces. Libpq returns a string from just about anything. The programmer has a responsibility to parse the string and create a data type from it. I know the author has probably abandoned the question but I am working on something similar and it is worth documenting a few important tricks here that are helpful in some cases.
Obviously if this is a C language type, with its own in and out representation, then you will have to parse the string the way you would normally.
However for arrays and tuples, the notation is basically
[open_type_identifier][csv_string][close_type_identifier]
For example a tuple may be represented as:
(35,65,1111111,f,f,2011-10-06,"2011-10-07 13:11:24.324195",186,chris,f,,,,f)
This makes it easy to parse. You can generally use existing csv processers once you trip off the first and last character. Moreover, consider:
select row('test', 'testing, inc', array['test', 'testing, inc']);
row
-------------------------------------------------
(test,"testing, inc","{test,""testing, inc""}")
(1 row)
As this shows you have standard CSV escaping inside nested attributes, so you can, in fact, determine that the third attribute is an array, and then (having undoubled the quotes), parse it as an array. In this way nested data structures can be processed in a manner roughly similar to what you might expect with a format like JSON. The trick though is that it is nested CSV.

Related

How to create an Array (IDataHolder) in Thinkscript? (Thinkorswim)

I am trying to create an irregular volume scanner on Thinkorswim using Thinkscript. I want to create an array of volume's in past periods so that I can compare them to the current period's volume (using fold or recursion). However, while the Thinkorswim documentation details what is called an IDataHolder datatype, which is an array of data, I cannot find how one can actually create one, as opposed to just referencing the historical data held by Thinkorswim. Here is the documentation: https://tlc.thinkorswim.com/center/reference/thinkScript/Data-Types/IDataHolder
I have tried coding something as simple as this to initialize an array:
def array = [];
This throws an error. I have tried different types of brackets, changing any possible syntax issues, etc.
Is this possible in the Thinkscript language? If not, are there any workarounds? If not even that, is there a third party programming interface that I could use to pull data from Thinkorswim and get a scanner that way? Thanks for any help.
IDataHolder represents data such as close, open, volume, etc, that is held across multiple bars or ticks. You can reference one of these pre-defined data series, or you can create your own using variables: def openPlus5cents = open + 0.05, say, would be an IDataHolder type value.
There's no way to create an array in the usual programming sense, as you've found, so you'll have to be a little creative. Perhaps, say, you could do the comparison within the fold? volume[1] > volume, or the like? Maybe post another question with an example of the comparison you're trying to do?

SPARQL: array-strings conversion / extraction

I am doing a SPARQL Query that for one variable gives me the output "[-1.6101874126499998e-19]". This is obviously a string containing an array (that could also contain more numbers). Is there a way to access the number in it or does it need to be done in the underlying graph?
If it needs to be changed in the graph, what is a good ontological way to create multi-dimensional arrays...?
xsd:float(SUBSTR(?var,2,STRLEN(?var)-2)) does the job in this case, but it's quite hacky :/

How can I parse this unknown array notation? What language is this array notation from?

I am accessing data from an old database and several of the records have this array notation which defines the criteria for a real estate search. There are several hundred and I need to convert them to data I can use in JS and PHP.
Here is an example of the array notation. I haven't been able to find any other questions asking about this format.
a:14:{s:2:"id";s:22:"our-listings-metrolist";s:3:"map";a:4:{s:8:"latitude";s:17:"38.93309311783631";s:9:"longitude";s:19:"-120.74187943878752";s:4:"zoom";s:1:"8";s:4:"open";s:1:"0";}s:4:"feed";s:15:"ncarmetrolistca";s:6:"panels";a:2:{s:9:"office_id";a:3:{s:7:"display";s:1:"1";s:9:"collapsed";s:1:"0";s:6:"hidden";s:1:"0";}s:4:"type";a:3:{s:7:"display";s:1:"1";s:9:"collapsed";s:1:"0";s:6:"hidden";s:1:"0";}}s:9:"office_id";s:5:"01PHA";s:11:"search_type";s:0:"";s:3:"idx";s:15:"ncarmetrolistca";s:14:"search_subtype";s:0:"";s:10:"snippet_id";s:22:"our-listings-metrolist";s:13:"snippet_title";s:42:"Our Sacramento / Sierra Foothills Listings";s:10:"page_limit";s:1:"6";s:7:"sort_by";s:17:"DESC-ListingPrice";s:4:"view";s:4:"grid";s:12:"price_ranges";s:4:"true";}
It's not hard to understand and I will write my own parser if I have to but I'm hoping I don't need to. a defines an array, s defines a string, and i defines an integer. The integer after each definition character defines the length of the array, string, or integer, and then the value defined at that position representing either a key or a value.
What kind of notation is this? Is there someway I can parse this quickly into a format that can be used in JS and PHP. Do I need to build my own parser?
That's the serialization of an object in php.
For instance:
$obj = ['a'=>1, 'b'=>true, 'c'=>'foo'];
echo serialize($obj); /* prints: a:3:{s:1:"a";i:1;s:1:"b";b:1;s:1:"c";s:3:"foo";} */
To unserialize, just use the unserialize() function.

Array parameters in FMI 2.0

In FMI 2.0, array parameters are serialized to scalar variables.
Importing tools can display them as arrays, but their size is fixed and their handling is inefficient.
Better array support is currently in development by a working group of the FMI project, but I would like to know about workarounds how to handle array parameters in the meantime.
Ideas are to
hard code them (disadvantage: the are no paramters any more ...)
put them in a CSV file in the resources folder and read them at the start of the simulation (disadvantage: no parameter mask support, complicated)
put them in a string parameter and parse it at simulation start (disadvantage: limited length of strings, complicated)
Are there other ideas / workarounds? Thanks in advance.
Combinations of the ideas outlined in your question are also possible.
Hard code with selector parameter
Here the idea is to hard code several variants of your array and allow the user to select one with a parameter.
I did this in a recent project where a user needed to choose between different spatially resolved initial conditions (e.g. temperature profiles). We used a model to generate more than 100 different sets of spatially resolved initial conditions (each representing a different "history" of the modeled object), hard coded them as FORTRAN arrays (the inner core of the FMU was in FORTRAN), and used a single integer parameter to select which profile he wanted to use.
It worked very well and the user has no way of breaking it.
Shorten the array and interpolate
If the data in your array is smooth, you might be able to dramatically reduce the number of values you actually need to pass to your simulation - which would make serialization into scalar parameters less painful.
Within the FMU, interpolate to get the resolution you need.
String parameter to select csv file
You can use a string parameter to provide the path to a user-provided csv-file. I would not recommend this, because the user will most likely break it.

Algo - key-values expression to parse (C)

I'm working on a way to write in a string key-values data like this :
{k1=v1__k2=v2__k3=v3}
Not a big deal to parse, but the problem became bigger when I wanted to add the possibility to write a set of key-values as a value of a key, like this :
{k1=v1__k2={k21=v21__k22=v22}__k3=v3}
Also, it would be better to enable the possibility to add more depth in my structure, example:
{k1=v1__k2=v2__k3={k31={k311=v311__k312=v312__k313={k3131=v3131}}__k32=v32}
I tried (in C) to parse it, but it becomes harder to parse it in a simple way (split __ and {} characters) and I also tried regex to split each key and value but I lose the hierarchy (depth) data...
Constraints of the problem:
Data structure can accept more than one depth
special characters (or set of characters) can be changed (different from __ or {})
Anyone knows a good algorithm?
I'm not sure, but JSon format has the same constraints, am I wrong?
Many thank #ll
Rather than attempt to write you own data representation, I suggest you use either XML or JSON. Both are more than up to the job. If you are using XML, libxml is your friend. There are many many JSON libraries, including (for example), libyajl. Both XML and JSON are tried and tested, cope with escaping and so fort. XML also allows querying with xpath and (if you want it) DTD functionality. Believe me, this is better than reinventing the wheel.

Resources