nalgeon / sqlean

The ultimate set of SQLite extensions
MIT License
3.65k stars 115 forks source link

IP address integer to string and string to integer functions #86

Closed cirosantilli closed 8 months ago

cirosantilli commented 1 year ago

It would be good to add functions that convert IPv4 and IPv6 address to/from strings.

A use case I had is you get a .csv file with string IPs, but it is much more efficient to handle them as integer in DB.

E.g.:

sqlite3 :memory: '.load ./ip' "SELECT printf('%08x', str2ipv4('1.128.2.10'))"

would output:

0180020a

At: https://stackoverflow.com/questions/7638238/sqlite-ip-address-storage/76520885#76520885 I show an implementation of str -> int, but would need to add the inverse and IPv6 support too.

Lazy to make a clean pull request here, but it's an idea.

terefang commented 10 months ago

let me also suggest

functions

ipmask(str)

sqlite> select ipmask('192.168.16.12/24');
255.255.255.0

ipmasktolen(str)

sqlite> select ipmasktolen('255.255.254.0');
23

ipmatch(pattern, needle)

sqlite> select ipmatch('192.168.*','192.168.16.12');
true
sqlite> select ipmatch('192.168.*.12','192.168.16.12');
true
sqlite> select ipmatch('192.*.*.12','192.168.16.12');
true
sqlite> select ipmatch('192.*','192.168.16.12');
true
sqlite> select ipmatch('*.12','192.168.16.12');
true
sqlite> select ipmatch('192.168.1-17.12','192.168.16.12');
true
sqlite> select ipmatch('192.168.1-14.12','192.168.16.12');
false
sqlite> select ipmatch('192.150-168.1-17.11-12','192.168.16.12');
true
sqlite> select ipmatch('192.150-168.*.10','192.168.16.12');
false

and the mysql functions

and the logical derivatives

nalgeon commented 8 months ago

Sorry, I have no plans to add new features at this time.