Example #1
0
File: main.go Project: rwcarlsen/ql
func do() (err error) {
	oDB := flag.String("db", "ql.db", "The DB file to open. It'll be created if missing")
	oFlds := flag.Bool("fld", false, "Show recordset's field names.")
	flag.Parse()

	var src string
	switch n := flag.NArg(); n {
	case 0:
		b, err := ioutil.ReadAll(bufio.NewReader(os.Stdin))
		if err != nil {
			return err
		}

		src = string(b)
	default:
		a := make([]string, n)
		for i := range a {
			a[i] = flag.Arg(i)
		}
		src = strings.Join(a, " ")
	}

	db, err := ql.OpenFile(*oDB, &ql.Options{CanCreate: true})
	if err != nil {
		return err
	}

	defer func() {
		ec := db.Close()
		switch {
		case ec != nil && err != nil:
			log.Println(ec)
		case ec != nil:
			err = ec
		}
	}()

	src = "BEGIN TRANSACTION; " + src + "; COMMIT;"
	l, err := ql.Compile(src)
	if err != nil {
		log.Println(src)
		return err
	}

	rs, i, err := db.Execute(ql.NewRWCtx(), l)
	if err != nil {
		a := strings.Split(strings.TrimSpace(fmt.Sprint(l)), "\n")
		return fmt.Errorf("%v: %s", err, a[i])
	}

	if len(rs) == 0 {
		return
	}

	return rs[len(rs)-1].Do(*oFlds, func(data []interface{}) (bool, error) {
		fmt.Println(str(data))
		return true, nil
	})
}
Example #2
0
File: db.go Project: 5Sigma/Conduit
func OpenDB() {
	var err error
	shouldCreate := false
	directory, _ := osext.ExecutableFolder()
	path := filepath.Join(directory, "mailboxes.db")
	if _, err := os.Stat(path); os.IsNotExist(err) {
		shouldCreate = true
	}
	DB, err = ql.OpenFile(path, &ql.Options{CanCreate: true})
	if err != nil {
		fmt.Println("Could not open mailbox database.")
		os.Exit(-1)
	}
	if shouldCreate {
		err := CreateDB()
		if err != nil {
			panic(err)
		}
	}
	err = migrateDatabase()
	if err != nil {
		panic(err)
	}
}
Example #3
0
File: mkdb.go Project: gernest/goat
func main() {

	if "" != *flagDbName {
		db, err = ql.OpenFile(*flagDbName, &ql.Options{CanCreate: true})
	} else {
		db, err = ql.OpenMem()
	}
	if nil != err {
		log.Fatalln(err.Error())
	}
	defer db.Close()

	basedir := ""
	if basedir = os.Getenv("GOPATH"); basedir != "" {
		basedir += "/src/github.com/mdlayher/goat/res/ql/"
	}
	files, err := filepath.Glob(basedir + "*.ql")
	if nil != err {
		log.Fatalln(err.Error())
	}

	ctx := ql.NewRWCtx()
	for _, file := range files {
		fmt.Println("Reading", file)
		q, err := ioutil.ReadFile(file)
		if nil != err {
			log.Panicln(err.Error())
		}
		if _, _, err = db.Run(ctx, string(q)); nil != err {
			log.Panicln(err.Error())
		}
	}

	info, err := db.Info()
	fmt.Printf("%#v Error=%s\n", info, err)
}
Example #4
0
// init performs startup routines for database_ql
func init() {
	// DBConnectFunc connects to ql database file
	DBConnectFunc = func() (dbModel, error) {
		if nil == qlwdb {
			// Database name
			name := "goat.db"

			// Load current user from OS, to get home directory
			var path string
			user, err := user.Current()
			if err != nil {
				log.Println(err.Error())
				path = "./"
			} else {
				// Store config in standard location
				path = user.HomeDir + "/.config/goat/"
			}

			// Allow manul override of db path, if flag is set
			if QLDBPath != nil && *QLDBPath != "" {
				// Split db path into path and filename
				path = ospath.Dir(*QLDBPath) + "/"
				name = ospath.Base(*QLDBPath)
			}

			log.Println("Loading ql database: " + path + name)

			// Check file existence
			_, err = os.Stat(path + name)
			if err != nil {
				if os.IsNotExist(err) {
					log.Println("Could not find ql database, attempting to create it...")

					err = os.MkdirAll(path, 0775)
					if err != nil {
						log.Println("Failed to create directory: " + path)
					}

					// Attempt to copy database file to home directory
					source, err := os.Open("./res/ql/" + name)
					if err != nil {
						log.Println("Failed to read source file: " + name)
					}

					// Open destination file
					dest, err := os.Create(path + name)
					if err != nil {
						log.Println("Failed to create destination file: " + path + name)
					}

					// Copy contents
					_, err = io.Copy(dest, source)
					if err != nil {
						log.Println("Failed to copy to database file: " + path + name)
					}

					// Close files
					source.Close()
					dest.Close()
				}
			}

			db, err := ql.OpenFile(path+name, &qlOptions)
			if err != nil {
				return nil, err
			}

			qlwdb = &qlw{db}
		}

		return qlwdb, nil
	}

	// Generate connection string using configuration
	DBCloseFunc = func() {
		if nil != qlwdb {
			log.Println("Closing ql database")
			qlwdb.Close()
		}
	}

	// DBNameFunc returns the name of this backend
	DBNameFunc = func() string {
		return "ql"
	}
}
Example #5
0
File: main.go Project: farss/ql
func do() (err error) {
	oDB := flag.String("db", "ql.db", "The DB file to open. It'll be created if missing.")
	oFlds := flag.Bool("fld", false, "Show recordset's field names.")
	oSchema := flag.String("schema", "", "If non empty, show the CREATE statements of matching tables and exit.")
	oTables := flag.String("tables", "", "If non empty, list matching table names and exit.")
	flag.Parse()

	db, err := ql.OpenFile(*oDB, &ql.Options{CanCreate: true})
	if err != nil {
		return err
	}

	defer func() {
		ec := db.Close()
		switch {
		case ec != nil && err != nil:
			log.Println(ec)
		case ec != nil:
			err = ec
		}
	}()

	if pat := *oSchema; pat != "" {
		re, err := regexp.Compile(pat)
		if err != nil {
			return err
		}

		nfo, err := db.Info()
		if err != nil {
			return err
		}

		r := []string{}
		for _, ti := range nfo.Tables {
			if !re.MatchString(ti.Name) {
				continue
			}

			a := []string{}
			for _, ci := range ti.Columns {
				a = append(a, fmt.Sprintf("%s %s", ci.Name, ci.Type))
			}
			r = append(r, fmt.Sprintf("CREATE TABLE %s (%s);", ti.Name, strings.Join(a, ", ")))
		}
		sort.Strings(r)
		if len(r) != 0 {
			fmt.Println(strings.Join(r, "\n"))
		}
		return nil
	}

	if pat := *oTables; pat != "" {
		re, err := regexp.Compile(pat)
		if err != nil {
			return err
		}

		nfo, err := db.Info()
		if err != nil {
			return err
		}

		r := []string{}
		for _, ti := range nfo.Tables {
			if !re.MatchString(ti.Name) {
				continue
			}

			r = append(r, ti.Name)
		}
		sort.Strings(r)
		if len(r) != 0 {
			fmt.Println(strings.Join(r, "\n"))
		}
		return nil
	}

	var src string
	switch n := flag.NArg(); n {
	case 0:
		b, err := ioutil.ReadAll(bufio.NewReader(os.Stdin))
		if err != nil {
			return err
		}

		src = string(b)
	default:
		a := make([]string, n)
		for i := range a {
			a[i] = flag.Arg(i)
		}
		src = strings.Join(a, " ")
	}

	src = "BEGIN TRANSACTION; " + src + "; COMMIT;"
	l, err := ql.Compile(src)
	if err != nil {
		log.Println(src)
		return err
	}

	rs, i, err := db.Execute(ql.NewRWCtx(), l)
	if err != nil {
		a := strings.Split(strings.TrimSpace(fmt.Sprint(l)), "\n")
		return fmt.Errorf("%v: %s", err, a[i])
	}

	if len(rs) == 0 {
		return
	}

	return rs[len(rs)-1].Do(*oFlds, func(data []interface{}) (bool, error) {
		fmt.Println(str(data))
		return true, nil
	})
}
Example #6
0
func OpenDB() (*ql.DB, error) {
	return ql.OpenFile("mailboxes.db", &ql.Options{CanCreate: true})
}