Can not create alias `IF` or `IFNULL` - h2

I am trying to create an alias in H2database for IF and I get the following error.
org.h2.jdbc.JdbcSQLSyntaxErrorException: Function alias "IF" already exists; SQL statement:
CREATE ALIAS `IF` FOR "test.Udf.ifAlias" [90076-212]
Then I try to drop the IF before creating, and I get the following error.
org.h2.jdbc.JdbcSQLSyntaxErrorException: Function alias "IF" not found; SQL statement:
DROP ALIAS `IF` [90077-212]
Similar happens for IFNULL. I am trying the following lines in my init script.
DROP ALIAS `IF`;
CREATE ALIAS `IF` FOR "test.Udf.ifAlias";
And the Java function for the ifAlias is
public static boolean ifAlias(boolean condition, boolean exp1, boolean exp2) {
if(condition) {
return exp1;
}
return exp2;
}

Since version 1.4.198, IF is a keyword in h2. See this issue and responses: https://github.com/h2database/h2database/issues/3018

Related

Update with $push MongoDB using bash script

I am using a bash script to update my mongoDB. Here is the code I use:
mongo 127.0.0.1/foo --eval 'db.coll.update({"Name1": '"$arg1"', "Name2": '"$arg2"'}, {$push: {"Name3": '"$arg3"'}}, {upsert: true});'
The code above should update if existing document which is matching Nam1 and Name2. The value to update is arg3 in field Name3. This value is pushed, which means a new value is appended to the existing list.
But I get the error code:
SyntaxError: Unexpected number
What am I doing wrong?

Why doesn't my alias show up in INFORMATION_SCHEMA.FUNCTION_ALIASES?

I'm using H2 DB v1.4.182 for my test environment. It acts as a substitute in place of Oracle, which is the production vendor. I have a few aliases I create using a script in my test environment:
CREATE ALIAS IF NOT EXISTS REGEXP_LIKE as $$ boolean regexpLike(String s, String p) { return java.util.regex.Pattern.compile(p).matcher(s).find(); } $$;
CREATE ALIAS IF NOT EXISTS TO_NUMBER as $$ long toNumber(String s) { return Long.valueOf(s); } $$;
CREATE ALIAS if NOT EXISTS fn_val_check3 for "fakedata.testutil.TestUtil.fn_val_check";
The first two aliases show up in the INFORMATION_SCHEMA.FUNCTION_ALIASES table, the last one (fn_val_check3) doesn't.
SELECT ALIAS_NAME FROM INFORMATION_SCHEMA.FUNCTION_ALIASES;
Gives me two rows
ALIAS_NAME
TO_NUMBER
REGEXP_LIKE
I verified that H2 has the other alias somewhere by running the CREATE ALIAS ... directly in the console and it says
CREATE ALIAS fn_val_check3 for "fakedata.testutil.TestUtil.fn_val_check";
Function alias "FN_VAL_CHECK3" already exists; SQL statement: CREATE ALIAS fn_val_check3 for "fakedata.testutil.TestUtil.fn_val_check" [90076-182] 90076/90076 (Help)
I'm using a validation component to verify the function exists in the database, which is why I'm wondering. Am I missing something or is this a known thing?

Could no use registered function in UFT/QTP

I have associated the following function to a tests in order to select a row in a java table when a the row text matches an expected value :
Public Function GetRowWithCellText(ByRef oJTable, sColName, sText)
bChkText=FALSE
iRowCount=oJTable.GetROProperty("rows")
For iCounter=0 to iRowCount-1
sGetCellText=oJTable.GetCellData(iCounter, sColName)
If instr(sText, sGetCellText)>0 Then
bChkText=TRUE
GetRowWithCellText=iCounter
End If
Next
ReportingFunction bChkText, "Row with desired text"
End Function
RegisterUserFunc "JavaTable", "GetRowWithCellText", "GetRowWithCellText", TRUE
The function is well registered and I got it in the list of available functions for a java table.
However when trying to apply the function into a JavaTable in my application :
JTable.GetRowWithCellText msg.users.list.table.header.user , LOGIN
I get the following error :
Object required: 'msg'
Line (122): "JTable.GetRowWithCellText msg.users.list.table.header.user , LOGIN".
Please note that UFT is not recoginising cols of my table so I had inserted its value manually
It appears from the 'Object required' error that the value for 'msg' is not defined. The first part of msg.users.list.table.header.user refers to an object named 'msg'. If that object does not exist or has not be initialized, you may get that error.
You can break your statement up to confirm where the error is located. Change this statement
JTable.GetRowWithCellText msg.users.list.table.header.user , LOGIN
To these statements
Dim user
Set user = msg.users.list.table.header.user
JTable.GetRowWithCellText user, LOGIN
When you re-execute, you should get an error on the second line if there was an issue with msg; otherwise you will get an error on the third line if it is truly an issue with your function.
I have used column index instead of column name and it worked , I guess there is an issue with special characters.
Thanks

Bash: Calling bash script from postgresql trigger function

I am calling a bash script from the trigger function in postgresql. The script is called after update of the value(log_interval) in the table in SQL.
The read the changed value(log_interval) from the table in the script that is called and store in a file (interval.txt). If I open the file I see the old value of log_interval and not the new value in the interval.txt file.
Below is the code snippet that I am using:
Trigger function:
CREATE OR REPLACE FUNCTION update_upload_interval()
RETURNS trigger AS
$BODY$
#!/bin/sh
exec /home/User/ReadInterval.sh &
$BODY$
LANGUAGE plsh VOLATILE
Trigger:
CREATE TRIGGER assign_new_interval
AFTER UPDATE OF log_interval
ON table_interval
FOR EACH ROW
WHEN ((old.log_interval IS DISTINCT FROM new.log_interval))
EXECUTE PROCEDURE update_upload_interval();
Script code:
function readinterval() {
logperiod=$(psql -c "select log_interval from table_interval where id=1;" -t $database_name)
echo "$logperiod" > /home/User/interval.txt
}
Am new to scripting and using SQL. Let me know the solution. Thanks in advance.
Unless there's more code that isn't being shown here, just calling ReadInterval.sh won't do anything because it's nothing but a function declaration. In addition, there's a variable $database_name that isn't being set.
CREATE OR REPLACE FUNCTION update_upload_interval()
RETURNS trigger AS
$BODY$
#!/bin/sh
database_name=???
psql -c "select log_interval from table_interval where id=1;" -t $database_name > /home/User/interval.txt
$BODY$
LANGUAGE plsh VOLATILE

Preventing sql injection with stored procedures

I'm calling a system stored procedure for full text search package. It generates terms used for full text search based on a sql literal.
ex: exec ctx_query.explain('index_name', 'full text filter', 'explain table') etc
I'm doing the following in my code:
using(OracleCommand command = new OracleCommand("ctx_query.explain", DataAccess.GetConnString()))
{
comm.Parameters.AddWithValue("index_name", "explain1");
//comm.Parameters.AddWithValue("text_query", "(test) OR (term1 ACCUM term2");
comm.Parameters.AddWithValue("text_query", txtUserInput.Text);
comm.Parameters.AddWithValue("explain_table", "explain_results");
comm.Parameters.AddWithValue("sharelevel", 0);
comm.Parameters.AddWithValue("explain_id", new Guid().ToString().Substring(0,30));
comm.ExecuteNonQuery();
}
The "text_query" parameter will be built from user input. Does the above prevent sql injection because the textUserInput.Text will be passed as a command parameter?
ctx_query.explain does not execute the query, it only examines it, so there is no SQL injection risk here.

Resources