-
Notifications
You must be signed in to change notification settings - Fork 18
/
functions.sql
77 lines (77 loc) · 2.95 KB
/
functions.sql
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
DROP FUNCTION IF EXISTS maxmind_asn;
CREATE FUNCTION maxmind_asn AS (ip) ->
multiIf(
isIPv4String(ip), dictGetUInt32('geoip_asn_blocks_ipv4',
'autonomous_system_number',
tuple(IPv4StringToNumOrDefault(toString(ip)))),
isIPv6String(ip), dictGetUInt32('geoip_asn_blocks_ipv6',
'autonomous_system_number',
tuple(IPv6StringToNumOrDefault(toString(ip)))),
NULL
)
;
DROP FUNCTION IF EXISTS maxmind_org;
CREATE FUNCTION maxmind_org AS (ip) ->
multiIf(
isIPv4String(ip), dictGetString('geoip_asn_blocks_ipv4',
'autonomous_system_organization',
tuple(IPv4StringToNumOrDefault(toString(ip)))),
isIPv6String(ip), dictGetString('geoip_asn_blocks_ipv6',
'autonomous_system_organization',
tuple(IPv6StringToNumOrDefault(toString(ip)))),
NULL
)
;
DROP FUNCTION IF EXISTS maxmind_geoname_id;
CREATE FUNCTION maxmind_geoname_id AS (ip) ->
toUInt64(multiIf(
isIPv4String(ip), dictGetUInt32('geoip_city_blocks_ipv4',
'geoname_id',
tuple(IPv4StringToNumOrDefault(toString(ip)))),
isIPv6String(ip), dictGetUInt32('geoip_city_blocks_ipv6',
'geoname_id',
tuple(IPv6StringToNumOrDefault(toString(ip)))),
0
))
;
DROP FUNCTION IF EXISTS maxmind_country;
CREATE FUNCTION maxmind_country AS (ip) ->
dictGetString('geoip_city_locations_en',
'country_name',
maxmind_geoname_id(ip)
)
;
DROP FUNCTION IF EXISTS maxmind_subdivision1;
CREATE FUNCTION maxmind_subdivision1 AS (ip) ->
dictGetString('geoip_city_locations_en',
'subdivision_1_name',
maxmind_geoname_id(ip)
)
;
DROP FUNCTION IF EXISTS maxmind_subdivision2;
CREATE FUNCTION maxmind_subdivision2 AS (ip) ->
dictGetString('geoip_city_locations_en',
'subdivision_2_name',
maxmind_geoname_id(ip)
)
;
DROP FUNCTION IF EXISTS maxmind_city;
CREATE FUNCTION maxmind_city AS (ip) ->
dictGetString('geoip_city_locations_en',
'city_name',
maxmind_geoname_id(ip)
)
;
DROP FUNCTION IF EXISTS maxmind;
CREATE FUNCTION maxmind AS (type, ip) ->
multiIf(
type = 'asn', toString(maxmind_asn(ip)),
type = 'org', maxmind_org(ip),
type = 'country', maxmind_country(ip),
type = 'subdivision1', maxmind_subdivision1(ip),
type = 'state', maxmind_subdivision1(ip),
type = 'subdivision2', maxmind_subdivision2(ip),
type = 'city', maxmind_city(ip),
NULL
)
;