18.2. Access To PostgreSQL Tables

18.2.1. Create Dataset

example:

18.2.2. Read Dataset

example:

18.2.3. Update Dataset

example:

18.2.4. Delete Dataset

example:

18.2.5. Pre-check Function

If the PostgreSQL function api_pre_check(IN TEXT,IN TEXT,IN TEXT,IN "char",IN TEXT,IN TEXT) exists, The server will execute this function before deciding whether to continue processing the subsequent API request. The server will ignore that API request and return HTTP response code 401 if this function returns false or raises execption. (Avoid raising exceptions because PostgreSQL is often configured to log exceptions, which consumes disk space.)

Function api_pre_check() parameters are described as follows:

  1. user id

  2. target table name

  3. company code

  4. c, r, u, or d

  5. condition

    If the API request is sent in as this:

    curl -v -H "Content-Type:Application/json" -H 'Authorization:abHEi47obYMiCi193xjEiWIFpOouNmkd' -H 'X-Auth-Database:dev5' -d '{"table":"t52","company":"1","query":[[["f0","1"],["f1",1]],[["f4",880],["f5",0]]]}' https://34.terarows.com/fastcgi/api/u
    					

    , the argument condition is passed by server with the value of key "query": [[["f0","1"],["f1",1]],[["f4",880],["f5",0]]]

These arguments are automatically provided by server when it executes the API requests.

api_pre_check() example:

CREATE OR REPLACE FUNCTION api_pre_check(user_id TEXT,table_name TEXT,company TEXT,crud "char",query TEXT) RETURNS BOOLEAN AS $$
DECLARE
	result BOOLEAN;
	j JSONB;
BEGIN
	IF crud IN ('u','d') THEN 
		j:=query::JSONB;
		EXECUTE FORMAT('
			SELECT EXISTS (SELECT 1 FROM t43 WHERE f1=$1 AND f8)
				AND EXISTS (SELECT 1 FROM %I WHERE %I=%L AND %I=$2 AND %I=$3 AND (%I=$4 OR %I IS NULL))'
			,table_name
			,j #>> '{0,0,0}'
			,j #>> '{0,0,1}'
			,j #>> '{0,1,0}'
			,j #>> '{1,0,0}'
			,j #>> '{1,1,0}'
			,j #>> '{1,1,0}'
		)
		INTO result
		USING 
			user_id
			,(j #>> '{0,1,1}')::INTEGER
			,(j #>> '{1,0,1}')::INTEGER
			,(j #>> '{1,1,1}')::INTEGER;
		RETURN company='1' AND result;
	END IF;
	RETURN TRUE;
END $$ LANGUAGE PLPGSQL STABLE;