Example #1
0
func InitializeWithPath(databaseFile string) (*Database, error) {
	dbExists, err := checkDatabaseExists(databaseFile)
	if err != nil {
		return nil, common.NewError("Cannot access database file", err)
	}

	db, err := sql.Open("sqlite3", databaseFile)
	if err != nil {
		return nil, common.NewError("Cannot open database", err)
	}
	dbmap := modl.NewDbMap(db, modl.SqliteDialect{})

	database := &Database{dbmap}

	if !dbExists {
		log.Info("Creating tables")
		if err = database.createTables(); err != nil {
			return nil, common.NewError("Cannot create tables", err)
		}
	}

	database.prepareTables()

	return database, nil
}
Example #2
0
func init() {
	usr, err := user.Current()
	if err != nil {
		panic(err)
	}
	workDir := filepath.Join(usr.HomeDir, ".pmusic")
	artDir := filepath.Join(workDir, "cArt")
	path := filepath.Join(workDir, "music.db3")

	err = os.MkdirAll(artDir, 0700)
	if err != nil {
		panic(err)
	}

	db, err = sqlx.Connect("sqlite3", path)
	if err != nil {
		panic(err)
	}

	dbm = modl.NewDbMap(&db.DB, modl.SqliteDialect{})
	dbm.AddTable(Musician{}, "musician").SetKeys(true, "Uid")
	dbm.AddTable(Stations{}, "station").SetKeys(true, "Uid")
	dbm.AddTable(Album{}, "album").SetKeys(true, "Uid")
	dbm.AddTable(Track{}, "track").SetKeys(true, "Uid")
	dbm.AddTable(PTimeStamp{}, "ptimestamp").SetKeys(true, "Uid")
	err = dbm.CreateTablesIfNotExists()
	if err != nil {
		panic(err)
	}
}
Example #3
0
func OpenDatabase(id string) (*modl.DbMap, error) {
	db, err := sqlx.Connect("sqlite3", path.Join(DBROOT, id+".db"))
	if err != nil {
		return nil, err
	}
	dbm := modl.NewDbMap(&db.DB, modl.SqliteDialect{})
	dbm.AddTableWithName(Employee{}, "Employees").SetKeys(true, "EmployeeID")
	dbm.AddTableWithName(Department{}, "Departments").SetKeys(true, "DepartmentID")
	dbm.AddTableWithName(Status{}, "_status").SetKeys(true, "CurrentQuestion")
	dbm.AddTableWithName(Answer{}, "_answers").SetKeys(true, "Number")
	return dbm, err
}
Example #4
0
// PathSQLRepository returns a new sqlBoardRepository or panics if it cannot
func PathSQLRepository(filename string) PathRepository {
	db, err := sql.Open("sqlite3", filename)
	if err != nil {
		panic("Error connecting to db: " + err.Error())
	}
	err = db.Ping()
	if err != nil {
		panic("Error connecting to db: " + err.Error())
	}
	r := &sqlPathRepository{
		dbmap: modl.NewDbMap(db, modl.SqliteDialect{}),
	}
	r.dbmap.TraceOn("", log.New(os.Stdout, "db: ", log.Lmicroseconds))
	r.dbmap.AddTable(Path{}).SetKeys(false, "hash")
	r.dbmap.CreateTablesIfNotExists()
	return r
}
Example #5
0
func init() {
	var err error
	path := environ("GOWIKI_PATH", "./wiki.db")
	db, err = sqlx.Connect("sqlite3", path)
	if err != nil {
		log.Fatal("Error: ", err)
	}
	dbm = modl.NewDbMap(&db.DB, modl.SqliteDialect{})
	dbm.AddTable(User{}, "user").SetKeys(true, "id")
	dbm.AddTable(Page{}, "page").SetKeys(false, "url")
	dbm.AddTable(Config{}, "config").SetKeys(false, "key")
	dbm.AddTable(File{}, "file").SetKeys(false, "path")
	dbm.AddTable(Crosslink{}, "crosslink").SetKeys(false, "from", "to")
	err = dbm.CreateTablesIfNotExists()

	if err != nil {
		log.Fatal("Database not creatable: ", err)
	}

	// load bundled data
	loadBundle()
	cfg = LoadConfig()

	bootstrap()

	// update bundled data with copies from the database
	updateBundle()
	cookies = sessions.NewCookieStore([]byte(cfg.Secret))

	// if we're developing, use /static/ and /templates/
	if MODE == DEVELOP {
		fmt.Println("Running in development mode without bundled resources.")
		http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
		templates = mandira.NewLoader("./templates/", false)
	} else {
		fmt.Println("Running in deployment mode with bundled resources.")
		http.Handle("/static/", http.HandlerFunc(bundleStatic))
		loadTemplatesFromBundle()
	}

	t = templates.MustGet
}
Example #6
0
func initdb(path string) {
	var err error

	db, err = sqlx.Connect("sqlite3", path)
	if err != nil {
		log.Fatal("Error: ", err)
	}
	dbm = modl.NewDbMap(db.DB, modl.SqliteDialect{})
	dbm.AddTable(User{}, "user").SetKeys(true, "id")
	dbm.AddTable(Page{}, "page").SetKeys(false, "url")
	dbm.AddTable(Config{}, "config").SetKeys(false, "key")
	dbm.AddTable(File{}, "file").SetKeys(false, "path")
	dbm.AddTable(Crosslink{}, "crosslink").SetKeys(false, "from", "to")
	err = dbm.CreateTablesIfNotExists()

	if err != nil {
		log.Fatal("Database not creatable: ", err)
	}
	// load bundled data
	loadBundle()
	cfg = LoadConfig()
}