Giter Club home page Giter Club logo

pg-jq's Introduction

WARNING

This is a toy PostgreSQL extension that exposes basic libjq functionality to PostgreSQL.

Do not use for anything serious. It might crash your DB server. You have been warned.

This "project" depends on the amazing pgx project.

To compile you must have libjq installed, often it is installed by installing jq itself. Use your favorite package manager to install it.

If your package manager installs things outside of the default search paths for your default compiler toolchain you'll need to tell the jq crates where to find the libraries using the enviornment variable JQ_LIB_DIR. For example on macOS using MacPorts one would use JQ_LIB_DIR=/opt/local/lib.

Reference the pgx documentation for information how to work with extensions using pgx. Its documentation is really good.

Installation Notes

This is purposefully empty. I don't want anyone to attempt to use this terrible thing in a real environment.

pg-jq's People

Contributors

chadcatlett avatar

Stargazers

Markus Geiger avatar

Watchers

 avatar  avatar

pg-jq's Issues

I was dorking around with a similar idea awhile back

Here's what I was playing with. I have no intentions of releasing it, so help yourself!

use pgx::*;
use rustc_hash::FxHashMap;

pg_module_magic!();

extension_sql!(r#"CREATE DOMAIN JsonString AS json;"#);

#[inline]
fn get_jq_program(q: &str) -> &'static mut jq_rs::JqProgram {
    unsafe {
        static mut JQ_PROGRAMS: Option<FxHashMap<String, jq_rs::JqProgram>> = None;

        let map = JQ_PROGRAMS.get_or_insert_with(|| Default::default());
        map.entry(q.to_string())
            .or_insert_with(|| jq_rs::compile(q).expect("failed to compile jq program"))
    }
}

#[pg_operator(parallel_safe, immutable)]
#[opname(~>)]
fn jqeval_json(input: JsonString, query: &str) -> JsonString {
    let program = get_jq_program(query);
    let results = program.run(&input.0).expect("failed to run jq program");
    JsonString(results)
}

#[pg_operator(parallel_safe, immutable)]
#[opname(~>)]
fn jqeval_jsonb(input: JsonB, query: &str) -> JsonB {
    let program = get_jq_program(query);
    let input = serde_json::to_string(&input.0).unwrap();
    let results = program.run(&input).expect("failed to run jq program");
    JsonB(serde_json::from_str(&results).expect("results not valid json"))
}

#[cfg(any(test, feature = "pg_test"))]
mod tests {
    use pgx::*;

    #[pg_test]
    fn test_hello_jq_pgx() {
        assert_eq!("Hello, jq_pgx", crate::hello_jq_pgx());
    }
}

#[cfg(test)]
pub mod pg_test {
    pub fn setup(_options: Vec<&str>) {
        // perform one-off initialization when the pg_test framework starts
    }

    pub fn postgresql_conf_options() -> Vec<&'static str> {
        // return any postgresql.conf settings that are required for your tests
        vec![]
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.