csvpg

package module
v0.0.0-...-2f75852 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2019 License: BSD-2-Clause Imports: 8 Imported by: 0

README

csvpg

Generate Postgresql tables from CSV files

csvpg name.csv

csvpg parses a CSV file and prints a PostgreSQL syntax "create table" statement into which the CSV can be imported.

By default it assumes that the first row of the CSV file is a header, and uses that to generate column names for the table. It then looks at the contents of each column to find an appropriate SQL type for it.

PostgreSQL supports NULL columns in CSV input, denoted by a zero length unquoted field. The CSV parser csvpg uses doesn't record whether a field is quoted or not, but if a zero length field is found then that SQL field will be generated as nullable.

There is untested support for generating enums for columns with a small number of values in it, disabled by default.

Usage

Usage of ./csvpg:
  -columns string
    	comma-separated list of column names
  -copy
    	include psql \copy command (default true)
  -enums
    	consider enum as a type
  -exclude string
    	exclude this comma-separated list of SQL types from consideration
  -header
    	treat the first row of the file as a header (default true)
  -notnull
    	check for not null (default true)
  -sample int
    	consider only this many rows of the input
  -snake
    	convert field names to snake_case (default true)
  -table string
    	name of the generated table

Installation

With Go >= 1.11 installed, run "go build" from the cmd/csvpg subdirectory.

Or download a pre-built binary

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func QuoteIdent

func QuoteIdent(s string) string

QuoteIdent attempts to quote a postgresql identifier

func QuoteName

func QuoteName(s string) string

QuoteName attempts to quote a postgresql identifier or keyword

Types

type Config

type Config struct {
	SnakeCase   bool
	Sample      int
	ColumnNames []string
	NotNull     bool
	ReadHeader  bool
	EnumLength  int
	EnumCount   int
	Types       []Type
	Exclude     []string
	TableName   string
}

Config for csvgpg

func NewConfig

func NewConfig() Config

NewConfig generates a default configuration

type Intuitor

type Intuitor struct {
	Config        *Config
	ColumnNames   []string
	PossibleTypes []int
	Enums         []map[string]struct{}
	ColumnTypes   []string
	Null          []bool

	EnumDDL  string
	TableDDL string
	// contains filtered or unexported fields
}

Intuitor intuits the types of the input

func NewIntuitor

func NewIntuitor(c *Config) *Intuitor

NewIntuitor creates a new Intuitor

func (*Intuitor) Intuit

func (c *Intuitor) Intuit(reader RowReader) error

Intuit does the work of identifying column types

type RowReader

type RowReader interface {
	Read() ([]string, error)
}

RowReader reads a single entry from, e.g., a CSV file

type Type

type Type struct {
	Name    string
	Pattern string
	Isa     func(string) bool
	Handle  func(string, int, *Intuitor) bool
	// contains filtered or unexported fields
}

Type holds the config info about each database type

Directories

Path Synopsis
cmd
csvpg command

Jump to

Keyboard shortcuts

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