SimpleJdbcCall skipping first input parameter - spring-data-jpa

I am executing following code:
simpleJdbcCall.withSchemaName("SCHEMA_NAME")
.withProcedureName(storedProcdure)
.addDeclaredParameter( new SqlParameter("INPUT_PARAMS",Types.LONGNVARCHAR));
// Creating String of input numbers
if (!loanNumbers.isEmpty()) {
for (String str : paramsList)
finalInput = finalInput + str + ";";
finalInput = finalInput + "'";
}
// Preparing map of input params for Stored Procedure
inputParams.put("INPUT_PARAMS", finalInput);
log.info("Input params for Stored Procedure :"+finalInput);
//Input Prepared and finally calling Stored Procedure
SqlParameterSource in = new MapSqlParameterSource(inputParams);
Map<String, Object> resultSet= simpleJdbcCall.execute(in);
If FinalInput have only one value ie. '123467', SimpleJdbcCall will omit this first parameters and I will not get any result. If I run the same SP in any IDE , for the same param , I will get the result.
If it have more than two parameters i.e '123467;98768', it will omit the first parameters(123467) and give result for 98768.
What is the reason for this kind of behavior and how can we fix this.

Related

Entity Framework 5 - RETURN(X) from a Stored Procedure

The last line of my stored procedure is this:
RETURN (22);
I am calling my stored procedure as follows:
var result = new ObjectParameter("result", typeof(double));
int success = context.MySP(code, Id, result);
The stored procedure works fine when called from my code.
code is a string, Id is a Guid and result is an OUTPUT parameter and works fine.
So I know the RETURN (22); statement is being reached (running in query analyser confirms this too, the return value is 22 when I run it there).
I was expecting (in this example), that success would contain 22, but it always contains -1.
What is the most straightforward way to get the 22 return value into the success variable?
Thanks
Rob
Your stored procedure should have an output declared. Assuming it is #result, you will need to set it and return it.
CREATE PROCEDURE MySP
/*
Input parameters here
*/
#result INT OUTPUT
AS
BEGIN
SET #result = 22
return #result
END
The return value of your call should be in result.
var result = new ObjectParameter("result", typeof(double));
context.MySP(code, Id, result);
int success = result.Value;

can i use custom lambda method in entity framework?

i have some methods like:
public static string ToOtherFormat (this string inp)
{
// some code to change inp
return inp;
}
and in my select i want to have code like this:
var DetailMembers = db.TB_Members
.Where(x=> x.FName == obj.ToOtherFormat())
.Select( x=> new { name = (x.FName.ToOtherFormat() + " " + x.LName) , x.ActCode });
i try and just have error. is it possible?
thanks!
i receive this error in simple convert to integer
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.
with this code
.Where(x => x.MemberID == Convert.ToInt32(Hmemid.Hash_two_Decrypt())
Looks like you are querying against the database. Your current query will get translated into SQL query and since SQL doesn't recognize your function that is why you get error.
You may get the data from the tables using a query without that function and then later do the formatting on the result set.
i found it on use .AsEnumerable() method like:
var DetailMembers = db.TB_Members.AsEnumerable()
.Where(x=> x.FName == obj.ToOtherFormat())
.Select( x=> new { name = (x.FName.ToOtherFormat() + " " + x.LName) , x.ActCode });

execute stored proc in ExecuteStoreQuery EF. is this a bug in EF?

trying to execute the stored proc in EF using the following code:
var params = new object[] {new SqlParameter("#FirstName", "Bob")};
return this._repositoryContext.ObjectContext.ExecuteStoreQuery<ResultType>("GetByName", params);
but keep getting this error:
Procedure or function 'GetByName' expects parameter '#FirstName',
which was not supplied.
and from sql profiler:
exec sp_executesql N'GetByName',N'#FirstName nvarchar(100),#FirstName=N'Bob'
what is wrong wit the above ExecuteStoreQuery code?
Ignoring the fact that params is a reserved word...
Think your query needs to be:
var params = new object[] {new SqlParameter("#FirstName", "Bob")};
return this._repositoryContext.ObjectContext.ExecuteStoreQuery<ResultType>("exec GetByName #FirstName", params);
Should also say that if that proc is a standard part of your database and data model then you should import it into your EDM so it's available directly on your context.
Use the ExecuteFunction instead of ExecuteStoreQuery which is more suitable for the "ad-hoc" queries.
var parameters = new ObjectParameter[] {new ObjectParameter("FirstName", "Bob")};
return this._repositoryContext.ObjectContext.ExecuteFunction<ResultType>("GetByName", parameters);
The stored procedures can also be mapped as function in the context and thus can be used as typed method. Take a look at Using stored procedures with Entity Framework.
This is what I did to use a SP in EF, if you have multiple parameters:-
public virtual ObjectResult<GetEpisodeCountByPracticeId_Result> GetEpisodeCountByPracticeId(Nullable<int> practiceId, Nullable<System.DateTime> dat1)
{
SqlParameter practiceIdParameter = practiceId.HasValue ?
new SqlParameter() { ParameterName = "practiceId", Value = practiceId, SqlDbType = SqlDbType.Int } :
new SqlParameter() { ParameterName = "practiceId", SqlDbType = SqlDbType.Int };
SqlParameter dat1Parameter = dat1.HasValue ?
new SqlParameter() { ParameterName = "dat1", Value = dat1, SqlDbType = SqlDbType.DateTime }:
new SqlParameter() { ParameterName = "dat1", SqlDbType = SqlDbType.DateTime };
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<GetEpisodeCountByPracticeId_Result>("exec GetEpisodeCountByPracticeId #practiceId, #dat1", practiceIdParameter, dat1Parameter);
}
If you dont add the parameters (e.g. #practiceId) in the commandText property then you get the error you received

FunctionImport in entity framework 4 issue

I'm using entity framework 4.
I have a stored procedure that just updates one value in my table, namely the application state ID. So I created a stored procedure that looks like this:
ALTER PROCEDURE [dbo].[UpdateApplicationState]
(
#ApplicationID INT,
#ApplicationStateID INT
)
AS
BEGIN
UPDATE
[Application]
SET
ApplicationStateID = #ApplicationStateID
WHERE
ApplicationID = #ApplicationID;
END
I created a function import called UpdateApplicationState. I had initially set its return type to null, but then it wasn't created in the context. So I changed its return type to int. Now it was created in the context. Is it wise to return something from my stored procedure?
Here is my method in my ApplicationRepository class:
public void UpdateApplicationState(int applicationID, int applicationStateID)
{
var result = context.UpdateApplicationState(applicationID, applicationStateID);
}
Here is my calling code to this method in my view:
applicationRepository.UpdateApplicationState(id, newApplicationStateID);
When I run it then I get the following error:
The data reader returned by the store
data provider does not have enough
columns for the query requested.
Any idea/advise on what I can do to get this to work?
Thanks
To get POCO to work with function imports that return null, you can customize the .Context.tt file like this.
Find the "Function Imports" named region (the section that starts with region.Begin("Function Imports"); and ends with region.End();) in the .Context.tt file and replace that whole section with the following:
region.Begin("Function Imports");
foreach (EdmFunction edmFunction in container.FunctionImports)
{
var parameters = FunctionImportParameter.Create(edmFunction.Parameters, code, ef);
string paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray());
var isReturnTypeVoid = edmFunction.ReturnParameter == null;
string returnTypeElement = String.Empty;
if (!isReturnTypeVoid)
returnTypeElement = code.Escape(ef.GetElementType(edmFunction.ReturnParameter.TypeUsage));
#>
<# if (isReturnTypeVoid) { #>
<#=Accessibility.ForMethod(edmFunction)#> void <#=code.Escape(edmFunction)#>(<#=paramList#>)
<# } else { #>
<#=Accessibility.ForMethod(edmFunction)#> ObjectResult<<#=returnTypeElement#>> <#=code.Escape(edmFunction)#>(<#=paramList#>)
<# } #>
{
<#
foreach (var parameter in parameters)
{
if (!parameter.NeedsLocalVariable)
{
continue;
}
#>
ObjectParameter <#=parameter.LocalVariableName#>;
if (<#=parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null"#>)
{
<#=parameter.LocalVariableName#> = new ObjectParameter("<#=parameter.EsqlParameterName#>", <#=parameter.FunctionParameterName#>);
}
else
{
<#=parameter.LocalVariableName#> = new ObjectParameter("<#=parameter.EsqlParameterName#>", typeof(<#=parameter.RawClrTypeName#>));
}
<#
}
#>
<# if (isReturnTypeVoid) { #>
base.ExecuteFunction("<#=edmFunction.Name#>"<#=code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()))#>);
<# } else { #>
return base.ExecuteFunction<<#=returnTypeElement#>>("<#=edmFunction.Name#>"<#=code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()))#>);
<# } #>
}
<#
}
region.End();
What I'm doing here is instead of ignoring all function imports that return null, I'm creating a method that returns null. I hope this is helpful.
It is because you do not actually returning anything from your stored procedure. Add a line like below to your SP (SELECT ##ROWCOUNT), and it will be executing properly.
BEGIN
...
SELECT ##ROWCOUNT
END
While this solution will address your issue and actually returns the number of effected rows by your SP, I am not clear on why this is an issue for you:
I had initially set its return type to null, but then it wasn't created in the context.
When doing a Function Import, you can select "None" as return type and it will generate a new method on your ObjectContext with a return type of int. This method basically executes a stored procedure that is defined in the data source; discards any results returned from the function; and returns the number of rows affected by the execution.
EDIT: Why a Function without return value is ignored in a POCO Scenario:
Drilling into ObjectContext T4 template file coming with ADO.NET C# POCO Entity Generator reveals why you cannot see your Function in your ObjectContext class: Simply it's ignored! They escape to the next iteration in the foreach loop that generates the functions.
The workaround for this is to change the T4 template to actually generate a method for Functions without return type or just returning something based on the first solution.
region.Begin("Function Imports");
foreach (EdmFunction edmFunction in container.FunctionImports)
{
var parameters = FunctionImportParameter.Create(edmFunction.Parameters, code, ef);
string paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray());
// Here is why a Function without return value is ignored:
if (edmFunction.ReturnParameter == null)
{
continue;
}
string returnTypeElement = code.Escape(ef.GetElementType(edmFunction.ReturnParameter.TypeUsage));
...

Writing NUnit test code

How can I write code for the below method so that it can be tested in NUnit? How to handle a Hashtable?
public DataSet MySampleMethod(int param1, string param2, Hashtable ht)
{
if(ht==null)
{
ht = new Hashtable();
}
ht.Add("testKey","testData");
DataSet ds = new DataSet();
ds.Tables.Add();
ds.Tables[0].Columns.Add("Column1");
ds.Tables[0].Columns.Add("Column2");
ds.Tables[0].Columns.Add("Column3");
DataRow dr = ds.Tables[0].NewRow();
dr["Column1"] = "My column 1";
dr["Column2"] = "My column 2";
dr["Column3"] = "My column 3";
ds.Tables[0].Rows.Add(dr);
DataRow dr1 = ds.Tables[0].NewRow();
dr1["Column1"] = param1.ToString();
dr1["Column2"] = param2;
dr1["Column3"] = ht["testKey"].ToString();
ds.Tables[0].Rows.Add(dr1);
return ds;
}
First question to ask is: Why do I need to write this method? What's it doing for me?
Give the method a more human-friendly name.
From what I can see, the method takes in an integer, a string and a hashtable. The method is then expected to return a dataset containing a solitary table with 3 columns,
the first row contains values like {"My Column {ColumnNo}"..}
the second row of which contains the [ intParam.ToString(), stringParam, hashtable["testKey"] ]
Testing this method should be trivial,
Test#1:
Arrange : Create known inputs (an int I , string S, a hashtable with some "testData"=> Y)
Act : Call the method and obtain the resulting dataset
Assert : Query the dataset to see if it has the single table with 2 records. Inspect the contents of the records of the table to see if they contain the header row and the row with [I, S, Y].
Test#2:
Similar to above test, except that you pass in null for the hashtable parameter.
That's all I could see based on the snippet you posted.
HTH
Update: Not sure what you mean here by "handle hashtable" or "write test fixture code for hashtable" ? The hashtable is just a parameter to your function.. so I reckon the test would look something like this (Forgive the bad naming and lack of constants... can't name them unless I know what this function is used for in real life)
[Test]
public void Test_NeedsABetterName()
{
int intVal = 101; string stringVal = "MyString"; string expectedHashValue = "expectedValue";
Hashtable ht = new Hashtable();
ht.Add("testKey", expectedHashValue);
Dataset ds = MySampleMethod(intVal, stringVal, ht);
Assert.AreEqual(1, ds.Tables.Count);
Assert.AreEqual(2, ds.Tables[0].Rows.Count);
// check header Row1.. similar to Row2 as shown below
DataRow row2 = ds.Tables[0].Rows[1];
Assert.AreEqual(intVal.ToString(), row2["Column1"]);
Assert.AreEqual(stringVal, row2["Column2"]);
Assert.AreEqual(expectedHashValue, row2["Column3"])
}
I'd recommend getting a good book like Pragmatic Unit Testing in C# with NUnit or one from the list here to speed you up here.