Calling Database Procedure From Ruby on Rails - sql

I'm developing an application using Ruby on Rails and NuoDB and I'm trying to CALL procedures from the database. I'm testing it using the Rails console but I'm only getting a "TRUE" response when I call it using the following command.
ActiveRecord::Base.connection.execute("CALL SHOW_FEEDBACKS_PRC( 'form_name' )")
My stored procedure is this:
CREATE PROCEDURE database_name.show_feedbacks_prc (IN P_IN_form string)
returns tmp_feedbacks (txt1 string, rating integer, comment string, created_at timestamp, updated_at timestamp)
language sql
security invoker
as
insert into tmp_feedbacks
select txt1, rating, comment, created_at, updated_at
from database_name.feedbacks
where form = p_in_form;
END_PROCEDURE
It's a simple query that returns only a list of "feedbacks" which are under a specific "form".
The procedure works when I use NuoDB's console and it returns a table that displays the requested data but when I call it using Rail's console it would only return a "true" response when I execute the SQL command.
Is it possible to get a response as an array of requested data and how do I do this?
I'm trying to execute procedures inside the database instead of making loops inside the rails controllers.

So I totally forgot about this but I solved this issue a while back and here's a sample of what I did:
SAMPLE_PROCEDURE:
CREATE PROCEDURE sample_procedure ( IN input_1 INTEGER )
RETURNS return_msg ( col_1 STRING , col_2 INTEGER ) AS
VAR value_string STRING;
VAR value_integer INTEGER;
value_string = input_1;
value_integer = input_1+10;
INSERT INTO return_msg VALUES ( value_string , value_integer);
RETURN;
END_PROCEDURE
And here is how I call it:
ActiveRecord::Base.connection.execute("call sample_procedure(1)")
Rails would return the following:
[{"col_1"=>"1", "col_2"=>11}]
I hope this helps.

Related

BigQuery "KEYS.KEYSET_CHAIN must be a literal or query parameter" when try to create a view

I have a table which some fields are encrypted using Tink which normally worked well with BigQuery's AEAD function (https://cloud.google.com/bigquery/docs/reference/standard-sql/aead_encryption_functions#aeaddecrypt_string)
When I ran a normal query to decrypt data. I can use
DECLARE KMS_RESOURCE_NAME STRING;
DECLARE FIRST_LEVEL_KEYSET BYTES;
SET KMS_RESOURCE_NAME= "<kms-uri>";
SET FIRST_LEVEL_KEYSET = from_base64("<encrypted-keyset>");
select
id,
encrypted_col,
AEAD.DECRYPT_STRING(
KEYS.KEYSET_CHAIN(KMS_RESOURCE_NAME, FIRST_LEVEL_KEYSET),
from_base64(encrypted_col),
"") as decrypted_col
from table
which will output the decrypted data that work fine.
But now I want to create a view on top of this table in which this view will show the decrypted value and view not accept the variable so I hard code into the select statement like this:
create view view_table as
select
id,
encrypted_col,
AEAD.DECRYPT_STRING(
KEYS.KEYSET_CHAIN("<kms-uri>", from_base64("<encrypted-keyset>") ),
from_base64(encrypted_col),
"") as decrypted_col
from table;
which basically hardcode the value from the declared variable into a select statement
But I got this error
Argument 2 to KEYS.KEYSET_CHAIN must be a literal or query parameter
How should I change this query to make it work with view?
Edit:
I also tried to select it first with this as well (a bit weird)
create view view_table as
select
id,
encrypted_col,
AEAD.DECRYPT_STRING(
KEYS.KEYSET_CHAIN("<kms-uri>", (select from_base64("<encrypted-keyset>")) ),
from_base64(encrypted_col),
"") as decrypted_col
from table;
but its also not working. (with the same error)

Conversion failed : converting from a character string to uniqueidentifier

