Can a return value from a function be named with a specific name for Postgraphile? - postgresql

I have this function in PostgreSQL:
CREATE FUNCTION it_exists(
email text,
name text
) RETURNS boolean AS $$
DECLARE
_eva1 boolean;
_eva2 boolean;
BEGIN
_eva1 := EXISTS(SELECT * FROM tableA AS A WHERE A.email = $1::citext);
_eva2 := EXISTS(SELECT * FROM tableB AS A WHERE A.name::citext = $2::citext);
RETURN _eva1 OR _eva2;
END;
$$ LANGUAGE plpgsql STRICT SECURITY DEFINER;
It is translated into Postgraphile like this:
mutation MyMutation($email: String!, $name: String!) {
itExists(
input: { email: $email, name: $name }
) {
boolean
}
}
I'd wish to change "boolean" name to something like "result", any suggestion? Consider I have many functions with different return values.

I think that Postgraphile does this to have its custom mutations follow the Relay specification which says
By convention, mutations are named as verbs, their inputs are the name with "Input" appended at the end, and they return an object that is the name with "Payload" appended.
So your custom mutation creates an ItExistsPayload type with only a single field on it instead of returning a GraphQL Boolean. In the future you might want to extend this payload object with more fields.
It is possible to rename that field by using the #resultFieldName smart tag. In your case:
COMMENT ON FUNCTION it_exists(text, text) IS '#resultFieldName result';

Try returning a table instead:
CREATE FUNCTION it_exists(
email text,
name text
) RETURNS TABLE (result boolean) AS $$ -- return TABLE type
DECLARE
_eva1 boolean;
_eva2 boolean;
BEGIN
_eva1 := EXISTS(SELECT * FROM tableA AS A WHERE A.email = $1::citext);
_eva2 := EXISTS(SELECT * FROM tableB AS A WHERE A.name::citext = $2::citext);
RETURN QUERY SELECT _eva1 OR _eva2; -- modify RETURN to suit TABLE type
END;
$$ LANGUAGE plpgsql STRICT SECURITY DEFINER;

Related

PostgREST POST method converts Array of ints to text

I have a PostgreSQL function logschema.movement that returns a subset of the table logschema.movement, the function looks like this:
CREATE OR REPLACE FUNCTION logschema.movement(
"ids" bigint[], "date" TEXT)
RETURNS SETOF logschema.movement
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $$
BEGIN
RETURN QUERY (SELECT * FROM logschema.movement WHERE "ids" = ANY ("ids") AND "day" >= $2::TIMESTAMP);
END;
$$;
This function works within PostgreSQL, e.g. SELECT logschema.movement(ARRAY[1,2,3], '2021-09-09') doesn't return an error.
If I try to query the function with PostgREST and axios like this:
let payload = {ids: [1,2,3], date: "2021-09-08"}
return await RequestService.postData(new URL(this.api + `/rpc/movement`, this.urlBase), payload);
I get a long error message which contains:
...
data:
{ hint:
'No function matches the given name and argument types. You might need to add explicit type casts.',
details: null,
code: '42883',
message:
'function logschema.movement(lastBatteryChangeDate => text, pseudoIds => text) does not exist' } }
...
It looks to me like my array got converted to a string but I'm not sure.
My PostgreSQL Version is PostgreSQL 12.8 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20210424) 10.3.1 20210424, 64-bit and my PostgREST Version is v7.0.1

default date value to store in a table in snowflake

I created below snowflake procedure and from that procedure I want to
insert default date value into a table. Below is the script.
create or replace procedure test_dt() returns string not null language
javascript //execute as owner
as
$$
try {
var c_dt=`select current_date()`;
snowflake.execute({sqlText:c_dt});
var sql_query = `insert into test_date values (:1)`;
var resultSet = snowflake.execute( {sqlText: sql_query, binds:c_dt});
}
catch(err) {
return err.message;
}
$$;
call test_dt();
while executing the procedure I am getting below error.
"Date 'select current_date()' is not recognized"
Please help me on this.
you are binding the "date" in the second query, to the input sql that "gets the current_date()" and not the actual result, thus it's the same as
insert into test_date values ('select current_date()');
so you ether want to save the result of the first query OR just use the current date aka CURRENT_DATE
insert into test_date values (CURRENT_DATE);
create or replace procedure test_dt() returns string not null language
javascript //execute as owner
as
$$
try {
var sql_query = `insert into test_date values (current_date)`;
var resultSet = snowflake.execute( {sqlText: sql_query});
}
catch(err) {
return err.message;
}
$$;```

Calling a function on insert with JDBC

I have the following function and table in my PostgreSQL database:
CREATE OR REPLACE FUNCTION generate_uid(size INT) RETURNS TEXT AS $$
DECLARE
characters TEXT := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
bytes BYTEA := gen_random_bytes(size);
l INT := length(characters);
i INT := 0;
output TEXT := '';
BEGIN
WHILE i < size LOOP
output := output || substr(characters, get_byte(bytes, i) % l + 1, 1);
i := i + 1;
END LOOP;
RETURN output;
END;
$$ LANGUAGE plpgsql VOLATILE;
create table users
(
userid text primary key default generate_uid(50)
, username varchar (50) not null
, pass varchar (50) not null
, firstname varchar (100) not null
, lastname varchar (100) not null
, email varchar (150) not null
, roleid int not null
, constraint fkrole foreign key(roleid) references userrole(roleid)
);
Then I call on the function in my DAO in JDBC with this block of code:
Account A = new Account();
String sha256hex = Hashing.sha256()
.hashString(password, StandardCharsets.UTF_8)
.toString();
try (Connection conn = CustomClassFactory.getConnection()) {
String sql = "INSERT INTO users (username, pass, firstname, lastname, email, roleid) VALUES (?,?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, sha256hex);
ps.setString(3, firstName);
ps.setString(4, lastName);
ps.setString(5, email);
ps.setInt(6, roleId);
System.out.println(ps.toString());
int i = ps.executeUpdate(); // <---update not query. this line is what sends the information to the DB
if (i == 0) {
System.out.println("Sorry, database was not updated. Returning to menu");
return null;
}
} catch (SQLException e) {
System.out.println("Sorry, database was not contacted. Bring your developer coffee. In the Insert Statement");
e.printStackTrace();
return null;
}
I am receiving the following error from the Stack Trace:
org.postgresql.util.PSQLException: ERROR: function gen_random_bytes(integer) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Where: PL/pgSQL function generate_uid(integer) line 8 during statement block local variable initialization
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2552)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:322)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:401)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:164)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:130)
at dao.AccountDaoImp.CreateAccount(AccountDaoImp.java:35)
at testing.Tester.main(Tester.java:11)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "models.Account.toString()" because the return value of "dao.AccountDaoImp.CreateAccount(String, String, String, String, String, int)" is null
at testing.Tester.main(Tester.java:11)
How do I make sure it sees the function when I create a new user? The function is designed to generate a random string of text to use as a unique ID.
gen_random_bytes is part of the pgcrypto extension.
So run this in your database:
CREATE EXTENSION pgcrypto SCHEMA public;
To make sure you don't have to rely on search_path, you can prefix public to the function call, like in public.gen_random_uuid().

Querying a many:many relationship on PK of the related table (ie. filtering by related table column)

I have a many:many relationship between 2 tables: note and tag, and want to be able to search all notes by their tagId. Because of the many:many I have a junction table note_tag.
My goal is to expose a computed field on my Postgraphile-generated Graphql schema that I can query against, along with the other properties of the note table.
I'm playing around with postgraphile-plugin-connection-filter. This plugin makes it possible to filter by things like authorId (which would be 1:many), but I'm unable to figure out how to filter by a many:many. I have a computed column on my note table called tags, which is JSON. Is there a way to "look into" this json and pick out where id = 1?
Here is my computed column tags:
create or replace function note_tags(note note, tagid text)
returns jsonb as $$
select
json_strip_nulls(
json_agg(
json_build_object(
'title', tag.title,
'id', tag.id,
)
)
)::jsonb
from note
inner join note_tag on note_tag.tag_id = tagid and note_tag.note_id = note.id
left join note_tag nt on note.id = nt.note_id
left join tag on nt.tag_id = tag.id
where note.account_id = '1'
group by note.id, note.title;
$$ language sql stable;
as I understand the function above, I am returning jsonb, based on the tagid that was given (to the function): inner join note_tag on note_tag.tag_id = tagid. So why is the json not being filtered by id when the column gets computed?
I am trying to make a query like this:
query notesByTagId {
notes {
edges {
node {
title
id
tags(tagid: "1")
}
}
}
}
but right now when I execute this query, I get back stringified JSON in the tags field. However, all tags are included in the json, whether or not the note actually belongs to that tag or not.
For instance, this note with id = 1 should only have tags with id = 1 and id = 2. Right now it returns every tag in the database
{
"data": {
"notes": {
"edges": [
{
"node": {
"id": "1",
"tags": "[{\"id\":\"1\",\"title\":\"Psychology\"},{\"id\":\"2\",\"title\":\"Logic\"},{\"id\":\"3\",\"title\":\"Charisma\"}]",
...
The key factor with this computed column is that the JSON must include all tags that the note belongs to, even though we are searching for notes on a single tagid
here are my simplified tables...
note:
create table notes(
id text,
title text
)
tag:
create table tag(
id text,
title text
)
note_tag:
create table note_tag(
note_id text FK references note.id
tag_id text FK references tag.id
)
Update
I am changing up the approach a bit, and am toying with the following function:
create or replace function note_tags(n note)
returns setof tag as $$
select tag.*
from tag
inner join note_tag on (note_tag.tag_id = tag.id)
where note_tag.note_id = n.id;
$$ language sql stable;
I am able to retrieve all notes with the tags field populated, but now I need to be able to filter out the notes that don't belong to a particular tag, while still retaining all of the tags that belong to a given note.
So the question remains the same as above: how do we filter a table based on a related table's PK?
After a while of digging, I think I've come across a good approach. Based on this response, I have made a function that returns all notes by a given tagid.
Here it is:
create or replace function all_notes_with_tag_id(tagid text)
returns setof note as $$
select distinct note.*
from tag
inner join note_tag on (note_tag.tag_id = tag.id)
inner join note on (note_tag.note_id = note.id)
where tag.id = tagid;
$$ language sql stable;
The error in approach was to expect the computed column to do all of the work, whereas its only job should be to get all of the data. This function all_nuggets_with_bucket_id can now be called directly in graphql like so:
query MyQuery($tagid: String!) {
allNotesWithTagId(tagid: $tagid) {
edges {
node {
id
title
tags {
edges {
node {
id
title
}
}
}
}
}
}
}

Error: cannot insert multiple commands into a prepared statement

I am trying to move a row from one table to another.
The problem is that if I put both queries together, I get "error: cannot insert multiple commands into a prepared statement". What can I do?
exports.deletePost = function(id) {
return db.query(`INSERT INTO deletedjobs
SELECT *
FROM jobs
WHERE id = $1;
DELETE FROM jobs WHERE id = $1;`, [id]).then(results => {
console.log("succesfull transfer");
return results.rows[0];
});
};
EDIT: Following Docs v7.0.0, I found out db.multi can execute a multi-query string, you can try this:
db.multi(`INSERT INTO deletedjobs
SELECT *
FROM jobs
WHERE id = $1;DELETE FROM jobs WHERE id = $1`, [id])
Other way I think the better solution is that you should wrap the query into a function for insert-delete at same time, like below:
CREATE FUNCTION moveJob(id character varying) RETURNs void AS
$BODY$
BEGIN
INSERT INTO deletedjobs
SELECT *
FROM jobs
WHERE id = id;
DELETE FROM jobs WHERE id = id;
END;
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY DEFINER
COST 100;
And call it as postgresql function in your js:
db.any('select moveJob($1)', [id]);
You can also use a WITH ... AS clause (https://www.postgresql.org/docs/9.1/queries-with.html) to execute both queries in one go. So it could look something like that:
exports.deletePost = function(id) {
return db.query(`
WITH
A AS (SELECT * FROM jobs WHERE id = $1),
B AS (INSERT INTO deletedjobs FROM A),
DELETE FROM jobs WHERE id IN (SELECT id FROM A);
`, [id]).then(results => {
console.log("succesfull transfer");
return results.rows[0];
});
};
Maybe you should not use AND clause, try semicolon ;.
You used select * you should enter columns name, because in later
times you may add a new column, your function does not execute.