• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang sqlpg.DB类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中dna/sqlpg.DB的典型用法代码示例。如果您正苦于以下问题:Golang DB类的具体用法?Golang DB怎么用?Golang DB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了DB类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: Save

func (sv *SongVideo) Save(db *sqlpg.DB) error {
	if sv.Item != nil {
		return db.InsertIgnore(sv.Item)
	} else {
		return errors.New("Item field of struct is nil")
	}
}
开发者ID:olragon,项目名称:dna,代码行数:7,代码来源:songvideo.go


示例2: SelectMissingIds

// SelectMissingIds accepts a table name as an input and a list of ids as a source.
// It returns a new list of ids that does not exist in the destination table
//
// 	* tblName : a table name
// 	* srcIds : a source ids
// 	* db : a pointer to connected databased
// 	* Returns a new list of ids which are not from the specified table
//
// The format of sql statement is:
// 	WITH dna (id) AS (VALUES (5),(6),(7),(8),(9))
// 	SELECT id FROM dna WHERE NOT EXISTS
// 	(SELECT 1 from ziartists WHERE id=dna.id)
func SelectMissingIds(tblName dna.String, srcIds *dna.IntArray, db *sqlpg.DB) (*dna.IntArray, error) {

	if srcIds.Length() > 0 {

		val := dna.StringArray(srcIds.Map(func(val dna.Int, idx dna.Int) dna.String {
			return "(" + val.ToString() + ")"
		}).([]dna.String))
		selectStmt := "with dna (id) as (values " + val.Join(",") + ") \n"
		selectStmt += "SELECT id FROM dna WHERE NOT EXISTS\n (SELECT 1 from " + tblName + "  WHERE id=dna.id)"
		ids := &[]dna.Int{}
		err := db.Select(ids, selectStmt)
		switch {
		case err != nil:
			return nil, err
		case err == nil && ids != nil:
			slice := dna.IntArray(*ids)
			return &slice, nil
		case err == nil && ids == nil:
			return &dna.IntArray{}, nil
		default:
			panic("Default case triggered. Case is not expected. Cannot select non existed ids")
		}
	} else {
		return nil, errors.New("Empty input array")
	}

}
开发者ID:olragon,项目名称:dna,代码行数:39,代码来源:func.go


示例3: RecoverSQLLogError

// RecoverSQLLogError re-executes failed sql queries in sql error log file from specified path.
// It returns the number of failed -reexec queries, and new failed
// queries will be written to the file
//
// The format of error file is:
// 	Error description - $$$error$$$SQL_QUERY$$$error$$$
// Therefore only get statements enclosed by special `$$$error$$$`
func RecoverSQLLogError(sqlErrFilePath dna.String, db *sqlpg.DB) dna.Int {
	var errCount = 0
	var errStrings = dna.StringArray{}
	b, err := ioutil.ReadFile(sqlErrFilePath.String())
	if err != nil {
		panic(err)
	}
	data := dna.String(string(b))
	// dna.Log("\n", data.Length())
	sqlArr := data.FindAllString(`(?mis)\$\$\$error\$\$\$.+?\$\$\$error\$\$\$`, -1)
	// dna.Log("\nTOTAL SQL STATEMENTS FOUND:", sqlArr.Length())
	for _, val := range sqlArr {
		sqlStmtArr := val.FindAllStringSubmatch(`(?mis)\$\$\$error\$\$\$(.+?)\$\$\$error\$\$\$`, -1)
		if len(sqlStmtArr) > 0 {
			_, err := db.Exec(sqlStmtArr[0][1].String())
			if err != nil {
				if dna.String(err.Error()).Contains(`duplicate key value violates unique constraint`) == false {
					errCount += 1
					errStrings.Push("$$$error$$$" + sqlStmtArr[0][1] + "$$$error$$$")
				}
			}
		}
	}
	if errCount == 0 {
		err = ioutil.WriteFile(sqlErrFilePath.String(), []byte{}, 0644)
	} else {
		err = ioutil.WriteFile(sqlErrFilePath.String(), []byte(errStrings.Join("\n").String()), 0644)
	}
	if err != nil {
		panic(err)
	}
	return dna.Int(errCount)
}
开发者ID:olragon,项目名称:dna,代码行数:40,代码来源:sqlrecover.go


