// Looks up a torrent based on its info hash, // this is a 20-byte SHA which is encoded as a string [2-chars per byte.] func (t *Torrent) SelectHash(hash string) error { torrents := codex.Table("torrents") torrentsProjection := torrents.Project( "torrent_id", "name", "info_hash", "created_by", "creation_date", "encoding", "info_bencoded", ) torrentsFilter, err := torrentsProjection.Where( torrents("info_hash").Eq(hash)).ToSql() if err != nil { return err } dba := func(dbConn *sql.DB) error { row := dbConn.QueryRow(torrentsFilter) err := row.Scan(&t.ID, &t.Name, &t.InfoHash, &t.CreatedBy, &t.CreationDate, &t.Encoding, &t.EncodedInfo) if err == nil { t.isInit = true } return err } return db.ExecuteFn(dba) }
// Selects an excerpt of torrents. Only fetches an ID, InfoHash, and CreatedBy func (t *Torrent) SelectSummaryPage() ([]*Torrent, error) { summaryList := make([]*Torrent, 0, 100) torrents := codex.Table("torrents") torrentsProjection := torrents.Project( "torrent_id", "name", "info_hash", "created_by", ) torrentsFilter, err := torrentsProjection.Limit(100).ToSql() if err != nil { return nil, err } dba := func(dbConn *sql.DB) error { rows, err := dbConn.Query(torrentsFilter) if err != nil { return err } for rows.Next() { t := &Torrent{isInit: true} _ = rows.Scan(&t.ID, &t.Name, &t.InfoHash, &t.CreatedBy) summaryList = append(summaryList, t) } return nil } return summaryList, db.ExecuteFn(dba) }
func InitializeRelation(table Table) (relation *Relation) { relation = new(Relation) relation.Table = table relation.Accessor = codex.Table(table.Name) relation.Mananger = managers.Selection(relation.Accessor.Relation()) return }
func accessorFor(table Table) managers.Accessor { return codex.Table(table.Name) }
func (t *Torrent) Write() error { torrents := codex.Table("torrents") torrentsInsert, err := torrents.Insert( t.Name, t.InfoHash, t.CreatedBy, t.CreationDate, t.Encoding, encodeBytesForPG(t.EncodedInfo), ).Into( "name", "info_hash", "created_by", "creation_date", "encoding", "info_bencoded", ).Returning("torrent_id").ToSql() if err != nil { return err } updateTorrent, err := torrents.Set( "name", "info_hash", "created_by", "creation_date", "encoding", "info_bencoded", ).To( t.Name, t.InfoHash, t.CreatedBy, t.CreationDate, t.Encoding, encodeBytesForPG(t.EncodedInfo), ).Where(torrents("torrent_id").Eq(t.ID)).ToSql() if err != nil { return err } dba := func(dbConn *sql.DB) error { noRowsUpdated := true //try update then insert if t.ID > 0 { res, err := dbConn.Exec(updateTorrent) if err != nil { return err } rowsAffected, err := res.RowsAffected() if err != nil { return err } if rowsAffected > 0 { noRowsUpdated = false } } //row not updated; do an insert. if noRowsUpdated { fmt.Printf("performing insert for torrent \n") err := dbConn.QueryRow(torrentsInsert).Scan(&t.ID) if err != nil { return err } if t.lazyAttributes == nil { //TODO: attributes not supplied? guess from filenames? t.lazyAttributes = &Attribute{} } err = t.lazyAttributes.WriteFor(t.ID) if err != nil { return err } } return nil } return db.ExecuteFn(dba) }