Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 363417 - SQL - Replace: Generated Java does not update parameters index acrrording with user's changing
Summary: SQL - Replace: Generated Java does not update parameters index acrrording wit...
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: EDT (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-09 22:00 EST by Thomas Wu CLA
Modified: 2017-02-23 14:16 EST (History)
2 users (show)

See Also:


Attachments
Parameters error (285.62 KB, image/pjpeg)
2011-11-10 00:42 EST, Thomas Wu CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Wu CLA 2011-11-09 22:00:59 EST
Build Identifier: 20111108

Manually changed setting parameters in egl sql statement. The generated java codes only change the stmtStr, but paramters index codes are not updated. 

Reproducible: Always

Steps to Reproduce:
1.Connect DB2 SAMPLE database.
2.New a basic record along with the table SALES.

record sales_rec {@table{name = "THOMASWU.SALES"}} 
    SALES_DATE date? = "08/24/1900";
    SALES_PERSON string? = "parma";
    REGION string? = "KK";
    SALES int? = 23;
end

3.New a basic program, add the following replace code with Control+1
		replace sales_rec to ds with
			#sql{
				update THOMASWU.SALES
				set
					SALES_DATE = ?,
					SALES_PERSON = ?,
					REGION = ?,
					SALES = ?
			};

4. Manually change the embeded sql statement as the following, then save the egl file. I removed the first parameter "SALES_DATE = ?" and added "where SALES_DATE = '08/24/1900'" here.
		replace sales_rec to ds with
			#sql{
				update THOMASWU.SALES
				set
					SALES_PERSON = ?,
					REGION = ?,
					SALES = ?
				where SALES_DATE = '08/24/1900'
			};

Actual results: The generated java codes as following only updated sql in stmtStr, but the parameters index still included SALE_DATE I deleted, which will lead to sql error during executing.

		try {
			java.sql.PreparedStatement ezeStatement = (java.sql.PreparedStatement)ds.getStatement("testdb2.testSQLforDB2", 8);
			if (ezeStatement== null) {
				String stmtStr = "update THOMASWU.SALES      set              SALES_PERSON = ?,       REGION = ?,       SALES = ?     where SALES_DATE = '08/24/1900'";
				ezeStatement = ds.getConnection().prepareStatement(stmtStr);
				ds.registerStatement("testdb2.testSQLforDB2", 8, ezeStatement);
			}
			if(null == sales_rec1.SALES_DATE){
				ezeStatement.setNull(1, java.sql.Types.DATE);
			}
			else{
				ezeStatement.setDate(1, new java.sql.Date(sales_rec1.SALES_DATE.getTimeInMillis()));
			}
			if(null == sales_rec1.SALES_PERSON){
				ezeStatement.setNull(2, java.sql.Types.VARCHAR);
			}
			else{
				ezeStatement.setString(2, sales_rec1.SALES_PERSON);
			}
			if(null == sales_rec1.REGION){
				ezeStatement.setNull(3, java.sql.Types.VARCHAR);
			}
			else{
				ezeStatement.setString(3, sales_rec1.REGION);
			}
			if(null == sales_rec1.SALES){
				ezeStatement.setNull(4, java.sql.Types.INTEGER);
			}
			else{
				ezeStatement.setInt(4, sales_rec1.SALES);
			}
			ezeStatement.executeUpdate();
		}
		catch(java.sql.SQLException ezeEx) {
			throw org.eclipse.edt.javart.util.JavartUtil.makeEglException(ezeEx);
		}
		;
Comment 1 Joseph Vincens CLA 2011-11-09 22:53:59 EST
The reason you don't get a where clause is because you don't have any key fields defined. Add @ID to the key fields and the key fields will be moved from the set to the where fields.
Comment 2 Thomas Wu CLA 2011-11-10 00:39:52 EST
Hello, Joe. The issue is not "where" clause missing. Here is another example. I have the following record mapping to a DB2 table.

