Spliting data in a db2 table using function - db2

I'm trying to select data using below sql and created the following function to split the data but I get SQL0440n error.

The error "SQL0440n" simply means that a function (aka routine) in your SQL statement has not been found.
SQL0440N No authorized routine named "<routine-name>" of type
"<routine-type>" having compatible arguments was found.
If it is a user defined function, you will need to qualify it with a schema name when you use it, or alter your CURRENT PATH to let Db2 find the function by looking in a given set of schemas.
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0001014.html
If you still get the same error, check that the data-types of the input values to the function match, or are implicitly castable to the data-types in your function parameters

Related

When is a Postgres function type-checked?

Postgres allows you to specify function parameter types and a return type. When are these checked? When the function is created? When it's called? Or is it just an annotation?
The types are checked for existence when the function is defined – you cannot define a function using non-existent types (that's why PostgreSQL has shell types).
The function body is only checked for syntax errors.
When the query is planned, PostgreSQL checks that the data types of the arguments and the return value match your query. If the argument types don't match exactly, type resolution kicks in and adds type casts.
Finally, at execution time PostgreSQL checks that the datum returned matches the function result data type.

Parametric query and hstore in PostgreSQL

I have a query with one parameter and am using jmoiron/sqlx to run it against Nominatim database that has a hstore field "name". The query itself is like
SELECT place_id, parent_place_id, name->'name:ru' as name from placesx WHERE admin_level = 3 and parent_place_id IN (?)
The problem when I use sqlx.In, sqlx.Bind and sqlx.Prepare functions that it takes :ru as a query parameter and complains about it.
Question is - how it can be avoided so that I can retrieve specific locale value ('name:en', 'name:de' etc) from hstore without this collision?
So far I use a regular expression and do not unmasrhal string to hstore’ map[string]string since I couldn’t figure out how to retrieve value from it by key.

PHP and sanitizing strings for use in dynamicly created DB2 queries

I'm relatively new to DB2 for IBMi and am wondering the methods of how to properly cleanse data for a dynamically generated query in PHP.
For example if writing a PHP class which handles all database interactions one would have to pass table names and such, some of which cannot be passed in using db2_bind_param(). Does db2_prepare() cleanse the structured query on its own? Or is it possible a malformed query can be "executed" within a db2_prepare() call? I know there is db2_execute() but the db is doing something in db2_prepare() and I'm not sure what (just syntax validation?).
I know if the passed values are in no way effected by the result of user input there shouldn't be much of an issue, but if one wanted to cleanse data before using it in a query (without using db2_prepare()/db2_execute()) what is the checklist for db2? The only thing I can find is to escape single quotes by prefixing them with another single quote. Is that really all there is to watch out for?
There is no magic "cleansing" happening when you call db2_prepare() -- it will simply attempt to compile the string you pass as a single SQL statement. If it is not a valid DB2 SQL statement, the error will be returned. Same with db2_exec(), only it will do in one call what db2_prepare() and db2_execute() do separately.
EDIT (to address further questions from the OP).
Execution of every SQL statement has three stages:
Compilation (or preparation), when the statement is parsed, syntactically and semantically analyzed, the user's privileges are determined, and the statement execution plan is created.
Parameter binding -- an optional step that is only necessary when the statement contains parameter markers. At this stage each parameter data type is verified to match what the statement text expects based on the preparation.
Execution proper, when the query plan generated at step 1 is performed by the database engine, optionally using the parameter (variable) values provided at step 2. The statement results, if any, are then returned to the client.
db2_prepare(), db2_bind_param(), and db2_execute() correspond to steps 1, 2 and 3 respectively. db2_exec() combines steps 1 and 3, skipping step 2 and assuming the absence of parameter markers.
Now, speaking about parameter safety, the binding step ensures that the supplied parameter values correspond to the expected data type constraints. For example, in the query containing something like ...WHERE MyIntCol = ?, if I attempt to bind a character value to that parameter it will generate an error.
If instead I were to use db2_exec() and compose a statement like so:
$stmt = "SELECT * FROM MyTab WHERE MyIntCol=" . $parm
I could easily pass something like "0 or 1=1" as the value of $parm, which would produce a perfectly valid SQL statement that only then will be successfully parsed, prepared and executed by db2_exec().

What is the purpose of the input output functions in Postgresql 9.2 user defined types?

I have been implementing user defined types in Postgresql 9.2 and got confused.
In the PostgreSQL 9.2 documentation, there is a section (35.11) on user defined types. In the third paragraph of that section, the documentation refers to input and output functions that are used to construct a type. I am confused about the purpose of these functions. Are they concerned with on-disk representation or only in-memory representation? In the section referred to above, after defining the input and output functions, it states that:
If we want to do anything more with the type than merely store it,
we must provide additional functions to implement whatever operations
we'd like to have for the type.
Do the input and output functions deal with serialization?
As I understand it, the input function is the one which will be used to perform INSERT INTO and the output function to perform SELECT on the type so basically if we want to perform an INSERT INTO then we need a serialization function embedded or invoked in the input or output function. Can anyone help explain this to me?
Types must have a text representation, so that values of this type can be expressed as literals in a SQL query, and returned as results in output columns.
For example, '2013-20-01' is a text representation of a date. It's possible to write VALUES('2013-20-01'::date) in a SQL statement, because the input function of the date type recognizes this string as a date and transforms it into an internal representation (for both using it in memory and storing to disk).
Conversely, when client code issues SELECT date_field FROM table, the values inside date_field are returned in their text representation, which is produced by the type's output function from the internal representation (unless the client requested a binary format for this column).

Multivalue parameter in PostgreSQL function

I am having a PostgreSQL function:
func(timestamp,timestamp,Integer[],varchar[])
I want to design a JasperReports report to fetch data from the function. For this, I created parameter of type list to take input from user and pass it as varchar array.
For this, I am trying to use the following query:
select * from func($P{interval_start}::timestamp,$P{interval_end}::timestamp,array[$P!{idParameter}],array[$P!{usersParameter}])
But when I am running the report, it gives error like column user1(passed from parameter) does not exist.
I also tried:
select * from func($P{interval_start}::timestamp,$P{interval_end}::timestamp,array[$P!{idParameter}],array[$P{usersParameter}])
But it says:
 Compiling to file... /home/Prashant/Desktop/jasper-report/report2.jasper
net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 
1. Parameter type not supported in query : usersParameter class java.util.Collection
at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:258)
at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:140)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:212)
at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:128)
at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:516)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
Compilation running time: 73!
Please suggest, how to pass string array to PostgreSQL function from iReport?