I have proram written using java,jps.Now i can see logs on cosole as folllows:
INSERT INTO XYZ (a,b,c) VALUES (?, ?, ?) [org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement]
I also want to see the values passed to insert query in log.How can i see it?AM using openjpa as jpa provider.
Set openjpa.ConnectionFactoryProperties=PrintParameters=True.
Related
I'm trying to query whether any of a set of polygons (passed in at runtime) intersects with a set of polygons stored in the database in the "enclosing_polygons" field, which is a MultiPolygonField.
Here is an example of the query:
select * from my_table where field1 = any (?) and field2 = any (?) and (
ST_Intersects(ST_GeometryFromText('POLYGON((? ?, ? ?, ? ?, ? ?, ? ?))'), enclosing_polygons) or
ST_Intersects(ST_GeometryFromText('POLYGON((? ?, ? ?, ? ?, ? ?, ? ?))'), enclosing_polygons))
and detection_type = 0 order by confidence desc limit 2000
The query works fine with hardcoded values, but when I try to parameterize it, Postgres does not seem to recognize the ? placehoders for the polygon points as parameters when I try to populate them.
When I set the first two parameters (for field1 and field2), these JDBC statements succeed:
statement.setArray(1, array1)
statement.setArray(2, array2)
However, if I try to set any parameters beyond these first two, they fail. This statement:
statement.setDouble(3, point1x)
fails with the following error:
The column index is out of range: 3, number of columns: 2.
Why does Postgres not recognize these ?s in the POLYGON constructor as query parameters?
How can I make this work?
It is up to your driver to implement the ? placeholders, PostgreSQL never sees them. In your driver, like in almost all drivers, the question marks occurring inside the single quotes are just literal question marks, not place holders.
You probably need to construct the POLYGON((...)) string yourself, then pass that whole string into the query as a single placeholder. So that part of the query would look like ST_Intersects(ST_GeometryFromText(?), enclosing_polygons)
There are alternatives but they mostly just move the problem around without directly solving it. If you really want to just use plain question marks with each bound to one number, you could replace the ST_GeometryFromText function with something like:
st_makepolygon(st_makeline(array[st_makepoint(?,?),st_makepoint(?,?),st_makepoint(?,?),st_makepoint(?,?),st_makepoint(?,?)]))
I am migrating an application from JPA 2.0 with OpenJPA on WebSphere 8.5 to JPA 2.1 with EclipseLink on WebSphere 9.0, using DB2 12 on z/OS. Generally it is working, but one rather complex query is failing. I could localize the problem to the usage of a custom DB2-function call within a criteria-query. The call looks something like this:
criteriaBuilder.function("REPLACE", String.class, fromMyEntity.get("myField"), criteriaBuilder.literal("a"), criteriaBuilder.literal("b"));
This produces the following error (had to translate some error texts, since WebSphere localizes them, and anonymize my field/table names, so labels/names might not be 100% exact):
Error: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.3.WAS-v20160414-bd51c70): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.ibm.websphere.ce.cm.StaleConnectionException: DB2 SQL Error: SQLCODE=-171, SQLSTATE=42815, SQLERRMC=2;REPLACE, DRIVER=3.72.44
Errorcode: -171
Call: SELECT COUNT(ID) FROM MY_TABLE WHERE REPLACE(MYFIELD, ?, ?) LIKE ?
bind => [abc, def, %g%]
Query: ReportQuery(referenceClass=MyEntity sql="SELECT COUNT(ID) FROM MY_TABLE WHERE REPLACE(MYFIELD, ?, ?) LIKE ?").
What really confuses me, if I take the generated query, replace the placeholders with the given bound parameters, and execute that in a database client myself, it works without error.
The documentation states, the first parameter must not be empty (https://www.ibm.com/support/knowledgecenter/en/SSEPEK_12.0.0/sqlref/src/tpc/db2z_bif_replace.html), and indeed if I use an empty string either as a literal in the query or in my database client, it will produce the above error. But none of the rows in the database contain an empty value. There are checks in place to prevent this in the old environment, but they don't appear to work with the new environment, so I disabled them while searching for the problem, and made sure myself no empty values exist. I can even use the primary key as the first parameter, and it will still fail, and that can't even contain an empty/null value.
Using other functions (like TRANSLATE) works, I also tried using "SYSIBM.REPLACE" as name, and different combinations of parameters, but as soon as I use a real column to replace data in, it fails. Anybody got any ideas what I am doing wrong here?
This is my table definition:
CREATE TABLE "MY_TABLE" (
"ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (NO MINVALUE NO MAXVALUE NO CYCLE CACHE 20 NO ORDER ),
"MYFIELD" VARCHAR(160) FOR MIXED DATA WITH DEFAULT NULL,
[....]
) IN "<Database>"."<Tablespace>" PARTITION BY SIZE EVERY 4 G AUDIT NONE DATA CAPTURE NONE CCSID UNICODE;
I'm using views in Postgresql and I encounter a problem in Zend: I can't retrieve the last inserted value of an insert request.
Example:
I have an user_view and when I insert in, it does instead:
INSERT INTO basic_user(name, email) VALUES [...]; INSERT INTO extended_user(id, phone) VALUES (lastval(), [...]);
When I execute the request in Zend and call getGeneratedValue() it returns NULL...
How can I get the lastval() ?
I tried to use the RETURNING clause in my INSERT but without success..
Thx for your help.
The solution in PGSQL is currval(Sequence name) PG Doc.
Example
INSERT INTO basic_user(name, email) VALUES [...]; INSERT INTO extended_user(id, phone) VALUES (currval(basic_user_seq)(), [...]);
So I have 2 tables that I need to insert similar data into. They are employee DBs that different applications access. They are:
dbo_Employees
dbo_EmpDefaultSchedules
dbo_EmpUsers
dbo_EmpDefaultLocation
So on dbo_Employees, when you insert a row, the primary key is auto-created. That column is called EmpID. There are a total of around 22 different columns being added to these different tables. They are things like FirstName, LastName, Address, Phone, etc., etc.
I'm trying to create a script where I can take a list of variables, enter them in once, and run that script to add it to the 4 tables at once (eventually a web page that HR will enter the info and create it themselves)
The only one that is giving me trouble is dbo_EmpDefaultSchedules. This is because I need the primary key from dbo_Employees (EmpID) to insert into dbo_EmpDefaultSchedules, and obviously it's not created until the first part of the script runs. My insert statement for the second part is this:
INSERT INTO [Database].[dbo].[EmpDefaultSchedules] (StaffCode, EmpID, LastName, FirstName, Dept, MgrStaffCode, IsMgr, PayrollStatus, PayFrequency, StdHrsWk, EmailAddress )
VALUES (#StaffCode, (select EmpID from [Database].[dbo].[Employees] WHERE StaffCode = #StaffCode), #LastName, #FirstName, #Dept, #MgrStaffCode, #IsMgr, #PayrollStatus, #PayrollFrequency, #StdHrsWk, #EmailAddress)
When I do this I get this error:
Subqueries are not allowed in this context. Only scalar expressions
are allowed.
I have seen/read this:
Subqueries are not allowed in this context. Only scalar expressions are allowed
And this:
MYSQL inserting into multiple tables
And this:
How do I store a value from a sql query into a variable?
And this:
How do I combine a SELECT + WHERE query with an INSERT query?
But still cannot get this to work.
You retrieve the identity to a variable using SCOPE_IDENTITY just after you insert into employees
SCOPE_IDENTITY (Transact-SQL)
What you can do is the following.
insert into Employees values (x,y,z)
insert into EmpDefaultSchedules (#staffcode, ##identity,....)
The ##identity is filled with the inserted EmpID, which you can use later on. If you insert another record after the Employees, and before the EmpDefaultschedules, make sure you put the ##identity in a variable
declare #EmpId int
insert into Employees values (x,y,z)
set #EmpId = ##identity
insert into EmpDefaultSchedules (#staffcode, #EmpId,....)
I'm using scalaquery to connect to both oracle and postgres servers.
This behaviour is occuring for both Oracle and Postgres, but it's only valid (and still incorrect) SQL in Postgres.
At some point, I'm running a query in scalaquery of the form:
row.foo.bind == parameter.foo || row.foo inSetBind parameter.foo.children
Parameter is a trait, which is known to have a foo in it.
The problem here is that out of the ~100 queries run, scala-query only generates the correct SQL once, of the form
...
WHERE row.foo = ? or row.foo in (?, ?, ?, ?, ?)
...
Most of the time it instead generates
...
WHERE row.foo = ? or false
...
Why is this happening inconsistently, is it a bug (I assume it is), and how do I work around it?
It turns out that the query was looking at an empty set, because parameter.foo had no childen in most cases.
Given that WHERE row.foo IN () is not valid SQL, it was instead written out as false.
This still leaves the issue of false being generated despite the code being targeted at oracle DB, but the root cause has now been cleared up.