当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.Exec方法代码示例

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


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

示例1: 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

示例2: 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

示例3: 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

示例4: 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

示例5: 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

示例6: 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

示例7: 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


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