sql2csv

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2023 License: GPL-3.0 Imports: 9 Imported by: 1

README

sql2csv

GoDoc Quality Test Release License

Go library for generating csv files based on SQL queries

Only supporting PostgreSQL

Usage

package main

import (
	"context"
	"github.com/frederikhs/sql2csv"
)

const (
	host     = "db.example.com"
	port     = "5432"
	user     = "example"
	password = "example"
	dbname   = "example"
)

func main() {
	conn, err := sql2csv.NewConnection(context.Background(), user, password, host, port, dbname)
	if err != nil {
		panic(err)
	}
	defer conn.Close(context.Background())

	query, err := sql2csv.NewQuery(context.Background(), "SELECT * FROM public.users LIMIT $1", 10)
	if err != nil {
		panic(err)
	}

	// writes results to ./results.csv
	err = conn.WriteQuery(query, "results.csv")
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TryDomain = "hiper.dk"

TryDomain is current just set to hiper.dk since I work at hiper

Functions

func NewConnectionString added in v0.0.2

func NewConnectionString(user, pass, host, port, dbname string) string

Types

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

func NewConnection

func NewConnection(ctx context.Context, connectiongString string) (*Connection, error)

NewConnection creates a new connection object

func NewConnectionFromPgPass added in v0.0.2

func NewConnectionFromPgPass(ctx context.Context, hostQuery string) (*Connection, error)

func (*Connection) Close

func (c *Connection) Close(ctx context.Context) error

func (*Connection) WriteQuery

func (c *Connection) WriteQuery(ctx context.Context, query *Query, path string, loggerFn func(ln string)) error

WriteQuery writes the results of executing the query to a csv file at the path given

type Query

type Query struct {
	// contains filtered or unexported fields
}

func NewQuery

func NewQuery(sql string, args ...any) (*Query, error)

NewQuery create a new query object for later use when executing a query against the database

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL