postgresql jsonpath support - json

I am running postgresql 12 and am trying to use jsonpath.
I'd like to use jsonb_path_query_first to get the length of an array. I know I can do json_array_length but would rather not.
Here is the query that I am trying to make work.
select jsonb_path_query_first('{"a":3,"b":6,"s":[1,2,3,4,5], "d":{"v":4}}'::jsonb, '$.s.length()'::jsonpath);
ERROR: syntax error, unexpected '(', expecting end of file at or near "(" of jsonpath input
LINE 1: ...a":3,"b":6,"s":[1,2,3,4,5], "d":{"v":4}}'::jsonb, '$.s.lengt...
Does the version of jsonpath in postgresql 12 not support this type of jsonpath functionality.

You can use the .size() method for this:
test# select
jsonb_path_query_first(
'{"a":3,"b":6,"s":[1,2,3,4,5], "d":{"v":4}}'::jsonb,
'$.s.size()'::jsonpath
);
jsonb_path_query_first
════════════════════════
5
(1 row)

Related

Converting raw laravel mysql json query into postgres syntax

I have the following query that works in MYSQL
$query->whereRaw('
ST_Distance_Sphere(
point(address->>"$.longitude", address->>"$.latitude"),
point(?, ?)
) * .000621371192 < ?
', [$longitude, $latitude, $distance]);
When i deployed to heroku im getting this error
SQLSTATE[42703]: Undefined column: 7 ERROR: column "$.longitude" does not exist
LINE 3: point(address->>"$.longitude", address->>"$.latitu...
Im using postgres on heroku and i know the issue is something to do with the json operator.
What is the $.<json key> syntax called? and how do i convert this raw sql query to postgres?
In the worse case i leaning on switching my database to mysql if i cant solve this by Wednesday as it wont be a big deal
Postgres uses double quotes as delimiter for columns, so you must use single quotes to indicate strings. Laravel will take care of it
st_distance_sphere(
st_point(address->>'$.longitude',address->>'$.latitude'),st_point(?, ?)
)

Unable to use ST_Intersects on geometry field

I have converted the Personal Geodatabase of ESRI (*.mdb file) into the PostGIS enabled PostgreSQL database using FWTools. On which I get my geometry field named as wkb_geometry as follows
wkb_geometry geometry(Geometry, 3148),
While querying the database using ST_Intersects on where I get following error
SQL
SELECT <other fields>,
"wkb_geometry" AS "_smtmp_" FROM parcel WHERE <condition>
AND ST_Intersects(((E'\\001\\003\\000\\000 L\\014\\000\\000\\001\\000\\000\\000\\005\\000\\000\\000\\020\\2625\\334i\\032\\034A\\273n\\256E\\033\\340GA\\020\\2625\\334i\\032\\034A\\017\\261\\014\\353\\037\\340GA\\262\\304\\047\\007\\217\\032\\034A\\017\\261\\014\\353\\037\\340GA\\262\\304\\047\\007\\217\\032\\034A\\273n\\256E\\033\\340GA\\020\\2625\\334i\\032\\034A\\273n\\256E\\033\\340GA'))
,"wkb_geometry")
Error
ERROR: parse error - invalid geometry
LINE 1: ... parcel WHERE parcelno < 50 AND ST_Intersects(((E'\001\0...
HINT: "\0" <-- parse error at position 2 within geometry
ERROR: parse error - invalid geometry
SQL state: XX000
Hint: "\0" <-- parse error at position 2 within geometry
Character: 245
I am using SharpMap in front end.
The problem here is not with the geometry field, but with the query.
You should use the ST_GeomFromEWKB function like this
SELECT <other fields>,
"wkb_geometry" AS "_smtmp_" FROM parcel WHERE <condition>
AND ST_Intersects(ST_GeomFromEWKB(E'\\001\\003\\000\\000 L\\014\\000\\000\\001\\000\\000\\000\\005\\000\\000\\000\\020\\2625\\334i\\032\\034A\\273n\\256E\\033\\340GA\\020\\2625\\334i\\032\\034A\\017\\261\\014\\353\\037\\340GA\\262\\304\\047\\007\\217\\032\\034A\\017\\261\\014\\353\\037\\340GA\\262\\304\\047\\007\\217\\032\\034A\\273n\\256E\\033\\340GA\\020\\2625\\334i\\032\\034A\\273n\\256E\\033\\340GA')
,"wkb_geometry")

How to use Postgres JSON operators in a WHERE condition with jOOQ?

I have a JSONB column which holds arrays of strings, eg: ["foo", "bar"]
I want to write the jOOQ equivalent of:
SELECT * FROM sometable WHERE somecolumn ?| <mylist>
...where should be bound to a java List of string tag names.
There doesn't appear to be any direct support for ?| in jOOQ 3.8. I have looked at binding to raw sql in a condition but I'm not quite sure the syntax; it gets even worse if trying to use the ? postgres operator which conflicts with the binding expression.
UPDATE: the stacktrace with 3.8.3
I stripped this down to a minimal test. When adding a condition like this using jOOQ 3.8.3:
query.addConditions(DSL.condition("sometable.tags ?| array['sometag']"));
Produces a stacktrace like this:
Caused by: org.postgresql.util.PSQLException: No value specified for parameter 1.
at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:228)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:163)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:622)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:472)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:465)
at org.jooq.tools.jdbc.DefaultPreparedStatement.execute(DefaultPreparedStatement.java:194)
at org.jooq.impl.AbstractResultQuery.execute(AbstractResultQuery.java:269)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:348)
... 36 more
An issue related to jOOQ parsing ?| and similar operators as bind variables has been addressed in jOOQ 3.8.3: https://github.com/jOOQ/jOOQ/issues/5307
JDBC limitation
Note, in addition to the above, there is also a JDBC limitation that I have documented in a separate question. In jOOQ, you can work around this JDBC limitation by specifying:
Settings settings = new Settings().withStatementType(StatementType.STATIC_STATEMENT);
See also: http://www.jooq.org/doc/latest/manual/sql-execution/statement-type
Or, alternatively, by falling back to using the jsonb_exists_any() function instead of the ?| operator.

