src-db/database/model/prescript-PostgreSql.sql
author RM packaging bot <staff.rm@openbravo.com>
Tue, 11 Dec 2018 12:26:44 +0000
changeset 35215 4c4039a9c917
parent 34978 388ac010944d
parent 34890 e86213540c78
child 35538 8b497714669e
permissions -rw-r--r--
Merge back from main
carlos@62
     1
CREATE OR REPLACE FUNCTION exist_language(varchar)
carlos@62
     2
RETURNS bigint AS ' 
carlos@62
     3
  SELECT count(*) from pg_language where lanname = $1;
antonio@1149
     4
' LANGUAGE 'sql' STABLE
carlos@62
     5
/-- END
carlos@62
     6
carlos@62
     7
CREATE OR REPLACE FUNCTION insert_pg_language()
carlos@62
     8
RETURNS integer AS ' 
carlos@62
     9
  CREATE TRUSTED PROCEDURAL LANGUAGE ''plpgsql''
carlos@0
    10
  HANDLER plpgsql_call_handler
carlos@62
    11
  VALIDATOR plpgsql_validator;
carlos@62
    12
  SELECT 1;
carlos@62
    13
' LANGUAGE 'sql'
carlos@0
    14
/-- END
carlos@0
    15
carlos@62
    16
CREATE OR REPLACE FUNCTION create_language(varchar)
carlos@62
    17
RETURNS integer AS '
carlos@62
    18
SELECT
carlos@62
    19
CASE WHEN exist_language($1)=0
carlos@62
    20
THEN insert_pg_language()
carlos@62
    21
END;
carlos@62
    22
SELECT 1;
carlos@62
    23
' LANGUAGE 'sql'
carlos@62
    24
/-- END
carlos@62
    25
carlos@62
    26
SELECT * FROM create_language('plpgsql')
carlos@62
    27
/-- END
carlos@62
    28
carlos@62
    29
--CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
carlos@62
    30
--  HANDLER plpgsql_call_handler
carlos@62
    31
--  VALIDATOR plpgsql_validator;
carlos@62
    32
--/--END
carlos@62
    33
adrian@236
    34
CREATE OR REPLACE FUNCTION dba_getattnumpos(conkey _int4, attnum int4)
adrian@236
    35
  RETURNS int4 AS
adrian@236
    36
$BODY$
adrian@236
    37
/*************************************************************************
adrian@236
    38
* The contents of this file are subject to the Openbravo  Public  License
priya@9085
    39
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
adrian@236
    40
* Version 1.1  with a permitted attribution clause; you may not  use this
adrian@236
    41
* file except in compliance with the License. You  may  obtain  a copy of
adrian@236
    42
* the License at http://www.openbravo.com/legal/license.html
adrian@236
    43
* Software distributed under the License  is  distributed  on  an "AS IS"
adrian@236
    44
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
adrian@236
    45
* License for the specific  language  governing  rights  and  limitations
adrian@236
    46
* under the License.
adrian@236
    47
* The Original Code is Openbravo ERP.
ggi@6702
    48
* The Initial Developer of the Original Code is Openbravo SLU
ggi@6702
    49
* All portions are Copyright (C) 2001-2006 Openbravo SLU
adrian@236
    50
* All Rights Reserved.
adrian@236
    51
* Contributor(s):  ______________________________________.
adrian@236
    52
************************************************************************/
adrian@236
    53
begin
adrian@236
    54
  for i in 1..array_upper(conkey,1)
adrian@236
    55
  loop     
adrian@236
    56
     IF (conkey[i] = attnum) THEN
adrian@236
    57
	RETURN i;
adrian@236
    58
     END IF;
adrian@236
    59
  end loop;  
adrian@236
    60
  return 0;
adrian@236
    61
end;
adrian@236
    62
$BODY$
adrian@236
    63
  LANGUAGE 'plpgsql' VOLATILE
adrian@236
    64
/-- END
adrian@236
    65
adrian@236
    66
adrian@236
    67
CREATE OR REPLACE FUNCTION dba_getstandard_search_text(text)
adrian@236
    68
  RETURNS text AS
adrian@236
    69
$BODY$
adrian@236
    70
/*************************************************************************
adrian@236
    71
* The contents of this file are subject to the Openbravo  Public  License
priya@9085
    72
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
adrian@236
    73
* Version 1.1  with a permitted attribution clause; you may not  use this
adrian@236
    74
* file except in compliance with the License. You  may  obtain  a copy of
adrian@236
    75
* the License at http://www.openbravo.com/legal/license.html
adrian@236
    76
* Software distributed under the License  is  distributed  on  an "AS IS"
adrian@236
    77
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
adrian@236
    78
* License for the specific  language  governing  rights  and  limitations
adrian@236
    79
* under the License.
adrian@236
    80
* The Original Code is Openbravo ERP.
ggi@6702
    81
* The Initial Developer of the Original Code is Openbravo SLU
ggi@6702
    82
* All portions are Copyright (C) 2001-2006 Openbravo SLU
adrian@236
    83
* All Rights Reserved.
adrian@236
    84
* Contributor(s):  ______________________________________.
adrian@236
    85
************************************************************************/
adrian@236
    86
  DECLARE  v_text TEXT;
adrian@236
    87
  DECLARE  v_p1 NUMERIC;
adrian@236
    88
  DECLARE  v_p2 NUMERIC;
adrian@236
    89
  DECLARE  v_p3 NUMERIC;
adrian@236
    90
  DECLARE  v_i NUMERIC;
adrian@236
    91
begin
adrian@236
    92
  v_text := replace($1, ' = ANY',' in');
adrian@236
    93
  v_text := replace(v_text, 'ARRAY[', '');
adrian@236
    94
  v_text := replace(v_text, ']' , '');
adrian@236
    95
  v_text := replace(v_text, '::bpchar', '');
adrian@236
    96
  v_text := replace(v_text, '::text', '');
adrian@236
    97
  v_text := substring(v_text,2,length(v_text)-2);
adrian@236
    98
adrian@236
    99
    WHILE (v_text LIKE '%::%') LOOP
adrian@236
   100
      v_p1 := INSTR(v_text, '::');
adrian@236
   101
      v_p2 := 0;
adrian@236
   102
      v_p3 := 0;
adrian@236
   103
      IF (SUBSTR(v_text,v_p1-1,1) = ')') THEN
adrian@236
   104
        v_i := 1;
adrian@236
   105
        WHILE (v_p2=0) LOOP
adrian@236
   106
          v_i := v_i + 1;
adrian@236
   107
          IF (SUBSTR(v_text, v_p1 - v_i, 1) = '(') THEN
adrian@236
   108
            v_p2 := v_p1 - v_i;
adrian@236
   109
          END IF;
adrian@236
   110
        END LOOP;
adrian@236
   111
      END IF;
adrian@236
   112
      v_i := 1;
adrian@236
   113
      WHILE (v_p3=0) LOOP
adrian@236
   114
        v_i := v_i + 1;
adrian@236
   115
        IF ((SUBSTR(v_text, v_p1 + v_i, 1) IN (' ',')',CHR(10),CHR(13)) OR (v_p1 + v_i>= length(v_text))))  THEN
adrian@236
   116
          v_p3 := v_p1 + v_i;
adrian@236
   117
        END IF;
adrian@236
   118
      END LOOP;
adrian@236
   119
      --RAISE_APPLICATION_ERROR(-20001,'v_p3 = '||v_p3);
adrian@236
   120
      IF (v_p2 = 0) THEN
adrian@236
   121
        v_text := SUBSTR(v_text, 1, v_p1-1) || SUBSTR(v_text, v_p3, 4000);
adrian@236
   122
      ELSE
adrian@236
   123
        v_text := SUBSTR(v_text, 1, v_p2-1) || SUBSTR(v_text, v_p2 + 1, v_p1 - (v_p2 + 2)) || SUBSTR(v_text, v_p3, 4000);
adrian@236
   124
      END IF;
adrian@236
   125
    END LOOP;
adrian@236
   126
adrian@236
   127
    RETURN upper(v_text);
adrian@236
   128
END;
adrian@236
   129
$BODY$
adrian@236
   130
  LANGUAGE 'plpgsql' VOLATILE
adrian@236
   131
/-- END
adrian@236
   132
carlos@0
   133
CREATE OR REPLACE FUNCTION dateFormat
carlos@0
   134
(
carlos@0
   135
)
carlos@0
   136
RETURNS VARCHAR AS ' 
carlos@0
   137
BEGIN
carlos@0
   138
RETURN ''DD-MM-YYYY'';
carlos@0
   139
EXCEPTION 
carlos@0
   140
  WHEN OTHERS THEN 
carlos@0
   141
    RETURN NULL;
carlos@0
   142
END;
antonio@1149
   143
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   144
/-- END
carlos@0
   145
carlos@1423
   146
CREATE or REPLACE FUNCTION update_dateFormat 
carlos@1423
   147
(
carlos@1423
   148
  format IN varchar
carlos@1423
   149
) 
carlos@1423
   150
RETURNS varchar AS '
carlos@1419
   151
DECLARE
carlos@1419
   152
BEGIN
martin@5952
   153
  IF (dateformat() <> format) THEN
martin@5952
   154
    EXECUTE ''CREATE OR REPLACE FUNCTION dateFormat() RETURNS VARCHAR AS '''' DECLARE BEGIN  RETURN '''''''''' || format || ''''''''''; EXCEPTION WHEN OTHERS THEN RETURN NULL; END; '''' LANGUAGE ''''plpgsql'''' IMMUTABLE; '';
martin@5952
   155
    RETURN ''dateFormat modified'';
martin@5952
   156
  END IF;
martin@5952
   157
  RETURN ''dateFormat not modified'';
carlos@1423
   158
END;
ivan@1562
   159
' LANGUAGE 'plpgsql' VOLATILE
carlos@1419
   160
/-- END
carlos@1419
   161
carlos@1419
   162
carlos@0
   163
CREATE OR REPLACE FUNCTION to_number
carlos@0
   164
(
carlos@0
   165
text
carlos@0
   166
)
carlos@0
   167
RETURNS NUMERIC AS '
carlos@0
   168
BEGIN
asier@29583
   169
  RETURN cast($1 as numeric);
augusto@15481
   170
EXCEPTION
augusto@15481
   171
  WHEN OTHERS THEN
carlos@0
   172
    RETURN NULL;
carlos@0
   173
END;
antonio@1149
   174
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   175
/-- END
carlos@169
   176
 
carlos@169
   177
CREATE OR REPLACE FUNCTION to_number
carlos@169
   178
(
carlos@169
   179
 interval
carlos@169
   180
)
carlos@169
   181
RETURNS NUMERIC AS '
carlos@169
   182
BEGIN
carlos@647
   183
RETURN extract(epoch FROM ($1))/86400;
carlos@169
   184
EXCEPTION 
carlos@169
   185
  WHEN OTHERS THEN 
carlos@169
   186
    RETURN NULL;
carlos@169
   187
END;
antonio@1149
   188
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@169
   189
/-- END
carlos@0
   190
carlos@423
   191
CREATE OR REPLACE FUNCTION to_number(integer)
carlos@423
   192
  RETURNS "numeric" AS
carlos@423
   193
$BODY$
carlos@423
   194
BEGIN
antonio@15479
   195
RETURN $1;
carlos@423
   196
EXCEPTION 
carlos@423
   197
  WHEN OTHERS THEN 
carlos@423
   198
    RETURN NULL;
carlos@423
   199
END;
carlos@423
   200
$BODY$
antonio@1149
   201
  LANGUAGE 'plpgsql' IMMUTABLE;
carlos@423
   202
/-- END
carlos@423
   203
ben@487
   204
CREATE OR REPLACE FUNCTION to_number(bigint)
ben@487
   205
  RETURNS "numeric" AS
ben@487
   206
$BODY$
ben@487
   207
BEGIN
ben@487
   208
RETURN cast($1 as numeric);
ben@487
   209
END;
ben@487
   210
$BODY$
antonio@1149
   211
  LANGUAGE 'plpgsql' IMMUTABLE;
ben@487
   212
/-- END
ben@487
   213
ben@487
   214
carlos@423
   215
CREATE OR REPLACE FUNCTION to_number(numeric)
carlos@423
   216
  RETURNS "numeric" AS
