Skip to content

Repository files navigation

SQLite URL Parser Extension

License

A C SQLite extension providing a suite of functions to parse and extract components from URL strings directly within your SQL queries. This extension leverages the furi library for URL parsing.

Features

  • Extract various components of a URL:
    • Scheme (e.g., http, https)
    • Authority (e.g., user:pass@example.com:8080)
    • Userinfo (e.g., user:pass)
    • Host (e.g., example.com)
    • Port (e.g., 8080)
    • Path (e.g., /path/to/resource)
    • Query (e.g., key1=value1&key2=value2)
    • Fragment (e.g., section1)

Prerequisites

  • SQLite (version 3.x.x with extension loading enabled)
  • SQLite Development Headers (e.g., sqlite3.h, sqlite3ext.h)
  • C Compiler (e.g., GCC, Clang)

Building the Extension

TODO: Cmake instructons per OS

Installation / Loading

Once you have compiled libsqlite_urlparser.so (or .dylib/.dll), you can load it into SQLite:

  1. Using the SQLite CLI:

    sqlite> .load ./libsqlite_urlparser.dll
    sqlite>SELECT url_host('https://www.example.com/path');
    www.example.com
  2. Programmatically (e.g., in C, Python, etc.): Most SQLite client libraries provide a way to load extensions.

    • C API:sqlite3_load_extension(db, "./libsqlite_urlparser.so", 0, 0);
    • Python (sqlite3 module):
      importsqlite3conn=sqlite3.connect(':memory:')
      conn.enable_load_extension(True)
      conn.load_extension('./library.so') # Adjust path and extension nameconn.enable_load_extension(False) # Optional: disable after loadingcursor=conn.cursor()
      forrowincursor.execute("SELECT url_scheme('ftp://files.server.net/data.zip')"):
      print(row[0]) # ftpconn.close()

Usage

After loading the extension, the following SQL functions become available:

FunctionDescriptionExample
url_schemeExtracts the scheme from a URL.SELECT url_scheme('https://ex.com'); -> https
url_authorityExtracts the authority part.SELECT url_authority('https://u:p@ex.com:80'); -> u:p@ex.com:80
url_userinfoExtracts user information from the authority.SELECT url_userinfo('https://u:p@ex.com'); -> u:p
url_hostExtracts the host from the authority.SELECT url_host('https://ex.com:8080/p'); -> ex.com
url_portExtracts the port from the authority.SELECT url_port('https://ex.com:8080'); -> 8080
url_pathExtracts the path component.SELECT url_path('https://ex.com/a/b?q'); -> /a/b
url_queryExtracts the query string (without '?').SELECT url_query('https://ex.com/p?k=v#f'); -> k=v
url_fragmentExtracts the fragment (without '#').SELECT url_fragment('https://ex.com/p#sec'); -> sec

Example Queries:

-- Load the extension (if not already loaded)-- .load ./library.so-- Basic usageSELECT url_scheme('http://example.com/path?query=value#fragment');
-- Result: httpSELECT url_host('http://user:password@example.com:8080/path');
-- Result: example.comSELECT url_path('http://example.com/some/resource.html');
-- Result: /some/resource.htmlSELECT url_query('http://example.com/search?q=sqlite&lang=en');
-- Result: q=sqlite&lang=enSELECT url_fragment('http://example.com/page#section-2');
-- Result: section-2-- Using with table dataCREATETABLEweblogs (id INTEGERPRIMARY KEY, referrer_url TEXT);
INSERT INTO weblogs (referrer_url) VALUES
('https://www.google.com/search?q=sqlite+extensions'),
('http://internal.app/dashboard#overview'),
(NULL),
('https://blog.example.org/article/123');
SELECT
id,
referrer_url,
url_host(referrer_url) AS referring_host,
url_path(referrer_url) AS referring_path
FROM weblogs;

About

SQLite URL parsing extension

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages