Exemple #1
0
func Open(dbname string) *sqlite.Conn {
	conn, err := sqlite.Open(dbname)
	if err != nil {
		log.Fatalf("Cannot open database: " + dbname)
	}
	return conn
}
Exemple #2
0
func Connect(filename string) (db Cache, err os.Error) {
	var c *sqlite.Conn
	if c, err = sqlite.Open(filename); err != nil {
		return
	}
	db = Cache{c}
	err = db.Exec("CREATE TABLE IF NOT EXISTS cache (\n" +
		"    path TEXT NOT NULL,\n" +
		"    md5 TEXT NOT NULL,\n" +
		"    uid INTEGER,\n" +
		"    gid INTEGER,\n" +
		"    mode INTEGER,\n" +
		"    mtime INTEGER,\n" +
		"    size INTEGER,\n" +
		"    PRIMARY KEY (path)\n" +
		")\n")
	if err != nil {
		db.Close()
		return
	}
	err = db.Exec("CREATE INDEX IF NOT EXISTS idx_md5 ON cache (md5)\n")
	if err != nil {
		db.Close()
		return
	}
	return
}
Exemple #3
0
func NewNodeStore(filename string) (*NodeStore, os.Error) {
	conn, err := sqlite.Open(filename)
	if err != nil {
		return nil, err
	}
	return &NodeStore{nodes: make(map[string]*Node), db: conn}, nil
}
Exemple #4
0
func SetupDatabase() {
	db, _ = sqlite.Open("foo.db")
	db.Exec("CREATE TABLE tweets (author, message, timestamp)")
	db.Exec("CREATE TABLE username (name)")
	db.Exec("CREATE TABLE key (key)")
	db.Exec("CREATE TABLE peers (ip)")
	db.Exec("CREATE TABLE friends (mod, username)")
	stmt, _ := db.Prepare("SELECT name FROM username")
	stmt.Exec()
	for {
		if !stmt.Next() {
			fmt.Println("no key found in database, generating... this may take a second")
			key := GenKey()
			username := Base64Encode(key.N.Bytes())
			WriteName(string(username))
			WriteFriend(string(username), "This Node")
			WriteKey(key)
			break
		}
		var unmarshalled rsa.PrivateKey
		var marshalled []byte
		stmt.Scan(&marshalled)
		json.Unmarshal(marshalled, unmarshalled)
		myUsername = "******"
		fmt.Println("found username:", myUsername)
		break
	}
	stmt.Finalize()
}
Exemple #5
0
func (self *SqliteDriver) Connect(dsn *http.URL) (conn dbi.Connection, err os.Error) {
	c, err := _sqlite.Open(dsn.Path[1:])
	if err != nil {
		return
	}
	conn = newConnection(c)
	return
}
func (myDHTForceSync *DHTForceSyncStruct) connectToDB() {
	var err os.Error

	myDHTForceSync.dht, err = sqlite.Open("/var/spool/goesmtp/DHT.db")
	if err != nil {
		fmt.Printf("Can't open the DHT database. FATAL: %s\n", err)
		os.Exit(-1)
	}
}
func (myRouter *routerStruct) connectToDB() {
	var err os.Error

	myRouter.nml, err = sqlite.Open("/var/spool/goesmtp/newMessageLog.db")
	if err != nil {
		fmt.Printf("Can't open the newMessageLog database. FATAL: %s\n", err)
		os.Exit(-1)
	}
}
Exemple #8
0
func PrepareConnection(dbDir string) *sqlite.Conn {
	_ = fmt.Fprintf
	cache["db:forums"] = path.Join(dbDir, "forums.db")
	cache["db:users"] = path.Join(dbDir, "users.db")
	conn, _ := sqlite.Open(cache["db:forums"])
	HandleError(conn.Exec("ATTACH DATABASE '" + cache["db:users"] + "' AS users;"))

	return conn
}
func (myDHTServer *DHTServerStruct) connectToDB() {
	var err os.Error

	myDHTServer.dht, err = sqlite.Open("/var/spool/goesmtp/DHT.db")
	if err != nil {
		myDHTServer.logger.Logf(LMIN, "Can't open the DHT database. FATAL: %s\n", err)
		os.Exit(-1)
	}

	myDHTServer.nml, err = sqlite.Open("/var/spool/goesmtp/newMessageLog.db")
	if err != nil {
		myDHTServer.logger.Logf(LMIN, "Can't open the newMessageLog database. FATAL: %s\n", err)
		os.Exit(-1)
	}

	myDHTServer.dml, err = sqlite.Open("/var/spool/goesmtp/delMessageLog.db")
	if err != nil {
		myDHTServer.logger.Logf(LMIN, "Can't open the delMessageLog database. FATAL: %s\n", err)
		os.Exit(-1)
	}
}
Exemple #10
0
func NewMonitorStore(filename string) *MonitorStore {
	store := &MonitorStore{}
	conn, err := sqlite.Open(filename)
	if err != nil {
		fmt.Println("Unable to open database: %s", err)
		os.Exit(1)
	}

	//create table
	conn.Exec("CREATE TABLE IF NOT EXISTS asin(id INTEGER PRIMARY KEY AUTOINCREMENT, asin VARCHAR(200));")

	store.conn = conn
	return store
}
Exemple #11
0
func main() {
	fmt.Println("\n********* Reading from a SQLite3 Database **********")
	db := "test.db"

	conn, err := sqlite.Open(db)
	if err != nil {
		fmt.Println("Unable to open the database: %s", err)
		os.Exit(1)
	}

	defer conn.Close()

	conn.Exec("CREATE TABLE articles(id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(200), body TEXT, date TEXT);")

	insertSql := `INSERT INTO articles(title, body, date) VALUES("This is a Test Article Title.",
        "MO, dates are a pain.  I spent considerable time trying to decide how best to
        store dates in my app(s), and eventually chose to use Unix times (integers).
        It seemed an easy choice as I program in Perl and JavaScript.",
        "12/05/2010");`

	err = conn.Exec(insertSql)
	if err != nil {
		fmt.Println("Error while Inserting: %s", err)
	}

	selectStmt, err := conn.Prepare("SELECT id, title, body, date FROM articles;")
	err = selectStmt.Exec()
	if err != nil {
		fmt.Println("Error while Selecting: %s", err)
	}

	for selectStmt.Next() {
		var article Article

		err = selectStmt.Scan(&article.Id, &article.Title, &article.Body, &article.Date)
		if err != nil {
			fmt.Printf("Error while getting row data: %s\n", err)
			os.Exit(1)
		}

		fmt.Printf("Id => %d\n", article.Id)
		fmt.Printf("Title => %s\n", article.Title)
		fmt.Printf("Body => %s\n", article.Body)
		fmt.Printf("Date => %s\n", article.Date)
	}
}
Exemple #12
0
func NewUserAccountDB(server *Server) *UserAccountDB {
	db := &UserAccountDB{server: server}
	dbcon, err := sqlite.Open(server.Capabilities().Domain + "_user.db")
	if err != nil {
		panic("Cannot access user database")
	}
	stmnt, err := dbcon.Prepare("CREATE TABLE IF NOT EXISTS Accounts ( id VARCHAR(20) PRIMARY KEY, email VARCHAR(30), nick VARCHAR(30), passwd VARCHAR(16) )")
	if err != nil {
		panic("Cannot prepare stmnt for create account table")
	}
	err = stmnt.Exec()
	if err != nil {
		panic("Cannot create account table")
	}
	stmnt.Next()
	db.dbcon = dbcon
	return db
}
Exemple #13
0
func main() {
	// Open/create the DB
	conn, err := sqlite.Open("goserver.db")
	if err != nil {
		panic(err.String())
	}
	defer conn.Close()

	// If our table doesn't exist create it.
	err = conn.Exec(`
create table if not exists redirects
 (key varchar(32) not null,
  url varchar(255) not null)`)
	if err != nil {
		panic(err.String())
	}

	// Create the channels that handler threads use to make db requests.
	// (SQLite is not threadsafe)
	lookup = make(chan LookupRequest)
	save = make(chan SaveRequest)

	// Run a goroutine to serialize requests to the db
	go func() {
		for {
			select {
			case req := <-lookup:
				req.reply <- doLookup(conn, req.key)
			case req := <-save:
				conn.Exec("delete from redirects where key = ?", req.key)
				conn.Exec("replace into redirects values (?, ?)", req.key, req.url)
				req.reply <- true
			}
		}
	}()

	// Serve pages
	http.HandleFunc("/", handle)
	http.ListenAndServe(":8080", nil)
}
Exemple #14
0
func newStore(filename string) (*store, error) {
	// make sure the parent directory exists
	dir, _ := filepath.Split(filename)
	if s, _ := os.Stat(dir); s == nil {
		os.MkdirAll(dir, 0700)
	}

	// open the database
	c, err := sqlite.Open(filename)
	if err != nil {
		return nil, err
	}

	// execute all commands in the setup script
	for _, s := range databaseSetupScript {
		if err := c.Exec(s); err != nil {
			return nil, err
		}
	}

	return &store{c}, nil
}