Project

General

Profile

« Previous | Next » 

Revision 2610

schemas/functions.sql: Added _nullIf relational function

View differences:

schemas/functions.sql
19 19
SET search_path = functions, pg_catalog;
20 20

  
21 21
--
22
-- Name: datatype; Type: TYPE; Schema: functions; Owner: -
23
--
24

  
25
CREATE TYPE datatype AS ENUM (
26
    'str',
27
    'float'
28
);
29

  
30

  
31
--
22 32
-- Name: _alt(); Type: FUNCTION; Schema: functions; Owner: -
23 33
--
24 34

  
......
91 101

  
92 102

  
93 103
--
104
-- Name: _nullIf(); Type: FUNCTION; Schema: functions; Owner: -
105
--
106

  
107
CREATE FUNCTION "_nullIf"() RETURNS trigger
108
    LANGUAGE plpgsql IMMUTABLE
109
    AS $$
110
BEGIN
111
    IF new.result = 'str' THEN -- no cast needed
112
        new.result := (SELECT nullif(new.value, new."null"));
113
    -- Invalid value -> warning, but invalid null value -> error
114
    ELSIF new.result = 'float' THEN
115
        new.result := (SELECT nullif(functions."double precision"(new.value),
116
            new."null"::double precision));
117
    END IF;
118
    
119
    RETURN new;
120
END;
121
$$;
122

  
123

  
124
--
94 125
-- Name: boolean(text); Type: FUNCTION; Schema: functions; Owner: -
95 126
--
96 127

  
......
244 275

  
245 276

  
246 277
--
278
-- Name: _nullIf; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
279
--
280

  
281
CREATE TABLE "_nullIf" (
282
    result text,
283
    not_null_col boolean DEFAULT true NOT NULL,
284
    "null" text,
285
    type datatype DEFAULT 'str'::datatype NOT NULL,
286
    value text
287
);
288

  
289

  
290
--
247 291
-- Name: _alt_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
248 292
--
249 293

  
......
265 309

  
266 310

  
267 311
--
312
-- Name: _nullIf_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
313
--
314

  
315
CREATE UNIQUE INDEX "_nullIf_unique" ON "_nullIf" USING btree (ensure_not_null("null"), type, ensure_not_null(value));
316

  
317

  
318
--
268 319
-- Name: _alt; Type: TRIGGER; Schema: functions; Owner: -
269 320
--
270 321

  
......
286 337

  
287 338

  
288 339
--
340
-- Name: _nullIf; Type: TRIGGER; Schema: functions; Owner: -
341
--
342

  
343
CREATE TRIGGER "_nullIf" BEFORE INSERT OR UPDATE ON "_nullIf" FOR EACH ROW EXECUTE PROCEDURE "_nullIf"();
344

  
345

  
346
--
289 347
-- PostgreSQL database dump complete
290 348
--
291 349

  

Also available in: Unified diff