103 lines
2.9 KiB
Ruby
103 lines
2.9 KiB
Ruby
class Ejdb < Formula
|
|
desc "Embeddable JSON Database engine C11 library"
|
|
homepage "https://ejdb.org"
|
|
url "https://github.com/Softmotions/ejdb/archive/v2.0.58.tar.gz"
|
|
sha256 "5ce47419fa9c6e9629313b24a2a72a6b1f582aeccaa05714cb48878b36336edf"
|
|
license "MIT"
|
|
head "https://github.com/Softmotions/ejdb.git"
|
|
|
|
bottle do
|
|
sha256 cellar: :any, arm64_big_sur: "c0dfbfc5731c6b16e39c79e27e5157687ce1e73268d8c57a2d832b53519c2aea"
|
|
sha256 cellar: :any, big_sur: "f63ee650d9e19612d1b40fec61eefba9b01d572d0225caea37adf84700bc30f9"
|
|
sha256 cellar: :any, catalina: "14e460d2d3c44b2c8cf1995d50431f5f8c0cf85c244082ab52f98a72d40df18d"
|
|
sha256 cellar: :any, mojave: "62739192755aaae8b7f23948be0d01e41c1e10277abe5b580f6c36bd01b21255"
|
|
end
|
|
|
|
depends_on "cmake" => :build
|
|
|
|
def install
|
|
mkdir "build" do
|
|
system "cmake", "..", *std_cmake_args
|
|
system "make", "install"
|
|
end
|
|
end
|
|
|
|
test do
|
|
(testpath/"test.c").write <<~EOS
|
|
#include <ejdb2/ejdb2.h>
|
|
|
|
#define RCHECK(rc_) \\
|
|
if (rc_) { \\
|
|
iwlog_ecode_error3(rc_); \\
|
|
return 1; \\
|
|
}
|
|
|
|
static iwrc documents_visitor(EJDB_EXEC *ctx, const EJDB_DOC doc, int64_t *step) {
|
|
// Print document to stderr
|
|
return jbl_as_json(doc->raw, jbl_fstream_json_printer, stderr, JBL_PRINT_PRETTY);
|
|
}
|
|
|
|
int main() {
|
|
|
|
EJDB_OPTS opts = {
|
|
.kv = {
|
|
.path = "testdb.db",
|
|
.oflags = IWKV_TRUNC
|
|
}
|
|
};
|
|
EJDB db; // EJDB2 storage handle
|
|
int64_t id; // Document id placeholder
|
|
JQL q = 0; // Query instance
|
|
JBL jbl = 0; // Json document
|
|
|
|
iwrc rc = ejdb_init();
|
|
RCHECK(rc);
|
|
|
|
rc = ejdb_open(&opts, &db);
|
|
RCHECK(rc);
|
|
|
|
// First record
|
|
rc = jbl_from_json(&jbl, "{\\"name\\":\\"Bianca\\", \\"age\\":4}");
|
|
RCGO(rc, finish);
|
|
rc = ejdb_put_new(db, "parrots", jbl, &id);
|
|
RCGO(rc, finish);
|
|
jbl_destroy(&jbl);
|
|
|
|
// Second record
|
|
rc = jbl_from_json(&jbl, "{\\"name\\":\\"Darko\\", \\"age\\":8}");
|
|
RCGO(rc, finish);
|
|
rc = ejdb_put_new(db, "parrots", jbl, &id);
|
|
RCGO(rc, finish);
|
|
jbl_destroy(&jbl);
|
|
|
|
// Now execute a query
|
|
rc = jql_create(&q, "parrots", "/[age > :age]");
|
|
RCGO(rc, finish);
|
|
|
|
EJDB_EXEC ux = {
|
|
.db = db,
|
|
.q = q,
|
|
.visitor = documents_visitor
|
|
};
|
|
|
|
// Set query placeholder value.
|
|
// Actual query will be /[age > 3]
|
|
rc = jql_set_i64(q, "age", 0, 3);
|
|
RCGO(rc, finish);
|
|
|
|
// Now execute the query
|
|
rc = ejdb_exec(&ux);
|
|
|
|
finish:
|
|
if (q) jql_destroy(&q);
|
|
if (jbl) jbl_destroy(&jbl);
|
|
ejdb_close(&db);
|
|
RCHECK(rc);
|
|
return 0;
|
|
}
|
|
EOS
|
|
system ENV.cc, "-I#{include}", "test.c", "-L#{lib}", "-lejdb2", "-o", testpath/"test"
|
|
system "./test"
|
|
end
|
|
end
|