I have the following function which returns the following result set when this string is called.
SELECT item
FROM dbo.DelimitedSplit8K('985B773F-5E36-47D4-9E84-E0CE35B34337,32237666-86F3-41FD-BCDE-794571CDAEA2',',')
Result set:
item
------------------------------------
985B773F-5E36-47D4-9E84-E0CE35B34337
32237666-86F3-41FD-BCDE-794571CDAEA2
Now the purpose of this function is to get the two or more IDs cause I will be passing multiple IDs from my C# program to one variable in my stored procedure.
Problem
The problem seems to be simple enough but I am not sure as to why it's occurring.
CREATE PROC [dbo].[usp_printMulitTest]
#multiApplicationId_FK uniqueidentifier = '',
#pDelimiter CHAR(1) = NULL
AS
;WITH image_CTE(imgBinary, imgCode, appID) AS
(
SELECT
[image], imageCode_FK, app
FROM
[dbo].Images
WHERE
CAST('985B773F-5E36-47D4-9E84-E0CE35B34337,32237666-86F3-41FD-BCDE-794571CDAEA2' AS UNIQUEIDENTIFIER)
IN ((SELECT item
FROM dbo.DelimitedSplit8K(CAST('985B773F-5E36-47D4-9E84-E0CE35B34337,32237666-86F3-41FD-BCDE-794571CDAEA2' AS UNIQUEIDENTIFIER),',')))
)
SELECT *
FROM image_CTE
This stored procedure works fine when I hard code the variables.
However, when I convert it to this
WHERE CAST(app AS UNIQUEIDENTIFIER)
IN((SELECT item FROM dbo.DelimitedSplit8K(CAST(#multiApplicationId_FK AS UNIQUEIDENTIFIER),',' )))
to get the results for the app IDs that I pass in, I get an error
Conversion failed when converting from a character string to uniqueidentifier
While looking for solutions two that were pointed out is the incorrect formation of the unique identifier and not using cast, however I checked the numbers and used a cast/convert and there has been no change.
Grateful for assistance in this.

String to CLOB with postgreSQL

I'm trying to read a clob from postgreSQL DB, change it, and write it back.
I was able to read the clob successfully using the following code:
PreparedStatement statement = connection.prepareStatement("SELECT clob_column from data where id = 1");
ResultSet executeQuery = statement.executeQuery();
executeQuery.next()
Clob fetchedClob = executeQuery.getClob("clob_column");
But when I'm trying to create a new clob with the new data using:
Clob newClob = connection.createClob();
I'm getting the following error:
java.lang.AbstractMethodError: com.mchange.v2.c3p0.impl.NewProxyConnection.createClob()Ljava/sql/Clob;
Moreover, If I try just to edit the fetched clob, using:
fetchedClob.setString(0, "new string");
I'm getting the following error:
Method org.postgresql.jdbc4.Jdbc4Clob.setString(long,str) is not yet implemented.
Any idea?
Update: here is the table definition
CREATE TABLE data (
id bigint NOT NULL,
clob_column text, );
Thanks
No need to use getClob().
ResultSet.getString() and setString() work perfectly fine on text columns (PostgreSQL does not have a clob datatype so I assume you are using text)

Passing user defined TABLE type to stored Oracle function

I have an oracle function defined as:
FUNCTION SELECTINBOX (FA_FROMUSERLIKE IN PKGSMSTYPES.MAXVARCHAR2_T DEFAULT NULL ,
FA_INBOXOWNER IN PKGSMSTYPES.MAXVARCHAR2_T,
FA_A_URGENCY IN G_INTARRAY_TBL DEFAULT NULL ,
FA_PAGENO IN NUMBER DEFAULT 1
) RETURN G_SMSNOTES_TBL;
where G_INTARRAY_TBL is defined as,
create or replace
TYPE G_INTARRAY_TBL AS TABLE OF NUMBER;
I am building the query using eclipselink. The query works fine if I hardcode G_INTARRAY_TBL as null in the query string but if I try to pass a List of BigDecimals to it, I get an error,
Internal Exception: java.sql.SQLException: Invalid column type
Error Code: 17004
Include your code for your query.
You need to use a PLSQLStoredFunctionCall (#NamedPLSQLStoredFunctionQuery) for this. You also need to mirror the PLSQL TABLE type with a VARRAY type.
See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/PLSQLStoredFunction

SQL syntax as parameters for a MySQL Routine?

I have the following MySQL routine:
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `getGroupOrders`(grp INT,
ord CHAR(20),
srt CHAR(4),
page INT,
count INT)
BEGIN
SELECT *
FROM `dbre`.`order_info`
WHERE username IN (SELECT `dbre`.`users`.`username`
FROM `dbre`.`users`
WHERE `dbre`.`users`.`id_group` = grp)
ORDER BY ord srt LIMIT page,count;
END
As you can see, I want to pass the ordering column and sorting as a parameters, however I get a syntax error is there a way to do this or do I have to make similar routines for each type of ordering?
I don't think this is possible in the way you try it.
You cannot use a variable to define the ORDER BY column an direction.
The only workaround I can think of is to create a prepared statement from a dynamically created string (where you can use the variables to specify the order by details) and then execute that prepared statement.
Here is an example of such a dynamic statement:
http://forums.mysql.com/read.php?98,393613,393642#msg-393642