How to call mysql date_format function in JPQL

I'm developing a webservice using eclipseLink 2.4.1, JPA 2.0.1 and other supporting tools. I have created a table which has a column of date type. I want to get records of a particular date. I'm using date_format of mysql and getting following error:
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing[select o from Clients o where FUNCTION('date_format',{o.effectiveStartDate},{d,'dd-mm-yyyy'})='27-05-2014']
[90, 111] The JDBC escape format does not start with either 'd', 't' or 'ts'.
[110, 110] The JDBC escape format is missing the close quote.
Even I tried to call date_format directly as :
Select o from Clients o where date_format(o.effectiveStartDate '%d-%m-%Y'})='27-05-2014'
Then I get syntax error parsing. invalid token [(].
I tried with namedQuery also but didn't get success.
How to call such function JPQL.
What is "{o.effectiveStartDate}" trying to do ? Remove the curly brackets or it will try to interpret that as a JDBC escape syntax date (which it isn't).
JPA 2.1 'FUNCTION' syntax would be
FUNCTION("date_format", o.effectiveStartDate, "the_date_format")
where "the_date_format" matches what MySQL accepts for its second arg as per http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
That of course requires you to use a JPA2.1 compliant implementation; your implementation may have some other vendor-specific FUNC or otherwise which is not portable and I've no time for when there's a standard variant that should do what you need

Get json value in pl/pgsql

I have json string like '{"ww":11}'
How can i get value 11 in pl/pgsql ?
I try:
json_var := '{"ww":11}'::json
json_var->ww;
json_var->"ww";
json_var.ww;
All of this failed.
Please help!
Dmitry
The json type is just text. The advantage of using it is syntax check. Just that.
Check this question
you need to typecast it to Json in order to do that.
select '{"ww":11}'::json->'ww' as ww_value
returns ....
11