csvutil

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 6 Imported by: 0

README

csvutil

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TransferCSVFileChunked

func TransferCSVFileChunked[T any](ginContext *gin.Context, dbconn *gorm.DB, query, filename string, chunkSize int) error

TransferCSVFileChunked is a function that responses a large csv file . The function works as follows: 1. Initializes the chunker and cursor. 2. Writes the header and the label of the CSV file. 3. Fetches the data from the cursor and writes the data in chunks. 4. Resets the writer. Need to gin.Context and gorm.DB to use this function. The query is the SQL query to be executed, and the filename is the name of the file to be downloaded. The fetchSize is the number of rows to be fetched at a time.

func TransferChunk

func TransferChunk[T any](chunker *Chunker[T], cursor *Cursor[T]) error

TransferChunk is a function that works as follows: 1. Fetches the data from the cursor. 2. Writes the data in chunks. 3. Resets the writer. Need to Chunker and Cursor to use this function.

Types

type Chunker

type Chunker[T any] struct {
	GinCtx *gin.Context
	Writer *csv.Writer
}

func NewChunker

func NewChunker[T any](ginContext *gin.Context) *Chunker[T]

func (*Chunker[T]) ResetWriter

func (chunker *Chunker[T]) ResetWriter()

ResetWriter resets the csv writer.

func (*Chunker[T]) SetHeader

func (chunker *Chunker[T]) SetHeader(filename string)

SetHeader sets the response header for the CSV file. The filename is the name of the file to be downloaded. The default encoding is UTF-8. Transfer-Encoding is set to chunked.

func (*Chunker[T]) WriteChunk

func (chunker *Chunker[T]) WriteChunk(data []T) error

WriteChunk writes the data in chunks.

func (*Chunker[T]) WriteCsvLabel

func (chunker *Chunker[T]) WriteCsvLabel()

WriteCsvLabel writes the Label of the CSV data.

type Cursor

type Cursor[T any] struct {
	DBconn    *gorm.DB
	Query     string
	FetchSize int
}

func NewCursor

func NewCursor[T any](dbconn *gorm.DB, query string, fetchSize int) (*Cursor[T], error)

func (*Cursor[T]) Close

func (cursor *Cursor[T]) Close() error

func (*Cursor[T]) FetchCursor

func (cursor *Cursor[T]) FetchCursor() ([]T, error)

Jump to

Keyboard shortcuts

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