carlos@423
   217
$BODY$
carlos@423
   218
BEGIN
carlos@423
   219
RETURN $1;
carlos@423
   220
EXCEPTION 
carlos@423
   221
  WHEN OTHERS THEN 
carlos@423
   222
    RETURN NULL;
carlos@423
   223
END;
carlos@423
   224
$BODY$
antonio@1149
   225
  LANGUAGE 'plpgsql' IMMUTABLE;
carlos@423
   226
/-- END
carlos@423
   227
carlos@0
   228
CREATE OR REPLACE FUNCTION to_date
carlos@0
   229
(
carlos@0
   230
text
carlos@0
   231
)
asier@33738
   232
RETURNS timestamp 
asier@33738
   233
SET SEARCH_PATH FROM CURRENT
asier@33738
   234
AS '
carlos@0
   235
BEGIN
carlos@0
   236
RETURN to_timestamp($1, dateFormat());
carlos@0
   237
END;
antonio@1149
   238
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   239
/-- END
carlos@0
   240
carlos@0
   241
CREATE OR REPLACE FUNCTION to_date
carlos@0
   242
(
carlos@0
   243
 timestamptz
carlos@0
   244
)
asier@33738
   245
  RETURNS timestamp
asier@33738
   246
  SET SEARCH_PATH FROM CURRENT
asier@33738
   247
  AS '
carlos@0
   248
BEGIN
carlos@681
   249
  RETURN to_timestamp(to_char($1, dateFormat()||'' HH24:MI:SS''), dateFormat()||'' HH24:MI:SS'');
carlos@0
   250
END;
antonio@1149
   251
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   252
/-- END
carlos@0
   253
naroa@29517
   254
CREATE OR REPLACE FUNCTION to_date
naroa@29517
   255
(
naroa@29517
   256
 timestamp, varchar
naroa@29517
   257
)
asier@33738
   258
  RETURNS timestamp
asier@33738
   259
  SET SEARCH_PATH FROM CURRENT
asier@33738
   260
  AS '
naroa@29517
   261
BEGIN
naroa@29517
   262
  RETURN to_timestamp(to_char($1), $2);
naroa@29517
   263
END;
naroa@29517
   264
' LANGUAGE 'plpgsql' IMMUTABLE
naroa@29517
   265
/-- END
naroa@29517
   266
carlos@0
   267
CREATE OR REPLACE FUNCTION to_timestamp
carlos@0
   268
(
carlos@0
   269
 timestamptz
carlos@0
   270
)
asier@33738
   271
  RETURNS timestamp
asier@33738
   272
  SET SEARCH_PATH FROM CURRENT
asier@33738
   273
  AS '
carlos@0
   274
BEGIN
carlos@0
   275
RETURN to_timestamp(to_char($1, dateFormat()), dateFormat());
carlos@0
   276
END;
antonio@1149
   277
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   278
/-- END
carlos@0
   279
carlos@0
   280
CREATE OR REPLACE FUNCTION to_char
carlos@0
   281
(
carlos@0
   282
integer
carlos@0
   283
)
asier@33738
   284
RETURNS  VARCHAR
asier@33738
   285
SET SEARCH_PATH FROM CURRENT
asier@33738
   286
AS '
carlos@0
   287
BEGIN
carlos@0
   288
RETURN to_char($1, ''999999999999D'');
carlos@0
   289
END;
antonio@1149
   290
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   291
/-- END
carlos@0
   292
carlos@0
   293
CREATE OR REPLACE FUNCTION to_char
carlos@0
   294
(
ben@487
   295
bigint
ben@487
   296
)
ben@487
   297
RETURNS  VARCHAR AS '
ben@487
   298
BEGIN
ben@487
   299
RETURN cast($1 as VARCHAR);
ben@487
   300
END;
antonio@1149
   301
' LANGUAGE 'plpgsql' IMMUTABLE
ben@487
   302
/-- END
ben@487
   303
ben@487
   304
ben@487
   305
CREATE OR REPLACE FUNCTION to_char
ben@487
   306
(
carlos@0
   307
timestamp
carlos@0
   308
)
asier@33738
   309
RETURNS  VARCHAR
asier@33738
   310
SET SEARCH_PATH FROM CURRENT
asier@33738
   311
AS '
carlos@0
   312
BEGIN
carlos@0
   313
RETURN to_char($1, dateFormat());
carlos@0
   314
END;
antonio@1149
   315
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   316
/-- END
carlos@0
   317
carlos@0
   318
CREATE OR REPLACE FUNCTION to_char
carlos@0
   319
(
carlos@767
   320
date
carlos@767
   321
)
asier@33738
   322
RETURNS  VARCHAR
asier@33738
   323
SET SEARCH_PATH FROM CURRENT
asier@33738
   324
AS '
carlos@767
   325
BEGIN
carlos@767
   326
RETURN to_char(to_date($1), dateFormat());
carlos@767
   327
END;
antonio@1149
   328
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@767
   329
/-- END
carlos@767
   330
carlos@767
   331
CREATE OR REPLACE FUNCTION to_char
carlos@767
   332
(
carlos@0
   333
VARCHAR
carlos@0
   334
)
carlos@0
   335
RETURNS  VARCHAR AS '
carlos@0
   336
BEGIN
carlos@0
   337
RETURN $1;
carlos@0
   338
END;
antonio@1149
   339
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   340
/-- END
carlos@0
   341
carlos@0
   342
CREATE OR REPLACE FUNCTION to_char
carlos@0
   343
(
carlos@0
   344
NUMERIC
carlos@0
   345
)
carlos@0
   346
RETURNS  VARCHAR AS '
carlos@0
   347
BEGIN
carlos@0
   348
