forked from orafce/orafce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathothers.c
911 lines (734 loc) · 18.9 KB
/
others.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
#include "postgres.h"
#include <stdlib.h>
#include <locale.h>
#if PG_VERSION_NUM < 160000
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#if PG_VERSION_NUM >= 120000
#include "access/table.h"
#endif
#endif
#include "catalog/indexing.h"
#include "catalog/pg_extension.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/extension.h"
#include "fmgr.h"
#include "lib/stringinfo.h"
#include "nodes/nodeFuncs.h"
#include "nodes/pg_list.h"
#include "nodes/primnodes.h"
#include "parser/parse_expr.h"
#include "parser/parse_oper.h"
#include "storage/lock.h"
#include "storage/proc.h"
#include "utils/array.h"
#include "utils/arrayaccess.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
#include "utils/uuid.h"
#include "orafce.h"
#include "builtins.h"
/*
* Source code for nlssort is taken from postgresql-nls-string
* package by Jan Pazdziora
*/
static char *lc_collate_cache = NULL;
static size_t multiplication = 1;
static text *def_locale = NULL;
char *orafce_sys_guid_source;
static Oid uuid_generate_func_oid = InvalidOid;
static FmgrInfo uuid_generate_func_finfo;
/* The oid of function should be valid in oene transaction */
static LocalTransactionId uuid_generate_func_lxid = InvalidLocalTransactionId;
static char uuid_generate_func_name[30] = "";
static Datum ora_greatest_least(FunctionCallInfo fcinfo, bool greater);
#if PG_VERSION_NUM >= 170000
#define CURRENT_LXID (MyProc->vxid.lxid)
#else
#define CURRENT_LXID (MyProc->lxid)
#endif
#if PG_VERSION_NUM < 160000
static Oid
get_extension_schema(Oid ext_oid)
{
Oid result;
Relation rel;
SysScanDesc scandesc;
HeapTuple tuple;
ScanKeyData entry[1];
#if PG_VERSION_NUM >= 120000
rel = table_open(ExtensionRelationId, AccessShareLock);
ScanKeyInit(&entry[0],
Anum_pg_extension_oid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(ext_oid));
#else
rel = heap_open(ExtensionRelationId, AccessShareLock);
ScanKeyInit(&entry[0],
ObjectIdAttributeNumber,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(ext_oid));
#endif
scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
NULL, 1, entry);
tuple = systable_getnext(scandesc);
/* We assume that there can be at most one matching tuple */
if (HeapTupleIsValid(tuple))
result = ((Form_pg_extension) GETSTRUCT(tuple))->extnamespace;
else
result = InvalidOid;
systable_endscan(scandesc);
#if PG_VERSION_NUM >= 120000
table_close(rel, AccessShareLock);
#else
heap_close(rel, AccessShareLock);
#endif
return result;
}
#endif
static Oid
get_uuid_generate_func_oid(bool *reset_fmgr)
{
Oid result = InvalidOid;
if (uuid_generate_func_lxid != CURRENT_LXID ||
uuid_generate_func_oid == InvalidOid ||
strcmp(orafce_sys_guid_source, uuid_generate_func_name) != 0)
{
if (strcmp(orafce_sys_guid_source, "gen_random_uuid") == 0)
{
/* generated uuid can have not nice features, but uses buildin functionality */
result = fmgr_internal_function("gen_random_uuid");
}
else
{
Oid uuid_ossp_oid = InvalidOid;
Oid uuid_ossp_namespace_oid = InvalidOid;
CatCList *catlist;
int i;
uuid_ossp_oid = get_extension_oid("uuid-ossp", true);
if (!OidIsValid(uuid_ossp_oid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("extension \"uuid-ossp\" is not installed"),
errhint("the extension \"uuid-ossp\" should be installed before using \"sys_guid\" function")));
uuid_ossp_namespace_oid = get_extension_schema(uuid_ossp_oid);
Assert(OidIsValid(uuid_ossp_namespace_oid));
/* Search syscache by name only */
catlist = SearchSysCacheList1(PROCNAMEARGSNSP,
CStringGetDatum(orafce_sys_guid_source));
for (i = 0; i < catlist->n_members; i++)
{
HeapTuple proctup = &catlist->members[i]->tuple;
Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
/*
* Consider only procs in specified namespace,
* with zero arguments and uuid type as result
*/
if (procform->pronamespace != uuid_ossp_namespace_oid ||
procform->pronargs != 0 || procform->prorettype != UUIDOID)
continue;
#if PG_VERSION_NUM >= 120000
result = procform->oid;
#else
result = HeapTupleGetOid(proctup);
#endif
break;
}
ReleaseSysCacheList(catlist);
}
/* should be available if extension uuid-ossp is installed */
if (!OidIsValid(result))
elog(ERROR, "function \"%s\" doesn't exist", orafce_sys_guid_source);
uuid_generate_func_lxid = CURRENT_LXID;
uuid_generate_func_oid = result;
strcpy(uuid_generate_func_name, orafce_sys_guid_source);
*reset_fmgr = true;
}
else
*reset_fmgr = false;
Assert(OidIsValid(uuid_generate_func_oid));
return uuid_generate_func_oid;
}
PG_FUNCTION_INFO_V1(ora_lnnvl);
Datum
ora_lnnvl(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(!PG_GETARG_BOOL(0));
}
PG_FUNCTION_INFO_V1(ora_concat);
Datum
ora_concat(PG_FUNCTION_ARGS)
{
text *t1;
text *t2;
int l1;
int l2;
text *result;
if (PG_ARGISNULL(0) && PG_ARGISNULL(1))
PG_RETURN_NULL();
if (PG_ARGISNULL(0))
PG_RETURN_DATUM(PG_GETARG_DATUM(1));
if (PG_ARGISNULL(1))
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
t1 = PG_GETARG_TEXT_PP(0);
t2 = PG_GETARG_TEXT_PP(1);
l1 = VARSIZE_ANY_EXHDR(t1);
l2 = VARSIZE_ANY_EXHDR(t2);
result = palloc(l1+l2+VARHDRSZ);
memcpy(VARDATA(result), VARDATA_ANY(t1), l1);
memcpy(VARDATA(result) + l1, VARDATA_ANY(t2), l2);
SET_VARSIZE(result, l1 + l2 + VARHDRSZ);
PG_RETURN_TEXT_P(result);
}
PG_FUNCTION_INFO_V1(ora_nvl);
Datum
ora_nvl(PG_FUNCTION_ARGS)
{
if (!PG_ARGISNULL(0))
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
if (!PG_ARGISNULL(1))
PG_RETURN_DATUM(PG_GETARG_DATUM(1));
PG_RETURN_NULL();
}
PG_FUNCTION_INFO_V1(ora_nvl2);
Datum
ora_nvl2(PG_FUNCTION_ARGS)
{
if (!PG_ARGISNULL(0))
{
if (!PG_ARGISNULL(1))
PG_RETURN_DATUM(PG_GETARG_DATUM(1));
}
else
{
if (!PG_ARGISNULL(2))
PG_RETURN_DATUM(PG_GETARG_DATUM(2));
}
PG_RETURN_NULL();
}
PG_FUNCTION_INFO_V1(ora_set_nls_sort);
Datum
ora_set_nls_sort(PG_FUNCTION_ARGS)
{
text *arg = PG_GETARG_TEXT_P(0);
if (def_locale != NULL)
{
pfree(def_locale);
def_locale = NULL;
}
def_locale = (text*) MemoryContextAlloc(TopMemoryContext, VARSIZE(arg));
memcpy(def_locale, arg, VARSIZE(arg));
PG_RETURN_VOID();
}
static text*
_nls_run_strxfrm(text *string, text *locale)
{
char *string_str;
int string_len;
char *locale_str = NULL;
text *result;
char *tmp = NULL;
size_t rest = 0;
bool changed_locale = false;
/*
* Save the default, server-wide locale setting.
* It should not change during the life-span of the server so it
* is safe to save it only once, during the first invocation.
*/
if (!lc_collate_cache)
{
if ((lc_collate_cache = setlocale(LC_COLLATE, NULL)))
/* Make a copy of the locale name string. */
#ifdef _MSC_VER
lc_collate_cache = _strdup(lc_collate_cache);
#else
lc_collate_cache = strdup(lc_collate_cache);
#endif
if (!lc_collate_cache)
elog(ERROR, "failed to retrieve the default LC_COLLATE value");
}
/*
* To run strxfrm, we need a zero-terminated strings.
*/
string_len = VARSIZE_ANY_EXHDR(string);
if (string_len < 0)
return NULL;
string_str = palloc(string_len + 1);
memcpy(string_str, VARDATA_ANY(string), string_len);
*(string_str + string_len) = '\0';
if (locale)
{
int locale_len = VARSIZE_ANY_EXHDR(locale);
/*
* If different than default locale is requested, call setlocale.
*/
if (locale_len > 0
&& (strncmp(lc_collate_cache, VARDATA_ANY(locale), locale_len)
|| *(lc_collate_cache + locale_len) != '\0'))
{
locale_str = palloc(locale_len + 1);
memcpy(locale_str, VARDATA_ANY(locale), locale_len);
*(locale_str + locale_len) = '\0';
/*
* Try to set correct locales.
* If setlocale failed, we know the default stayed the same,
* co we can safely elog.
*/
if (!setlocale(LC_COLLATE, locale_str))
elog(ERROR, "failed to set the requested LC_COLLATE value [%s]", locale_str);
changed_locale = true;
}
}
/*
* We do TRY / CATCH / END_TRY to catch ereport / elog that might
* happen during palloc. Ereport during palloc would not be
* nice since it would leave the server with changed locales
* setting, resulting in bad things.
*/
PG_TRY();
{
size_t size = 0;
/*
* Text transformation.
* Increase the buffer until the strxfrm is able to fit.
*/
size = string_len * multiplication + 1;
tmp = palloc(size + VARHDRSZ);
rest = strxfrm(tmp + VARHDRSZ, string_str, size);
while (rest >= size)
{
pfree(tmp);
size = rest + 1;
tmp = palloc(size + VARHDRSZ);
rest = strxfrm(tmp + VARHDRSZ, string_str, size);
/*
* Cache the multiplication factor so that the next
* time we start with better value.
*/
if (string_len)
multiplication = (rest / string_len) + 2;
}
}
PG_CATCH ();
{
if (changed_locale) {
/*
* Set original locale
*/
if (!setlocale(LC_COLLATE, lc_collate_cache))
elog(FATAL, "failed to set back the default LC_COLLATE value [%s]", lc_collate_cache);
}
PG_RE_THROW();
}
PG_END_TRY ();
if (changed_locale)
{
/*
* Set original locale
*/
if (!setlocale(LC_COLLATE, lc_collate_cache))
elog(FATAL, "failed to set back the default LC_COLLATE value [%s]", lc_collate_cache);
pfree(locale_str);
}
pfree(string_str);
/*
* If the multiplication factor went down, reset it.
*/
if (string_len && rest < string_len * multiplication / 4)
multiplication = (rest / string_len) + 1;
result = (text *) tmp;
SET_VARSIZE(result, rest + VARHDRSZ);
return result;
}
PG_FUNCTION_INFO_V1(ora_nlssort);
Datum
ora_nlssort(PG_FUNCTION_ARGS)
{
text *locale;
text *result;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
if (PG_ARGISNULL(1))
{
if (def_locale != NULL)
locale = def_locale;
else
{
locale = palloc(VARHDRSZ);
SET_VARSIZE(locale, VARHDRSZ);
}
}
else
{
locale = PG_GETARG_TEXT_PP(1);
}
result = _nls_run_strxfrm(PG_GETARG_TEXT_PP(0), locale);
if (! result)
PG_RETURN_NULL();
PG_RETURN_BYTEA_P(result);
}
PG_FUNCTION_INFO_V1(ora_decode);
/*
* decode(lhs, [rhs, ret], ..., [default])
*/
Datum
ora_decode(PG_FUNCTION_ARGS)
{
int nargs;
int i;
int retarg;
/* default value is last arg or NULL. */
nargs = PG_NARGS();
if (nargs % 2 == 0)
{
retarg = nargs - 1;
nargs -= 1; /* ignore the last argument */
}
else
retarg = -1; /* NULL */
if (PG_ARGISNULL(0))
{
for (i = 1; i < nargs; i += 2)
{
if (PG_ARGISNULL(i))
{
retarg = i + 1;
break;
}
}
}
else
{
FmgrInfo *eq;
Oid collation = PG_GET_COLLATION();
/*
* On first call, get the input type's operator '=' and save at
* fn_extra.
*/
if (fcinfo->flinfo->fn_extra == NULL)
{
MemoryContext oldctx;
Oid typid = get_fn_expr_argtype(fcinfo->flinfo, 0);
Oid eqoid = equality_oper_funcid(typid);
oldctx = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
eq = palloc(sizeof(FmgrInfo));
fmgr_info(eqoid, eq);
MemoryContextSwitchTo(oldctx);
fcinfo->flinfo->fn_extra = eq;
}
else
eq = fcinfo->flinfo->fn_extra;
for (i = 1; i < nargs; i += 2)
{
Datum result;
if (PG_ARGISNULL(i))
continue;
result = FunctionCall2Coll(eq,
collation,
PG_GETARG_DATUM(0),
PG_GETARG_DATUM(i));
if (DatumGetBool(result))
{
retarg = i + 1;
break;
}
}
}
if (retarg < 0 || PG_ARGISNULL(retarg))
PG_RETURN_NULL();
else
PG_RETURN_DATUM(PG_GETARG_DATUM(retarg));
}
Oid
equality_oper_funcid(Oid argtype)
{
Oid eq;
get_sort_group_operators(argtype, false, true, false, NULL, &eq, NULL, NULL);
return get_opcode(eq);
}
/*
* dump(anyexpr [,format])
*
* the dump function returns a varchar2 value that includes the datatype code,
* the length in bytes, and the internal representation of the expression.
*/
PG_FUNCTION_INFO_V1(orafce_dump);
static void
appendDatum(StringInfo str, const void *ptr, size_t length, int format)
{
if (!PointerIsValid(ptr))
appendStringInfoChar(str, ':');
else
{
const unsigned char *s = (const unsigned char *) ptr;
const char *formatstr;
size_t i;
switch (format)
{
case 8:
formatstr = "%ho";
break;
case 10:
formatstr = "%hu";
break;
case 16:
formatstr = "%hx";
break;
case 17:
formatstr = "%hc";
break;
default:
elog(ERROR, "unknown format");
formatstr = NULL; /* quite compiler */
}
/* append a byte array with the specified format */
for (i = 0; i < length; i++)
{
if (i > 0)
appendStringInfoChar(str, ',');
/* print only ANSI visible chars */
if (format == 17 && (iscntrl(s[i]) || !isascii(s[i])))
appendStringInfoChar(str, '?');
else
appendStringInfo(str, formatstr, s[i]);
}
}
}
Datum
orafce_dump(PG_FUNCTION_ARGS)
{
Oid valtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
int16 typlen;
bool typbyval;
Size length;
Datum value;
int format;
StringInfoData str;
if (!OidIsValid(valtype))
elog(ERROR, "function is called from invalid context");
if (PG_ARGISNULL(0))
elog(ERROR, "argument is NULL");
value = PG_GETARG_DATUM(0);
format = PG_GETARG_IF_EXISTS(1, INT32, 10);
get_typlenbyval(valtype, &typlen, &typbyval);
length = datumGetSize(value, typbyval, typlen);
initStringInfo(&str);
appendStringInfo(&str, "Typ=%d Len=%d: ", valtype, (int) length);
if (!typbyval)
{
appendDatum(&str, DatumGetPointer(value), length, format);
}
else if (length <= 1)
{
char v = DatumGetChar(value);
appendDatum(&str, &v, sizeof(char), format);
}
else if (length <= 2)
{
int16 v = DatumGetInt16(value);
appendDatum(&str, &v, sizeof(int16), format);
}
else if (length <= 4)
{
int32 v = DatumGetInt32(value);
appendDatum(&str, &v, sizeof(int32), format);
}
else
{
int64 v = DatumGetInt64(value);
appendDatum(&str, &v, sizeof(int64), format);
}
PG_RETURN_TEXT_P(cstring_to_text(str.data));
}
PG_FUNCTION_INFO_V1(ora_get_major_version);
/*
* Returns current version etc, PostgreSQL 9.6, PostgreSQL 10, ..
*/
Datum
ora_get_major_version(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(cstring_to_text(PACKAGE_STRING));
}
PG_FUNCTION_INFO_V1(ora_get_major_version_num);
/*
* Returns major version number 9.5, 9.6, 10, 11, ..
*/
Datum
ora_get_major_version_num(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(cstring_to_text(PG_MAJORVERSION));
}
PG_FUNCTION_INFO_V1(ora_get_full_version_num);
/*
* Returns version number string - 9.5.1, 10.2, ..
*/
Datum
ora_get_full_version_num(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(cstring_to_text(PG_VERSION));
}
PG_FUNCTION_INFO_V1(ora_get_platform);
/*
* 32bit, 64bit
*/
Datum
ora_get_platform(PG_FUNCTION_ARGS)
{
#ifdef USE_FLOAT8_BYVAL
PG_RETURN_TEXT_P(cstring_to_text("64bit"));
#else
PG_RETURN_TEXT_P(cstring_to_text("32bit"));
#endif
}
PG_FUNCTION_INFO_V1(ora_get_status);
/*
* Production | Debug
*/
Datum
ora_get_status(PG_FUNCTION_ARGS)
{
#ifdef USE_ASSERT_CHECKING
PG_RETURN_TEXT_P(cstring_to_text("Debug"));
#else
PG_RETURN_TEXT_P(cstring_to_text("Production"));
#endif
}
PG_FUNCTION_INFO_V1(ora_greatest);
/*
* ora_greatest(anyarray) returns anyelement
*/
Datum
ora_greatest(PG_FUNCTION_ARGS)
{
PG_RETURN_DATUM(ora_greatest_least(fcinfo, true));
}
PG_FUNCTION_INFO_V1(ora_least);
/*
* ora_least(anyarray) returns anyelement
*/
Datum
ora_least(PG_FUNCTION_ARGS)
{
PG_RETURN_DATUM(ora_greatest_least(fcinfo, false));
}
/*
* ora_greatest_least(anyarray, bool) returns anyelement
* Boolean parameter is true for greatest and false for least.
*/
static Datum
ora_greatest_least(FunctionCallInfo fcinfo, bool greater)
{
Datum result;
Datum value;
ArrayType *array;
ArrayIterator array_iterator;
Oid element_type;
Oid collation = PG_GET_COLLATION();
ArrayMetaState *my_extra = NULL;
bool isnull;
/* caller functions are marked as strict */
Assert(!PG_ARGISNULL(0));
Assert(!PG_ARGISNULL(1));
array = PG_GETARG_ARRAYTYPE_P(1);
element_type = ARR_ELEMTYPE(array);
Assert(element_type == get_fn_expr_argtype(fcinfo->flinfo, 0));
/* fast return */
if (array_contains_nulls(array))
PG_RETURN_NULL();
my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
if (my_extra == NULL)
{
my_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
sizeof(ArrayMetaState));
my_extra->element_type = ~element_type;
fcinfo->flinfo->fn_extra = my_extra;
}
if (my_extra->element_type != element_type)
{
Oid sortop_oid;
get_typlenbyvalalign(element_type,
&my_extra->typlen,
&my_extra->typbyval,
&my_extra->typalign);
if (greater)
get_sort_group_operators(element_type, false, false, true,
NULL, NULL, &sortop_oid, NULL);
else
get_sort_group_operators(element_type, true, false, false,
&sortop_oid, NULL, NULL, NULL);
my_extra->element_type = element_type;
fmgr_info_cxt(get_opcode(sortop_oid), &my_extra->proc,
fcinfo->flinfo->fn_mcxt);
}
/* Let's return the first parameter by default */
result = PG_GETARG_DATUM(0);
array_iterator = array_create_iterator(array, 0, my_extra);
while (array_iterate(array_iterator, &value, &isnull))
{
/* not nulls, so run the operator */
if (!DatumGetBool(FunctionCall2Coll(&my_extra->proc, collation,
result, value)))
result = value;
}
result = datumCopy(result, my_extra->typbyval, my_extra->typlen);
array_free_iterator(array_iterator);
/* Avoid leaking memory when handed toasted input */
PG_FREE_IF_COPY(array, 1);
PG_RETURN_DATUM(result);
}
#if PG_VERSION_NUM < 120000
static Datum
FunctionCall0Coll(FmgrInfo *flinfo, Oid collation)
{
FunctionCallInfoData fcinfo_data;
FunctionCallInfo fcinfo = &fcinfo_data;
Datum result;
InitFunctionCallInfoData(*fcinfo, flinfo, 0, collation, NULL, NULL);
result = FunctionCallInvoke(fcinfo);
/* Check for null result, since caller is clearly not expecting one */
if (fcinfo->isnull)
elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
return result;
}
#endif
PG_FUNCTION_INFO_V1(orafce_sys_guid);
/*
* Implementation of sys_guid() function
*
* Oracle uses guid based on mac address. The calculation is not too
* difficult, but there are lot of depenedencies necessary for taking
* mac address. Instead to making some static dependecies orafce uses
* dynamic dependency on "uuid-ossp" extension, and calls choosed function
* from this extension.
*/
Datum
orafce_sys_guid(PG_FUNCTION_ARGS)
{
bool reset_fmgr;
Oid funcoid;
pg_uuid_t *uuid;
bytea *result;
funcoid = get_uuid_generate_func_oid(&reset_fmgr);
if (reset_fmgr)
fmgr_info_cxt(funcoid,
&uuid_generate_func_finfo,
TopTransactionContext);
uuid = DatumGetUUIDP(FunctionCall0Coll(&uuid_generate_func_finfo,
InvalidOid));
result = palloc(VARHDRSZ + UUID_LEN);
SET_VARSIZE(result, VARHDRSZ + UUID_LEN);
memcpy(VARDATA(result), uuid->data, UUID_LEN);
PG_RETURN_BYTEA_P(result);
}