I'm connecting Salesforce Lightning Connect (OData connector) to Teiid.
Behind Teiid is a stored proc called getCustomers which takes 3 paraneters: #field, #operator, #value. For example, getCustomers('Last_Name', '=', 'Porter'). All calls will be read only.
Salesforce cannot call ActionImports and FunctionImports directly, I need them to be EntitySets. Salesforce will be sending the details in OData as "$filter=Last_Name+eq+'Porter'".
I can use a view to ensure an EntitySet is exposed, and the view can call the stored procedure. My question is how do I detect the WHERE clause ($filter=Last_Name+eq+'Porter') and pass it to the stored procedure?
Since the $filter might be a chain of items, I suspect I'll need Dynamic SQL in a virtual procedure, but still have the issue of passing in the OData properties that hit the view.
<metadata type="DDL"><![CDATA[
CREATE FOREIGN PROCEDURE getCustomers(field varchar, operator varchar, value varchar)
RETURNS TABLE (Id, integer, First_Name varchar, Last_Name varchar)
OPTIONS (UPDATECOUNT 0);]]>
</metadata>
<metadata type="DDL"><![CDATA[
CREATE VIEW SalesforceGetCustomers (Id PRIMARY KEY, First_Name varchar, Last_Name varchar)
AS SELECT * FROM (CALL getCustomers('?????','?????','?????')) t;]]>
</metadata>
My question is how do I detect the WHERE clause ($filter=Last_Name+eq+'Porter') and pass it to the stored procedure?
The simplest approach is to let this happen implicitly using a procedural relational mapping: http://teiid.github.io/teiid-documents/master/content/reference/r_procedural-relational-command.html
Simply put you can call the procedure as if it were a table. If you want to add a view, it would look like:
CREATE VIEW SalesforceGetCustomers
AS SELECT Id, First_Name, Last_Name FROM getCustomers;
As along as the user provides the inputs as predicates, it will work as expected:
SELECT * FROM SalesforceGetCustomers WHERE field = '...' and operator = '...' and "value" = '...'
Related
I am using Prisma as my schema and migrating it to supabase with prisma migrate dev
One of my tables Profiles, should reference the auth.users table in supabase, in sql something like this id uuid references auth.users not null,
Now since that table is automatically created in supabase do I still add it to my prisma schema? It's not in public either it is in auth.
model Profiles {
id String #id #db.Uuid
role String
subId String
stripeCustomerId String
refundId String[]
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
}
The reason I want the relation is because I want a trigger to automatically run a function that inserts an id and role into the profiles table when a new users is invited.
This is that trigger and function
-- inserts a row into public.profiles
create function public.handle_new_user()
returns trigger
language plpgsql
security definer
as $$
begin
insert into public.Profiles (id, role)
values (new.id, 'BASE_USER');
return new;
end;
$$;
-- trigger the function every time a user is created
create trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user();
I had this working when I created the profiles table manually in supabase I included the reference to the auth.users, that's the only reason I can think of why the user Id and role won't insert into the profiles db when I invite a user, the trigger and function are failing
create table public.Profiles (
id uuid references auth.users not null,
role text,
primary key (id)
);
Update from comment:
One error I found is
relation "public.profiles" does not exist
I change it to "public.Profiles" with a capital in supabase, but the function seem to still be looking for lowercase.
What you show should just work:
db<>fiddle here
Looks like you messed up capitalization with Postgres identifiers.
If you (or your ORM) created the table as "Profiles" (with double-quotes), non-standard capitalization is preserved and you need to double-quote the name for the rest of its life.
So the trigger function body must read:
...
insert into public."Profiles" (id, role) -- with double-quotes
...
Note that schema and table (and column) have to be quoted separately.
See:
Are PostgreSQL column names case-sensitive?
I have tree tables in a database: users (user_id (auto increment), fname, lname), roles (role_id, role_desc) and users_roles (user_id, role_id). What I'd like to do is to have a function create_user_with_role. The function takes 3 arguments: first name, last name and role_id. The function inserts a new row into the users table and a new user_id is created automatically. Now I want to insert a new record to the users_roles table: user_id is the newly created value and the role_id is taken from the function's arguments list.
Is it possible to pass the role_id argument to an after insert trigger (defined on users table) so another automatic insert can be performed? Or can you suggest any other solution?
First #Pavel Stehule is right:
Don't try to pass parameters to triggers, ever!
Second, you just have to get the inserted id into a variable.
CREATE FUNCTION create_user_with_role(first_name text, last_name text, new_role_id integer)
RETURNS VOID AS $$
DECLARE
new_user_id integer;
BEGIN
INSERT INTO users (fname, lname) VALUES (first_name, last_name)
RETURNING id INTO new_user_id;
INSERT INTO users_roles (user_id, role_id)
VALUES (new_user_id, new_role_id);
END;$$ LANGUAGE plpgsql;
Obviously, this is completely inefficient if you want to insert multiple rows but that's another question ;)
When you need to pass any parameter to trigger, then there is clean, so your design is wrong. Usually triggers should to have check or audit functionality. Not more. You can use a function, and call function directly from your application. Don't try to pass parameters to triggers, ever! Another bad sign are artificial columns in table used just only for trigger parametrization. This is pretty bad design!
Suppose I created the following domain:
CREATE DOMAIN MY_STATUS AS VARCHAR NOT NULL DEFAULT 'STATUS0' CHECK(VALUE in ('STATUS1', 'STATUS2', 'STATUS3'));
As expected, in a column whose type is MY_STATUS, i can put only the values:
'STATUS0'
'STATUS1'
'STATUS2'
'STATUS3'
Now, let's suppose that I want to validate this column before to send an insert or update to my DB. I need to know which values are allowed so that, if I have status = STATUS4, I will have an error before to send the insert to DB and I can manage it. Since the domain may change in the future, I need to select all the allowed values from DB rather than hardcode all possible values creating constants.
Shortly: how to make a query that selects all the possible values of the domain?
In my example, I would like to have a query that will return:
'STATUS0', 'STATUS1', 'STATUS2', 'STATUS3'
I would recommend that you use a foreign key reference rather than a type or check constraint. Although you can devise a complex query to parse the the constraint, this is so much easier with a foreign key:
create table valid_domain_statuses (
status varchar(32) primary key
);
insert into valid_domain_statuses (status)
select 'STATUS0' union all
select 'STATUS1' union all
select 'STATUS2' union all
select 'STATUS3' ;
. . .
alter table t add constraint fk_my_status
foreign key (status) references valid_domain_statuses(status);
This has other advantages:
You can readily see the valid values by looking at the table.
You can add additional information about the statuses.
It is easy to add new status values.
We're in process of converting over from SQL Server to Postgres. I have a scenario that I am trying to accommodate. It involves inserting records from one table into another, WITHOUT listing out all of the columns. I realize this is not recommended practice, but let's set that aside for now.
drop table if exists pk_test_table;
create table public.pk_test_table
(
recordid SERIAL PRIMARY KEY NOT NULL,
name text
);
--example 1: works and will insert a record with an id of 1
insert into pk_test_table values(default,'puppies');
--example 2: fails
insert into pk_test_table
select first_name from person_test;
Error I receive in the second example:
column "recordid" is of type integer but expression is of type
character varying Hint: You will need to rewrite or cast the
expression.
The default keyword will tell the database to grab the next value.
Is there any way to utilize this keyword in the second example? Or some way to tell the database to ignore auto-incremented columns and just them be populated like normal?
I would prefer to not use a subquery to grab the next "id".
This functionality works in SQL Server and hence the question.
Thanks in advance for your help!
If you can't list column names, you should instead use the DEFAULT keyword, as you've done in the simple insert example. This won't work with a in insert into ... select ....
For that, you need to invoke nextval. A subquery is not required, just:
insert into pk_test_table
select nextval('pk_test_table_id_seq'), first_name from person_test;
You do need to know the sequence name. You could get that from information_schema based on the table name and inferring its primary key, using a function that takes just the table name as an argument. It'd be ugly, but it'd work. I don't think there's any way around needing to know the table name.
You're inserting value into the first column, but you need to add a value in the second position.
Therefore you can use INSERT INTO table(field) VALUES(value) syntax.
Since you need to fetch values from another table, you have to remove VALUES and put the subquery there.
insert into pk_test_table(name)
select first_name from person_test;
I hope it helps
I do it this way via a separate function- though I think I'm getting around the issue via the table level having the DEFAULT settings on a per field basis.
create table public.pk_test_table
(
recordid integer NOT NULL DEFAULT nextval('pk_test_table_id_seq'),
name text,
field3 integer NOT NULL DEFAULT 64,
null_field_if_not_set integer,
CONSTRAINT pk_test_table_pkey PRIMARY KEY ("recordid")
);
With function:
CREATE OR REPLACE FUNCTION func_pk_test_table() RETURNS void AS
$BODY$
INSERT INTO pk_test_table (name)
SELECT first_name FROM person_test;
$BODY$
LANGUAGE sql VOLATILE;
Then just execute the function via a SELECT FROM func_pk_test_table();
Notice it hasn't had to specify all the fields- as long as constraints allow it.
Here i need to select a column name by using function(stored procedure) which is present in other database table using PostgreSQL.
I have sql server query as shown below.
Example:
create procedure sp_testing
as
if not exists ( select ssn from testdb..testtable) /*ssn is the column-name of testtable which exists in testdb database */
...
Q: Can i do the same in PostgreSQL?
Your question is not very clear, but if you want to know if a column by a certain name exists in a table by a certain name in a remote PostgreSQL database, then you should first set up a foreign data wrapper, which is a multi-stage process. Then to test the existence of a certain column in a table you need to formulate a query that conforms to the standards of the particular DBMS that you are connecting to. Use the remote information_schema.tables table for optimal compatibility (which is here specified as remote_tables which you must have defined with a prior CREATE FOREIGN TABLE command):
CREATE FUNCTION sp_testing () AS $$
BEGIN
PERFORM *
FROM remote_tables
WHERE table_name = 'testtable'
AND column_name = 'ssn';
IF NOT FOUND THEN
...
END IF;
END;
$$ LANGUAGE plpgsql;
If you want to connect to another type of DBMS, you need to write some custom function in f.i. C or perl and then call that from within a PostgreSQL function on your local machine. The test on the column is then best done inside the function which should therefore take connection parameters, table name and column name as parameters, and return a boolean to inform the result.
Before you start testing this, make sure that you read all the documentation on connecting to remote servers and learning PL/pgSQL first would also be a nice gesture to demonstrate your own efforts before you ask for help.