-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathall_hashids.sql
More file actions
776 lines (690 loc) · 27.7 KB
/
all_hashids.sql
File metadata and controls
776 lines (690 loc) · 27.7 KB
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
-- our usage of these functions places all of them in a separate schema, with "hashids" being the default name
-- change this or don't use it (but know that the other sql functions will not work out of the box, they are assuming the hashids schema).
CREATE SCHEMA hashids;
-- drop all functions out of schema
DROP FUNCTION if exists hashids.consistent_shuffle(text, text);
DROP FUNCTION if exists hashids.clean_seps_from_alphabet(text, text);
DROP FUNCTION if exists hashids.clean_alphabet_from_seps(text, text);
DROP FUNCTION if exists hashids.distinct_alphabet(text);
DROP FUNCTION if exists hashids.setup_alphabet(text, text, text, text);
DROP FUNCTION if exists hashids.hash(bigint, text, boolean);
DROP FUNCTION if exists hashids.unhash(text, text, boolean);
DROP FUNCTION if exists hashids.encode_list(bigint[], text, integer, text, boolean);
DROP FUNCTION if exists hashids.encode_list(bigint[], text, integer, text);
DROP FUNCTION if exists hashids.encode_list(bigint[], text, integer);
DROP FUNCTION if exists hashids.encode_list(bigint[], text);
DROP FUNCTION if exists hashids.encode_list(bigint[]);
DROP FUNCTION if exists hashids.encode(bigint);
DROP FUNCTION if exists hashids.encode(bigint, text);
DROP FUNCTION if exists hashids.encode(bigint, text, integer);
DROP FUNCTION if exists hashids.encode(bigint, text, integer, text);
DROP FUNCTION if exists hashids.encode(bigint, text, integer, text, boolean);
DROP FUNCTION if exists hashids.decode(text, text, integer, text, boolean);
DROP FUNCTION if exists hashids.decode(text, text, integer, text);
DROP FUNCTION if exists hashids.decode(text, text, integer);
DROP FUNCTION if exists hashids.decode(text, text);
DROP FUNCTION if exists hashids.decode(text);
-- constent shuffle
CREATE OR REPLACE FUNCTION hashids.consistent_shuffle
(
p_alphabet text,
p_salt text
)
RETURNS text AS
$$
DECLARE p_alphabet ALIAS FOR $1;
p_salt ALIAS FOR $2;
v_ls int;
v_i int;
v_v int := 0;
v_p int := 0;
v_n int := 0;
v_j int := 0;
v_temp char(1);
BEGIN
-- Null or Whitespace?
IF p_salt IS NULL OR length(LTRIM(RTRIM(p_salt))) = 0 THEN
RETURN p_alphabet;
END IF;
v_ls := length(p_salt);
v_i := length(p_alphabet) - 1;
WHILE v_i > 0 LOOP
v_v := v_v % v_ls;
v_n := ascii(SUBSTRING(p_salt, v_v + 1, 1)); -- need some investigation to see if +1 here is because of 1 based arrays in sql ... this isn't in the reference JS or .net code.
v_p := v_p + v_n;
v_j := (v_n + v_v + v_p) % v_i;
v_temp := SUBSTRING(p_alphabet, v_j + 1, 1);
p_alphabet :=
SUBSTRING(p_alphabet, 1, v_j) ||
SUBSTRING(p_alphabet, v_i + 1, 1) ||
SUBSTRING(p_alphabet, v_j + 2, 255);
p_alphabet := SUBSTRING(p_alphabet, 1, v_i) || v_temp || SUBSTRING(p_alphabet, v_i + 2, 255);
v_i := v_i - 1;
v_v := v_v + 1;
END LOOP; -- WHILE
RETURN p_alphabet;
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 200;
-- setup seps
CREATE OR REPLACE FUNCTION hashids.clean_seps_from_alphabet(
in p_seps text,
in p_alphabet text
)
RETURNS text AS
$$
DECLARE
p_seps ALIAS for $1;
p_alphabet ALIAS for $2;
v_split_seps text[] := regexp_split_to_array(p_seps, '');
v_split_alphabet text[] := regexp_split_to_array(p_alphabet, '');
v_i integer := 1;
v_length integer := length(p_seps);
v_ret text := '';
BEGIN
-- had to add this function because doing this:
-- p_seps := array_to_string(ARRAY(select chars.cha from (select unnest(regexp_split_to_array(p_seps, '')) as cha intersect select unnest(regexp_split_to_array(p_alphabet, '')) as cha ) as chars order by ascii(cha) desc), '');
-- doesn't preserve the order of the input
for v_i in 1..v_length loop
-- raise notice 'v_split_seps[%]: % == %', v_i, v_split_seps[v_i], v_split_seps[v_i] = any (v_split_alphabet);
if (v_split_seps[v_i] = any (v_split_alphabet)) then
v_ret = v_ret || v_split_seps[v_i];
end if;
end loop;
-- raise notice 'v_ret: %', v_ret;
return v_ret;
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 200;
CREATE OR REPLACE FUNCTION hashids.clean_alphabet_from_seps(
in p_seps text,
in p_alphabet text
)
RETURNS text AS
$$
DECLARE
p_seps ALIAS for $1;
p_alphabet ALIAS for $2;
v_split_seps text[] := regexp_split_to_array(p_seps, '');
v_split_alphabet text[] := regexp_split_to_array(p_alphabet, '');
v_i integer := 1;
v_length integer := length(p_alphabet);
v_ret text := '';
BEGIN
-- had to add this function because doing this:
-- p_alphabet := array_to_string(ARRAY( select chars.cha from (select unnest(regexp_split_to_array(p_alphabet, '')) as cha EXCEPT select unnest(regexp_split_to_array(p_seps, '')) as cha) as chars ), '');
-- doesn't preserve the order of the input
for v_i in 1..v_length loop
--raise notice 'v_split_alphabet[%]: % != %', v_i, v_split_alphabet[v_i], v_split_alphabet[v_i] <> all (v_split_seps);
if (v_split_alphabet[v_i] <> all (v_split_seps)) then
v_ret = v_ret || v_split_alphabet[v_i];
end if;
end loop;
-- raise notice 'v_ret: %', v_ret;
return v_ret;
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 200;
CREATE OR REPLACE FUNCTION hashids.distinct_alphabet(in p_alphabet text)
RETURNS text AS
$$
DECLARE
p_alphabet ALIAS for $1;
v_split_alphabet text[] := regexp_split_to_array(p_alphabet, '');
v_i integer := 2;
v_length integer := length(p_alphabet);
v_ret_array text[];
BEGIN
-- had to add this function because doing this:
-- p_alphabet := string_agg(distinct chars.split_chars, '') from (select unnest(regexp_split_to_array(p_alphabet, '')) as split_chars) as chars;
-- doesn't preserve the order of the input, which was causing issues
if (v_length = 0) then
RAISE EXCEPTION 'alphabet must contain at least 1 char' USING HINT = 'Please check your alphabet';
end if;
v_ret_array := array_append(v_ret_array, v_split_alphabet[1]);
-- starting at 2 because already appended 1 to it.
for v_i in 2..v_length loop
-- raise notice 'v_split_alphabet[%]: % != %', v_i, v_split_alphabet[v_i], v_split_alphabet[v_i] <> all (v_ret_array);
if (v_split_alphabet[v_i] <> all (v_ret_array)) then
v_ret_array := array_append(v_ret_array, v_split_alphabet[v_i]);
end if;
end loop;
-- raise notice 'v_ret_array: %', array_to_string(v_ret_array, '');
return array_to_string(v_ret_array, '');
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 200;
-- setup alphabet
CREATE OR REPLACE FUNCTION hashids.setup_alphabet(
in p_salt text default '',
inout p_alphabet text default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
out p_seps text,
out p_guards text)
AS
$$
DECLARE
p_salt ALIAS for $1;
p_alphabet ALIAS for $2;
p_seps ALIAS for $3;
p_guards ALIAS for $4;
v_sep_div float := 3.5;
v_guard_div float := 12.0;
v_guard_count integer;
v_seps_length integer;
v_seps_diff integer;
BEGIN
p_seps := 'cfhistuCFHISTU';
-- p_alphabet := string_agg(distinct chars.split_chars, '') from (select unnest(regexp_split_to_array(p_alphabet, '')) as split_chars) as chars;
-- this also doesn't preserve the order of alphabet, but it doesn't appear to matter, never mind on that
p_alphabet := hashids.distinct_alphabet(p_alphabet);
if length(p_alphabet) < 16 then
RAISE EXCEPTION 'alphabet must contain 16 unique characters, it is: %', length(p_alphabet) USING HINT = 'Please check your alphabet';
end if;
-- seps should only contain character present in the passed alphabet
-- p_seps := array_to_string(ARRAY(select chars.cha from (select unnest(regexp_split_to_array(p_seps, '')) as cha intersect select unnest(regexp_split_to_array(p_alphabet, '')) as cha ) as chars order by ascii(cha) desc), '');
-- this doesn't preserve the input order, which is bad
p_seps := hashids.clean_seps_from_alphabet(p_seps, p_alphabet);
-- alphabet should not contain seps.
-- p_alphabet := array_to_string(ARRAY( select chars.cha from (select unnest(regexp_split_to_array(p_alphabet, '')) as cha EXCEPT select unnest(regexp_split_to_array(p_seps, '')) as cha) as chars ), '');
-- this also doesn't prevserve the order
p_alphabet := hashids.clean_alphabet_from_seps(p_seps, p_alphabet);
p_seps := hashids.consistent_shuffle(p_seps, p_salt);
if (length(p_seps) = 0) or ((length(p_alphabet) / length(p_seps)) > v_sep_div) then
v_seps_length := cast( ceil( length(p_alphabet)/v_sep_div ) as integer);
if v_seps_length = 1 then
v_seps_length := 2;
end if;
if v_seps_length > length(p_seps) then
v_seps_diff := v_seps_length - length(p_seps);
p_seps := p_seps || SUBSTRING(p_alphabet, 1, v_seps_diff);
p_alphabet := SUBSTRING(p_alphabet, v_seps_diff + 1);
else
p_seps := SUBSTRING(p_seps, 1, v_seps_length + 1);
end if;
end if;
p_alphabet := hashids.consistent_shuffle(p_alphabet, p_salt);
v_guard_count := cast(ceil(length(p_alphabet) / v_guard_div ) as integer);
if length(p_alphabet) < 3 then
p_guards := SUBSTRING(p_seps, 1, v_guard_count);
p_seps := SUBSTRING(p_seps, v_guard_count + 1);
else
p_guards := SUBSTRING(p_alphabet, 1, v_guard_count);
p_alphabet := SUBSTRING(p_alphabet, v_guard_count + 1);
end if;
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 200;
-- create hash
CREATE OR REPLACE FUNCTION hashids.hash(
p_input bigint,
p_alphabet text,
p_zero_offset boolean DEFAULT true)
RETURNS text AS
$$
DECLARE
p_input ALIAS for $1;
p_alphabet ALIAS for $2;
p_zero_offset integer := case when $3 = true then 1 else 0 end ; -- adding an offset so that this can work with values from a zero based array language
v_hash varchar(255) := '';
v_alphabet_length integer := length($2);
v_pos integer;
BEGIN
WHILE 1 = 1 LOOP
v_pos := (p_input % v_alphabet_length) + p_zero_offset; -- have to add one, because SUBSTRING in SQL starts at 1 instead of 0 (like it does in other languages)
--raise notice '% mod % == %', p_input, v_alphabet_length, v_pos;
--raise notice 'SUBSTRING(%, %, 1): %', p_alphabet, v_pos, (SUBSTRING(p_alphabet, v_pos, 1));
--raise notice '% || % == %', SUBSTRING(p_alphabet, v_pos, 1), v_hash, SUBSTRING(p_alphabet, v_pos, 1) || v_hash;
v_hash := SUBSTRING(p_alphabet, v_pos, 1) || v_hash;
p_input := CAST((p_input / v_alphabet_length) as bigint);
--raise notice 'p_input %', p_input;
IF p_input <= 0 THEN
EXIT;
END IF;
END LOOP;
RETURN v_hash;
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 250;
-- unhash
CREATE OR REPLACE FUNCTION hashids.unhash(
p_input text,
p_alphabet text,
p_zero_offset boolean DEFAULT true)
RETURNS bigint AS
$$
DECLARE
p_input ALIAS for $1;
p_alphabet ALIAS for $2;
p_zero_offset integer := case when $3 = true then 1 else 0 end ; -- adding an offset so that this can work with values from a zero based array language
v_input_length integer := length($1);
v_alphabet_length integer := length($2);
v_ret bigint := 0;
v_input_char char(1);
v_pos integer;
v_i integer := 1;
BEGIN
for v_i in 1..v_input_length loop
v_input_char := SUBSTRING(p_input, (v_i), 1);
v_pos := POSITION(v_input_char in p_alphabet) - p_zero_offset; -- have to remove one to interface with .net because it is a zero based index
--raise notice '%[%] is % to position % in %', p_input, v_i, v_input_char, v_pos, p_alphabet;
--raise notice ' % + (% * power(%, % - % - 1)) == %', v_ret, v_pos, v_alphabet_length, v_input_length, (v_i - 1), v_ret + (v_pos * power(v_alphabet_length, v_input_length - (v_i-1) - 1));
v_ret := v_ret + (v_pos * power(v_alphabet_length, v_input_length - (v_i-p_zero_offset) - 1));
end loop;
RETURN v_ret;
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 100;
-- encode list
CREATE OR REPLACE FUNCTION hashids.encode_list(
in p_numbers bigint[],
in p_salt text, -- DEFAULT '',
in p_min_hash_length integer, -- integer default 0,
in p_alphabet text, -- DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
in p_zero_offset boolean DEFAULT true)
RETURNS text AS
$$
DECLARE
p_numbers ALIAS for $1;
p_salt ALIAS for $2;
p_min_hash_length ALIAS for $3;
p_alphabet ALIAS for $4;
p_zero_offset integer := case when $5 = true then 1 else 0 end ; -- adding an offset so that this can work with values from a zero based array language
v_seps text;
v_guards text;
-- Working Data
v_alphabet text := p_alphabet;
v_numbersHashInt int = 0;
v_lottery char(1);
v_buffer varchar(255);
v_last varchar(255);
v_ret varchar(255);
v_sepsIndex int;
v_lastId int;
v_count int = array_length(p_numbers, 1);
v_i int = 0;
v_id int = 0;
v_number bigint;
v_guardIndex int;
v_guard char(1);
v_halfLength int;
v_excess int;
BEGIN
select * from hashids.setup_alphabet(p_salt, p_alphabet) into v_alphabet, v_seps, v_guards;
--raise notice 'v_seps: %', v_seps;
--raise notice 'v_alphabet: %', v_alphabet;
--raise notice 'v_guards: %', v_guards;
-- Calculate numbersHashInt
for v_lastId in 1..v_count LOOP
v_numbersHashInt := v_numbersHashInt + (p_numbers[v_lastId] % ((v_lastId-p_zero_offset) + 100));
END LOOP;
-- Choose lottery
v_lottery := SUBSTRING(v_alphabet, (v_numbersHashInt % length(v_alphabet)) + 1, 1); -- is this a +1 because of sql 1 based index, need to double check to see if can be replaced with param.
v_ret := v_lottery;
-- Encode many
v_i := 0;
v_id := 0;
for v_i in 1..v_count LOOP
v_number := p_numbers[v_i];
raise notice '%[%]: % for %', p_numbers, v_i, v_number, v_count;
v_buffer := v_lottery || p_salt || v_alphabet;
v_alphabet := hashids.consistent_shuffle(v_alphabet, SUBSTRING(v_buffer, 1, length(v_alphabet)));
v_last := hashids.hash(v_number, v_alphabet, cast(p_zero_offset as boolean));
v_ret := v_ret || v_last;
--raise notice 'v_ret: %', v_ret;
--raise notice '(v_i < v_count: % < % == %', v_i, v_count, (v_i < v_count);
IF (v_i) < v_count THEN
--raise notice 'v_sepsIndex: % mod (% + %) == %', v_number, ascii(SUBSTRING(v_last, 1, 1)), v_i, (v_number % (ascii(SUBSTRING(v_last, 1, 1)) + v_i));
v_sepsIndex := v_number % (ascii(SUBSTRING(v_last, 1, 1)) + (v_i-p_zero_offset)); -- since this is 1 base vs 0 based bringing the number back down so that the mod is the same for zero based records
v_sepsIndex := v_sepsIndex % length(v_seps);
v_ret := v_ret || SUBSTRING(v_seps, v_sepsIndex+1, 1);
END IF;
END LOOP;
----------------------------------------------------------------------------
-- Enforce minHashLength
----------------------------------------------------------------------------
IF length(v_ret) < p_min_hash_length THEN
------------------------------------------------------------------------
-- Add first 2 guard characters
------------------------------------------------------------------------
v_guardIndex := (v_numbersHashInt + ascii(SUBSTRING(v_ret, 1, 1))) % length(v_guards);
v_guard := SUBSTRING(v_guards, v_guardIndex + 1, 1);
--raise notice '% || % is %', v_guard, v_ret, v_guard || v_ret;
v_ret := v_guard || v_ret;
IF length(v_ret) < p_min_hash_length THEN
v_guardIndex := (v_numbersHashInt + ascii(SUBSTRING(v_ret, 3, 1))) % length(v_guards);
v_guard := SUBSTRING(v_guards, v_guardIndex + 1, 1);
v_ret := v_ret || v_guard;
END IF;
------------------------------------------------------------------------
-- Add the rest
------------------------------------------------------------------------
WHILE length(v_ret) < p_min_hash_length LOOP
v_halfLength := COALESCE(v_halfLength, CAST((length(v_alphabet) / 2) as int));
v_alphabet := hashids.consistent_shuffle(v_alphabet, v_alphabet);
v_ret := SUBSTRING(v_alphabet, v_halfLength + 1, 255) || v_ret || SUBSTRING(v_alphabet, 1, v_halfLength);
v_excess := length(v_ret) - p_min_hash_length;
IF v_excess > 0 THEN
v_ret := SUBSTRING(v_ret, CAST((v_excess / 2) as int) + 1, p_min_hash_length);
END IF;
END LOOP;
END IF;
RETURN v_ret;
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 350;
CREATE OR REPLACE FUNCTION hashids.encode_list( in p_numbers bigint[] )
RETURNS text AS
$$
-- Options Data - generated by hashids-tsql
DECLARE
p_numbers ALIAS for $1;
p_salt text := ''; -- default
p_min_hash_length integer := 0; -- default
p_alphabet text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.encode_list(p_numbers, p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.encode_list(
in p_numbers bigint[],
in p_salt text )
RETURNS text AS
$$
-- Options Data - generated by hashids-tsql
DECLARE
p_numbers ALIAS for $1;
p_salt ALIAS for $2; -- default
p_min_hash_length integer := 0; -- default
p_alphabet text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.encode_list(p_numbers, p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.encode_list(
in p_numbers bigint[],
in p_salt text,
in p_min_hash_length integer )
RETURNS text AS
$$
-- Options Data - generated by hashids-tsql
DECLARE
p_numbers ALIAS for $1;
p_salt ALIAS for $2; -- default
p_min_hash_length ALIAS for $3; -- default
p_alphabet text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.encode_list(p_numbers, p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
-- encode
CREATE OR REPLACE FUNCTION hashids.encode(in p_number bigint)
RETURNS text AS
$$
DECLARE
p_number ALIAS for $1;
p_salt text := ''; -- default
p_min_hash_length integer := 0; -- default
p_alphabet text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.encode_list(ARRAY[p_number::bigint]::bigint[], p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.encode(
in p_number bigint,
in p_salt text)
RETURNS text AS
$$
DECLARE
p_number ALIAS for $1;
p_salt ALIAS for $2;
p_min_hash_length integer := 0; -- default
p_alphabet text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.encode_list(ARRAY[p_number::bigint]::bigint[], p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.encode(
in p_number bigint,
in p_salt text,
in p_min_hash_length integer)
RETURNS text AS
$$
DECLARE
p_number ALIAS for $1;
p_salt ALIAS for $2;
p_min_hash_length ALIAS for $3; -- default
p_alphabet text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.encode_list(ARRAY[p_number::bigint]::bigint[], p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.encode(
in p_number bigint,
in p_salt text,
in p_min_hash_length integer,
in p_alphabet text)
RETURNS text AS
$$
DECLARE
p_number ALIAS for $1;
p_salt ALIAS for $2;
p_min_hash_length ALIAS for $3; -- default
p_alphabet ALIAS for $4; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.encode_list(ARRAY[p_number::bigint]::bigint[], p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.encode(
in p_number bigint,
in p_salt text,
in p_min_hash_length integer,
in p_alphabet text,
in p_zero_offset boolean)
RETURNS text AS
$$
DECLARE
p_number ALIAS for $1;
p_salt ALIAS for $2;
p_min_hash_length ALIAS for $3; -- default
p_alphabet ALIAS for $4; -- default
p_zero_offset ALIAS for $5 ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.encode_list(ARRAY[p_number::bigint]::bigint[], p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
-- decode
CREATE OR REPLACE FUNCTION hashids.decode(
in p_hash text,
in p_salt text,
in p_min_hash_length integer,
in p_alphabet text,
p_zero_offset boolean DEFAULT true)
RETURNS bigint[] AS
$$
DECLARE
p_hash ALIAS for $1;
p_salt ALIAS for $2;
p_min_hash_length ALIAS for $3;
p_alphabet ALIAS for $4;
p_zero_offset ALIAS for $5; -- adding an offset so that this can work with values from a zero based array language
v_seps text;
v_guards text;
v_alphabet text := p_alphabet;
v_lottery char(1);
v_hashBreakdown varchar(255);
v_hashArray text[];
v_index integer := 1;
v_j integer := 1;
v_hashArrayLength integer;
v_subHash varchar;
v_buffer varchar(255);
v_encodeCheck varchar(255);
v_ret_temp bigint;
v_ret bigint[];
BEGIN
select * from hashids.setup_alphabet(p_salt, v_alphabet) into v_alphabet, v_seps, v_guards;
--raise notice 'v_seps: %', v_seps;
--raise notice 'v_alphabet: %', v_alphabet;
--raise notice 'v_guards: %', v_guards;
v_hashBreakdown := regexp_replace(p_hash, '[' || v_guards || ']', ' ');
v_hashArray := regexp_split_to_array(p_hash, '[' || v_guards || ']');
-- take the guards and replace with space,
-- split on space
-- if length is 3 or 2, set index to 1 else start at zero
-- if first index in idBreakDown isn't default
if ((array_length(v_hashArray, 1) = 3) or (array_length(v_hashArray, 1) = 2)) then
v_index := 2; -- in the example code (C# and js) it is 1 here, but postgresql arrays start at 1, so switching to 2
END IF;
--raise notice '%', v_hashArray;
v_hashBreakdown := v_hashArray[v_index];
--raise notice 'v_hashArray[%] %', v_index, v_hashBreakdown;
if (left(v_hashBreakdown, 1) <> '') IS NOT false then
v_lottery := left(v_hashBreakdown, 1);
--raise notice 'v_lottery %', v_lottery;
--raise notice 'SUBSTRING(%, 2, % - 1) %', v_hashBreakdown, length(v_hashBreakdown), SUBSTRING(v_hashBreakdown, 2);
v_hashBreakdown := SUBSTRING(v_hashBreakdown, 2);
v_hashArray := regexp_split_to_array(v_hashBreakdown, '[' || v_seps || ']');
--raise notice 'v_hashArray % -- %', v_hashArray, array_length(v_hashArray, 1);
v_hashArrayLength := array_length(v_hashArray, 1);
for v_j in 1..v_hashArrayLength LOOP
v_subHash := v_hashArray[v_j];
--raise notice 'v_subHash %', v_subHash;
v_buffer := v_lottery || p_salt || v_alphabet;
--raise notice 'v_buffer %', v_buffer;
--raise notice 'v_alphabet: hashids.consistent_shuffle(%, %) == %', v_alphabet, SUBSTRING(v_buffer, 1, length(v_alphabet)), hashids.consistent_shuffle(v_alphabet, SUBSTRING(v_buffer, 1, length(v_alphabet)));
v_alphabet := hashids.consistent_shuffle(v_alphabet, SUBSTRING(v_buffer, 1, length(v_alphabet)));
v_ret_temp := hashids.unhash(v_subHash, v_alphabet, p_zero_offset);
--raise notice 'v_ret_temp: %', v_ret_temp;
v_ret := array_append(v_ret, v_ret_temp);
END LOOP;
v_encodeCheck := hashids.encode_list(v_ret, p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
IF (v_encodeCheck <> p_hash) then
raise notice 'hashids.encodeList(%): % <> %', v_ret, v_encodeCheck, p_hash;
return ARRAY[]::bigint[];
end if;
end if;
RETURN v_ret;
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.decode( in p_hash text )
RETURNS bigint[] AS
$$
DECLARE
p_numbers ALIAS for $1;
p_salt text := ''; -- default
p_min_hash_length integer := 0; -- default
p_alphabet text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.decode(p_hash, p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.decode(
in p_hash text,
in p_salt text)
RETURNS text AS
$$
DECLARE
p_numbers ALIAS for $1;
p_salt ALIAS for $2; -- default
p_min_hash_length integer := 0; -- default
p_alphabet text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.decode(p_hash, p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.decode(
in p_hash text,
in p_salt text,
in p_min_hash_length integer)
RETURNS bigint[] AS
$$
DECLARE
p_numbers ALIAS for $1;
p_salt ALIAS for $2; -- default
p_min_hash_length ALIAS for $3; -- default
p_alphabet text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.decode(p_hash, p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.decode(
in p_hash text,
in p_salt text,
in p_min_hash_length integer,
in p_alphabet text)
RETURNS bigint[] AS
$$
DECLARE
p_numbers ALIAS for $1;
p_salt ALIAS for $2; -- default
p_min_hash_length ALIAS for $3; -- default
p_alphabet ALIAS for $4; -- default
p_zero_offset boolean := true ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.decode(p_hash, p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;
CREATE OR REPLACE FUNCTION hashids.decode(
in p_hash text,
in p_salt text,
in p_min_hash_length integer,
in p_alphabet text,
in p_zero_offset boolean DEFAULT true)
RETURNS bigint[] AS
$$
DECLARE
p_numbers ALIAS for $1;
p_salt ALIAS for $2; -- default
p_min_hash_length ALIAS for $3; -- default
p_alphabet ALIAS for $4; -- default
p_zero_offset ALIAS for $5 ; -- adding an offset so that this can work with values from a zero based array language
BEGIN
RETURN hashids.decode(p_hash, p_salt, p_min_hash_length, p_alphabet, p_zero_offset);
END;
$$
LANGUAGE plpgsql IMMUTABLE
COST 300;