I have a type defined in Types like this "create or replace type numbers_table is table of numbers". I have a procedure that has a parameter of this type (ids_list IN numbers_table).
On calling the procedure with Oracle Client I keep receiving the "..Wrong number or types of arguments ..." error.
I am passing this parameter like this:
OracleParameter param1 = new OracleParameter("ids_list", OracleDbType.Decimal, ParamDirection.Input);
param1.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
param1.ArrayBindSize = new int[collectioncount];
param1.ArrayBindStatus = new OracleParameterStatus[collectioncount];
param1.Size = collectioncount;
for(int i = 0; i < collectioncount; i++)
{
param1[i].ArrayBindSize = 8000;
param1[i].ArrayBindStatus = new OracleParameterStatus.Success;
}
param1.Value = collection;
Still no luck ... I have tried to change OracleDbType.Decimal to Double, Long, Int64 etc but nothing works for me.
Related
How can I return a simple jsonb object in a PostgreSQL function written in C?
I don't know enough about postgres server side programming. And below is my attempt to return a simple json/jsonb object based on the C source code for hstore_to_jsonb_loose, which is the closest example I can find. I am trying to return {"hi": -101} from the C function, but got an error:
=> ERROR: unexpected jsonb type as object key
Can anyone help exaplain how to get this right?
My C code is:
PG_FUNCTION_INFO_V1(test_return_jsonb);
Datum
test_return_jsonb( PG_FUNCTION_ARGS) {
JsonbParseState *state = NULL;
JsonbValue *res;
StringInfoData tmp;
initStringInfo(&tmp);
(void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
JsonbValue key, val;
//key
key.type = jbvString;
key.val.string.len = 2;
key.val.string.val = "hi";
Datum numd;
//value
val.type = jbvNumeric;
numd = DirectFunctionCall3(numeric_in, CStringGetDatum("-101"), //!tmp.data),
ObjectIdGetDatum(InvalidOid), Int32GetDatum(-1));
val.val.numeric = DatumGetNumeric(numd);
(void) pushJsonbValue(&state, WJB_VALUE, &val);
res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
PG_RETURN_POINTER(JsonbValueToJsonb(res));
}
And the SQL interface code is:
CREATE OR REPLACE FUNCTION test_return_jsonb()
RETURNS jsonb
AS '$libdir/pgtest', 'test_return_jsonb'
LANGUAGE 'c' IMMUTABLE STRICT COST 100; -- Guessed cost
This is with PostgreSQL 12 and Ubuntu 18.04 LTS.
I'm learning too currently and encountered the same issue. I solved it like following (not sure if this is the right way, but it works for now):
// Defined in "/src/backend/utils/adt/numeric.c"
extern Datum int8_numeric(PG_FUNCTION_ARGS);
extern Datum float8_numeric(PG_FUNCTION_ARGS);
extern Datum numeric_int8(PG_FUNCTION_ARGS);
extern Datum numeric_float8(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(test_return_jsonb);
Datum test_return_jsonb(PG_FUNCTION_ARGS) {
JsonbPair *pair = palloc(sizeof(JsonbPair));
pair->key.type = jbvString;
pair->key.val.string.len = 3;
pair->key.val.string.val = "foo";
pair->value.type = jbvNumeric;
pair->value.val.numeric = DatumGetNumeric(DirectFunctionCall1(int8_numeric, (int64_t)100));
JsonbValue *object = palloc(sizeof(JsonbValue));
object->type = jbvObject;
object->val.object.nPairs = 1;
object->val.object.pairs = pair;
PG_RETURN_POINTER(JsonbValueToJsonb(object));
}
Need to sort/order a list of data based on an undetermined number of columns (1 or more).
What i'm trying to do is loop through the desired columns and add an OrderBy or ThenBy based on their number to the query'd list, but i'm unsuccessful...
Done this, but it doesn't compile:
var query = GetAllItems(); //returns a IQueriable list of items
//for each selected column
for (int i = 0; i < param.Columns.Length; i++)
{
if (i == 0)
{
query = query.OrderBy(x => x.GetType().GetProperty(param.Columns[i].Name));
}
else
{
//ERROR: IQueriable does not contain a definition for "ThenBy" and no extension method "ThenBy"...
query = query.ThenBy(x => x.GetType().GetProperty(param.Columns[i].Data));
}
}
How can i resolve this issue? Or any alternative to accomplish this requirement?
SOLUTION: #Dave-Kidder's solution is well thought and resolves the compile errors i had. Just one problem, OrderBy only executes (actually sorts the results) after a ToList() cast. This is an issue because i can't convert a ToList back to an IOrderedQueryable.
So, after some research i came across a solution that resolve all my issues.
Microsoft assembly for the .Net 4.0 Dynamic language functionality: https://github.com/kahanu/System.Linq.Dynamic
using System.Linq.Dynamic; //need to install this package
Updated Code:
var query = GetAllItems(); //returns a IQueriable list of items
List<string> orderByColumnList = new List<string>(); //list of columns to sort
for (int i = 0; i < param.Columns.Length; i++)
{
string column = param.Columns[i].Name;
string direction = param.Columns[i].Dir;
//ex.: "columnA ASC"
string orderByColumn = column + " " + direction;
//add column to list
orderByColumnList.Add(orderBy);
}
//convert list to comma delimited string
string orderBy = String.Join(",", orderByColumnList.ToArray());
//sort by all columns, yay! :-D
query.OrderBy(orderBy).ToList();
The problem is that ThenBy is not defined on IQueryable, but on the IOrderedQueryable interface (which is what IQueryable.OrderBy returns). So you need to define a new variable for the IOrderedQueryable in order to do subsequent ThenBy calls. I changed the original code a bit to use System.Data.DataTable (to get a similar structure to your "param" object). The code also assumes that there is at least one column in the DataTable.
// using System.Data.DataTable to provide similar object structure as OP
DataTable param = new DataTable();
IQueryable<DataTable> query = new List<DataTable>().AsQueryable();
// OrderBy returns IOrderedQueryable<TSource>, which is the interface that defines
// "ThenBy" so we need to assign it to a different variable if we wish to make subsequent
// calls to ThenBy
var orderedQuery = query.OrderBy(x => x.GetType().GetProperty(param.Columns[0].ColumnName));
//for each other selected column
for (int i = 1; i < param.Columns.Count; i++)
{
orderedQuery = orderedQuery.ThenBy(x => x.GetType().GetProperty(param.Columns[i].ColumnName));
}
you should write ThenBy after OrderBy like this:
query = query
.OrderBy(t=> // your condition)
.ThenBy(t=> // next condition);
I wrote the code to update my table (SecurityQuestionAnswer) with new security password questions and move to old questions to another table (SecurityQuestionAnswersArchives). Total no of security questions is 3. I am able to update the current table, but when I add the same rows to history table, it shows weird data: only two records are added instead of 3 and the data is also duplicated. My code is as follows:
if (oldQuestions.Any())
{
var oldquestionstoarchivelist = new List<SecurityQuestionAnswersArchives>();
var oldquestionstoarchive =new SecurityQuestionAnswersArchives();
for (int i = 0; i < 3; i++)
{
oldquestionstoarchive.Id = oldQuestions[i].Id;
oldquestionstoarchive.SecurityQuestionId = oldQuestions[i].SecurityQuestionId;
oldquestionstoarchive.Answer = oldQuestions[i].Answer;
oldquestionstoarchive.UpdateDate = oldQuestions[i].UpdateDate;
oldquestionstoarchive.IpAddress = oldQuestions[i].IpAddress;
oldquestionstoarchive.SecurityQuestion = oldQuestions[i].SecurityQuestion;
oldquestionstoarchive.User = oldQuestions[i].User;
oldquestionstoarchivelist.Add(oldquestionstoarchive);
}
user.SecurityQuestionAnswersArchives = oldquestionstoarchivelist;
//await Store.UpdateAsync(user);
_dbContext.ArchiveSecurityQuestionAnswers.AddRange(oldquestionstoarchivelist);
_dbContext.SecurityQuestionAnswers.RemoveRange(oldQuestions);
await _dbContext.SaveChangesAsync();
oldquestionstoarchivelist.Clear();
}
UPDATE 1
The loop looks fine, It iterates three times(0,1,2), which is expected. First issue is with AddRange function to which I was passing a list , but it takes an IEnumerable input, I rectified it using following code.
IEnumerable<SecurityQuestionAnswersArchives> finalArchiveses = oldquestionstoarchivelist;
_dbContext.ArchiveSecurityQuestionAnswers.AddRange(finalArchiveses);
The other issue is duplicate data , which I am unable to figure out where the issue is. Please help me in finding this out.
Your help is much appreciated !
Got it ! Just sharing in case anybody has same issue.
The problem was with initialization at wrong place. I moved
var oldquestionstoarchive =new SecurityQuestionAnswersArchives();
in side the Forloop, now the variable will hold the unique values over each iteration.
var oldquestionstoarchivelist = new List<SecurityQuestionAnswersArchives>();
for (int i = 0; i < 3; i++)
{
var oldquestionstoarchive = new SecurityQuestionAnswersArchives();
oldquestionstoarchive.SecurityQuestionId = oldQuestions[i].SecurityQuestionId;
oldquestionstoarchive.Answer = oldQuestions[i].Answer;
oldquestionstoarchive.UpdateDate = oldQuestions[i].UpdateDate;
oldquestionstoarchive.IpAddress = oldQuestions[i].IpAddress;
oldquestionstoarchive.SecurityQuestion = oldQuestions[i].SecurityQuestion;
oldquestionstoarchive.User = oldQuestions[i].User;
oldquestionstoarchivelist.Add(oldquestionstoarchive);
}
I've called my SQL query like so (ommited error checking for post)
PGresult *result = PQexec(self.connection, "SELECT * FROM \"getNewPaths\"()");
const int numRows = PQntuples(result);
int row = 0;
for (int i = 0; i < numRows; i++) {
char *path = PQgetvalue(result, row, 0);
As soon as I hit the PQgetvalue call I get the exception. If I run that same SQL command inside of psql I do get back exactly what I'd expect to see. The getNewPaths() function has a return type of: TABLE(fullpath varchar, uuid uuid)
Since I'm asking for the first column of the first row, I'm not sure why I'm getting this error.
My mac PostgreSQL server is running 9.2.1, and my client is running 9.1.5
Just figured this out. A macro was calling PQclear(result) before I was actually using the results. Removing that fixed the problem.
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.