예제 #1
0
func main() {
	log.SetFlags(0)
	flag.Parse()

	if *help || flag.NArg() != 1 {
		fmt.Println("Usage: inventory [cyclus-db]")
		fmt.Println("Creates a fast queryable inventory table for a cyclus sqlite output file.\n")
		flag.PrintDefaults()
		return
	}

	fname := flag.Arg(0)

	conn, err := sqlite3.Open(fname)
	fatalif(err)
	defer conn.Close()

	fatalif(Prepare(conn))
	defer Finish(conn)

	simids, err := GetSimIds(conn)
	fatalif(err)

	for _, simid := range simids {
		ctx := NewContext(conn, simid, nil)
		fatalif(ctx.WalkAll())
	}
}
예제 #2
0
func NewSQL(path string) *SQL {
	conn, _ := sqlite3.Open(":memory:")
	sql := &SQL{
		conn: conn,
	}
	return sql
}
예제 #3
0
func mainHandler(w http.ResponseWriter, r *http.Request) {
	link, err := getLink(w, r)
	if err != nil {
		renderError(w, r, "URL parse error!", err)
		return
	}

	if len(link) == 0 {
		renderTemplate(w, r, nil)
		return
	}

	connection, err := sqlite3.Open("src/tinyurl/url.db")
	if err != nil {
		renderError(w, r, "Database error!", err)
		return
	}
	defer connection.Close()

	sql := fmt.Sprintf("SELECT url FROM url_mapping WHERE hash = \"%s\"", link)
	url_query, err := connection.Query(sql)
	if err != nil {
		renderError(w, r, "That tinyurl link does not exist!", err)
		return
	}
	defer url_query.Close()

	var url string
	url_query.Scan(&url)
	http.Redirect(w, r, url, http.StatusMovedPermanently)
}
예제 #4
0
파일: main.go 프로젝트: nilzen/gcnotify-go
func main() {

	dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))

	db, err := sqlite3.Open(dir + "/gcnotify.db")

	if err != nil {
		fmt.Printf("%v - Database error: %v\n", time.Now(), err)
		os.Exit(1)
	}

	defer db.Close()

	createDatabaseSchema(db)

	settingsFile, err := ioutil.ReadFile(dir + "/settings.json")

	if err != nil {
		fmt.Printf("%v - Settings file error: %v\n", time.Now(), err)
		os.Exit(1)
	}

	var settings SettingsObject
	json.Unmarshal(settingsFile, &settings)

	var wg sync.WaitGroup

	for _, location := range settings.SearchLocations {
		wg.Add(1)

		go getGeocaches(&wg, location, db, settings)
	}

	wg.Wait()
}
예제 #5
0
// OpenDb blah
func OpenDb() (conn *sqlite.Conn, err error) {
	conn, err = sqlite.Open("file:myDb.sqlite?cache=shared")
	if err != nil {
		log.Fatal("Connect: ", err)
	}

	err = conn.Exec(`ATTACH DATABASE 'file::memory:?cache=shared' AS mem;`)
	return
}
func NewSqliteConn(dbName string, connectionId int) (conn Connection, err error) {
	s3conn, err := sqlite.Open(dbName)
	if err != nil {
		return
	}
	sConn := &SqliteConn{conn: s3conn, preparedStatements: make(map[string]*sqlite.Stmt), id: connectionId}
	conn = sConn
	return
}
예제 #7
0
func get_info_from_file(path string) {
	dbh, err := sqlite3.Open(path)
	if err != nil {
		fmt.Println("get_info_from_file")
		log.Fatal(err)
	}

	defer dbh.Close()

	if exists_message_table(dbh) == 1 {
		get_message_from_db(dbh)
	}
}
예제 #8
0
/*
Creates the empty build database.
*/
func createdb() {
	filename := ".scrdkd.db"
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		// Our db is missing, time to recreate it.
		conn, err := sqlite3.Open(filename)
		if err != nil {
			fmt.Println("Unable to open the database: %s", err)
			os.Exit(1)
		}
		defer conn.Close()
		conn.Exec("CREATE TABLE builds(id INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT, hash TEXT);")
	}
}
예제 #9
0
func saveUrlHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	link := r.Form.Get("url-input")

	connection, err := sqlite3.Open("src/tinyurl/url.db")
	if err != nil {
		renderError(w, r, "Database error!", err)
		return
	}
	defer connection.Close()

	url, err := url.Parse(link)
	if err != nil {
		renderError(w, r, "Invalid URL", err)
		return
	}

	//if the website uses http(s), append http
	if len(url.Scheme) == 0 {
		link = "http://" + link
	}

	//check if this link is already in the database
	sql := fmt.Sprintf("SELECT hash FROM url_mapping WHERE url = \"%s\"", link)
	hash_query, err := connection.Query(sql)

	//if not found...
	if err == io.EOF && hash_query == nil {
		h := hash(link)
		sql := fmt.Sprintf("INSERT INTO url_mapping VALUES(NULL, \"%s\", \"%s\");", h, link)
		err = connection.Exec(sql)
		if err != nil {
			renderError(w, r, "Failed to update database!", err)
			return
		}
		txt := fmt.Sprintf("%s mapped to %s", link, h)
		msg := &Message{Text: txt, Type: "success"}
		renderTemplate(w, r, msg)
		return
	}
	defer hash_query.Close()

	// already in DB, return hash that exists
	var return_hash string
	//wtf???
	hash_query.Scan(&return_hash)
	txt := fmt.Sprintf("%s mapped to %s", link, return_hash)
	msg := &Message{Text: txt, Type: "success"}
	renderTemplate(w, r, msg)
}
예제 #10
0
파일: sqlite.go 프로젝트: steder/go-sandbox
func main() {
	c, _ := sqlite3.Open(":memory:")
	c.Exec("CREATE TABLE x(a, b, c)")

	args := sqlite3.NamedArgs{"$a": 1, "$b": "demo"}
	c.Exec("INSERT INTO x VALUES($a, $b, $c)", args) // $c will be NULL

	sql := "SELECT rowid, * FROM x"
	row := make(sqlite3.RowMap)
	for s, err := c.Query(sql); err == nil; err = s.Next() {
		var rowid int64
		s.Scan(&rowid, row)     // Assigns 1st column to rowid, the rest to row
		fmt.Println(rowid, row) // Prints "1 map[a:1 b:demo c:<nil>]"
	}
}
예제 #11
0
func InitDatabase(path string) error {
	c, err := sqlite3.Open(path)
	if err != nil {
		return err
	}
	conn = c

	conn.Exec(`CREATE TABLE IF NOT EXISTS address (
                       address TEXT,
                       raw TEXT,
                       count INTEGER
                   );`)

	return nil
}
예제 #12
0
func TestRegression1(t *testing.T) {
	if err := os.RemoveAll(tmpDbFile); err != nil {
		t.Fatal(err)
	}

	conn, err := sqlite3.Open(tmpDbFile)
	if err != nil {
		t.Fatal(err)
	}

	for _, sql := range rawSimSql {
		if err := conn.Exec(sql); err != nil {
			t.Fatal(err)
		}
	}

	if err := Prepare(conn); err != nil {
		t.Fatal(err)
	}

	simids, err := GetSimIds(conn)
	if err != nil {
		t.Fatal(err)
	}

	ch := make(chan string, 1000)
	for _, simid := range simids {
		ctx := NewContext(conn, simid, ch)
		if err := ctx.WalkAll(); err != nil {
			t.Fatal(err)
		}
	}
	close(ch)

	i := 0
	for sql := range ch {
		if sql != inventorySql[i] {
			t.Errorf("[node %v] expected \"%s\", got \"%s\"", i, inventorySql[i], sql)
		}
		i++
	}

	if err := Finish(conn); err != nil {
		t.Fatal(err)
	}

}
/*
   Opens a connection to a sqlite database of the specified name.
   Creates the database file if does not already exist.
   Returns a handle to the database that holds the open connection
   and has lots of specific methods for creating and working with relish
   data and metadata in the database.
*/
func NewDB(dbName string) *SqliteDB {
	db := &SqliteDB{dbName: dbName, statementQueue: make(chan string, 1000)}
	conn, err := sqlite.Open(dbName)
	if err != nil {
		panic(fmt.Sprintf("Unable to open the database '%s': %s", dbName, err))
	}
	db.conn = conn

	db.preparedStatements = make(map[string]*sqlite.Stmt)
	db.EnsureObjectTable()
	db.EnsureObjectNameTable()
	db.EnsurePackageTable()

	go db.executeStatements()

	return db
}
예제 #14
0
파일: db.go 프로젝트: pgm/gozzle
func OpenDb(filename string) *Db {
	_, exists_err := os.Stat(filename)
	newDb := os.IsNotExist(exists_err)

	c, err := sqlite3.Open(filename)
	if err != nil {
		panic(fmt.Sprintf("Could not open %s: %s", filename, err.Error()))
	}

	db := &Db{c}
	if newDb {
		stmt, err2 := db.CreateSchema()
		if err2 != nil {
			c.Close()
			os.Remove(filename)
			panic(fmt.Sprintf("Could not execute: %s. Got error: %s", stmt, err2.Error()))
		}
	}

	return &Db{c}
}
예제 #15
0
파일: info.go 프로젝트: nimezhu/dbtools
func CmdInfo(cl *cli.Context) {
	Args := cl.Args()
	if len(Args) == 0 {
		log.Fatal("info file.db")
	}
	fn := Args[0]
	c, err := sqlite3.Open(fn)
	checkErr(err)
	sql := "SELECT name FROM sqlite_master WHERE type='table';"
	row := make(sqlite3.RowMap)
	fmt.Printf("tablename\trows\n")
	for s, err := c.Query(sql); err == nil; err = s.Next() {
		s.Scan(row) // Assigns 1st column to rowid, the rest to row
		tablename := row["name"]
		sql2 := fmt.Sprintf("select count(*) from %s ", tablename)
		c.Exec(sql)
		s2, err2 := c.Query(sql2)
		checkErr(err2)
		row2 := make(sqlite3.RowMap)
		s2.Scan(row2)
		fmt.Printf("%s\t%d\n", tablename, row2["count(*)"])

	}
}
예제 #16
0
func (db *Db) open(file string) {
	db.conn, _ = sqlite3.Open(file)
	// todo: catch errors
}
예제 #17
0
func main() {
	c, _ := sqlite3.Open("sqlite.db")
	fmt.Println("c is ", c)
}
예제 #18
0
파일: load.go 프로젝트: nimezhu/dbtools
func CmdLoad(cl *cli.Context) {
	Args := cl.Args()
	if len(Args) == 0 {
		log.Fatal("load file.tsv [tablename] [file.db]")
	}
	fn := Args[0]
	root, _ := splitExt(Args[0])
	baseroot := filepath.Base(root)
	file, err := os.Open(fn)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	tablename := strings.Replace(baseroot, ".", "_", -1)
	if len(Args) > 1 {
		tablename = Args[1]
	}
	db := "output.db"
	if len(Args) > 2 {
		db = Args[2]
	} else {
		db = root + ".sqlite3.db"
	}
	defer file.Close()
	reader := csv.NewReader(file)
	reader.Comma = '\t'
	//lineCount := 0
	header, err := reader.Read()
	checkErr(err)
	firstLine, err := reader.Read()
	checkErr(err)
	//types := make([]string, len(firstLine))
	c, _ := sqlite3.Open(db)
	cstr := "Create Table if not exists " + tablename + " ("
	istr := "Insert into " + tablename + " Values("
	m := make(map[string]interface{})
	for i, v := range firstLine {
		fmt.Println(i, header[i], v, guess_type(v), covt_type(v))
		cstr += header[i] + ","
		istr += "$" + header[i] + ","
		m["$"+header[i]] = covt_type(v)
	}
	cstr = strings.TrimRight(cstr, ",")
	istr = strings.TrimRight(istr, ",")
	cstr += ")"
	istr += ")"
	fmt.Println(cstr)
	fmt.Println(istr)
	c.Exec(cstr)
	args := sqlite3.NamedArgs(m)
	c.Exec(istr, args)
	//rows, err := c.Query(sql)
	//checkErr(err)
	j := 1

	for {
		record, err := reader.Read()
		if err == io.EOF {
			break
		} else if err != nil {
			fmt.Println("Error:", err)
			return
		}
		checkErr(err)
		for i, v := range record {
			m["$"+header[i]] = covt_type(v)
		}
		args := sqlite3.NamedArgs(m)
		c.Exec(istr, args)
		j += 1
	}
	fmt.Println("Loaded ", j, " rows")
	/*
		sql := "select * from test"
		row := make(sqlite3.RowMap)
		for s, err := c.Query(sql); err == nil; err = s.Next() {
			s.Scan(row)      // Assigns 1st column to rowid, the rest to row
			fmt.Println(row) // Prints "1 map[a:1 b:demo c:<nil>]"
		}
	*/
}