Exemplo n.º 1
0
// Set associates the plain text password given with the user that is uniquely
// identified by id. The password is hashed with bcrypt. If there is a problem
// with hashing or with storing the password, an error is returned.
//
// This may be called on a new user.
func (s *Store) Set(id, password string) (cerr error) {
	defer csql.Safe(&cerr)

	hash, err := bcrypt.GenerateFromPassword(
		[]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return err
	}

	// This lock can be avoided if we use some sort of upsert.
	// It's possible with Postgres, but this is just way easier.
	locker.Lock(id)
	defer locker.Unlock(id)

	n := csql.Count(s, `
		SELECT COUNT(*) FROM `+SqlTableName+` WHERE id = $1
		`, id)
	if n == 0 {
		csql.Exec(s, `
			INSERT INTO `+SqlTableName+` (id, hash) VALUES ($1, $2)
			`, id, hash)
	} else {
		csql.Exec(s, `
			UPDATE `+SqlTableName+` SET id = $1, hash = $2 WHERE id = $1
			`, id, hash)
	}
	return nil
}
Exemplo n.º 2
0
func (d *document) isDuplicate() bool {
	n := csql.Count(db, `
		SELECT COUNT(*)
		FROM document
		WHERE project_owner = $1 AND project_name = $2
			AND name = $3 AND recorded = $4
		`, d.Project.Owner.Id, d.Project.Name, d.Name, d.Recorded)
	return n > 0
}
Exemplo n.º 3
0
// countEpisodes returns the number of episodes for the TV show given.
func countEpisodes(e imdb.Entity) int {
	assertDB()
	q := `
		SELECT COUNT(*)
		FROM episode
		WHERE tvshow_atom_id = $1 AND season > 0 AND episode_num > 0
	`
	return csql.Count(tplDB, q, e.Ident())
}
Exemplo n.º 4
0
// tableSize returns a pretty string indicating the size in table. Row count
// is always include, but the size on disk is only included if it's supported
// by the database.
// Note that 'name' is assumed to be SQL-safe.
func tableSize(db *imdb.DB, name string) string {
	count := csql.Count(db, sf("SELECT COUNT(*) AS count FROM %s", name))
	if db.Driver == "sqlite3" {
		return sf("%d rows", count)
	}
	var size string
	q := sf("SELECT pg_size_pretty(pg_relation_size('%s'))", name)
	csql.Scan(db.QueryRow(q), &size)
	return sf("%d rows (%s)", count, size)
}
Exemplo n.º 5
0
// isDuplicate returns true if the project already exists.
func (proj *project) isDuplicate() bool {
	n := csql.Count(db, `
		SELECT
			COUNT(*)
		FROM
			project
		WHERE
			owner = $1 AND name = $2
		`, proj.Owner.Id, proj.Name)
	return n > 0
}
Exemplo n.º 6
0
func TestLoadMovies(t *testing.T) {
	var exp = map[string]int{
		"movies": 4, "tvs": 1, "episodes": 2,
	}
	if err := loadMovies(testDriver, testDsn, testLists); err != nil {
		t.Fatal(err)
	}
	movies := csql.Count(testDB, "SELECT COUNT(*) FROM movie")
	tvs := csql.Count(testDB, "SELECT COUNT(*) FROM tvshow")
	episodes := csql.Count(testDB, "SELECT COUNT(*) FROM episode")
	if movies != exp["movies"] {
		t.Fatalf("Expected %d movies but got %d", exp["movies"], movies)
	}
	if tvs != exp["tvs"] {
		t.Fatalf("Expected %d tvs but got %d", exp["tvs"], tvs)
	}
	if episodes != exp["episodes"] {
		t.Fatalf("Expected %d episodes but got %d", exp["episodes"], episodes)
	}
}
Exemplo n.º 7
0
// countSeasons returns the number of seasons for the TV show given.
func countSeasons(e imdb.Entity) int {
	assertDB()
	q := `
		SELECT COUNT(*)
		FROM (
			SELECT season
			FROM episode
			WHERE tvshow_atom_id = $1 AND season > 0 AND episode_num > 0
			GROUP BY season
		) AS sub
	`
	return csql.Count(tplDB, q, e.Ident())
}
Exemplo n.º 8
0
// Returns the number of rows in the table given. This will panic with a
// csql.Panic error if the query fails.
func rowCount(db *imdb.DB, table string) int {
	return csql.Count(db, sf("SELECT COUNT(*) FROM %s", table))
}