Example #1
0
func LoadTestDataAndExecuteQuery(t *testing.T, testData string, sqlString string) (map[int]map[string]interface{}, []string) {
	storage := NewSQLite3Storage(storageOpts)
	fp := test_util.OpenFileFromString(testData)

	opts := &inputs.CSVInputOptions{
		HasHeader: true,
		Seperator: ',',
		ReadFrom:  fp,
	}

	input, _ := inputs.NewCSVInput(opts)
	defer fp.Close()
	defer os.Remove(fp.Name())
	defer storage.Close()

	storage.LoadInput(input)

	rows, rowsErr := storage.ExecuteSQLString(sqlString)

	if rowsErr != nil {
		t.Fatalf(rowsErr.Error())
	}

	cols, colsErr := rows.Columns()

	if colsErr != nil {
		t.Fatalf(colsErr.Error())
	}

	rowNumber := 0
	result := make(map[int]map[string]interface{})
	rawResult := make([]interface{}, len(cols))
	dest := make([]interface{}, len(cols))

	for i := range cols {
		dest[i] = &rawResult[i]
	}

	for rows.Next() {
		scanErr := rows.Scan(dest...)

		if scanErr != nil {
			t.Fatalf(scanErr.Error())
		}

		result[rowNumber] = make(map[string]interface{})
		for i, raw := range rawResult {
			result[rowNumber][cols[i]] = raw
		}
		rowNumber++
	}

	return result, cols
}
Example #2
0
func NewTestCSVInput() (input inputs.Input, fp *os.File) {
	fp = test_util.OpenFileFromString(simpleCSV)

	opts := &inputs.CSVInputOptions{
		HasHeader: true,
		Seperator: ',',
		ReadFrom:  fp,
	}

	newInput, _ := inputs.NewCSVInput(opts)
	return newInput, fp
}
Example #3
0
func main() {
	cmdLineOpts := newCommandLineOptions()
	var outputer outputs.Output

	if cmdLineOpts.GetVersion() {
		fmt.Println(VERSION)
		os.Exit(0)
	}

	if len(cmdLineOpts.GetSourceFiles()) == 0 && !util.IsThereDataOnStdin() {
		cmdLineOpts.Usage()
	}

	if cmdLineOpts.GetQuiet() {
		log.SetOutput(ioutil.Discard)
	}

	if cmdLineOpts.GetConsole() {
		if util.IsThereDataOnStdin() {
			log.Fatalln("Can not open console with pipe input, read a file instead")
		}
		_, sqlite3ConsolePathErr := exec.LookPath("sqlite3")
		if sqlite3ConsolePathErr != nil {
			log.Fatalln("Console requested but unable to locate `sqlite3` program on $PATH")
		}
	}

	var inputSources []string

	if util.IsThereDataOnStdin() {
		inputSources = append(inputSources, "stdin")
	}

	for _, sourceFile := range cmdLineOpts.GetSourceFiles() {
		if util.IsPathDir(sourceFile) {
			for _, file := range util.AllFilesInDirectory(sourceFile) {
				inputSources = append(inputSources, file)
			}
		} else {
			inputSources = append(inputSources, sourceFile)
		}
	}

	storage := storage.NewSQLite3StorageWithDefaults()

	for _, file := range inputSources {
		fp := util.OpenFileOrStdDev(file, false)

		inputOpts := &inputs.CSVInputOptions{
			HasHeader: cmdLineOpts.GetHeader(),
			Seperator: util.DetermineSeparator(cmdLineOpts.GetDelimiter()),
			ReadFrom:  fp,
		}

		input, inputErr := inputs.NewCSVInput(inputOpts)

		if inputErr != nil {
			log.Printf("Unable to load %v\n", file)
		}

		storage.LoadInput(input)
	}

	sqlStrings := strings.Split(cmdLineOpts.GetStatements(), ";")

	if cmdLineOpts.GetOutputFile() != "" {
		if cmdLineOpts.GetPretty() {
			displayOpts := &outputs.PrettyCSVOutputOptions{
				WriteHeader: cmdLineOpts.GetOutputHeader(),
				WriteTo:     util.OpenFileOrStdDev(cmdLineOpts.GetOutputFile(), true),
			}

			outputer = outputs.NewPrettyCSVOutput(displayOpts)
		} else {
			displayOpts := &outputs.CSVOutputOptions{
				WriteHeader: cmdLineOpts.GetOutputHeader(),
				Seperator:   util.DetermineSeparator(cmdLineOpts.GetOutputDelimiter()),
				WriteTo:     util.OpenFileOrStdDev(cmdLineOpts.GetOutputFile(), true),
			}

			outputer = outputs.NewCSVOutput(displayOpts)
		}
	}

	for _, sqlQuery := range sqlStrings {
		queryResults := storage.ExecuteSQLString(sqlQuery)

		if queryResults != nil && cmdLineOpts.GetOutputFile() != "" {
			outputer.Show(queryResults)
		}
	}

	if cmdLineOpts.GetSaveTo() != "" {
		storage.SaveTo(cmdLineOpts.GetSaveTo())
	}

	if cmdLineOpts.GetConsole() {
		var args []string

		if cmdLineOpts.GetOutputHeader() {
			args = []string{"-header"}
		} else {
			args = []string{}
		}

		if cmdLineOpts.GetSaveTo() != "" {
			args = append(args, cmdLineOpts.GetSaveTo())
		} else {
			tempFile, err := ioutil.TempFile(os.TempDir(), "textql")
			if err != nil {
				log.Fatalln(err)
			}
			defer os.Remove(tempFile.Name())
			tempFile.Close()
			saveErr := storage.SaveTo(tempFile.Name())

			if saveErr != nil {
				log.Fatalln(saveErr)
			}

			args = append(args, tempFile.Name())
		}

		cmd := exec.Command("sqlite3", args...)

		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		cmdErr := cmd.Run()

		if cmd.Process != nil {
			cmd.Process.Release()
		}

		if cmdErr != nil {
			log.Fatalln(cmdErr)
		}
	} else {
		storage.Close()
	}
}