I'm trying to issue an update ExecuteStatementRequest using DynamoDB and Java SDK 2.0. I'm struggling to escape keywords that are columns in my table schema.
The following statement:
var response = client.executeStatement(ExecuteStatementRequest.builder()
.statement("""
UPDATE "my-table"
SET value=12.5
WHERE assignmentId='item1#123#item2#456#item3#789'
RETURNING ALL NEW *
""")
.build());
When I run the following statement (notice that value column is a reserved keyword) I get the following error:
Exception in thread "main"
software.amazon.awssdk.services.dynamodb.model.DynamoDbException:
Statement wasn't well formed, can't be processed: Expected identifier
for simple path (Service: DynamoDb, Status Code: 400, Request ID: XXX)
If instead of value I change the column name to val, the statement works fine. I know that in UdateItem operations I can pass in an array of expressionAttributeNames to replace keywords with aliases. Is there a similar primitive I can use for ExecuteStatementRequest?
The documentation you link explicitly says what you need to do:
You can use a reserved keyword as a quoted identifier with double
quotation marks (for example, "user").
That is the SQL standard way of escaping reserved words. In other words, use "value" (or possibly "VALUE") in your statement instead of value.
Related
I figured out that of course . and SPACE aren't allowed. Are there other forbidden characters ?
You can use any (UTF8) character in the field name which aren't
special (contains ".", or starts with "$").
https://jira.mongodb.org/browse/SERVER-3229
https://stackoverflow.com/a/7976235/311220
It's generally best to stick with lowercase alphanumeric with underscores though.
Something else to look out for is the fact that you can make a property name called "query" but then use query operators on it, making it awkward to do a large number of queries.
Example:
Insert document with a property named
db.coll.insert({ query: 'foo' });
Equality query works:
db.coll.findOne({ query: 'foo' });
Not equal ($ne) does not:
db.coll.findOne({ query: { $ne: 'bar' } });
I'm using the statement below to update/insert some data to a table and, if I run it without parameters, it's fine. However, as soon as I try to execute it using parameters it throws:
SQL0418N - A statement contains a use of an untyped parameter marker, the DEFAULT keyword, or a null value that is not valid.
I've read the error information here, but I'm still struggling with why my statement won't execute.
--This statement works
MERGE Into AB.Testing_Table A
USING (VALUES('TEST', 'P')) B(TEST_ID, "ACTION")
ON (A.TEST_ID = B.TEST_ID)
WHEN NOT MATCHED THEN
INSERT (TEST_ID, "ACTION")
VALUES ('TEST', 'P')
WHEN MATCHED THEN
UPDATE SET TEST_ID = 'TEST'
,"ACTION" = 'P';
--This statement fails with error SQL0418N
MERGE Into AB.Testing_Table A
USING (VALUES(#TEST, #ACTION)) B(TEST_ID, "ACTION")
ON (A.TEST_ID = B.TEST_ID)
WHEN NOT MATCHED THEN
INSERT (TEST_ID, "ACTION")
VALUES (#TEST, #ACTION)
WHEN MATCHED THEN
UPDATE SET TEST_ID = #Test
,"ACTION" = #Action;
Thanks in advance for the help!
Basically, DB2 doesn't know what data types you're sending in on those parameters. I'm guessing you're either on an older version of DB2 (less than 9.7 on Linux/Unix/Windows, or on a Mainframe version older than 10.1), which doesn't do a whole lot of "automatic" type conversion. Or you're sending in NULL values (which still have to be "typed", strange as it sounds).
You can fix the problem by creating your parameter markers as typed parameters (I'm assuming data types here, use what would be appropriate):
MERGE INTO AB.TESTING_TABLE A
USING (VALUES (
CAST(#TEST AS CHAR(4))
,CAST(#ACTION AS CHAR(1))
)) B(TEST_ID, "ACTION")
ON (A.TEST_ID = B.TEST_ID)
WHEN NOT MATCHED THEN
INSERT (TEST_ID, "ACTION")
VALUES (B.TEST_ID, B.ACTION)
WHEN MATCHED THEN
UPDATE SET "ACTION" = B.ACTION
Additionally, since you're using the MERGE, you don't have to use parameters in the UPDATE or INSERT parts, you can refer to the values in the USING table you passed in. Also, since you're matching on TEST_ID, you don't need to include that in your UPDATE statement, since it wouldn't be updated, anyway.
This is the query I am trying to execute:
UPDATE TABLE users SET metadata = metadata - 'keyA' - 'keyB'
WHERE <condition>;
Here, metadata is of type jsonb and the - operator removes a key from the JSON object. However, when I do this in jooq:
this.ctx.update(Tables.USERS)
.set(Tables.USERS.METADATA, "metadata-'keyA'-'keyB'")
.where(<condition>)
.execute();
I get an error saying that the value is a CHARACTER VARYING and not JSONB, which I am guessing is because the query is being created with a bind value, and then entire string is being trying to be inserted rather than as an expression.
How do I execute this value-expression in jooq?
What you're passing to the set method:
"metadata-'keyA'-'keyB'"
... is not an expression that is directly injected into the resulting SQL string. It's a bind variable of type String (i.e. VARCHAR). The easiest way forward would be to resort to using "plain SQL":
.set(USERS.METADATA, field(
"{0} - {1} - {2}",
USERS.METADATA.getDataType(),
USERS.METADATA, val("keyA"), val("keyB")
))
For more information related to using "plain SQL" with jOOQ, please refer to this section of the manual:
http://www.jooq.org/doc/latest/manual/sql-building/plain-sql
I am using text fields for displaying column names . For showing the corresponding name of the column I have tried the following method:
Method 1:
textField.setX(currentXPos);
textField.setY(0);
textField.setWidth(columnWidth);
textField.setPrintWhenDetailOverflows(false);
textField.setHeight(colDtlBandHeight);
textField.setStretchWithOverflow(true);
textField.setStretchType(StretchTypeEnum.RELATIVE_TO_BAND_HEIGHT);
textField.setStyle(normalFont);
textField.setBlankWhenNull(true);
JRDesignExpression expression = new JRDesignExpression();
expression.setValueClass(columnClass);
expression.setText("$F{" + columnName + "}");
But on using the above method it throws an exception saying:
net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. Syntax error on token "ID", delete this token
value = SHIFT ID; //$JR_EXPR_ID=44$
2. Syntax error, insert ";" to complete BlockStatements
value = BILL NO.; //$JR_EXPR_ID=45$
3. Syntax error on token ".", invalid VariableDeclarator
value = BILL NO.; //$JR_EXPR_ID=45$
4. Syntax error on token "DATE", delete this token
value = BILL DATE; //$JR_EXPR_ID=46$
But on using the below lines the column Names are set correctly .
Method 2:
textField.setExpression(new JRDesignExpression("new String(\""+colTitle+"\")"));
My doubts are:
1. For displaying the data the first method mentioned is used . Then how come there are no exceptions in that case ?
2. Why did it throw those exceptions when the same method was used for displaying column names?
3. How did the 2nd method work ?
1.:
I suppose the data is properly enclosed in quotes.
2.:
Judging by the exception explanation (e.g. Syntax error on token "ID", delete this token) the interpreter sees two values, SHIFT and ID. It seems here that quotes are missing, e.g.
"SHIFT ID"
"BILL NO."
3.:
In your first example, you create a JRDesignExpression, set the value class and set the text.
The field isn't enclosed in quotes as seen in your lower example. It should look like this:
expression.setText("\"$F{" + columnName + "}\"");
Also, you didn't assign the expression to your textField:
textField.setExpression(expression)
I have a double precision array field dblArrayFld in a table myTable and I'd like to update it using Spring's NamedParameterJdbcTemplate (I'm using Postgres).
I'm running code like this:
SqlParameterSource params = (new MapSqlParameterSource())
.addValue("myarray", myDblArrayListVar)
.addValue("myid", 123);
namedJdbcTemplate.update("UPDATE myTable SET dblArrayFld = :myarray WHERE idFld = :myid", params);
This returns an error that reads syntax error at or near "$2"
I'm assuming my syntax on :myarray is at fault here. I've also tried encasing :myarray in the following ways:
dblArrayFld={:myarray}
dblArrayFld={ :myarray }
dblArrayFld=[:myarray]
dblArrayFld=ARRAY[:myarray]
dblArrayFld=(:myarray)
What's the correct syntax here?
Wehn you try to bind Collection or array as named parameter, NamedParameterJdbcTemplate explodes the appropriate named parameter in your statement into a number of positional parameters matching the length of your array / collection. This is useful for WHERE column IN (:param) statements, but is not going to work in this case.
In order to set an actual Postgres array you have to supply your parameter as java.sql.Array. You can create its instance using Connection#createArrayOf() method.