github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/ext/csv/schema.go (about)

     1  package csv
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/ncruces/go-sqlite3"
     8  )
     9  
    10  func getSchema(header bool, columns int, row []string) string {
    11  	var sep string
    12  	var str strings.Builder
    13  	str.WriteString("CREATE TABLE x(")
    14  
    15  	if 0 <= columns && columns < len(row) {
    16  		row = row[:columns]
    17  	}
    18  	for i, f := range row {
    19  		str.WriteString(sep)
    20  		if header && f != "" {
    21  			str.WriteString(sqlite3.QuoteIdentifier(f))
    22  		} else {
    23  			str.WriteString("c")
    24  			str.WriteString(strconv.Itoa(i + 1))
    25  		}
    26  		str.WriteString(" TEXT")
    27  		sep = ","
    28  	}
    29  	for i := len(row); i < columns; i++ {
    30  		str.WriteString(sep)
    31  		str.WriteString("c")
    32  		str.WriteString(strconv.Itoa(i + 1))
    33  		str.WriteString(" TEXT")
    34  		sep = ","
    35  	}
    36  	str.WriteByte(')')
    37  
    38  	return str.String()
    39  }