Giter Club home page Giter Club logo

sql-agent's Introduction

SQL Agent

GoDoc

SQL Agent is an HTTP service for executing ad-hoc queries on remote databases. The motivation for this service is to be part of a data monitoring process or system in which the query results will be evaluated against previous snapshots of the results.

The supported databases are:

  • PostgreSQL
  • MySQL, MariaDB
  • Oracle
  • Microsoft SQL Server
  • SQLite
  • Snowflake
  • Presto

In addition to the service, this repo also defines a sqlagent package for using in other Go programs.

Install

At the moment, it is recommended to run the service using Docker because there are no pre-built binaries yet.

docker run -d -p 5000:5000 dbhi/sql-agent

Usage

To execute a query, simply send a POST request with a payload containing the driver name of the database, connection information to with, and the SQL statement with optional parameters. The service will connect to the database, execute the query and return the results as a JSON-encoded array of maps (see details below).

Request

POST

Headers:

  • 'Accept' - required
    • application/json
    • application/x-ldjson
    • text/csv
{
    "driver": "postgres",
    "connection": {
        "host": "localhost",
        "user": "postgres"
    },
    "sql": "SELECT name FROM users WHERE zipcode = :zipcode",
    "params": {
        "zipcode": 18019
    }
}

Response

[
    {
        "name": "George"
    },
    ...
]

Connection Options

The core option names are standardized for ease of use.

  • host - The host of the database.
  • port - The port of the database.
  • user - The user to connect with.
  • password - The password to authenticate with.
  • database - The name of the database to connect to. For SQLite, this will be a filesystem path. For Oracle, this would be the SID.

Other options that are supplied are passed query options if they are known, otherwise they are they ignored.

Alternatively, the dsn parameter can be specified which will used directly when opening a connection.

Ping Connection

To validate the connection, send a POST with the ?ping query parameter present and it will test the connection only.

Details

  • Only SELECT statements are supported.
  • Statements using parameters must use the :param syntax and must have a corresponding entry in the params map.

Constraints

  • Columns must be uniquely named, otherwise the conversion into a map will include only one of the values.

Library

The SQL Agent library does not include any drivers by default. To add them include them like so:

package main

import (
	_ "github.com/alexbrainman/odbc"
	_ "github.com/denisenkom/go-mssqldb"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/lib/pq"
	_ "github.com/mattn/go-oci8"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
    //...
}

Help

Install Oracle client libraries

General

In order to install the go-oci8 driver, you must install Oracle's client libraries.

Download the instantclient-basic and instantclient-sdk package from Oracle's website and uncompress to the same directory. Make sure that you selected the platform and architecture.

The installations instructions are listed at the bottom of the page with the download links.

Install pkg-config.

Create oci8.pc file in your $PKG_CONFIG_PATH (such as /usr/local/lib/pkgconfig) and add the below contents:

prefix=/usr/local/lib/instantclient_11_2
libdir=${prefix}
includedir=${prefix}/sdk/include/

Name: OCI
Description: Oracle database engine
Version: 11.2
Libs: -L${libdir} -lclntsh
Libs.private:
Cflags: -I${includedir}

Change the prefix to path to location of the Oracle libraries.

OS X specific Help

Assuming the instantclient_11_2 folder is located in /usr/loca/lib, link the following files:

ln /usr/local/lib/instantclient_11_2/libclntsh.dylib /usr/local/lib/libclntsh.dylib
ln /usr/local/lib/instantclient_11_2/libocci.dylib.* /usr/local/lib/libocci.dylib.*
ln /usr/local/lib/instantclient_11_2/libociei.dylib /usr/local/lib/libociei.dylib
ln /usr/local/lib/instantclient_11_2/libnnz11.dylib /usr/local/lib/libnnz11.dylib

Install pkg-config via Homebrew, brew install pkg-config.

sql-agent's People

Contributors

brokenintuition avatar bruth avatar dependabot[bot] avatar gerpsh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sql-agent's Issues

driver.go: mssql semicolon delimited string format

I'm not a Go developer, but I'm trying to get this working with the mssql driver and I'm wondering if there is a typo here in strings.Join:

// MSSQL supports semicolon delimited key=value parameters.
// See https://github.com/denisenkom/go-mssqldb#connection-parameters
"mssql": func(params map[string]interface{}) string {
	toks := make([]string, len(params))
	i := 0

	for k, v := range params {
		toks[i] = fmt.Sprintf("%s=%v", k, v)
		i++
	}

	return strings.Join(toks, " ")
},

The comment above suggests that they should be semicolon delimited, but the strings.Join appears to be space delimiting them. Is that the case?

Cheers

Exits after several minutes

I'm using sql-agent along with prometheus-sql. Sql-agent runs fine - but only for a few minutes, then it exits. I get the following output:

C:\hjmetrics\sql-agent>sql-agent.exe
2017/05/31 17:40:22 main.go:107: * Listening on localhost:5000...
Errors in file :
ORA-21500: internal error code, arguments: [17147], [0x6372098], [], [], [], [], [], []


----- Call Stack Trace -----
calling              call     entry                argument values in hex
location             type     point                (? means dubious value)
-------------------- -------- -------------------- ----------------------------
_skgudmp()+117       CALLrel  _kgdsdst()           68FEFF0 1 59CEB80 61E165D2
                                                   61E1676A 61E165D2
