Querycrate - A simple library that loads and keeps SQL queries from files

QueryCrate

A simple library for loading & getting string queries from files.

How to install

go get github.com/fakefloordiv/querycrate/tree/master

Docs

Code is placed in qc folder. So import should look like this: import "github.com/fakefloordiv/querycrate/qc".

Library also has it's own queries locators - this is unix-like path to file, including file name, but excluding file extension. For example, path/to/my/query.sql may be gotten from QueryCrate by path/to/my/query locator. Also locator does not starts with a root path


Filters

Filters are just structs that implement interface querycrate.Filter with a single method IsAllowed(file querycrate.File) bool. Library already has 2 simple filters - AllowExtensions(extensions... string) and IgnoreExtensions(extensions... string) (to be honest they're just returning an initialized struct). Filters may stacking, so you can add multiple filters

Usage

import (
  "log"

  querycrate "github.com/fakefloordiv/querycrate/qc"
)

func main() {
  root := "somefolder/"
  qc := querycrate.NewQueryCrate()
  err := qc.FromFolder(root) // by default only .sql files are allowed
  // or querycrate.FromFolder(root, SomeFilter1, SomeFilter2, ...) to add filters
  
  if err != nil {
    log.Fatal("unexpected error:", err)
  }
  
  query, err := qc.Get("somerepo/myquery")
  
  if err != nil {
    if err = qc.AddQuery("somefolder/somerepo/myquery.sql"); err != nil {
      log.Fatal("unexpected error during adding query")
    }
    
    instead := "default query"
    query = qc.GetOr("somefolder/somerepo/myquery", instead).(string)
  }
  
  // For example
  sql.Exec(query)
}

What problems this library solves?

Not all libs provide adequate way to keep queries not directly in the code, but in files with .sql extensions. This small library solves this problem by reading query files recursively and providing a friendly interface to access them. Flexible files filters are allowed, so you can keep any text files here (binary aren't supported as internally using string type for keeping query files content).

Owner
A Really Fake Floordiv Since 4th January, 2022
Pythonyasha & Gonyasha. Web-server dev. HTTP freak. Allocations hater. Really allocations hater. Anti-heap activist
A Really Fake Floordiv Since 4th January, 2022
Similar Resources

pggen - generate type safe Go methods from Postgres SQL queries

pggen - generate type safe Go methods from Postgres SQL queries pggen is a tool that generates Go code to provide a typesafe wrapper around Postgres q

Jan 3, 2023

Go-postgres - go-postgres library provide NoSQL functionality which can execute queries with pool of connections

GO Postgres go-postgres library provide NoSQL functionality which can execute queries with pool of connections. What is this repository for? Establish

Dec 31, 2021

A simple Golang-based application that queries a PostgreSQL database

Qwik-E-Mart Demo App A simple Golang-based application that queries a PostgreSQL database named qwikemart to read and return customer data stored in t

Nov 6, 2021

Go-sql-reader - Go utility to read the externalised sql with predefined tags

go-sql-reader go utility to read the externalised sql with predefined tags Usage

Jan 25, 2022

This Service collects slow queries and returns them in response.

pgsql-api-go This Service collects slow queries and returns them in response. Status This service is the very first version of the project. App is up

Dec 30, 2021

A database connection wrapper to cache prepared statements by transforming queries to use with array arguments.

sqlpp sqlpp is a sql(MySQL and PostgreSQL) database connection wrapper to cache prepared statements by transforming queries ("...in (?)...", []) to us

Feb 9, 2022

A reverse proxy for postgres which rewrites queries.

pg-rewrite-proxy A reverse proxy for postgres which rewrites queries. Arbitrary rewriting is supported by supplying an LUA script to the proxy applica

Dec 12, 2022

Find files with SQL

Find files with SQL

fsql Search through your filesystem with SQL-esque queries. Contents Demo Installation Usage Query Syntax Examples Contribute License Demo Installatio

Jan 8, 2023

Use SQL to data from CSV files. Open source CLI. No DB required.

Use SQL to data from CSV files. Open source CLI. No DB required.

CSV Plugin for Steampipe Use SQL to query data from CSV files. Get started → Documentation: Table definitions & examples Community: Slack Channel Get

Nov 10, 2022
write APIs using direct SQL queries with no hassle, let's rethink about SQL

SQLer SQL-er is a tiny portable server enables you to write APIs using SQL query to be executed when anyone hits it, also it enables you to define val

Jan 7, 2023
Get data from .csv files use SQL-like queries.

csvql Get data from .csv files use SQL-like queries. Задание Необходимо написать консольную программу, которая по заданному клиентом запросу осуществл

Dec 7, 2021
Opionated sql formatter for use with .go files containing backticked queries

fumpt-the-sql Opionated sql formatter for use with .go files containing backticked queries. Uses https://sqlformat.darold.net/ for the actual sql form

Dec 10, 2021
CloudQuery extracts, transforms, and loads your cloud assets into normalized PostgreSQL tables.
CloudQuery extracts, transforms, and loads your cloud assets into normalized PostgreSQL tables.

The open-source cloud asset inventory backed by SQL. CloudQuery extracts, transforms, and loads your cloud assets into normalized PostgreSQL tables. C

Dec 31, 2022
SQL API is designed to be able to run queries on databases without any configuration by simple HTTP call.

SQL API SQL API is designed to be able to run queries on databases without any configuration by simple HTTP call. The request contains the DB credenti

Dec 2, 2022
A tool to run queries in defined frequency and expose the count as prometheus metrics. Supports MongoDB and SQL
A tool to run queries in defined frequency and expose the count as prometheus metrics. Supports MongoDB and SQL

query2metric A tool to run db queries in defined frequency and expose the count as prometheus metrics. Why ? Product metrics play an important role in

Jul 1, 2022
CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. Can output to various formats.
CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. Can output to various formats.

trdsql CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. It is a tool like q, textql and others. The difference from these tools is t

Jan 1, 2023
sqlbench measures and compares the execution time of one or more SQL queries.

sqlbench sqlbench measures and compares the execution time of one or more SQL queries. The main use case is benchmarking simple CPU-bound query varian

Dec 6, 2022
Run SQL queries against JSON, CSV, Excel, Parquet, and more.

Run SQL queries against JSON, CSV, Excel, Parquet, and more This is a CLI companion to DataStation (a GUI) for running SQL queries against data files.

Dec 31, 2022
Mergestat - a command-line tool for running SQL queries on git repositories and related data sources
Mergestat - a command-line tool for running SQL queries on git repositories and related data sources

Query git repositories with SQL. Generate reports, perform status checks, analyze codebases. ?? ??

Dec 30, 2022