RETURN ''''||$1;
carlos@0
   349
END;
antonio@1149
   350
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   351
/-- END
carlos@0
   352
carlos@0
   353
CREATE OR REPLACE FUNCTION to_char
carlos@0
   354
(
carlos@0
   355
text
carlos@0
   356
)
carlos@0
   357
RETURNS  VARCHAR AS '
carlos@0
   358
BEGIN
carlos@0
   359
RETURN $1;
carlos@0
   360
END;
antonio@1149
   361
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   362
/-- END
carlos@0
   363
carlos@0
   364
CREATE OR REPLACE FUNCTION round
carlos@0
   365
(
carlos@0
   366
numeric,
carlos@0
   367
numeric
carlos@0
   368
)
carlos@0
   369
RETURNS  NUMERIC AS '
carlos@0
   370
BEGIN
carlos@0
   371
RETURN round($1,CAST($2 AS INTEGER));
carlos@0
   372
END;
antonio@1149
   373
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   374
/-- END
carlos@0
   375
carlos@0
   376
CREATE OR REPLACE FUNCTION rpad
carlos@0
   377
(
carlos@0
   378
varchar,
carlos@0
   379
numeric,
carlos@0
   380
varchar
carlos@0
   381
)
asier@33738
   382
RETURNS VARCHAR
asier@33738
   383
SET SEARCH_PATH FROM CURRENT
asier@33738
   384
AS '
carlos@0
   385
BEGIN
carlos@0
   386
return to_char(rpad($1::text,CAST($2 AS INTEGER), $3::text));
carlos@0
   387
END;
antonio@1149
   388
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   389
/-- END
carlos@0
   390
carlos@0
   391
CREATE OR REPLACE FUNCTION substr
carlos@0
   392
(
carlos@0
   393
varchar,
carlos@0
   394
numeric,
carlos@0
   395
numeric
carlos@0
   396
)
carlos@0
   397
RETURNS VARCHAR AS '
carlos@0
   398
BEGIN
carlos@0
   399
return substr($1,CAST((CASE $2 WHEN 0 THEN 1 ELSE $2 END) AS INTEGER), CAST($3 AS INTEGER));
carlos@0
   400
END;
antonio@1149
   401
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   402
/-- END
carlos@0
   403
carlos@0
   404
create or replace function to_interval (
carlos@0
   405
       integer,
carlos@0
   406
       varchar
carlos@0
   407
)
carlos@0
   408
returns interval 
carlos@62
   409
as '    
carlos@0
   410
declare    
carlos@62
   411
       interval__number      alias for $1;
carlos@62
   412
       interval__units       alias for $2;
carlos@0
   413
begin
carlos@0
   414
carlos@62
   415
    -- We should probably do unit checking at some point
carlos@62
   416
    return ('''''''' || interval__number || '' '' || interval__units || '''''''')::interval;
carlos@0
   417
carlos@0
   418
END;
antonio@1149
   419
' language 'plpgsql' IMMUTABLE
carlos@0
   420
/-- END
carlos@0
   421
carlos@0
   422
create or replace function add_months (
carlos@0
   423
       timestamptz, 
carlos@0
   424
       integer
carlos@0
   425
)
asier@33738
   426
returns timestamptz
asier@33738
   427
SET SEARCH_PATH FROM CURRENT
asier@33738
   428
as '
carlos@0
   429
begin
carlos@62
   430
    return $1 + to_interval($2,to_char(''months''));
carlos@0
   431
carlos@0
   432
END;
antonio@1149
   433
' language 'plpgsql' IMMUTABLE
carlos@0
   434
/-- END
carlos@0
   435
naroa@30310
   436
DROP function IF EXISTS hex_to_int(varchar);
naroa@30310
   437
naroa@30310
   438
DROP function IF EXISTS hex_to_int(numeric);
naroa@30310
   439
carlos@0
   440
CREATE OR REPLACE FUNCTION add_months
carlos@0
   441
(
carlos@0
   442
date,
carlos@0
   443
numeric
carlos@0
   444
)
asier@33738
   445
RETURNS timestamptz
asier@33738
   446
SET SEARCH_PATH FROM CURRENT
asier@33738
   447
AS '
carlos@0
   448
BEGIN
carlos@62
   449
    return $1 + to_interval($2::INTEGER,to_char(''months''));
carlos@0
   450
END;
antonio@1149
   451
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   452
/-- END
carlos@0
   453
carlos@0
   454
CREATE OR REPLACE FUNCTION add_months
carlos@0
   455
(
carlos@0
   456
timestamp,
carlos@0
   457
integer
carlos@0
   458
)
asier@33738
   459
RETURNS timestamptz 
asier@33738
   460
SET SEARCH_PATH FROM CURRENT
asier@33738
   461
AS '
carlos@0
   462
BEGIN
carlos@62
   463
    return $1 + to_interval($2,to_char(''months''));
carlos@0
   464
END;
antonio@1149
   465
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   466
/-- END
carlos@0
   467
carlos@0
   468
CREATE OR REPLACE FUNCTION add_months
carlos@0
   469
(
carlos@0
   470
timestamp,
carlos@0
   471
numeric
carlos@0
   472
)
asier@33738
   473
RETURNS timestamptz 
asier@33738
   474
SET SEARCH_PATH FROM CURRENT
asier@33738
   475
AS '
carlos@0
   476
BEGIN
carlos@62
   477
    return $1 + to_interval($2::INTEGER,to_char(''months''));
carlos@0
   478
END;
antonio@1149
   479
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   480
/-- END
carlos@0
   481
carlos@0
   482
CREATE OR REPLACE FUNCTION add_days
carlos@0
   483
(
carlos@0
   484
timestamp with time zone,
carlos@0
   485
INTEGER
carlos@0
   486
)
carlos@0
   487
RETURNS DATE AS '
carlos@0
   488
BEGIN
carlos@0
   489
RETURN cast($1 AS date) + $2 ;
carlos@0
   490
END;
antonio@1149
   491
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   492
/-- END
carlos@0
   493
carlos@0
   494
CREATE OR REPLACE FUNCTION add_days
carlos@0
   495
(
carlos@0
   496
date,
carlos@0
   497
NUMERIC
carlos@0
   498
)
carlos@0
   499
RETURNS DATE AS '
carlos@0
   500
BEGIN
carlos@0
   501
RETURN $1 + cast($2 AS INTEGER) ;
carlos@0
   502
END;
antonio@1149
   503
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   504
/-- END
carlos@0
   505
carlos@0
   506
CREATE OR REPLACE FUNCTION add_days(timestamptz, numeric)
carlos@0
   507
  RETURNS date 
carlos@0
   508
AS '
carlos@0
   509
BEGIN
carlos@0
   510
RETURN cast($1 AS date) + cast($2 AS integer) ;
carlos@0
   511
END;
antonio@1149
   512
'  LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   513
/-- END
carlos@0
   514
carlos@250
   515
CREATE OR REPLACE FUNCTION months_between (init_date timestamptz, end_date timestamptz) 
carlos@250
   516
RETURNS integer 
carlos@250
   517
AS '
carlos@250
   518
DECLARE
carlos@250
   519
  v_months INTEGER;
carlos@250
   520
  v_months_total INTEGER;
carlos@250
   521
  v_years INTEGER;
carlos@250
   522
begin
carlos@250
   523
  v_months=extract(month from (age(init_date,end_date)));
carlos@250
   524
  v_years=extract(year from (age(init_date,end_date)));
carlos@250
   525
carlos@250
   526
  v_months_total=abs((v_years*12) + v_months);
carlos@250
   527
  return v_months_total;
carlos@250
   528
end
antonio@1149
   529
'  LANGUAGE 'plpgsql' IMMUTABLE
carlos@250
   530
/-- END
carlos@62
   531
carlos@62
   532
CREATE OR REPLACE FUNCTION type_oid(varchar)
carlos@62
   533
RETURNS oid AS ' 
carlos@62
   534
  SELECT pg_type.oid from pg_type WHERE pg_type.typname = $1;
antonio@1149
   535
' LANGUAGE 'sql' STABLE
carlos@62
   536
/-- END
carlos@62
   537
carlos@62
   538
-- Creating auxiliar functions for operator dropping
carlos@62
   539
CREATE or REPLACE function drop_operator (operator_name IN varchar,param1 IN varchar,param2 IN varchar) returns varchar as '
carlos@62
   540
DECLARE
carlos@62
   541
  cnt int4;
carlos@62
   542
BEGIN
carlos@62
   543
  SELECT into cnt count(*) from pg_operator where upper(oprname) = upper(operator_name::name) and oprleft = type_oid(param1) and oprright = type_oid(param2);
carlos@62
   544
  if cnt > 0 then
carlos@62
   545
    execute ''DROP OPERATOR '' || operator_name || ''('' || param1 || '','' || param2 || '') CASCADE;'';
carlos@62
   546
    return operator_name || '' DROPPED'';
carlos@62
   547
  end if;
carlos@62
   548
  return operator_name || '' does not exist'';
carlos@62
   549
END;'
carlos@62
   550
language 'plpgsql' 
carlos@62
   551
/-- END
carlos@62
   552
carlos@62
   553
--DROP OPERATOR + (timestamptz, numeric) CASCADE;
carlos@647
   554
SELECT * FROM drop_operator('+'::varchar,'timestamptz'::varchar,'numeric'::varchar)
carlos@62
   555
/-- END
carlos@62
   556
carlos@0
   557
CREATE OPERATOR +(
carlos@0
   558
  PROCEDURE = "add_days",
carlos@0
   559
  LEFTARG = timestamptz,
carlos@0
   560
  RIGHTARG = numeric,
carlos@0
   561
  COMMUTATOR = +)
carlos@0
   562
/-- END
carlos@0
   563
carlos@62
   564
--DROP OPERATOR + (timestamptz, integer);
carlos@647
   565
--SELECT * FROM drop_operator('+'::varchar,'timestamptz'::varchar,'integer'::varchar);
carlos@62
   566
--CREATE OPERATOR + (
carlos@62
   567
--   LEFTARG = timestamptz,
carlos@62
   568
--   RIGHTARG = integer,
carlos@62
   569
--   PROCEDURE = add_days,
carlos@62
   570
--   COMMUTATOR = +
carlos@62
   571
--)
carlos@62
   572
--/--END
carlos@62
   573
carlos@62
   574
--DROP OPERATOR + (date, numeric);
carlos@647
   575
SELECT * FROM drop_operator('+'::varchar,'date'::varchar,'numeric'::varchar)
carlos@0
   576
/-- END
carlos@0
   577
carlos@0
   578
CREATE OPERATOR + (
carlos@0
   579
   LEFTARG = date,
carlos@0
   580
   RIGHTARG = numeric,
carlos@0
   581
   PROCEDURE = add_days,
carlos@0
   582
   COMMUTATOR = +
carlos@0
   583
)
carlos@0
   584
/-- END
carlos@0
   585
carlos@0
   586
CREATE OR REPLACE FUNCTION substract_days(timestamptz, numeric)
carlos@0
   587
  RETURNS date 
carlos@0
   588
AS '
carlos@0
   589
BEGIN
carlos@0
   590
RETURN cast($1 AS date) - cast($2 AS int4) ;
carlos@0
   591
END;
antonio@1149
   592
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   593
/-- END
carlos@0
   594
carlos@0
   595
CREATE OR REPLACE FUNCTION substract_days
carlos@0
   596
(
carlos@0
   597
timestamp with time zone,
carlos@0
   598
INTEGER
carlos@0
   599
)
carlos@0
   600
RETURNS DATE AS '
carlos@0
   601
BEGIN
carlos@0
   602
RETURN cast($1 AS date) - $2 ;
carlos@0
   603
END;
antonio@1149
   604
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   605
/-- END
carlos@0
   606
carlos@0
   607
CREATE OR REPLACE FUNCTION substract_days
carlos@0
   608
(
carlos@0
   609
date,
carlos@0
   610
NUMERIC
carlos@0
   611
)
carlos@0
   612
RETURNS DATE AS '
carlos@0
   613
BEGIN
carlos@0
   614
RETURN $1 - cast($2 AS INTEGER) ;
carlos@0
   615
END;
antonio@1149
   616
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   617
/-- END
carlos@0
   618
carlos@0
   619
--DROP OPERATOR - (timestamptz, integer)/-- END
carlos@647
   620
--SELECT * FROM drop_operator('-'::varchar,'timestamptz'::varchar,'integer'::varchar);
carlos@62
   621
--CREATE OPERATOR - (
carlos@62
   622
--   LEFTARG = timestamp with time zone,
carlos@62
   623
--   RIGHTARG = integer,
carlos@62
   624
--   PROCEDURE = substract_days
carlos@62
   625
--)
carlos@62
   626
--/-- END
carlos@62
   627
carlos@62
   628
--DROP OPERATOR - (date, numeric);
carlos@647
   629
SELECT * FROM drop_operator('-'::varchar,'date'::varchar,'numeric'::varchar)
carlos@0
   630
/-- END
carlos@0
   631
carlos@0
   632
CREATE OPERATOR - (
carlos@0
   633
   LEFTARG = date,
carlos@0
   634
   RIGHTARG = numeric,
carlos@0
   635
   PROCEDURE = substract_days
carlos@0
   636
)
carlos@0
   637
/-- END
carlos@0
   638
carlos@0
   639
carlos@0
   640
CREATE OR REPLACE FUNCTION negation(boolean)
carlos@62
   641
  RETURNS boolean AS '
carlos@0
   642
BEGIN
carlos@0
   643
RETURN NOT $1 ;
carlos@0
   644
END;
antonio@1149
   645
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   646
/-- END
carlos@0
   647
carlos@62
   648
--DROP OPERATOR ! (NONE, boolean);
carlos@647
   649
--SELECT * FROM drop_operator('!'::varchar,'NONE'::varchar,'boolean'::varchar);
carlos@62
   650
--CREATE OPERATOR !(
carlos@62
   651
--  PROCEDURE = "negation",
carlos@62
   652
--  RIGHTARG = boolean)
carlos@62
   653
--/-- END
carlos@0
   654
carlos@423
   655
CREATE OR REPLACE FUNCTION equal(numeric, varchar)
carlos@423
   656
  RETURNS boolean AS '
carlos@423
   657
BEGIN
carlos@423
   658
RETURN $1 = TO_NUMBER($2);
carlos@423
   659
END;
antonio@1149
   660
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@423
   661
/-- END
carlos@423
   662
carlos@423
   663
--DROP OPERATOR = (numeric, varchar);
carlos@647
   664
SELECT * FROM drop_operator('='::varchar,'numeric'::varchar,'varchar'::varchar);
carlos@456
   665
/-- END
carlos@456
   666
carlos@423
   667
CREATE OPERATOR =(
carlos@423
   668
  PROCEDURE = "equal",
carlos@423
   669
  LEFTARG = numeric,
carlos@423
   670
  RIGHTARG = varchar)
carlos@423
   671
--/-- END
carlos@423
   672
carlos@456
   673
CREATE OR REPLACE FUNCTION lowerequalnumeric(numeric, varchar)
asier@33738
   674
  RETURNS boolean 
asier@33738
   675
  SET SEARCH_PATH FROM CURRENT
asier@33738
   676
  AS '
carlos@423
   677
BEGIN
carlos@423
   678
RETURN $1 <= TO_NUMBER($2);
carlos@423
   679
END;
antonio@1149
   680
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@423
   681
/-- END
carlos@423
   682
carlos@423
   683
--DROP OPERATOR <= (numeric, varchar);
carlos@647
   684
SELECT * FROM drop_operator('<='::varchar,'numeric'::varchar,'varchar'::varchar);
carlos@456
   685
/-- END
carlos@456
   686
carlos@423
   687
CREATE OPERATOR <=(
carlos@456
   688
  PROCEDURE = "lowerequalnumeric",
carlos@423
   689
  LEFTARG = numeric,
carlos@423
   690
  RIGHTARG = varchar)
carlos@456
   691
/-- END
carlos@423
   692
carlos@456
   693
CREATE OR REPLACE FUNCTION lowerequaltimestamp(timestamp, varchar)
asier@33738
   694
  RETURNS boolean 
asier@33738
   695
  SET SEARCH_PATH FROM CURRENT
asier@33738
   696
  AS '
carlos@423
   697
BEGIN
carlos@423
   698
RETURN $1 <= TO_DATE($2);
carlos@423
   699
END;
antonio@1149
   700
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@423
   701
/-- END
carlos@423
   702
carlos@423
   703
--DROP OPERATOR <= (timestamp, varchar);
carlos@647
   704
SELECT * FROM drop_operator('<='::varchar,'timestamp'::varchar,'varchar'::varchar);
carlos@456
   705
/-- END
carlos@456
   706
carlos@423
   707
CREATE OPERATOR <=(
carlos@456
   708
  PROCEDURE = "lowerequaltimestamp",
carlos@423
   709
  LEFTARG = timestamp,
carlos@423
   710
  RIGHTARG = varchar)
carlos@456
   711
/-- END
carlos@423
   712
carlos@423
   713
CREATE OR REPLACE FUNCTION greaterequal(timestamp, varchar)
asier@33738
   714
  RETURNS boolean 
asier@33738
   715
  SET SEARCH_PATH FROM CURRENT
asier@33738
   716
  AS '
carlos@423
   717
BEGIN
carlos@423
   718
RETURN $1 >= TO_DATE($2);
carlos@423
   719
END;
antonio@1149
   720
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@423
   721
/-- END
carlos@423
   722
carlos@423
   723
--DROP OPERATOR >= (timestamp, varchar);
carlos@647
   724
SELECT * FROM drop_operator('>='::varchar,'timestamp'::varchar,'varchar'::varchar);
carlos@456
   725
/-- END
carlos@456
   726
carlos@423
   727
CREATE OPERATOR >=(
carlos@423
   728
  PROCEDURE = "greaterequal",
carlos@423
   729
  LEFTARG = timestamp,
carlos@423
   730
  RIGHTARG = varchar)
carlos@456
   731
/-- END
carlos@0
   732
carlos@0
   733
CREATE OR REPLACE FUNCTION trunc
carlos@0
   734
(
carlos@0
   735
date,
carlos@0
   736
varchar
carlos@0
   737
)
carlos@0
   738
RETURNS DATE AS '
carlos@0
   739
DECLARE 
carlos@0
   740
  p_transformation VARCHAR;
carlos@0
   741
BEGIN
david@3083
   742
IF UPPER($2) = ''CC'' THEN
david@3083
   743
  p_transformation := ''century'';
david@3083
   744
ELSIF UPPER($2) = ''YY'' THEN
david@3083
   745
  p_transformation := ''year'';
david@3083
   746
ELSIF UPPER($2) = ''MM'' THEN
carlos@0
   747
  p_transformation := ''month'';
david@3083
   748
ELSIF UPPER($2) = ''Q'' THEN
david@3083
   749
  p_transformation := ''quarter'';
david@3083
   750
ELSIF UPPER($2) = ''DAY'' THEN
david@3083
   751
  p_transformation := ''week'';
carlos@0
   752
ELSIF UPPER($2) = ''DD'' THEN
carlos@0
   753
  p_transformation := ''day'';
david@3083
   754
ELSIF UPPER($2) = ''HH'' THEN
david@3083
   755
  p_transformation := ''hour'';
david@3083
   756
ELSIF UPPER($2) = ''MI'' THEN
david@3083
   757
  p_transformation := ''minute'';
carlos@0
   758
ELSE 
carlos@0
   759
  p_transformation := $2;
carlos@0
   760
END IF;
carlos@0
   761
RETURN date_trunc( p_transformation , $1) ;
carlos@0
   762
END;
antonio@1149
   763
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   764
/-- END
carlos@0
   765
carlos@0
   766
CREATE OR REPLACE FUNCTION trunc
carlos@0
   767
(
carlos@0
   768
timestamp with time zone,
carlos@0
   769
varchar
carlos@0
   770
)
carlos@0
   771
RETURNS DATE AS '
carlos@0
   772
DECLARE 
carlos@0
   773
  p_transformation VARCHAR;
carlos@0
   774
BEGIN
david@3083
   775
IF UPPER($2) = ''CC'' THEN
david@3083
   776
  p_transformation := ''century'';
david@3083
   777
ELSIF UPPER($2) = ''YY'' THEN
david@3083
   778
  p_transformation := ''year'';
david@3083
   779
ELSIF UPPER($2) = ''MM'' THEN
carlos@0
   780
  p_transformation := ''month'';
david@3083
   781
ELSIF UPPER($2) = ''Q'' THEN
david@3083
   782
  p_transformation := ''quarter'';
david@3083
   783
ELSIF UPPER($2) = ''DAY'' THEN
david@3083
   784
  p_transformation := ''week'';
carlos@0
   785
ELSIF UPPER($2) = ''DD'' THEN
carlos@0
   786
  p_transformation := ''day'';
david@3083
   787
ELSIF UPPER($2) = ''HH'' THEN
david@3083
   788
  p_transformation := ''hour'';
david@3083
   789
ELSIF UPPER($2) = ''MI'' THEN
david@3083
   790
  p_transformation := ''minute'';
carlos@0
   791
ELSE 
carlos@0
   792
  p_transformation := $2;
carlos@0
   793
END IF;
carlos@0
   794
RETURN date_trunc( p_transformation , $1) ;
carlos@0
   795
END;
antonio@1149
   796
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   797
/-- END
carlos@0
   798
carlos@0
   799
CREATE OR REPLACE FUNCTION trunc
carlos@0
   800
(
carlos@0
   801
timestamp
carlos@0
   802
)
carlos@0
   803
RETURNS DATE AS '
carlos@0
   804
BEGIN
carlos@0
   805
RETURN to_timestamp(to_char($1, dateFormat()), dateFormat());
carlos@0
   806
END;
antonio@1149
   807
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   808
/-- END
carlos@0
   809
carlos@0
   810
CREATE OR REPLACE FUNCTION trunc
carlos@0
   811
(
carlos@0
   812
timestamp with time zone
carlos@0
   813
)
carlos@0
   814
RETURNS DATE AS '
carlos@0
   815
BEGIN
carlos@0
   816
RETURN to_timestamp(to_char($1, dateFormat()), dateFormat());
carlos@0
   817
END;
antonio@1149
   818
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   819
/-- END
carlos@0
   820
carlos@0
   821
CREATE OR REPLACE FUNCTION instr(varchar, varchar)
asier@33738
   822
  RETURNS int4 
asier@33738
   823
  SET SEARCH_PATH FROM CURRENT
asier@33738
   824
  AS 
carlos@0
   825
'DECLARE
carlos@0
   826
    pos integer;
carlos@0
   827
BEGIN
carlos@0
   828
    pos:= instr($1, $2, 1);
carlos@0
   829
    RETURN pos;
carlos@0
   830
END;
antonio@1149
   831
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   832
/-- END
carlos@0
   833
carlos@0
   834
CREATE OR REPLACE FUNCTION instr(string varchar, string_to_search varchar, beg_index int4)
asier@33738
   835
  RETURNS int4 
asier@33738
   836
  SET SEARCH_PATH FROM CURRENT
asier@33738
   837
  AS 
carlos@0
   838
'DECLARE
carlos@0
   839
    pos integer NOT NULL DEFAULT 0;
carlos@0
   840
    temp_str varchar;
carlos@0
   841
    beg integer;
carlos@0
   842
    length integer;
carlos@0
   843
    ss_length integer;
carlos@0
   844
BEGIN
carlos@0
   845
    IF ((string IS NULL) OR (string_to_search IS NULL) OR (beg_index IS NULL)) THEN RETURN 0; END IF;
carlos@0
   846
    IF beg_index > 0 THEN
carlos@0
   847
      temp_str := substring(string FROM beg_index);
carlos@0
   848
      pos := position(string_to_search IN temp_str);
carlos@0
   849
      IF pos = 0 THEN
carlos@0
   850
        RETURN 0;
carlos@0
   851
      ELSE
carlos@0
   852
        RETURN pos + beg_index - 1;
carlos@0
   853
      END IF;
carlos@0
   854
    ELSE
carlos@0
   855
      ss_length := char_length(string_to_search);
carlos@0
   856
      length := char_length(string);
carlos@0
   857
      beg := length + beg_index - ss_length + 2;
carlos@0
   858
      WHILE beg > 0 LOOP
carlos@0
   859
        temp_str := substring(string FROM beg FOR ss_length);
carlos@0
   860
        pos := position(string_to_search IN temp_str);
carlos@0
   861
        IF pos > 0 THEN
carlos@0
   862
          RETURN beg;
carlos@0
   863
        END IF;
carlos@0
   864
        beg := beg - 1;
carlos@0
   865
      END LOOP;
carlos@0
   866
      RETURN 0;
carlos@0
   867
    END IF;
carlos@0
   868
END;
antonio@1149
   869
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   870
/-- END
carlos@0
   871
carlos@0
   872
CREATE OR REPLACE FUNCTION instr(string varchar, string_to_search varchar, beg_index int4, occur_index int4)
asier@33738
   873
  RETURNS int4 
asier@33738
   874
  SET SEARCH_PATH FROM CURRENT
asier@33738
   875
  AS 
carlos@0
   876
'DECLARE
carlos@0
   877
pos integer NOT NULL DEFAULT 0;
carlos@0
   878
occur_number integer NOT NULL DEFAULT 0;
carlos@0
   879
temp_str varchar;
carlos@0
   880
beg integer;
carlos@0
   881
length integer;
carlos@0
   882
ss_length integer; BEGIN
carlos@0
   883
    IF ((string IS NULL) OR (string_to_search IS NULL) OR (beg_index IS NULL) OR (occur_index IS NULL)) THEN RETURN 0; END IF;
carlos@0
   884
IF beg_index > 0 THEN
carlos@0
   885
    beg := beg_index;
carlos@0
   886
    temp_str := substring(string FROM beg_index);
carlos@0
   887
carlos@0
   888
    FOR i IN 1..occur_index LOOP
carlos@0
   889
        pos := position(string_to_search IN temp_str);
carlos@0
   890
         IF i = 1 THEN
carlos@0
   891
            beg := beg + pos - 1;
carlos@0
   892
        ELSE
carlos@0
   893
            beg := beg + pos;
carlos@0
   894
        END IF;
carlos@0
   895
         temp_str := substring(string FROM beg + 1);
carlos@0
   896
    END LOOP;          
carlos@0
   897
    IF pos = 0 THEN
carlos@0
   898
        RETURN 0;
carlos@0
   899
    ELSE
carlos@0
   900
        RETURN beg;
carlos@0
   901
    END IF;
carlos@0
   902
ELSE
carlos@0
   903
    ss_length := char_length(string_to_search);
carlos@0
   904
    length := char_length(string);
carlos@0
   905
    beg := length + beg_index - ss_length + 2;
carlos@0
   906
     WHILE beg > 0 LOOP
carlos@0
   907
        temp_str := substring(string FROM beg FOR ss_length);
carlos@0
   908
        pos := position(string_to_search IN temp_str);
carlos@0
   909
         IF pos > 0 THEN
carlos@0
   910
            occur_number := occur_number + 1;
carlos@0
   911
             IF occur_number = occur_index THEN
carlos@0
   912
                RETURN beg;
carlos@0
   913
            END IF;
carlos@0
   914
        END IF;
carlos@0
   915
         beg := beg - 1;
carlos@0
   916
    END LOOP;
carlos@0
   917
     RETURN 0;
carlos@0
   918
END IF; 
carlos@0
   919
END;
antonio@1149
   920
' LANGUAGE 'plpgsql'IMMUTABLE
carlos@0
   921
/-- END
carlos@0
   922
asier@33738
   923
create or replace function last_day(date) returns date 
asier@33738
   924
SET SEARCH_PATH FROM CURRENT
asier@33738
   925
as 'select
carlos@0
   926
cast(date_trunc(''month'', $1) + ''1 month''::interval as date) - 1'
carlos@0
   927
language sql
carlos@0
   928
/-- END
carlos@0
   929
asier@33738
   930
create or replace function last_day(timestamptz) returns date 
asier@33738
   931
SET SEARCH_PATH FROM CURRENT
asier@33738
   932
as 'select
carlos@0
   933
cast(date_trunc(''month'', cast($1 AS date)) + ''1 month''::interval as date) - 1'
carlos@0
   934
language sql
carlos@0
   935
/-- END
carlos@0
   936
carlos@0
   937
carlos@0
   938
CREATE OR REPLACE FUNCTION DUMP(varchar)
carlos@0
   939
  RETURNS varchar AS 
carlos@0
   940
'DECLARE
carlos@0
   941
BEGIN
carlos@0
   942
    RETURN $1;
carlos@0
   943
END;
carlos@0
   944
'
carlos@0
   945
  LANGUAGE 'plpgsql'
carlos@0
   946
/-- END
carlos@0
   947
carlos@0
   948
CREATE OR REPLACE FUNCTION DUMP(NUMERIC)
carlos@0
   949
  RETURNS NUMERIC AS 
carlos@0
   950
'DECLARE
carlos@0
   951
BEGIN
carlos@0
   952
    RETURN $1;
carlos@0
   953
END;
carlos@0
   954
'
carlos@0
   955
  LANGUAGE 'plpgsql'
carlos@0
   956
/-- END
carlos@0
   957
carlos@0
   958
CREATE OR REPLACE FUNCTION substract_days
carlos@0
   959
(
carlos@0
   960
timestamp with time zone,
carlos@0
   961
timestamp with time zone
carlos@0
   962
)
carlos@0
   963
RETURNS float AS '
carlos@0
   964
BEGIN
carlos@0
   965
RETURN extract(epoch from ($1 - $2)) / 86400.0::float;
carlos@0
   966
END;
carlos@0
   967
' LANGUAGE 'plpgsql'
carlos@0
   968
/-- END
carlos@0
   969
carlos@0
   970
CREATE OR REPLACE FUNCTION substract_days
carlos@0
   971
(
carlos@0
   972
timestamp,
carlos@0
   973
timestamp
carlos@0
   974
)
carlos@0
   975
RETURNS float AS '
carlos@0
   976
BEGIN
carlos@0
   977
RETURN extract(epoch from ($1 - $2)) / 86400.0::float;
carlos@0
   978
END;
antonio@1149
   979
' LANGUAGE 'plpgsql' IMMUTABLE
carlos@0
   980
/-- END
carlos@0
   981
carlos@0
   982
--DROP OPERATOR - (timestamp, timestamp)/-- END
carlos@0
   983
--CREATE OPERATOR - (
carlos@0
   984
--   LEFTARG = timestamp,
carlos@0
   985
--   RIGHTARG = timestamp,
carlos@0
   986
--   PROCEDURE = substract_days
carlos@0
   987
--)
carlos@0
   988
/-- END
carlos@0
   989
carlos@0
   990
--DROP OPERATOR - (timestamptz,timestamptz);
carlos@0
   991
--CREATE OPERATOR - (
carlos@0
   992
--  LEFTARG = timestamptz,
carlos@0
   993
--   RIGHTARG = timestamptz,
carlos@0
   994
--   PROCEDURE = substract_days
carlos@0
   995
--)
carlos@62
   996
--/--END
carlos@62
   997
carlos@62
   998
--DROP OPERATOR - (timestamptz, numeric)
carlos@62
   999
--/--END
carlos@62
  1000
carlos@62
  1001
SELECT * FROM drop_operator('-','timestamptz','numeric')
carlos@0
  1002
/-- END
carlos@0
  1003
carlos@0
  1004
CREATE OPERATOR -(
carlos@0
  1005
  PROCEDURE = substract_days,
carlos@0
  1006
  LEFTARG = timestamptz,
carlos@0
  1007
  RIGHTARG = numeric)
carlos@0
  1008
/-- END
carlos@0
  1009
naroa@30310
  1010
DROP function IF EXISTS is_Trigger_Enabled(text);
naroa@30310
  1011
carlos@62
  1012
-- Creating auxiliar functions for view dropping
carlos@62
  1013
CREATE or REPLACE function drop_view (view_name IN varchar) returns varchar as '
carlos@62
  1014
DECLARE
carlos@62
  1015
  cnt int4;
carlos@62
  1016
BEGIN
carlos@62
  1017
  SELECT into cnt count(*) from pg_views where upper(viewname) = upper(view_name::name);
carlos@62
  1018
  if cnt > 0 then
carlos@62
  1019
    execute ''DROP VIEW '' || view_name || '';'';
carlos@62
  1020
    return view_name || '' DROPPED'';
carlos@62
  1021
  end if;
carlos@62
  1022
  return view_name || '' does not exist'';
carlos@62
  1023
END;'
carlos@62
  1024
language 'plpgsql' 
carlos@0
  1025
/-- END
carlos@0
  1026
carlos@62
  1027
SELECT * FROM drop_view('DUAL')
carlos@62
  1028
/-- END
carlos@62
  1029
carlos@62
  1030
CREATE OR REPLACE VIEW DUAL AS SELECT 'X'::text AS dummy
carlos@62
  1031
/-- END
carlos@62
  1032
carlos@62
  1033
SELECT * FROM drop_view('USER_TABLES')
carlos@62
  1034
/-- END
carlos@0
  1035
carlos@0
  1036
CREATE OR REPLACE VIEW USER_TABLES
carlos@0
  1037
(TABLE_NAME, BLOCKS, DURATION, LAST_ANALYZED)
carlos@0
  1038
AS 
carlos@62
  1039
SELECT UPPER(TABLENAME), NULL::numeric, NULL::varchar, NULL::date 
carlos@0
  1040
FROM PG_TABLES 
carlos@0
  1041
WHERE SCHEMANAME = CURRENT_SCHEMA()
carlos@0
  1042
/-- END
carlos@0
  1043
carlos@62
  1044
--DROP VIEW USER_CONSTRAINTS
carlos@62
  1045
--/--END
carlos@62
  1046
SELECT * FROM drop_view('USER_CONSTRAINTS')
carlos@62
  1047
/-- END
carlos@0
  1048
carlos@0
  1049
CREATE OR REPLACE VIEW user_constraints AS 
adrian@236
  1050
 SELECT upper(pg_class.relname::text) AS table_name, upper(pg_constraint.conname::text) AS constraint_name, 
adrian@236
  1051
        CASE upper(pg_constraint.contype::text)
adrian@236
  1052
            WHEN 'F'::text THEN 'R'::text
adrian@236
  1053
            ELSE upper(pg_constraint.contype::text)
adrian@236
  1054
        END AS constraint_type, upper(pg_constraint.confdeltype::text) AS delete_rule, array_to_string(ARRAY( SELECT ('"'::text || pg_attribute.attname::text) || '"'::text AS attr_name
adrian@236
  1055
           FROM pg_attribute
adrian@236
  1056
          WHERE pg_attribute.attrelid = pg_constraint.conrelid AND (pg_attribute.attnum = ANY (pg_constraint.conkey))
adrian@236
  1057
          ORDER BY "position"(array_to_string(pg_constraint.conkey, '-'::text), pg_attribute.attnum::text)), ','::text) AS column_names, upper(fk_table.relname::text) AS fk_table, array_to_string(ARRAY( SELECT ('"'::text || pg_attribute.attname::text) || '"'::text AS attr_name
adrian@236
  1058
           FROM pg_attribute
adrian@236
  1059
          WHERE pg_attribute.attrelid = pg_constraint.confrelid AND (pg_attribute.attnum = ANY (pg_constraint.confkey))
adrian@236
  1060
          ORDER BY "position"(array_to_string(pg_constraint.confkey, '-'::text), pg_attribute.attnum::text)), ','::text) AS fk_column_names, pg_constraint.confmatchtype AS fk_matchtype, 
adrian@236
  1061
        CASE upper(pg_constraint.contype::text)
adrian@236
  1062
            WHEN 'P'::text THEN upper(pg_constraint.conname::text)
adrian@236
  1063
            WHEN 'U'::text THEN upper(pg_constraint.conname::text)
adrian@236
  1064
            ELSE ''::text
carlos@496
  1065
        END AS index_name, dba_getstandard_search_text(pg_constraint.consrc) AS search_condition, 'ENABLED'::text AS STATUS
adrian@236
  1066
   FROM pg_constraint
adrian@236
  1067
   JOIN pg_class ON pg_class.oid = pg_constraint.conrelid
adrian@236
  1068
   LEFT JOIN pg_class fk_table ON fk_table.oid = pg_constraint.confrelid
carlos@0
  1069
/-- END
carlos@0
  1070
carlos@62
  1071
--DROP VIEW USER_INDEXES
carlos@62
  1072
--/--END
carlos@62
  1073
carlos@62
  1074
SELECT * FROM drop_view('USER_INDEXES')
carlos@62
  1075
/-- END
carlos@0
  1076
carlos@0
  1077
CREATE OR REPLACE VIEW USER_INDEXES
carlos@0
  1078
(TABLE_NAME, INDEX_NAME, TABLESPACE_NAME, UNIQUENESS, INDEX_TYPE, TABLE_TYPE)
carlos@0
  1079
AS 
carlos@0
  1080
SELECT UPPER(PG_CLASS1.RELNAME), UPPER(PG_CLASS.RELNAME), UPPER(PG_NAMESPACE.NSPNAME), CASE PG_INDEX.indisunique WHEN true THEN 'UNIQUE' ELSE 'NONUNIQUE' END, 
carlos@0
  1081
TO_CHAR('NORMAL'), TO_CHAR('TABLE') 
carlos@0
  1082
FROM PG_INDEX, PG_CLASS, PG_CLASS PG_CLASS1, PG_NAMESPACE
carlos@0
  1083
WHERE PG_INDEX.indexrelid = PG_CLASS.OID
carlos@0
  1084
AND PG_INDEX.indrelid = PG_CLASS1.OID
carlos@0
  1085
AND PG_CLASS.RELNAMESPACE = PG_NAMESPACE.OID
carlos@0
  1086
AND PG_CLASS1.RELNAMESPACE = PG_NAMESPACE.OID
carlos@0
  1087
AND PG_NAMESPACE.NSPNAME = CURRENT_SCHEMA()
carlos@0
  1088
/-- END
carlos@0
  1089
carlos@62
  1090
--DROP VIEW USER_IND_COLUMNS
carlos@62
  1091
--/--END
carlos@62
  1092
carlos@62
  1093
SELECT * FROM drop_view('USER_IND_COLUMNS')
carlos@62
  1094
/-- END
carlos@0
  1095
adrian@236
  1096
CREATE OR REPLACE VIEW user_ind_columns AS
adrian@236
  1097
SELECT upper(pg_class1.relname::text) AS table_name, upper(pg_class.relname::text) AS index_name,
adrian@236
  1098
       upper(pg_attribute.attname::text) AS column_name,
adrian@236
  1099
       upper(pg_namespace.nspname::text) AS tablespace_name,
adrian@236
  1100
       (dba_getattnumpos(pg_index.indkey, pg_attribute.attnum)+1) AS column_position
adrian@236
  1101
   FROM pg_index, pg_class, pg_class pg_class1, pg_namespace, pg_attribute
adrian@236
  1102
  WHERE pg_index.indexrelid = pg_class.oid
adrian@236
  1103
    AND pg_index.indrelid = pg_class1.oid
adrian@236
  1104
    AND pg_attribute.attrelid = pg_index.indrelid
adrian@236
  1105
    AND pg_attribute.attnum = ANY (indkey)
adrian@236
  1106
    AND pg_class.relnamespace = pg_namespace.oid
adrian@236
  1107
    AND pg_class1.relnamespace = pg_namespace.oid
adrian@236
  1108
    AND pg_namespace.nspname = current_schema()
adrian@236
  1109
  ORDER BY table_name, index_name, column_position
carlos@0
  1110
/-- END
carlos@0
  1111
adrian@236
  1112
SELECT * FROM drop_view('USER_CONS_COLUMNS')
adrian@236
  1113
/-- END
adrian@236
  1114
adrian@236
  1115
CREATE OR REPLACE VIEW user_cons_columns AS 
adrian@236
  1116
 SELECT upper(pg_constraint.conname::text) AS constraint_name, upper(pg_class.relname::text) AS table_name, upper(pg_attribute.attname::text) AS column_name, dba_getattnumpos(pg_constraint.conkey::integer[], pg_attribute.attnum::integer) AS "position"
adrian@236
  1117
   FROM pg_constraint, pg_class, pg_attribute
adrian@236
  1118
  WHERE pg_constraint.conrelid = pg_class.oid AND pg_attribute.attrelid = pg_constraint.conrelid AND (pg_attribute.attnum = ANY (pg_constraint.conkey))
adrian@236
  1119
  ORDER BY pg_class.relname, pg_constraint.conname
adrian@236
  1120
/-- END
adrian@236
  1121
adrian@236
  1122
carlos@62
  1123
--DROP VIEW USER_TAB_COLUMNS
carlos@62
  1124
--/--END
carlos@62
  1125
carlos@62
  1126
SELECT * FROM drop_view('USER_TAB_COLUMNS')
carlos@62
  1127
/-- END
carlos@62
  1128
adrian@236
  1129
CREATE OR REPLACE VIEW user_tab_columns AS
asier@4317
  1130
 SELECT upper(pg_class.relname::text) AS table_name, upper(pg_attribute.attname::text) AS column_name, 
asier@4317
  1131
        (CASE WHEN upper(pg_type.typname::text)='NUMERIC' 
asier@4317
  1132
             THEN 'NUMBER'
asier@4318
  1133
             ELSE upper(pg_type.typname::text)
asier@4318
  1134
          END) AS data_type,
adrian@236
  1135
        CASE pg_type.typname
adrian@236
  1136
            WHEN 'varchar'::name THEN pg_attribute.atttypmod - 4
adrian@236
  1137
            WHEN 'bpchar'::name THEN pg_attribute.atttypmod - 4
adrian@236
  1138
            ELSE NULL::integer
adrian@236
  1139
        END AS char_col_decl_length,
adrian@236
  1140
adrian@236
  1141
        CASE pg_type.typname
adrian@236
  1142
            WHEN 'bytea'::name THEN 4000
adrian@236
  1143
            WHEN 'text'::name THEN 4000
adrian@266
  1144
            WHEN 'oid'::name THEN 4000
carlos@285
  1145
            ELSE CASE PG_ATTRIBUTE.ATTLEN 
carlos@285
  1146
                     WHEN -1 THEN PG_ATTRIBUTE.ATTTYPMOD-4 
carlos@285
  1147
                     ELSE PG_ATTRIBUTE.ATTLEN 
carlos@285
  1148
                 END
adrian@236
  1149
        END AS data_length,
adrian@236
  1150
adrian@236
  1151
        CASE pg_type.typname
adrian@236
  1152
            WHEN 'bytea'::name THEN 4000
adrian@236
  1153
            WHEN 'text'::name THEN 4000
adrian@266
  1154
            WHEN 'oid'::name THEN 4000
adrian@236
  1155
            ELSE 
adrian@236
  1156
                CASE atttypmod
adrian@236
  1157
                    WHEN -1 THEN 0
adrian@236
  1158
                    ELSE 10 
adrian@236
  1159
                END
adrian@236
  1160
        END AS data_precision,
miguel@15747
  1161
        CASE 
miguel@15747
  1162
            WHEN upper(pg_type.typname) = 'NUMERIC' and cols.numeric_scale is not null THEN cols.numeric_scale
miguel@15747
  1163
            ELSE 0
miguel@15747
  1164
        END AS data_scale,
adrian@236
  1165
        CASE pg_attribute.atthasdef
adrian@236
  1166
            WHEN true THEN ( SELECT pg_attrdef.adsrc
adrian@236
  1167
               FROM pg_attrdef
adrian@236
  1168
              WHERE pg_attrdef.adrelid = pg_class.oid AND pg_attrdef.adnum = pg_attribute.attnum)
adrian@236
  1169
            ELSE NULL::text
carlos@285
  1170
        END AS data_default, not pg_attribute.attnotnull AS nullable, pg_attribute.attnum AS column_id
miguel@15747
  1171
   FROM pg_class, pg_namespace, pg_attribute, pg_type, information_schema.columns cols
miguel@15747
  1172
  WHERE pg_attribute.attrelid = pg_class.oid AND pg_attribute.atttypid = pg_type.oid AND pg_class.relnamespace = pg_namespace.oid AND pg_namespace.nspname = current_schema() AND pg_attribute.attnum > 0 
miguel@15747
  1173
  AND upper(cols.table_name)=upper(pg_class.relname) AND upper(cols.column_name)=upper(pg_attribute.attname) AND cols.table_schema = current_schema()
carlos@0
  1174
/-- END
carlos@0
  1175
asier@709
  1176
SELECT * FROM drop_view('v$version')
asier@709
  1177
/-- END
asier@709
  1178
asier@709
  1179
CREATE OR REPLACE VIEW v$version
asier@709
  1180
AS 
asier@709
  1181
 SELECT setting as banner
asier@709
  1182
   FROM pg_settings
asier@709
  1183
  WHERE name = 'server_version';
asier@709
  1184
/-- END
asier@709
  1185
carlos@62
  1186
--DROP VIEW USER_TRIGGERS
carlos@62
  1187
--/--END
carlos@62
  1188
carlos@62
  1189
SELECT * FROM drop_view('USER_TRIGGERS')
carlos@62
  1190
/-- END
carlos@0
  1191
carlos@0
  1192
CREATE OR REPLACE VIEW USER_TRIGGERS
carlos@496
  1193
(TABLE_NAME, TABLESPACE_NAME, TRIGGER_NAME, STATUS)
carlos@0
  1194
AS 
javier@17996
  1195
SELECT UPPER(PG_CLASS.RELNAME), UPPER(PG_NAMESPACE.NSPNAME), PG_TRIGGER.TGNAME, CASE WHEN pg_trigger.tgenabled = 'D'::"char" THEN 'DISABLED'::text ELSE 'ENABLED'::text END AS status
carlos@0
  1196
FROM PG_TRIGGER, PG_CLASS, PG_NAMESPACE
carlos@0
  1197
WHERE PG_TRIGGER.tgrelid = PG_CLASS.OID
carlos@0
  1198
AND PG_CLASS.RELNAMESPACE = PG_NAMESPACE.OID
carlos@0
  1199
AND PG_NAMESPACE.NSPNAME = CURRENT_SCHEMA()
carlos@0
  1200
/-- END
carlos@0
  1201
carlos@0
  1202
carlos@62
  1203
-- DROP TEMPORARY FUNCTIONS
carlos@62
  1204
DROP FUNCTION exist_language(varchar)
carlos@62
  1205
/-- END
carlos@62
  1206
carlos@62
  1207
DROP FUNCTION insert_pg_language()
carlos@62
  1208
/-- END
carlos@62
  1209
carlos@62
  1210
DROP FUNCTION create_language(varchar)
carlos@62
  1211
/-- END
carlos@62
  1212
carlos@62
  1213
DROP FUNCTION type_oid(varchar)
carlos@62
  1214
/-- END
carlos@62
  1215
carlos@62
  1216
DROP FUNCTION drop_operator (varchar,varchar,varchar)
carlos@62
  1217
/-- END
carlos@62
  1218
carlos@62
  1219
DROP FUNCTION drop_view (varchar)
carlos@62
  1220
/-- END
carlos@62
  1221
juanpablo@1605
  1222
CREATE OR REPLACE FUNCTION get_uuid()
juanpablo@1605
  1223
  RETURNS varchar AS
juanpablo@1605
  1224
$BODY$ DECLARE
juanpablo@1605
  1225
/*************************************************************************
juanpablo@1605
  1226
* The contents of this file are subject to the Openbravo  Public  License
priya@9085
  1227
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
juanpablo@1605
  1228
* Version 1.1  with a permitted attribution clause; you may not  use this
juanpablo@1605
  1229
* file except in compliance with the License. You  may  obtain  a copy of
juanpablo@1605
  1230
* the License at http://www.openbravo.com/legal/license.html
juanpablo@1605
  1231
* Software distributed under the License  is  distributed  on  an "AS IS"
juanpablo@1605
  1232
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
juanpablo@1605
  1233
* License for the specific  language  governing  rights  and  limitations
juanpablo@1605
  1234
* under the License.
juanpablo@1605
  1235
* The Original Code is Openbravo ERP.
ggi@6702
  1236
* The Initial Developer of the Original Code is Openbravo SLU
ggi@6702
  1237
* All portions are Copyright (C) 2008-2009 Openbravo SLU
juanpablo@1605
  1238
* All Rights Reserved.
juanpablo@1605
  1239
* Contributor(s):  ______________________________________.
juanpablo@1605
  1240
************************************************************************/
asier@4296
  1241
var VARCHAR:=uuid_generate_v4();
juanpablo@1605
  1242
BEGIN
asier@4296
  1243
 WHILE var=uuid_generate_v4()::varchar LOOP
gorka@1800
  1244
END LOOP; 
asier@4296
  1245
  return replace(upper(uuid_generate_v4()::varchar),'-','');
juanpablo@1605
  1246
END;   $BODY$
juanpablo@1605
  1247
  LANGUAGE 'plpgsql' VOLATILE
juanpablo@1605
  1248
/-- END
juanpablo@1605
  1249
david@1911
  1250
CREATE OR REPLACE FUNCTION c_create_temporary_tables()
david@1911
  1251
  RETURNS varchar AS
david@1911
  1252
$BODY$ 
david@1911
  1253
BEGIN
david@2361
  1254
 -- create temporary tables. This function is used in  M_PRICELIST_CREATE() and M_PRODUCT_BOM_CHECK() functions.
david@2361
  1255
 -- This function has been created as a result of fix for bug 0005229: Temporary tables T_Selection and T_Selection2 have been ported from v2.22 to 2.35 improperly 
david@1911
  1256
 drop table if exists C_TEMP_SELECTION;
augusto@20111
  1257
 CREATE TEMPORARY TABLE C_TEMP_SELECTION
david@1911
  1258
 (
david@1911
  1259
  C_TEMP_SELECTION_ID  character varying(32) NOT NULL,
david@1911
  1260
  CONSTRAINT C_TEMP_SELECTION_key PRIMARY KEY (C_TEMP_SELECTION_ID)
david@1911
  1261
  )
david@1911
  1262
  ON COMMIT DELETE ROWS;
david@1911
  1263
david@1911
  1264
  drop table if exists C_TEMP_SELECTION2;
augusto@20111
  1265
  CREATE TEMPORARY TABLE C_TEMP_SELECTION2
david@1911
  1266
  (
david@1911
  1267
   QUERY_ID        character varying(32)             NOT NULL,
david@1911
  1268
   C_TEMP_SELECTION_ID  character varying(32)             NOT NULL,
david@1911
  1269
   CONSTRAINT C_TEMP_SELECTION2_key PRIMARY KEY (QUERY_ID, C_TEMP_SELECTION_ID)	
david@1911
  1270
   )
david@1911
  1271
   ON COMMIT PRESERVE ROWS;
david@1911
  1272
RETURN null;
david@1911
  1273
END;
david@1911
  1274
$BODY$
david@1911
  1275
  LANGUAGE 'plpgsql' VOLATILE
asier@2078
  1276
/-- END
asier@2084
  1277
antonio@2955
  1278
CREATE or replace FUNCTION AD_DB_MODIFIED(p_Update char) RETURNS CHAR AS
antonio@2955
  1279
$BODY$
antonio@2955
  1280
/*************************************************************************
antonio@2955
  1281
* The contents of this file are subject to the Openbravo  Public  License
priya@9085
  1282
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
antonio@2955
  1283
* Version 1.1  with a permitted attribution clause; you may not  use this
antonio@2955
  1284
* file except in compliance with the License. You  may  obtain  a copy of
antonio@2955
  1285
* the License at http://www.openbravo.com/legal/license.html
antonio@2955
  1286
* Software distributed under the License  is  distributed  on  an "AS IS"
antonio@2955
  1287
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
antonio@2955
  1288
* License for the specific  language  governing  rights  and  limitations
antonio@2955
  1289
* under the License.
antonio@2955
  1290
* The Original Code is Openbravo ERP.
ggi@6702
  1291
* The Initial Developer of the Original Code is Openbravo SLU
asier@34796
  1292
* All portions are Copyright (C) 2009-2018 Openbravo SLU
antonio@2955
  1293
* All Rights Reserved.
antonio@2955
  1294
* Contributor(s):  ______________________________________.
antonio@2955
  1295
************************************************************************/
antonio@2955
  1296
declare
antonio@2955
  1297
  c numeric;
antonio@2955
  1298
  v_md5 varchar(32);
antonio@2955
  1299
  i record;
antonio@2955
  1300
  j record;
antonio@2955
  1301
  aux varchar(32);
antonio@2955
  1302
  aux_char char[];
antonio@2955
  1303
  aux_text text[];
antonio@2955
  1304
  v_Modified char(1);
antonio@2955
  1305
begin
antonio@2955
  1306
  v_md5 = ''; 
antonio@2955
  1307
  --Checksum for PL functions
antonio@2955
  1308
  for i in (
antonio@11089
  1309
	select upper(proname) as proname, p.proname as realname, pronargs, oidvectortypes(p.proargtypes) as proargtypes
antonio@2955
  1310
	from pg_proc p, pg_namespace n 
antonio@2955
  1311
         where  pronamespace = n.oid   and n.nspname=current_schema() 
antonio@2955
  1312
         and p.oid not in (select tgfoid   from pg_trigger) 
asier@5075
  1313
         and p.proname not in ('temp_findinarray', 'ad_db_modified', 'dateformat')
asier@34872
  1314
         and p.probin is null
antonio@5660
  1315
         order by 1,2,3,4) loop 
asier@5074
  1316
      --note that for overloaded functions more than one line will be obtained
asier@5074
  1317
asier@5074
  1318
     
antonio@2955
  1319
     --name
antonio@2955
  1320
     v_md5 := md5(v_md5||i.proname);
asier@2084
  1321
antonio@2955
  1322
     --body
antonio@2955
  1323
     select md5(p.prosrc) 
antonio@2955
  1324
       into aux
antonio@2955
  1325
       from pg_proc p
stefan@5414
  1326
      where p.proname = i.realname
asier@5074
  1327
        and p.pronargs = i.pronargs
antonio@11089
  1328
        and oidvectortypes(p.proargtypes) = i.proargtypes;
asier@5074
  1329
        
antonio@2955
  1330
      v_md5 := md5(v_md5||aux);
asier@2084
  1331
antonio@2955
  1332
     --parameters
antonio@2955
  1333
        
antonio@2955
  1334
       SELECT    pg_proc.proargmodes,
antonio@2955
  1335
                 pg_proc.proargnames
antonio@2955
  1336
            into aux_char, aux_text
antonio@2955
  1337
	    FROM pg_catalog.pg_proc         JOIN pg_catalog.pg_namespace
antonio@2955
  1338
		 ON (pg_proc.pronamespace = pg_namespace.oid)
antonio@2955
  1339
	   WHERE pg_proc.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype
antonio@2955
  1340
	     AND (pg_proc.proargtypes[0] IS NULL
antonio@2955
  1341
	      OR pg_proc.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype)
antonio@2955
  1342
	     AND pg_catalog.pg_function_is_visible(pg_proc.oid)
stefan@5414
  1343
	     AND pg_proc.proname = i.realname
asier@5074
  1344
             and pg_proc.pronargs = i.pronargs
antonio@11089
  1345
             and oidvectortypes(pg_proc.proargtypes) = i.proargtypes
antonio@2955
  1346
	     and (pg_proc.proargmodes is not null
antonio@2955
  1347
	     or pg_proc.proargnames is not null)
antonio@2955
  1348
		 ORDER BY 1,2;
antonio@2955
  1349
antonio@2955
  1350
         c := array_lower(aux_char, 1);
antonio@2955
  1351
antonio@2955
  1352
        while (c <= array_upper(aux_char, 1)) loop
antonio@2955
  1353
          v_md5 := md5(v_md5||coalesce(aux_char[c],'.')||coalesce(aux_text[c],'.'));
antonio@2955
  1354
          c := c +1;
antonio@2955
  1355
        end loop;
antonio@2955
  1356
  end loop;
antonio@2955
  1357
antonio@2955
  1358
  --triggers
antonio@2955
  1359
  for i in (
antonio@2955
  1360
          SELECT md5(upper(trg.tgname)||upper(tbl.relname)||( 
antonio@2955
  1361
          CASE trg.tgtype & cast(3 as int2) 
antonio@2955
  1362
          WHEN 0 THEN 'AFTER EACH STATEMENT' 
antonio@2955
  1363
          WHEN 1 THEN 'AFTER EACH ROW' 
antonio@2955
  1364
          WHEN 2 THEN 'BEFORE EACH STATEMENT' 
antonio@2955
  1365
          WHEN 3 THEN 'BEFORE EACH ROW' END) || ( 
antonio@2955
  1366
          CASE trg.tgtype & cast(28 as int2) WHEN 16 THEN 'UPDATE' 
antonio@2955
  1367
          WHEN  8 THEN 'DELETE' 
antonio@2955
  1368
          WHEN  4 THEN 'INSERT' 
antonio@2955
  1369
          WHEN 20 THEN 'INSERT, UPDATE' 
antonio@2955
  1370
          WHEN 28 THEN 'INSERT, UPDATE, DELETE' 
antonio@2955
  1371
          WHEN 24 THEN 'UPDATE, DELETE' 
antonio@2955
  1372
          WHEN 12 THEN 'INSERT, DELETE' 
antonio@2955
  1373
          END)||p.prosrc) AS trg_md5 
antonio@2955
  1374
          FROM pg_trigger trg, pg_class tbl, pg_proc p 
asier@6294
  1375
          WHERE trg.tgrelid = tbl.oid 
asier@6294
  1376
            AND trg.tgfoid = p.oid 
asier@6294
  1377
            AND tbl.relname !~ '^pg_' 
asier@6294
  1378
            AND trg.tgname !~ '^RI'
asier@6294
  1379
            AND UPPER(trg.tgname) NOT LIKE 'AU_%'
antonio@2955
  1380
          order by trg.tgname) loop
antonio@2955
  1381
    v_md5 := md5(v_md5||i.trg_md5);
antonio@2955
  1382
  end loop;
antonio@2955
  1383
antonio@2955
  1384
  --tables
stefan@5414
  1385
  for i in (SELECT UPPER(TABLENAME) as tablename, t.tablename as realname
antonio@2955
  1386
              FROM PG_TABLES t
antonio@2955
  1387
              WHERE SCHEMANAME = CURRENT_SCHEMA()
antonio@2955
  1388
             ORDER BY 1) loop
antonio@2955
  1389
    v_md5 := md5(v_md5||i.tablename);
antonio@2955
  1390
    
antonio@2955
  1391
    --pk
antonio@2955
  1392
    for j in (SELECT md5(upper(PG_CONSTRAINT.CONNAME)||upper(pg_attribute.attname::text)) as pk
antonio@2955
  1393
              FROM  pg_attribute, pg_constraint JOIN PG_CLASS ON PG_CLASS.OID = PG_CONSTRAINT.CONRELID 
antonio@2955
  1394
              WHERE pg_constraint.conrelid = pg_class.oid AND pg_attribute.attrelid = pg_constraint.conrelid AND (pg_attribute.attnum = ANY (pg_constraint.conkey))
antonio@2955
  1395
              and PG_CONSTRAINT.CONTYPE::TEXT = 'p' 
stefan@5414
  1396
	      AND PG_CLASS.RELNAME =  i.realname
antonio@2955
  1397
              ORDER BY PG_CONSTRAINT.CONNAME, pg_attribute.attnum::integer) loop
antonio@2955
  1398
        v_md5 := md5(v_md5||j.pk);
antonio@2955
  1399
     end loop;
antonio@2955
  1400
antonio@2955
  1401
     --columns
antonio@2955
  1402
     for j in (SELECT MD5(UPPER(PG_ATTRIBUTE.ATTNAME::TEXT)|| COALESCE(UPPER(PG_TYPE.TYPNAME::TEXT),'.')||
antonio@2955
  1403
              COALESCE(TO_CHAR(CASE PG_TYPE.TYPNAME
antonio@2955
  1404
                  WHEN 'varchar'::name THEN pg_attribute.atttypmod - 4
antonio@2955
  1405
                  WHEN 'bpchar'::name THEN pg_attribute.atttypmod - 4
antonio@2955
  1406
                  ELSE NULL::integer
antonio@2955
  1407
              END),'.')||
antonio@2955
  1408
              COALESCE(TO_CHAR(CASE PG_TYPE.TYPNAME
antonio@5073
  1409
                  WHEN 'bytea'::name  THEN 4000
antonio@2955
  1410
                  WHEN 'text'::name THEN 4000
antonio@2955
  1411
                  WHEN 'oid'::name THEN 4000
antonio@2955
  1412
                  ELSE CASE PG_ATTRIBUTE.ATTLEN 
antonio@2955
  1413
                           WHEN -1 THEN PG_ATTRIBUTE.ATTTYPMOD - 4 
antonio@2955
  1414
                           ELSE PG_ATTRIBUTE.ATTLEN 
antonio@2955
  1415
                       END
antonio@2955
  1416
              END),'.') ||
antonio@2955
  1417
              COALESCE(TO_CHAR(
antonio@2955
  1418
              CASE pg_type.typname
antonio@2955
  1419
                  WHEN 'bytea'::name THEN 4000
antonio@2955
  1420
                  WHEN 'text'::name THEN 4000
antonio@2955
  1421
                  WHEN 'oid'::name THEN 4000
antonio@2955
  1422
                  ELSE
antonio@2955
  1423
                      CASE atttypmod
antonio@2955
  1424
                          WHEN -1 THEN 0
antonio@2955
  1425
                          ELSE 10
antonio@2955
  1426
                      END
antonio@2955
  1427
              END),'.')|| (not pg_attribute.attnotnull)::TEXT||
antonio@2955
  1428
              COALESCE(
antonio@2955
  1429
              (CASE pg_attribute.atthasdef
antonio@2955
  1430
                  WHEN true THEN ( SELECT pg_attrdef.adsrc FROM pg_attrdef WHERE pg_attrdef.adrelid = pg_class.oid AND pg_attrdef.adnum = pg_attribute.attnum)
antonio@2955
  1431
                  ELSE NULL::text
antonio@2955
  1432
              END),'.')) as cl
antonio@2955
  1433
              FROM pg_class, pg_namespace, pg_attribute, pg_type
antonio@2955
  1434
              WHERE pg_attribute.attrelid = pg_class.oid AND pg_attribute.atttypid = pg_type.oid AND pg_class.relnamespace = pg_namespace.oid AND pg_namespace.nspname = current_schema() AND pg_attribute.attnum > 0 
stefan@5414
  1435
              AND pg_class.relname = i.realname
antonio@2955
  1436
              ORDER BY pg_attribute.attnum) loop
antonio@2955
  1437
      v_md5 := md5(v_md5||j.cl);
antonio@2955
  1438
    end loop;
antonio@2955
  1439
antonio@2955
  1440
    --fk
antonio@2955
  1441
    for j in (SELECT md5(upper(pc.conname::text)|| upper(fk_table.relname::text)|| upper(pc.confdeltype::text)||  upper(pa1.attname)|| upper(pa2.attname)) as ck
antonio@2955
  1442
              FROM pg_class pc1, pg_attribute pa1, pg_class pc2, pg_attribute pa2, pg_constraint pc JOIN pg_class ON pg_class.oid = pc.conrelid LEFT JOIN pg_class fk_table ON fk_table.oid = pc.confrelid
stefan@5414
  1443
              WHERE pc.contype::text = 'f' and pg_class.relname = i.realname
antonio@2955
  1444
              AND  pc.conrelid= pc1.oid and upper(pc.conname) = upper(pc.conname) 
antonio@2955
  1445
              and pa1.attrelid = pc1.oid and pa1.attnum = ANY(pc.conkey)
antonio@2955
  1446
              and pc.confrelid = pc2.oid and pa2.attrelid = pc2.oid and pa2.attnum = ANY(pc.confkey)
antonio@2955
  1447
              ORDER BY upper(pc.conname::text), upper(pa1.attname), upper(pa2.attname)) loop
antonio@2955
  1448
      v_md5 := md5(v_md5||j.ck);
antonio@2955
  1449
    end loop;
antonio@2955
  1450
    
antonio@2955
  1451
antonio@2955
  1452
    --indexes
antonio@2955
  1453
    for j in (
antonio@2955
  1454
             SELECT md5(upper(pg_attribute.attname::text)||UPPER(PG_CLASS.RELNAME)||(CASE PG_INDEX.indisunique WHEN true THEN 'UNIQUE' ELSE 'NONUNIQUE' END)) as ck
antonio@2955
  1455
              FROM PG_INDEX, PG_CLASS, PG_CLASS PG_CLASS1, PG_NAMESPACE, pg_attribute
antonio@2955
  1456
              WHERE PG_INDEX.indexrelid = PG_CLASS.OID
antonio@2955
  1457
              AND PG_INDEX.indrelid = PG_CLASS1.OID
antonio@2955
  1458
              AND PG_CLASS.RELNAMESPACE = PG_NAMESPACE.OID
antonio@2955
  1459
              AND PG_CLASS1.RELNAMESPACE = PG_NAMESPACE.OID
antonio@2955
  1460
              AND PG_NAMESPACE.NSPNAME = CURRENT_SCHEMA()
antonio@2955
  1461
              AND PG_INDEX.INDISPRIMARY ='f'
antonio@2955
  1462
               AND pg_attribute.attrelid = pg_index.indrelid
antonio@2955
  1463
          AND pg_attribute.attnum = ANY (indkey)
stefan@5414
  1464
              AND PG_CLASS1.RELNAME = i.realname
antonio@2955
  1465
              ORDER BY UPPER(PG_CLASS.RELNAME), upper(pg_attribute.attname::text)) loop
antonio@2955
  1466
      v_md5 := md5(v_md5||j.ck);
antonio@2955
  1467
    end loop;
antonio@2955
  1468
    
antonio@2955
  1469
  end loop;
antonio@2955
  1470
antonio@2955
  1471
  select db_checksum
antonio@2955
  1472
    into aux
antonio@2955
  1473
    from ad_system_info;
antonio@2955
  1474
antonio@5076
  1475
  if ((aux is null) or (aux = v_md5)) then
antonio@2955
  1476
    v_Modified = 'N';
antonio@2955
  1477
  else
antonio@2955
  1478
    v_Modified = 'Y';
antonio@2955
  1479
  end if;
antonio@2955
  1480
  
antonio@2955
  1481
  if p_Update = 'Y' then
antonio@2955
  1482
    update ad_system_info
antonio@2955
  1483
       set LAST_DBUPDATE = NOW(),
antonio@2955
  1484
           DB_CHECKSUM = v_md5;
antonio@2955
  1485
  end if;
antonio@2955
  1486
  return v_Modified;
antonio@2955
  1487
EXCEPTION 
antonio@2955
  1488
     WHEN OTHERS THEN
antonio@2955
  1489
       RETURN 'N';
antonio@2955
  1490
end;$BODY$
antonio@2955
  1491
LANGUAGE 'plpgsql' VOLATILE;
antonio@2955
  1492
antonio@2955
  1493
/-- END
antonio@2955
  1494
david@4502
  1495
CREATE OR REPLACE FUNCTION drop_acctschema_default()
david@4502
  1496
RETURNS integer AS ' 
david@4502
  1497
BEGIN
david@4502
  1498
UPDATE C_ACCTSCHEMA_DEFAULT SET CB_CASHTRANSFER_ACCT=CB_ASSET_ACCT WHERE CB_CASHTRANSFER_ACCT IS NULL;
david@4502
  1499
RETURN 0;
david@4502
  1500
EXCEPTION
david@4502
  1501
  WHEN OTHERS THEN
david@4502
  1502
    RETURN NULL;
david@4502
  1503
END;
david@4502
  1504
' LANGUAGE 'plpgsql' VOLATILE
david@4500
  1505
/-- END
david@4502
  1506
david@4502
  1507
SELECT * FROM drop_acctschema_default()
david@4502
  1508
/-- END
david@4502
  1509
david@4502
  1510
DROP FUNCTION drop_acctschema_default ()
david@4502
  1511
/-- END
asier@16870
  1512
 
asier@16870
  1513
CREATE OR REPLACE FUNCTION AD_GET_RDBMS()
asier@16870
  1514
  RETURNS varchar AS
asier@16870
  1515
$BODY$ DECLARE
asier@16870
  1516
/*************************************************************************
asier@16870
  1517
* The contents of this file are subject to the Openbravo  Public  License
asier@16870
  1518
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
asier@16870
  1519
* Version 1.1  with a permitted attribution clause; you may not  use this
asier@16870
  1520
* file except in compliance with the License. You  may  obtain  a copy of
asier@16870
  1521
* the License at http://www.openbravo.com/legal/license.html
asier@16870
  1522
* Software distributed under the License  is  distributed  on  an "AS IS"
asier@16870
  1523
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
asier@16870
  1524
* License for the specific  language  governing  rights  and  limitations
asier@16870
  1525
* under the License.
asier@16870
  1526
* The Original Code is Openbravo ERP.
asier@16870
  1527
* The Initial Developer of the Original Code is Openbravo SLU
asier@16870
  1528
* All portions are Copyright (C) 2009-2012 Openbravo SLU
asier@16870
  1529
* All Rights Reserved.
asier@16870
  1530
* Contributor(s):  ______________________________________.
asier@16870
  1531
************************************************************************/
asier@16870
  1532
BEGIN
asier@16870
  1533
  return 'POSTGRE';
asier@16870
  1534
END;   $BODY$
asier@16870
  1535
  LANGUAGE 'plpgsql' IMMUTABLE
asier@16870
  1536
/-- END
david@4502
  1537
david@19587
  1538
david@19587
  1539
CREATE OR REPLACE VIEW AD_INTEGER AS
david@19587
  1540
SELECT a.value::numeric AS value
david@19587
  1541
   FROM generate_series(1, 1024) a(value);
david@19587
  1542
/-- END
david@19587
  1543
ioritz@20676
  1544
CREATE OR REPLACE FUNCTION add_hms(p_date timestamp without time zone, p_hours numeric, p_minutes numeric, p_seconds numeric)
ioritz@20676
  1545
  RETURNS timestamp without time zone AS
ioritz@20676
  1546
$BODY$ DECLARE 
ioritz@20676
  1547
/*************************************************************************
ioritz@20676
  1548
* The contents of this file are subject to the Openbravo  Public  License
ioritz@20676
  1549
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
ioritz@20676
  1550
* Version 1.1  with a permitted attribution clause; you may not  use this
ioritz@20676
  1551
* file except in compliance with the License. You  may  obtain  a copy of
ioritz@20676
  1552
* the License at http://www.openbravo.com/legal/license.html
ioritz@20676
  1553
* Software distributed under the License  is  distributed  on  an "AS IS"
ioritz@20676
  1554
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
ioritz@20676
  1555
* License for the specific  language  governing  rights  and  limitations
ioritz@20676
  1556
* under the License.
ioritz@20676
  1557
* The Original Code is Openbravo ERP.
ioritz@20676
  1558
* The Initial Developer of the Original Code is Openbravo SLU
ioritz@20676
  1559
* All portions are Copyright (C) 2013 Openbravo SLU
ioritz@20676
  1560
* All Rights Reserved.
ioritz@20676
  1561
* Contributor(s):  ______________________________________.
ioritz@20676
  1562
************************************************************************/
ioritz@20676
  1563
BEGIN
ioritz@20676
  1564
  RETURN p_date + p_hours * interval '1 hour' + p_minutes * interval '1 minute' + p_seconds * interval '1 second';
ioritz@20676
  1565
END ; $BODY$
ioritz@20697
  1566
  LANGUAGE plpgsql IMMUTABLE
ioritz@20676
  1567
/-- END
carlos@31152
  1568
victor@31690
  1569
CREATE OR REPLACE FUNCTION obequals(
victor@31690
  1570
    p_number_a numeric,
victor@31690
  1571
    p_number_b numeric)
victor@31690
  1572
  RETURNS char AS
victor@31690
  1573
$BODY$ DECLARE
victor@31690
  1574
/*************************************************************************
victor@31690
  1575
* The contents of this file are subject to the Openbravo  Public  License
victor@31690
  1576
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
victor@31690
  1577
* Version 1.1  with a permitted attribution clause; you may not  use this
victor@31690
  1578
* file except in compliance with the License. You  may  obtain  a copy of
victor@31690
  1579
* the License at http://www.openbravo.com/legal/license.html
victor@31690
  1580
* Software distributed under the License  is  distributed  on  an "AS IS"
victor@31690
  1581
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
victor@31690
  1582
* License for the specific  language  governing  rights  and  limitations
victor@31690
  1583
* under the License.
victor@31690
  1584
* The Original Code is Openbravo ERP.
victor@31690
  1585
* The Initial Developer of the Original Code is Openbravo SLU
victor@31690
  1586
* All portions are Copyright (C) 2017 Openbravo SLU
victor@31690
  1587
* All Rights Reserved.
victor@31690
  1588
* Contributor(s):  ______________________________________.
victor@31690
  1589
************************************************************************/
victor@31690
  1590
/**
victor@31690
  1591
* Returns 'Y' when both numbers are equals, else returns 'N'
victor@31690
  1592
* This function is used as index in FIN_Payment table
victor@31690
  1593
**/
victor@31690
  1594
 v_dif NUMERIC;
victor@31690
  1595
BEGIN
victor@31690
  1596
    v_dif := coalesce(p_number_a, 0) - coalesce(p_number_b, 0);
victor@31690
  1597
    IF (v_dif = 0) THEN
victor@31690
  1598
     return 'Y';
victor@31690
  1599
    ELSE
victor@31690
  1600
     return 'N';
victor@31690
  1601
    END IF;
victor@31690
  1602
END; $BODY$
victor@31690
  1603
  LANGUAGE plpgsql IMMUTABLE
victor@31690
  1604
/-- END
asier@34076
  1605
 
asier@34080
  1606
CREATE OR REPLACE FUNCTION AD_IsTriggerEnabled()
asier@34076
  1607
  RETURNS character AS
asier@34076
  1608
$BODY$ DECLARE 
asier@34076
  1609
/*************************************************************************
asier@34076
  1610
* The contents of this file are subject to the Openbravo  Public  License
asier@34076
  1611
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
asier@34076
  1612
* Version 1.1  with a permitted attribution clause; you may not  use this
asier@34076
  1613
* file except in compliance with the License. You  may  obtain  a copy of
asier@34076
  1614
* the License at http://www.openbravo.com/legal/license.html
asier@34076
  1615
* Software distributed under the License  is  distributed  on  an "AS IS"
asier@34076
  1616
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
asier@34076
  1617
* License for the specific  language  governing  rights  and  limitations
asier@34076
  1618
* under the License.
asier@34076
  1619
* The Original Code is Openbravo ERP.
asier@34076
  1620
* The Initial Developer of the Original Code is Openbravo SLU
asier@34076
  1621
* All portions are Copyright (C) 2008-2018 Openbravo SLU
asier@34076
  1622
* All Rights Reserved.
asier@34076
  1623
* Contributor(s):  ______________________________________.
asier@34076
  1624
************************************************************************/
asier@34076
  1625
BEGIN
asier@34076
  1626
  if current_setting('my.triggers_disabled') = 'Y' then
asier@34076
  1627
    return 'N';
asier@34076
  1628
  end if;
asier@34076
  1629
  return 'Y';
asier@34076
  1630
EXCEPTION
asier@34076
  1631
WHEN OTHERS THEN
asier@34076
  1632
  RETURN 'Y';
asier@34076
  1633
END ; $BODY$
asier@34076
  1634
  LANGUAGE plpgsql VOLATILE
asier@34076
  1635
/-- END
asier@34076
  1636
 
asier@34080
  1637
CREATE OR REPLACE FUNCTION AD_Disable_Triggers()
asier@34080
  1638
  RETURNS void AS
asier@34080
  1639
$BODY$ DECLARE 
asier@34080
  1640
/*************************************************************************
asier@34080
  1641
* The contents of this file are subject to the Openbravo  Public  License
asier@34080
  1642
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
asier@34080
  1643
* Version 1.1  with a permitted attribution clause; you may not  use this
asier@34080
  1644
* file except in compliance with the License. You  may  obtain  a copy of
asier@34080
  1645
* the License at http://www.openbravo.com/legal/license.html
asier@34080
  1646
* Software distributed under the License  is  distributed  on  an "AS IS"
asier@34080
  1647
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
asier@34080
  1648
* License for the specific  language  governing  rights  and  limitations
asier@34080
  1649
* under the License.
asier@34080
  1650
* The Original Code is Openbravo ERP.
asier@34080
  1651
* The Initial Developer of the Original Code is Openbravo SLU
asier@34080
  1652
* All portions are Copyright (C) 2018 Openbravo SLU
asier@34080
  1653
* All Rights Reserved.
asier@34080
  1654
* Contributor(s):  ______________________________________.
asier@34080
  1655
************************************************************************/
asier@34080
  1656
BEGIN
asier@34084
  1657
  perform set_config('my.triggers_disabled', 'Y', true);
asier@34080
  1658
END ; $BODY$
asier@34080
  1659
  LANGUAGE plpgsql VOLATILE
asier@34080
  1660
/-- END
asier@34080
  1661
asier@34080
  1662
CREATE OR REPLACE FUNCTION AD_Enable_Triggers()
asier@34080
  1663
  RETURNS void AS
asier@34080
  1664
$BODY$ DECLARE 
asier@34080
  1665
/*************************************************************************
asier@34080
  1666
* The contents of this file are subject to the Openbravo  Public  License
asier@34080
  1667
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
asier@34080
  1668
* Version 1.1  with a permitted attribution clause; you may not  use this
asier@34080
  1669
* file except in compliance with the License. You  may  obtain  a copy of
asier@34080
  1670
* the License at http://www.openbravo.com/legal/license.html
asier@34080
  1671
* Software distributed under the License  is  distributed  on  an "AS IS"
asier@34080
  1672
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
asier@34080
  1673
* License for the specific  language  governing  rights  and  limitations
asier@34080
  1674
* under the License.
asier@34080
  1675
* The Original Code is Openbravo ERP.
asier@34080
  1676
* The Initial Developer of the Original Code is Openbravo SLU
asier@34080
  1677
* All portions are Copyright (C) 2018 Openbravo SLU
asier@34080
  1678
* All Rights Reserved.
asier@34080
  1679
* Contributor(s):  ______________________________________.
asier@34080
  1680
************************************************************************/
asier@34080
  1681
BEGIN
asier@34084
  1682
  perform set_config('my.triggers_disabled', 'N', true);
asier@34080
  1683
END ; $BODY$
asier@34080
  1684
  LANGUAGE plpgsql VOLATILE
asier@34080
  1685
/-- END
asier@34080
  1686