Skip to content

Commit

Permalink
Modify variable name
Browse files Browse the repository at this point in the history
Add and modify comments.
  • Loading branch information
noborus committed Jun 10, 2019
1 parent 97dd4ff commit b645d9c
Show file tree
Hide file tree
Showing 21 changed files with 237 additions and 193 deletions.
2 changes: 1 addition & 1 deletion csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func TestCsvIndefiniteInputFile3(t *testing.T) {
}

func TestCsvOutNew(t *testing.T) {
out := NewCSVWrite(NewWriteOpts())
out := NewCSVWriter(NewWriteOpts())
if out == nil {
t.Error(`csvOut error`)
}
Expand Down
38 changes: 18 additions & 20 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,15 @@ func (db *DB) Select(query string) (*sql.Rows, error) {

// Table is import Table data
type Table struct {
tableName string
columnNames []string
columns []string
query string
place string
maxCap int
preRead int
row []interface{}
lastCount int
count int
tableName string
columns []string
query string
place string
maxCap int
preRead int
row []interface{}
lastCount int
count int
}

// Import is import to the table.
Expand All @@ -105,13 +104,12 @@ func (db *DB) Import(tableName string, columnNames []string, reader Reader, preR
}
row := make([]interface{}, len(columnNames))
table := &Table{
tableName: tableName,
columnNames: columnNames,
columns: columns,
preRead: preRead,
row: row,
lastCount: 0,
count: 0,
tableName: tableName,
columns: columns,
preRead: preRead,
row: row,
lastCount: 0,
count: 0,
}
if db.driver == "postgres" {
err = db.copyImport(table, reader)
Expand Down Expand Up @@ -167,7 +165,7 @@ func (db *DB) insertImport(table *Table, reader Reader) error {
defer db.stmtClose(stmt)
// #nosec G202
table.query = "INSERT INTO " + table.tableName + " (" + strings.Join(table.columns, ",") + ") VALUES "
table.place = "(" + strings.Repeat("?,", len(table.columnNames)-1) + "?)"
table.place = "(" + strings.Repeat("?,", len(table.columns)-1) + "?)"
table.maxCap = (db.maxBulk / len(table.row)) * len(table.row)
bulk := make([]interface{}, 0, table.maxCap)

Expand Down Expand Up @@ -263,8 +261,8 @@ func (db *DB) insertPrepare(table *Table) (*sql.Stmt, error) {
return stmt, nil
}

// EscapeTable is escape table name.
func (db *DB) EscapeTable(oldName string) string {
// EscapeName is escape table name.
func (db *DB) EscapeName(oldName string) string {
var newName string
if oldName[0] != db.escape[0] {
newName = db.escape + oldName + db.escape
Expand Down
12 changes: 6 additions & 6 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,23 @@ func TestRewrite(t *testing.T) {
}
}

func TestEscapetable(t *testing.T) {
func TestEscapeName(t *testing.T) {
db, err := Connect("sqlite3", "")
if err != nil {
t.Fatal("Escapetable error")
t.Fatal("EscapeName error")
}
defer func() {
err = db.Disconnect()
if err != nil {
t.Fatalf("Disconnect error")
}
}()
es := db.EscapeTable("test.csv")
es := db.EscapeName("test.csv")
if es != "`test.csv`" {
t.Fatalf("Escapetable error %s", es)
t.Fatalf("EscapeName error %s", es)
}
es = db.EscapeTable("`test.csv`")
es = db.EscapeName("`test.csv`")
if es != "`test.csv`" {
t.Fatalf("Escapetable error %s", es)
t.Fatalf("EscapeName error %s", es)
}
}
3 changes: 3 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package trdsql

import "log"

// debugT is a type of debug flag.
type debugT bool

// debug is a flag for detailed output.
var debug = debugT(false)

// EnableDebug is enable verbose output for debug.
func EnableDebug() {
debug = true
}
Expand Down
42 changes: 11 additions & 31 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"unicode/utf8"
)

// Exporter is the interface for processing query results.
type Exporter interface {
Export(db *DB, query string) error
}

// WriteOpts is the option to determine the writer process.
type WriteOpts struct {
OutFormat Format
OutDelimiter string
Expand All @@ -23,6 +25,7 @@ type WriteOpts struct {
ErrStream io.Writer
}

// NewWriteOpts Returns WriteOpts.
func NewWriteOpts() WriteOpts {
return WriteOpts{
OutDelimiter: ",",
Expand All @@ -32,20 +35,23 @@ func NewWriteOpts() WriteOpts {
}
}

type exporter struct {
// WriteFormat is a structure that includes Writer and WriteOpts,
// and is an implementation of the Exporter interface.
type WriteFormat struct {
WriteOpts
Writer
}

func NewExporter(writeOpts WriteOpts, writer Writer) *exporter {
return &exporter{
// NewExporter returns trdsql default Exporter.
func NewExporter(writeOpts WriteOpts, writer Writer) *WriteFormat {
return &WriteFormat{
WriteOpts: writeOpts,
Writer: writer,
}
}

// Export is execute SQL and Exporter the result.
func (e *exporter) Export(db *DB, query string) error {
func (e *WriteFormat) Export(db *DB, query string) error {
rows, err := db.Select(query)
if err != nil {
return err
Expand Down Expand Up @@ -93,33 +99,7 @@ func (e *exporter) Export(db *DB, query string) error {
return e.Writer.PostWrite()
}

func ConvertTypes(dbTypes []string) []string {
ret := make([]string, len(dbTypes))
for i, t := range dbTypes {
ret[i] = convertType(t)
}
return ret
}

func convertType(dbType string) string {
switch strings.ToLower(dbType) {
case "smallint", "integer", "int", "int2", "int4", "smallserial", "serial":
return "int"
case "bigint", "int8", "bigserial":
return "bigint"
case "float", "decimal", "numeric", "real", "double precision":
return "numeric"
case "bool":
return "bool"
case "timestamp", "timestamptz", "date", "time":
return "timestamp"
case "string", "text", "char", "varchar":
return "text"
default:
return "text"
}
}

// ValString converts database value to string.
func ValString(v interface{}) string {
var str string
switch t := v.(type) {
Expand Down
33 changes: 26 additions & 7 deletions importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"strings"
)

// Importer is the interface import data into the database.
type Importer interface {
Import(db *DB, query string) (string, error)
}

// ReadOpts option to determine reader.
type ReadOpts struct {
InFormat Format
InPreRead int
Expand All @@ -23,6 +25,7 @@ type ReadOpts struct {
InHeader bool
}

// NewReadOpts Returns ReadOpts.
func NewReadOpts() ReadOpts {
return ReadOpts{
InDelimiter: ",",
Expand All @@ -32,20 +35,26 @@ func NewReadOpts() ReadOpts {
}
}

type importer struct {
// ReadFormat is a structure that includes ReadOpts,
// and is an implementation of the Importer interface.
type ReadFormat struct {
ReadOpts
}

func NewImporter(readOpts ReadOpts) *importer {
return &importer{
// NewImporter returns trdsql default Importer.
func NewImporter(readOpts ReadOpts) *ReadFormat {
return &ReadFormat{
ReadOpts: readOpts,
}
}

// DefaultDBType is default type.
const DefaultDBType = "text"

// Import is parses the SQL statement and imports one or more tables.
// Return the rewritten SQL and error.
// No error is returned if there is no table to import.
func (i *importer) Import(db *DB, query string) (string, error) {
func (i *ReadFormat) Import(db *DB, query string) (string, error) {
tables := listTable(query)
if len(tables) == 0 {
// without FROM clause. ex. SELECT 1+1;
Expand Down Expand Up @@ -160,7 +169,12 @@ func ImportFile(db *DB, fileName string, opts ReadOpts) (string, error) {
debug.Printf("%s\n", err)
return "", nil
}
defer file.Close()
defer func() {
err = file.Close()
if err != nil {
log.Printf("file close:%s", err)
}
}()

if opts.InFormat == GUESS {
opts.InFormat = guessExtension(fileName)
Expand All @@ -170,7 +184,7 @@ func ImportFile(db *DB, fileName string, opts ReadOpts) (string, error) {
return "", err
}

tableName := db.EscapeTable(fileName)
tableName := db.EscapeName(fileName)
columnNames, err := reader.GetColumn(opts.InPreRead)
if err != nil {
if err != io.EOF {
Expand Down Expand Up @@ -257,7 +271,12 @@ func globFileOpen(globName string) (*io.PipeReader, error) {
}
pipeReader, pipeWriter := io.Pipe()
go func() {
defer pipeWriter.Close()
defer func() {
err = pipeWriter.Close()
if err != nil {
log.Printf("pipe close:%s", err)
}
}()
for _, fileName := range fileNames {
f, err := os.Open(fileName)
debug.Printf("Open: [%s]", fileName)
Expand Down
17 changes: 9 additions & 8 deletions input_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ import (
"strconv"
)

// CSVRead provides methods of the Reader interface
type CSVRead struct {
// CSVReader provides methods of the Reader interface.
type CSVReader struct {
reader *csv.Reader
names []string
types []string
preRead [][]string
inHeader bool
}

func NewCSVReader(reader io.Reader, opts ReadOpts) (Reader, error) {
// NewCSVReader returns CSVReader and error.
func NewCSVReader(reader io.Reader, opts ReadOpts) (*CSVReader, error) {
var err error

if opts.InHeader {
opts.InPreRead--
}
r := &CSVRead{}
r := &CSVReader{}
r.reader = csv.NewReader(reader)
r.reader.LazyQuotes = true
r.reader.FieldsPerRecord = -1 // no check count
Expand Down Expand Up @@ -60,7 +61,7 @@ func delimiter(sepString string) (rune, error) {

// GetColumn is reads the specified number of rows and determines the column name.
// The previously read row is stored in preRead.
func (r *CSVRead) GetColumn(rowNum int) ([]string, error) {
func (r *CSVReader) GetColumn(rowNum int) ([]string, error) {
// Header
if r.inHeader {
row, err := r.reader.Read()
Expand Down Expand Up @@ -95,7 +96,7 @@ func (r *CSVRead) GetColumn(rowNum int) ([]string, error) {
}

// GetTypes is reads the specified number of rows and determines the column type.
func (r *CSVRead) GetTypes() ([]string, error) {
func (r *CSVReader) GetTypes() ([]string, error) {
r.types = make([]string, len(r.names))
for i := 0; i < len(r.names); i++ {
r.types[i] = DefaultDBType
Expand All @@ -104,7 +105,7 @@ func (r *CSVRead) GetTypes() ([]string, error) {
}

// PreReadRow is returns only columns that store preread rows.
func (r *CSVRead) PreReadRow() [][]interface{} {
func (r *CSVReader) PreReadRow() [][]interface{} {
rowNum := len(r.preRead)
rows := make([][]interface{}, rowNum)
for n := 0; n < rowNum; n++ {
Expand All @@ -117,7 +118,7 @@ func (r *CSVRead) PreReadRow() [][]interface{} {
}

// ReadRow is read the rest of the row.
func (r *CSVRead) ReadRow(row []interface{}) ([]interface{}, error) {
func (r *CSVReader) ReadRow(row []interface{}) ([]interface{}, error) {
record, err := r.reader.Read()
if err != nil {
return row, err
Expand Down
Loading

0 comments on commit b645d9c

Please sign in to comment.