_kgeriv_int()+147    CALLreg  00000000             59CEB80 3
_kgeriv()+22         CALLrel  _kgeriv_int()        59CEB80 0 42FB 0 1 68FF0AC
_kgeric1()+20        CALLrel  _kgeriv()            59CEB80 0 42FB 1 68FF0AC
_kgherror()+223      CALLrel  _kgeric1()           59CEB80 0 42FB 2 6372098 0
__VInfreq__kgh_heap  CALLrel  _kgherror()          59CEB80 6FDA8AC 42FB 6372098
_sizes()+351
_kghrsp()+189        CALLrel  _kghfrmrg()          59CEB80 0 6FDA8AC 6372098 0
_kpuhhrsm()+198      CALLrel  _kghrsp()            59CEB80 6FDA8AC 6FD999C
_kpufhndl0()+529     CALLrel  _kpuhhrsm()          6FDA514 6FD999C
_kpufhndl()+13       CALLrel  _kpufhndl0()         6FDA514 4 0
_OCIHandleFree()+23  CALLrel  _kpufhndl()          6FDA514 4
_OCIHandleFree()+40  CALLptr  00000000             6FDA514 4
009635CD             CALLrel  009F9CF8             6FDA514 4 0 430301 2031EF74
                                                   2033C780
00000000             CALL???  00000000
068FFF31             CALLreg  00000000             6802AA4 6802AA4 0 0 20338280
                                                   0


call stack performance statistics:
total                  : 0.096000 sec
setup                  : 0.067000 sec
stack unwind           : 0.001000 sec
symbol translation     : 0.023000 sec
printing the call stack: 0.005000 sec
printing frame data    : 0.000000 sec
printing argument data : 0.000000 sec


----- End of Call Stack Trace -----

This occurred after about 10 minutes (I'm connecting to an Oracle database)

I've had to build it on Windows 7 using go install since I'm not using envirnment that has Docker available.

Which SSL arguments should I use?

Hello, everyone.
Started to use with prometheus-sql. On database server turned on TLS. (It's impossible to turn off on mysql by requirements)
Getting an error:
[some_query] 503 Service Unavailable: problem connecting to database: Error 3159: Connections using insecure transport are prohibited while --require_secure_transport=ON
In parameters I didn't find any arguments in settings except sslmode: disable. Can you point me please on configuration with SSL enabled?

I tried
sslmode: enable
sslmode: enabled
ssl_ca: /path/to/cert.pem
ssl_cert: /path/to/cert.pem
ssl-ca: /path/to/cert.pem
ssl-cert: /path/to/cert.pem etc.

my basic config:

connection:
        host: sql.apples.com
        port: 3306
        user: sql-agent
        password: superpassword
        database: specialdatabase
        #sslmode: enable?

sql-agent dont respond

Hi.

I use sql-agent on docker with prometheus-sql. Periodicaly sql-agent stuck, only restart is helps.
No errors in log

15.01.2018 18:12:592018/01/15 14:12:59 main.go:107: * Listening on 0.0.0.0:5000...
16.01.2018 11:04:082018/01/16 07:04:08 main.go:107: * Listening on 0.0.0.0:5000...
16.01.2018 11:41:172018/01/16 07:41:17 main.go:107: * Listening on 0.0.0.0:5000...
17.01.2018 11:46:452018/01/17 07:46:45 main.go:107: * Listening on 0.0.0.0:5000...
22.01.2018 11:25:162018/01/22 07:25:16 main.go:107: * Listening on 0.0.0.0:5000...
23.01.2018 11:04:592018/01/23 07:04:59 main.go:107: * Listening on 0.0.0.0:5000...
23.01.2018 11:44:362018/01/23 07:44:36 main.go:107: * Listening on 0.0.0.0:5000...
23.01.2018 18:11:312018/01/23 14:11:31 main.go:107: * Listening on 0.0.0.0:5000...
23.01.2018 22:30:202018/01/23 18:30:20 main.go:107: * Listening on 0.0.0.0:5000...
24.01.2018 14:23:532018/01/24 10:23:53 main.go:107: * Listening on 0.0.0.0:5000...

docker-compose.yml

version: '2'
services:
  sql-agent:
    image: dbhi/sql-agent
    tty: true
    ports:
    - 5000:5000/tcp
    labels:
      io.rancher.container.pull_image: always

Missing dependecy: lib/pq (?)

I'm trying to build this on Windows (I don't have a Docker environment available to me). I'm getting this:

C:\Users\username\go\src\github.com\chop-dbhi\sql-agent\cmd\sql-agent>go install
main.go:15:2: cannot find package "github.com/lib/pq" in any of:
        C:\Users\username\go\src\github.com\chop-dbhi\sql-agent\cmd\sql-agent\vendor\github.com\lib\pq (vendor tree)
        C:\Go\src\github.com\lib\pq (from $GOROOT)
        C:\Users\username\go\src\github.com\lib\pq (from $GOPATH)

If I download the zip of the master branch from https://github.com/lib/pq and put it in C:\Users\username\go\src\github.com\chop-dbhi\sql-agent\cmd\sql-agent\vendor\github.com\lib\pq, then the install completes, apparently successfully.

I don't know if this is happening because of the way I'm building it? I can submit a pull request if you want me to add lib/pq into the /vendor directory - is the current master branch the best one to use?

pq: unknown authentication response

Hello everyone,

I wanted to use the go program in combination with prometheus-sql to monitor a postgres database.
However, I get an error when trying to connect to the database.
following error:
503 Service Unavailable: problem connecting to database: pq: unknown authentication response: 10

Is it possible that the problem occurs due to a postgres go library that is too old in combination with a database version that is too new?

If so, could you please update the Postgres library in the program?

The Postgres go library used in this program is from 2018.

Regards,
xQuai

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.