Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rnixik committed Jul 29, 2018
1 parent c985c97 commit be6a10e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions csv_writer.go
Expand Up @@ -5,13 +5,15 @@ import (
"strings"
)

// CsvWriter writes data in csv format using FileWriter
type CsvWriter struct {
fw FileWriter
dstFile string
dstDir string
delimiter string
}

// NewCsvWriter builds new CsvWriter
func NewCsvWriter(fw FileWriter, dstFile, dstDir, delimiter string) *CsvWriter {
return &CsvWriter{
fw,
Expand All @@ -21,10 +23,12 @@ func NewCsvWriter(fw FileWriter, dstFile, dstDir, delimiter string) *CsvWriter {
}
}

// WriteDDL is part of interface. It is not usefull for csv.
func (w *CsvWriter) WriteDDL(tableName string, ddl string) (err error) {
return
}

// WriteRows writes result rows in csv format
func (w *CsvWriter) WriteRows(tableName string, columns []string, rows []*map[string]interface{}) (err error) {
f, err := w.fw.getFileHandler(w.getFilename(tableName))
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions filewriter.go
Expand Up @@ -5,19 +5,23 @@ import (
"os"
)

// File is interface which can writes strings to file
type File interface {
WriteString(s string) (n int, err error)
Close() error
}

// FileWriter is interface which can instantiate File
type FileWriter interface {
getFileHandler(filename string) (File, error)
}

// FileWriter writes files using filesystem and methods from OS
type OsFileWriter struct {
openFiles map[string]bool
}

// NewOsFileWriter builds new OsFileWriter
func NewOsFileWriter() *OsFileWriter {
return &OsFileWriter{
make(map[string]bool, 0),
Expand Down
2 changes: 2 additions & 0 deletions query.go
Expand Up @@ -37,6 +37,7 @@ type ConnectionSettings struct {
dbhost string
}

// TableColumnDDL represents result row from DESCRIBE command
type TableColumnDDL struct {
Field string `db:"Field"`
Type string `db:"Type"`
Expand All @@ -46,6 +47,7 @@ type TableColumnDDL struct {
Extra string `db:"Extra"`
}

// Result contains DDL and rows which were got from DB
type Result struct {
DDL *map[string]string
Rows *map[string]([]*map[string]interface{})
Expand Down
1 change: 1 addition & 0 deletions run.go
Expand Up @@ -6,6 +6,7 @@ import (
"os"
)

// Run is entry point for application
func Run(dbConnect dbConnector, argsTail []string, configFile string, format string, fw FileWriter, dstFile string, dstDir string, csvDelimiter string) (err error) {
if len(argsTail) != 2 && len(argsTail) != 3 {
showHelp()
Expand Down
4 changes: 4 additions & 0 deletions sql_writer.go
Expand Up @@ -5,12 +5,14 @@ import (
"strings"
)

// SqlWriter writes data in sql format using FileWriter
type SqlWriter struct {
fw FileWriter
dstFile string
dstDir string
}

// NewSqlWriter builds new SqlWriter
func NewSqlWriter(fw FileWriter, dstFile, dstDir string) *SqlWriter {
return &SqlWriter{
fw,
Expand All @@ -19,6 +21,7 @@ func NewSqlWriter(fw FileWriter, dstFile, dstDir string) *SqlWriter {
}
}

// WriteDDL writes SQL-query which creates tables - DDL
func (w *SqlWriter) WriteDDL(tableName string, ddl string) (err error) {
contents := "SET FOREIGN_KEY_CHECKS=0;\n"
contents += ddl + "\n"
Expand All @@ -35,6 +38,7 @@ func (w *SqlWriter) WriteDDL(tableName string, ddl string) (err error) {
return
}

// WriteRows writes result rows in sql-insert format
func (w *SqlWriter) WriteRows(tableName string, columns []string, rows []*map[string]interface{}) (err error) {
f, err := w.fw.getFileHandler(w.getFilename(tableName))
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions writer.go
Expand Up @@ -4,14 +4,17 @@ import (
"fmt"
)

// DataWriter is interface which can write result somewhere
type DataWriter interface {
WriteRows(tableName string, columns []string, results []*map[string]interface{}) (err error)
WriteDDL(tableName string, ddl string) (err error)
}

// SimpleWriter writes result into stdout using simple format (concatinated values)
type SimpleWriter struct {
}

// WriteRows prints result rows in simple format (concatinated values) into stdout
func (w *SimpleWriter) WriteRows(tableName string, _ []string, rows []*map[string]interface{}) (err error) {
fmt.Println(tableName)
for _, row := range rows {
Expand Down Expand Up @@ -44,6 +47,7 @@ func (w *SimpleWriter) WriteRows(tableName string, _ []string, rows []*map[strin
return nil
}

// WriteDDL prints DDL as is into stdout
func (w *SimpleWriter) WriteDDL(tableName string, ddl string) (err error) {
fmt.Println(ddl)
return nil
Expand Down

0 comments on commit be6a10e

Please sign in to comment.