record act_std type Entity{@table{name = "THOMASWU.ACT"}}
    ACTNO smallInt{@id} = 123;
    ACTKWD string = "BMW";
    ACTDESC string = "Car";
end

I write a replace sql in egl as following, and it could be run successfully.
The egl codes are:
		replace actStd to ds using(123) with
			#sql{
				update THOMASWU.ACT
				set
					ACTKWD = ?,
					ACTDESC = ?
				where
					ACTNO = ?
			};

The generated java codes are: 
		try {
			java.sql.PreparedStatement ezeStatement = (java.sql.PreparedStatement)ds.getStatement("testdb2.testSQLforDB2", 8);
			if (ezeStatement== null) {
				String stmtStr = "update THOMASWU.ACT      set       ACTKWD = ?,       ACTDESC = ?      where       ACTNO = ?";
				ezeStatement = ds.getConnection().prepareStatement(stmtStr);
				ds.registerStatement("testdb2.testSQLforDB2", 8, ezeStatement);
			}
			ezeStatement.setShort(1, (short) 123);
			ezeStatement.setString(1, actStd.ACTKWD);
			ezeStatement.setString(2, actStd.ACTDESC);
			ezeStatement.setObject(3, actStd.ACTNO);
			ezeStatement.executeUpdate();
		}
		catch(java.sql.SQLException ezeEx) {
			throw org.eclipse.edt.javart.util.JavartUtil.makeEglException(ezeEx);
		}
		;

If I remove the follwoing line, "ACTKWD = ?," in egl codes, and save the change.
The revised egl codes are:
		replace actStd to ds using(123) with
			#sql{
				update THOMASWU.ACT
				set
					ACTDESC = ?
				where
					ACTNO = ?
			};

The generated java codes will be changed as following:
		try {
			java.sql.PreparedStatement ezeStatement = (java.sql.PreparedStatement)ds.getStatement("testdb2.testSQLforDB2", 8);
			if (ezeStatement== null) {
				String stmtStr = "update THOMASWU.ACT      set              ACTDESC = ?      where       ACTNO = ?";
				ezeStatement = ds.getConnection().prepareStatement(stmtStr);
				ds.registerStatement("testdb2.testSQLforDB2", 8, ezeStatement);
			}
			ezeStatement.setShort(1, (short) 123);
			ezeStatement.setString(1, actStd.ACTKWD);
			ezeStatement.setString(2, actStd.ACTDESC);
			ezeStatement.setObject(3, actStd.ACTNO);
			ezeStatement.executeUpdate();
		}
		catch(java.sql.SQLException ezeEx) {
			throw org.eclipse.edt.javart.util.JavartUtil.makeEglException(ezeEx);
		}
		;

You could see that stmtStr has removed ACTKWD, there are only 2 parameters "?". But in the ezeStatement.setXXX codes, there are still 3 parameters, the  actStd.ACTKWD is still there. Please refer to screenshot.
Comment 3 Thomas Wu CLA 2011-11-10 00:42:14 EST
Created attachment 206752 [details]
Parameters error
Comment 4 Joseph Vincens CLA 2011-11-10 09:15:19 EST
When a GET action has a USING clause the USING clause is used to generate the set statements for the SQL where clause and the @ID columns are ignored. I made the DELETE and REPLACE consistent with the GET. When the DELETE and REPLACE have a USING clause the USING fields are used to generate the set statements for the SQL where clause, the @ID columns will be ignored. 

Your example is not valid, your SQL must match the record being used. We don't parse the SQL so we are nat able to determine that you have more fields than ?'s. If the record has 5 non ID fields your SQL must have 5 ? in the SET. For 070 only a record can be used as a target for a REPLACE action. In 1.0 we will investigate opening up the spec to allow scalars as a target for a REPLACE action.
Comment 5 Thomas Wu CLA 2011-11-10 20:02:46 EST
Thanks for your explaination.