sql2csv

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2023 License: GPL-3.0 Imports: 7 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 := sql2csv.NewQuery(context.Background(), "SELECT * FROM public.users LIMIT $1", 10)

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

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

func NewConnection

func NewConnection(ctx context.Context, user, pass, host, port, dbname string) (*Connection, error)

NewConnection creates a new connection object

func (*Connection) Close

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

func (*Connection) WriteQuery

func (c *Connection) WriteQuery(query *Query, path 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(ctx context.Context, sql string, args ...any) *Query

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