示例4: SelectMissingKeys

// SelectMissingKeys accepts a table name as an input and a list of keys as a source.
// It returns a new list of keys that does not exist in the destination table
//
// 	* tblName : a table name
// 	* srcKeys : a source keys
// 	* db : a pointer to connected databased
// 	* Returns a new list of keys which are not from the specified table
//
// Notice: Only applied to a table having a column named "key".
// The column has to be indexed to ensure good performance
//
// The format of sql statement is:
//	with dna (key) as (values ('43f3HhhU6DGV'),('uFfgQhKbwAfN'),('RvFDlckJB5QU'),('uIF7rwd5wo4p'),('Kveukbhre1ry'),('oJ1lzAlKwJX6'),('43f3HhhU6DGV'),('uFfgQhKbwAfN'),('hfhtyMdywMau'),('PpZuccjYqy1b'))
//	select key from dna where key not in
//	(select key from nctalbums where key in ('43f3HhhU6DGV','uFfgQhKbwAfN','RvFDlckJB5QU','uIF7rwd5wo4p','Kveukbhre1ry','oJ1lzAlKwJX6','43f3HhhU6DGV','uFfgQhKbwAfN','hfhtyMdywMau','PpZuccjYqy1b'))
func SelectMissingKeys(tblName dna.String, srcKeys *dna.StringArray, db *sqlpg.DB) (*dna.StringArray, error) {
	if srcKeys.Length() > 0 {
		val := dna.StringArray(srcKeys.Map(func(val dna.String, idx dna.Int) dna.String {
			return `('` + val + `')`
		}).([]dna.String))
		val1 := dna.StringArray(srcKeys.Map(func(val dna.String, idx dna.Int) dna.String {
			return `'` + val + `'`
		}).([]dna.String))
		selectStmt := "with dna (key) as (values " + val.Join(",") + ") \n"
		selectStmt += "select key from dna where key not in \n(select key from " + tblName + " where key in (" + val1.Join(",") + "))"
		keys := &[]dna.String{}
		err := db.Select(keys, selectStmt)
		switch {
		case err != nil:
			return nil, err
		case err == nil && keys != nil:
			slice := dna.StringArray(*keys)
			return &slice, nil
		case err == nil && keys == nil:
			return &dna.StringArray{}, nil
		default:
			panic("Default case triggered. Case is not expected. Cannot select non existed keys")
		}
	} else {
		return nil, errors.New("Empty input array")
	}
}
开发者ID:olragon,项目名称:dna,代码行数:42,代码来源:func.go


示例5: Save

func (lyric *Lyric) Save(db *sqlpg.DB) error {
	song := NewSong()
	song.Id = lyric.Id
	song.Lyric = lyric.Content
	song.HasLyric = true
	return db.Update(song, "id", "lyric", "has_lyric")
}
开发者ID:olragon,项目名称:dna,代码行数:7,代码来源:lyric.go


示例6: Save

func (apiSong *APIFullSong) Save(db *sqlpg.DB) error {

	var queries = dna.StringArray{}
	var err error

	// Getting artist queries
	artists := apiSong.ToArtists()
	for _, artist := range artists {
		queries.Push(sqlpg.GetInsertIgnoreStatement(sqlpg.GetTableName(artist), artist, "id", artist.Id, false))
	}

	// Getting album query
	album := apiSong.ToAlbum()
	queries.Push(sqlpg.GetInsertIgnoreStatement(sqlpg.GetTableName(album), album, "id", album.Id, false))

	// Getting song query
	song := apiSong.ToSong()
	queries.Push(sqlpg.GetInsertStatement(sqlpg.GetTableName(song), song, false))

	for _, query := range queries {
		_, err = db.Exec(query.String())
	}

	if err != nil {
		errQueries := dna.StringArray(queries.Map(func(val dna.String, idx dna.Int) dna.String {
			return "$$$error$$$" + val + "$$$error$$$"
		}).([]dna.String))
		return errors.New(err.Error() + errQueries.Join("\n").String())
	} else {
		return nil
	}

}
开发者ID:olragon,项目名称:dna,代码行数:33,代码来源:apifullsong.go


示例7: Save

func (episode *Episode) Save(db *sqlpg.DB) error {
	insertStmt := getInsertStmt(episode, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE movie_id=%v and ep_id=%v)", getTableName(episode), episode.MovieId, episode.EpId))
	_, err := db.Exec(insertStmt.String())
	if err != nil {
		err = errors.New(err.Error() + " $$$error$$$" + insertStmt.String() + "$$$error$$$")
	}
	return err
}
开发者ID:olragon,项目名称:dna,代码行数:8,代码来源:episode.go


示例8: Save

func (movie *Movie) Save(db *sqlpg.DB) error {
	insertStmt := getInsertStmt(movie, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE id=%v)", getTableName(movie), movie.Id))
	_, err := db.Exec(insertStmt.String())
	if err != nil {
		err = errors.New(err.Error() + " $$$error$$$" + insertStmt.String() + "$$$error$$$")
	}
	return err
}
开发者ID:olragon,项目名称:dna,代码行数:8,代码来源:movie.go


示例9: IsValidTable

func IsValidTable(tblName dna.String, db *sqlpg.DB) dna.Bool {
	_, err := db.Exec("select * from " + tblName.String() + " limit 0")
	if err == nil {
		return true
	} else {
		return false
	}
}
开发者ID:olragon,项目名称:dna,代码行数:8,代码来源:func.go


示例10: Save

func (soca *SongCategory) Save(db *sqlpg.DB) error {

	var last error
	for _, song := range *(soca.Songs) {
		// dna.Log(song)
		last = db.Update(song, "id", "category")
	}
	return last
}
开发者ID:olragon,项目名称:dna,代码行数:9,代码来源:category_song.go


示例11: SaveLastestMovieCurrentEps

func SaveLastestMovieCurrentEps(db *sqlpg.DB, tblName dna.String, logger *terminal.Logger) {
	for mvid, currentEp := range LastestMovieCurrentEps {
		query := "UPDATE " + tblName + " SET current_eps=" + currentEp.ToString()
		query += " WHERE id=" + mvid.ToString() + ";"
		_, err := db.Exec(query.String())
		if err != nil {
			logger.Println("$$$error$$$" + query + "$$$error$$$")
		}
	}
}
开发者ID:olragon,项目名称:dna,代码行数:10,代码来源:utils.go


示例12: Save

func (sf *APISongFreaksAlbum) Save(db *sqlpg.DB) error {
	album, err := sf.ToAlbum()

	if err != nil {
		return err
	} else {
		return db.Update(album, "id", "ratings", "songids", "review_author", "review")
	}

}
开发者ID:olragon,项目名称:dna,代码行数:10,代码来源:apisongfreaks_album.go


示例13: Save

// Save stores some fields to DB.
func (svu *SongVideoUpdater) Save(db *sqlpg.DB) error {
	switch svu.Item.(type) {
	case *Song:
		return db.Update(svu.Item, "id", "title", "artists", "authors", "topics", "album_title", "album_href", "album_coverart", "producer", "lyric", "date_released", "date_created", "is_lyric")
	case *Video:
		return db.Update(svu.Item, "id", "title", "artists", "authors", "topics", "producer", "lyric", "date_released", "date_created", "is_lyric")
	default:
		panic("Invalid type of SongVideoUpdater.Item")
	}

}
开发者ID:olragon,项目名称:dna,代码行数:12,代码来源:songvideo_updater.go


示例14: GetMaxId

// GetMaxId returns max id of a specified table.
func GetMaxId(tableName dna.String, db *sqlpg.DB) (dna.Int, error) {
	var maxid dna.Int
	err := db.QueryRow("SELECT max(id) FROM " + tableName).Scan(&maxid)
	switch {
	case err == sqlpg.ErrNoRows:
		return 0, err
	case err != nil:
		return 0, err
	default:
		return maxid, nil
	}
}
开发者ID:olragon,项目名称:dna,代码行数:13,代码来源:func.go


示例15: GetLastedChecktime

// GetLastedChecktime returns a map which maps site to lasted checktime.
// Ex: "nssongs" => 2014-03-17 12:09:37
func GetLastedChecktime(db *sqlpg.DB) (map[dna.String]time.Time, error) {
	siteCts := &[]site_checktime{}
	ret := make(map[dna.String]time.Time)
	err := db.Select(siteCts, "select * from get_lasted_checktime();")
	if err == nil {
		for _, sitect := range *siteCts {
			ret[sitect.Site] = sitect.Checktime
			// dna.Log(sitect.Site, sitect.Checktime.Format(utils.DefaultTimeLayout))

		}
		return ret, nil
	} else {
		return nil, err
	}
}
开发者ID:olragon,项目名称:dna,代码行数:17,代码来源:ref.go


示例16: SelectLastMissingIds

// SelectLastMissingIds returns a list of ids which is missing from a table.
// nLastIds is the total number of last ids in the table.
//
// Example: 3 last ids from table nssongs are 1,4,5. Therefore, the missing
// ids whose range is from 1->5 are 2,3.
func SelectLastMissingIds(tblName dna.String, nLastIds dna.Int, db *sqlpg.DB) (*dna.IntArray, error) {
	var min, max dna.Int

	query := dna.Sprintf("SELECT min(id), max(id) FROM (SELECT id FROM %v ORDER BY id DESC LIMIT %v) as AB", tblName, nLastIds)
	// dna.Log(query)
	db.QueryRow(query).Scan(&min, &max)
	// totalItem := max - min + 1
	// var ids = make(dna.IntArray, totalItem, totalItem+100)
	// var idx = 0
	// for i := min; i < max; i++ {
	// 	ids[idx] = i
	// 	idx += 1
	// }
	return SelectMissingIdsWithRange(tblName, min, max, db)
}
开发者ID:olragon,项目名称:dna,代码行数:20,代码来源:func.go


示例17: GetMoviesCurrentEps

// GetMoviesCurrentEps returns a map of MovideId and CurrentEps
// if CurrentEps is less than MaxEp.
// it returns an error if available.
//
// This function is used when we need to find all possible movie ids
// to update.
func GetMoviesCurrentEps(db *sqlpg.DB, tblName dna.String) (map[dna.Int]dna.Int, error) {
	var movieCurrentEps = make(map[dna.Int]dna.Int)
	ids := &[]dna.Int{}
	currentEps := &[]dna.Int{}
	err := db.Select(ids, dna.Sprintf(`SELECT id from %v where current_eps < max_ep order by id DESC`, tblName))
	if err != nil {
		return nil, err
	}
	err = db.Select(currentEps, dna.Sprintf(`SELECT current_eps from %v where current_eps < max_ep order by id DESC`, tblName))
	if err != nil {
		return nil, err
	}
	if len(*currentEps) != len(*ids) {
		return nil, errors.New("Length of IDs and CurrentEps is not correspondent")
	}
	for idx, movieid := range *ids {
		movieCurrentEps[movieid] = (*currentEps)[idx]
	}
	return movieCurrentEps, nil
}
开发者ID:olragon,项目名称:dna,代码行数:26,代码来源:utils.go


示例18: Save

func (alca *AlbumCategory) Save(db *sqlpg.DB) error {

	var last error
	var aids = dna.IntArray{}
	albums := &[]Album{}
	for _, album := range *(alca.Albums) {
		aids.Push(album.Id)
		// dna.Log(album)
	}
	query := "SELECT id, topics, genres from nsalbums WHERE id IN (" + aids.Join(",") + ")"
	// dna.Log(query)
	err := db.Select(albums, query)
	if err != nil {
		dna.Log(query, alca, *alca.Albums)
		dna.PanicError(err)
	}

	for _, album := range *(alca.Albums) {
		foundIndex := 0
		for j, anotherAlbum := range *(albums) {
			if album.Id == anotherAlbum.Id {
				foundIndex = j
			}
		}
		if foundIndex < len(*albums) {
			cat := album.Category.Concat((*albums)[foundIndex].Topics).Concat((*albums)[foundIndex].Genres).Unique()
			album.Category = cat.Filter(func(v dna.String, i dna.Int) dna.Bool {
				if v != "" {
					return true
				} else {
					return false
				}
			})

		}
		last = db.Update(album, "id", "category")

	}

	return last
}
开发者ID:olragon,项目名称:dna,代码行数:41,代码来源:category_album.go


