-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PostgreSQL - Functions #578
Conversation
set search path for schema. schema qualified name no longer needed for creation and access of functions.
snake_case column fix for user_create, rename of users.sql file to lowercase
|
||
CREATE OR REPLACE FUNCTION user_update | ||
( | ||
_id UUID, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatting seems weird in this file. Looks like tabs mixed with spaces. should just be spaces.
_public_key TEXT, | ||
_private_key TEXT , | ||
_premium INT, | ||
_premium_expiration_date TEXT, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this passed in as text and then cast to TIMESTAMPTZ
later?
_key TEXT, | ||
_public_key TEXT, | ||
_private_key TEXT , | ||
_premium INT, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this passed as int and cast to bit later?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually ive fixed that timestamp issue now, i was messing with datatypes and missed that one initially when i was passing through NULLS and having issues due to the case (ie null vs NULL)
im recasting some of the datatypes within the functions (ie int/smallint) due to the values being passed as INT and having issues.
also bit v boolean, its better/easier to have the datatype as boolean to begin with, else it has to be passed in as int, and then cast to bit in the function.
this is just a weirdness of the function when its called normally, im not sure how your C code process would call it, but with these datatypes, it has to be cast before calling the function, or the function has to do it when doing the insert/update
eg
select * from fn_some_function(1::BIT,123::SMALLINT) ;
vs
select * from fn_some_function(false,123) ;
vs
for now i've chosen boolean instead of bit, but it can be changed back if this is too much of a change to work with.
last update has the case fixed not sure on int v smallint change you want |
It should be like your update function where no casting is needed. |
i've gone back and reviewed all the functions, and recast within the function to match the current table DDL. tested the removal statements for each also |
Isn't that the opposite of what I suggested? I was suggesting that you do it like your update function was originally so that casting wasn't necessary. The proper type is defined in the parameters from the beginning. |
ok, comms problem, lets resolve it -1/ the function code to cast it (pass in INT value and cast with the update statement), currently it's set to -1 and there is no need to cast outside the function call (just pass in the value as-is), which is simpler and my preference, and allows for extra error control code within the function later to return values if these numbers are out of range or -2, the code that calls the function (your c code) will need to cast it beforehand else PostgreSQL will complain the function does not exist since it cant resolve int v smallint with numbers smaller than the smallint range, and PostgreSQL is very specific about datatypes, more so than SQL Server. the error control code can still be added, but would need to be on the c code side, and may not show anything useful (just could not find function(unknown) kind of error) If it's -2, great, no problem, ill make the changes, and assume the value is passed correctly |
I am suggesting option 2. Dapper is what calls these functions from the .NET code, and I believe it will properly handle casting the type, if necessary, as it passes the value in. I have not tested this yet with Postgres, but it does with SQL Server. Maybe I should test before we commit to one way or the other. |
I changed the query to the following, which uses the same parameter types as defined in the tables. Tested it with my local server and .NET code w/ dapper and it worked fine. No casting needed.
|
great! |
_email VARCHAR, | ||
_email_verified BOOLEAN, | ||
_master_password VARCHAR, | ||
_master_password_hINT VARCHAR, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still typo with uppercase INT here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
AS | ||
$BODY$ | ||
BEGIN | ||
-- functions dont support commit/rollback transactions, only v11+ procedures can do this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well that sucks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Papina why not use a procedure then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mnuccioarpae
I don't know Dapper, but apparently it cant use procedures
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Papina Me neither, but it looks strange to me since CALL conforms to the SQL standard.
@@ -0,0 +1,75 @@ | |||
DROP FUNCTION IF EXISTS user_update(UUID,VARCHAR,VARCHAR,BOOLEAN,VARCHAR,VARCHAR,VARCHAR,VARCHAR,TEXT,VARCHAR,TEXT,TEXT,TIMESTAMPTZ,TEXT,TEXT,TEXT,BOOLEAN,TIMESTAMPTZ,TIMESTAMPTZ,BIGINT,SMALLINT,SMALLINT,VARCHAR,VARCHAR,VARCHAR,SMALLINT,INT,TIMESTAMPTZ,TIMESTAMPTZ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really need these drops if we are using CREATE OR REPLACE
? just seems like added headache to manage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from the postgres doco:
To replace the current definition of an existing function, use CREATE OR REPLACE FUNCTION. It is not possible to change the name or argument types of a function this way (if you tried, you would actually be creating a new, distinct function). Also, CREATE OR REPLACE FUNCTION will not let you change the return type of an existing function. To do that, you must drop and recreate the function. (When using OUT parameters, that means you cannot change the types of any OUT parameters except by dropping the function.)
So yes, ultimately not required once a function is confirmed as working as intended, but i'm using it currently to assist with the conversion, and not having it would be more of a headache for me. i can comment it out if you want
Functions to be converted to PostgreSQL