示例19: SaveNewAlbums

// SaveNewAlbums finds all new albums available in new found songs.
// New albums depends on aggregate funcs from the last songid.
// It returns an error or nil and the number of new albums inserted into DB
func SaveNewAlbums(db *sqlpg.DB) (nAlbumsInserted dna.Int, err error) {
	if LastSongId <= 0 {
		return 0, errors.New("Last song id has to be greater than Zero (0)")
	}
	query := dna.Sprintf(`insert into %v 
		(title,artists,topics,href,nsongs,coverart,producer,downloads,plays,date_released,date_created,songids) 
		(
		select title,array_agg_csn_artist_cat(artists) as artists,array_agg_csn_cat(topics) as topics, min(link) as href ,
		sum(nsongs)::int8 as nsongs,max(coverart) as coverart, max(producer) as producer,
		floor(avg(downloads))::int8 as downloads, floor(avg(plays))::int8 as plays,
		max(date_released) as date_released , max(date_created) as date_created, array_agg_cat_unique(songids) as songids  
		from 
		(
		select max(album_title) as title,array_agg_csn_artist_cat(artists) as artists,
		array_agg_csn_cat(topics) as topics , min(album_href) as link, 
		count(*) as nsongs, album_coverart as coverart, max(producer) as producer, 
		floor(avg(downloads)) as downloads, floor(avg(plays)) as plays, 
		max(date_released) as date_released,max(date_created) as date_created, array_agg(id) as songids 
		from %v
		where id > %v 
		and album_title <> '' 
		and album_href <> '' 
		group by album_coverart 
		) as anbinh
		group by title
		)`, "csnalbums", "csnsongs", LastSongId)
	// dna.Log(query)
	ret, err := db.Exec(query.String())
	if err != nil {
		return 0, err
	} else {
		nAl, err := ret.RowsAffected()
		nAlbumsInserted = dna.Int(nAl)
		if err != nil {
			return 0, nil
		} else {
			return nAlbumsInserted, nil
		}

	}
}
开发者ID:olragon,项目名称:dna,代码行数:44,代码来源:album.go


示例20: SelectMissingIdsWithRange

// SelectMissingIds accepts a table name as an input and a range as a source.
// It returns a new list of ids that does not exist in the destination table
//
// 	* tblName : a table name
// 	* head, tail : first and last number defines a range
// 	* db : a pointer to connected databased
// 	* Returns a new list of ids which are not from the specified table
//
// The format of sql statement is:
// 	SELECT id FROM generate_series(5,9) id
// 	WHERE NOT EXISTS (SELECT 1 from ziartists where id = id.id)
func SelectMissingIdsWithRange(tblName dna.String, head, tail dna.Int, db *sqlpg.DB) (*dna.IntArray, error) {

	if head > tail {
		panic("Cannot create range: head has to be less than tail")
	}

	selectStmt := dna.Sprintf("SELECT id FROM generate_series(%v,%v) id \n", head, tail)
	selectStmt += "WHERE NOT EXISTS (SELECT 1 from " + tblName + " where id = id.id)"
	ids := &[]dna.Int{}
	err := db.Select(ids, selectStmt)
	switch {
	case err != nil:
		return nil, err
	case err == nil && ids != nil:
		slice := dna.IntArray(*ids)
		return &slice, nil
	case err == nil && ids == nil:
		return &dna.IntArray{}, nil
	default:
		panic("Default case triggered. Case is not expected. Cannot select non existed ids")
	}

}
开发者ID:olragon,项目名称:dna,代码行数:34,代码来源:func.go



注:本文中的dna/sqlpg.DB类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang dns.Client类代码示例发布时间:2022-05-24
下一篇:
Golang item.Item函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap