本文整理匯總了Golang中database/sql.NullInt64.Int64方法的典型用法代碼示例。如果您正苦於以下問題:Golang NullInt64.Int64方法的具體用法?Golang NullInt64.Int64怎麽用?Golang NullInt64.Int64使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類database/sql.NullInt64
的用法示例。
在下文中一共展示了NullInt64.Int64方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: SaveThing
func (w *DatabaseWorld) SaveThing(thing *Thing) (ok bool) {
tabletext, err := json.Marshal(thing.Table)
if err != nil {
log.Println("Error serializing table data for thing", thing.Id, ":", err.Error())
return false
}
var parent sql.NullInt64
if thing.Parent != 0 {
parent.Int64 = int64(thing.Parent)
parent.Valid = true
}
var owner sql.NullInt64
if thing.Owner != 0 && thing.Type.HasOwner() {
owner.Int64 = int64(thing.Owner)
owner.Valid = true
}
var program sql.NullString
if thing.Program != nil {
program.String = thing.Program.Text
program.Valid = true
}
// TODO: save the allow list
_, err = w.db.Exec("UPDATE thing SET name = $1, parent = $2, owner = $3, adminlist = $4, denylist = $5, tabledata = $6, program = $7 WHERE id = $8",
thing.Name, parent, owner, thing.AdminList, thing.DenyList,
types.JsonText(tabletext), program, thing.Id)
if err != nil {
log.Println("Error saving a thing", thing.Id, ":", err.Error())
return false
}
return true
}
示例2: writeMessage
func (w writer) writeMessage() error {
start := time.Now()
defer w.stats.recordWrite(start)
channel := fmt.Sprintf("room-%d", rand.Int31n(int32(w.numChannels)))
message := start.String()
// TODO(bdarnell): retry only on certain errors.
for {
txn, err := w.db.Begin()
if err != nil {
continue
}
// TODO(bdarnell): make this a subquery when subqueries are supported on insert.
row := txn.QueryRow(`select max(msg_id) from fakerealtime.messages where channel=$1`, channel)
var maxMsgID sql.NullInt64
if err := row.Scan(&maxMsgID); err != nil {
_ = txn.Rollback()
continue
}
if !maxMsgID.Valid {
maxMsgID.Int64 = 0
}
newMsgID := maxMsgID.Int64 + 1
row = txn.QueryRow(`select max(update_id) from fakerealtime.updates`, channel)
var maxUpdateID sql.NullInt64
if err := row.Scan(&maxUpdateID); err != nil {
_ = txn.Rollback()
continue
}
if !maxUpdateID.Valid {
maxUpdateID.Int64 = 0
}
newUpdateID := maxUpdateID.Int64 + 1
if _, err := txn.Exec(`insert into fakerealtime.messages (channel, msg_id, message) values ($1, $2, $3)`,
channel, newMsgID, message); err != nil {
_ = txn.Rollback()
continue
}
if _, err := txn.Exec(`insert into fakerealtime.updates (update_id, channel, msg_id) values ($1, $2, $3)`,
newUpdateID, channel, newMsgID); err != nil {
_ = txn.Rollback()
continue
}
if err := txn.Commit(); err == nil {
return nil
}
}
}
示例3: AllAgents
func AllAgents(db *sql.DB, simid []byte, proto string) (ags []AgentInfo, err error) {
s := `SELECT AgentId,Kind,Spec,Prototype,ParentId,EnterTime,ExitTime,Lifetime FROM
Agents
WHERE Agents.SimId = ?`
var rows *sql.Rows
if proto != "" {
s += ` AND Agents.Prototype = ?`
rows, err = db.Query(s, simid, proto)
} else {
rows, err = db.Query(s, simid)
}
if err != nil {
return nil, err
}
for rows.Next() {
ai := AgentInfo{}
var exit sql.NullInt64
if err := rows.Scan(&ai.Id, &ai.Kind, &ai.Impl, &ai.Proto, &ai.Parent, &ai.Enter, &exit, &ai.Lifetime); err != nil {
return nil, err
}
if !exit.Valid {
exit.Int64 = -1
}
ai.Exit = int(exit.Int64)
ags = append(ags, ai)
}
if err := rows.Err(); err != nil {
return nil, err
}
return ags, nil
}
示例4: createLoginLog
func createLoginLog(succeeded bool, remoteAddr, login string, user *User) error {
succ := 0
if succeeded {
succ = 1
}
var userId sql.NullInt64
if user != nil {
userId.Int64 = int64(user.ID)
userId.Valid = true
mu.Lock()
if succeeded {
resetUserFailCount(user.ID)
resetIpFailCount(remoteAddr)
} else {
incrUserFailCount(user.ID)
incrIpFailCount(remoteAddr)
}
mu.Unlock()
}
_, err := db.Exec(
"INSERT INTO login_log (`created_at`, `user_id`, `login`, `ip`, `succeeded`) "+
"VALUES (?,?,?,?,?)",
time.Now(), userId, login, remoteAddr, succ,
)
return err
}
示例5: addPublicAccess
func addPublicAccess(app *app, houseID, html string) error {
// Parse title of page
title := titleReg.FindStringSubmatch(html)[1]
if !validTitle(title) {
// <title/> was invalid, so look for an <h1/>
header := headerReg.FindStringSubmatch(html)[1]
if validTitle(header) {
// <h1/> was valid, so use that instead of <title/>
title = header
}
}
title = strings.TrimSpace(title)
// Get thumbnail
data := url.Values{}
data.Set("url", fmt.Sprintf("%s/%s.html", app.cfg.HostName, houseID))
u, err := url.ParseRequestURI("https://peeper.html.house")
u.Path = "/"
urlStr := fmt.Sprintf("%v", u)
client := &http.Client{}
r, err := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
if err != nil {
fmt.Printf("Error creating request: %v", err)
}
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
var thumbURL string
resp, err := client.Do(r)
if err != nil {
fmt.Printf("Error requesting thumbnail: %v", err)
return impart.HTTPError{http.StatusInternalServerError, "Couldn't generate thumbnail"}
} else {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode == http.StatusOK {
thumbURL = string(body)
}
}
// Add to public houses table
approved := sql.NullInt64{Valid: false}
if app.cfg.AutoApprove {
approved.Int64 = 1
approved.Valid = true
}
_, err = app.db.Exec("INSERT INTO publichouses (house_id, title, thumb_url, added, updated, approved) VALUES (?, ?, ?, NOW(), NOW(), ?) ON DUPLICATE KEY UPDATE title = ?, updated = NOW()", houseID, title, thumbURL, approved, title)
if err != nil {
return err
}
// Tweet about it
tweet(app, houseID, title)
return nil
}
示例6: tonullint64
func tonullint64(d *int64) sql.NullInt64 {
var n sql.NullInt64
n.Valid = (d != nil)
if !n.Valid {
return n
}
n.Int64 = *d
return n
}
示例7: tonullint8
func tonullint8(d *int8) sql.NullInt64 {
var n sql.NullInt64
n.Valid = (d != nil)
if n.Valid {
return n
}
n.Int64 = int64(*d)
return n
}
示例8: preparePutTweet
func preparePutTweet(db *sql.DB) func(*twitter.Tweet) {
putTweetStmt, err := db.Prepare(putTweetSql)
if err != nil {
panic(err)
}
return func(t *twitter.Tweet) {
var retweetedStatusId sql.NullInt64
if t.RetweetedStatus != nil {
retweetedStatusId.Int64 = t.RetweetedStatus.Id
retweetedStatusId.Valid = true
}
putTweetStmt.Exec(t.Id, t.Text, t.CreatedAt.Time, t.InReplyToStatusId, t.InReplyToUserId, retweetedStatusId, t.Source, t.User.Id)
}
}
示例9: createLoginLog
func createLoginLog(succeeded bool, remoteAddr, login string, user *User) error {
succ := 0
if succeeded {
succ = 1
}
var userId sql.NullInt64
if user != nil {
userId.Int64 = int64(user.ID)
userId.Valid = true
}
_, err := prepareInsertLog.Exec(time.Now(), userId, login, remoteAddr, succ)
return err
}
示例10: InsertLine
func (r *repository) InsertLine(userID, convoID string, line *Line) error {
var publicID string
var convo convoRec
err := r.getConvo.Get(&convo, struct{ UserID, PublicID string }{userID, convoID})
if err != nil {
return errors.Trace(err)
}
for i := 0; i < maxInsertRetries; i++ {
rv, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
return errors.Trace(err)
}
publicID = lineIDPrefix + strconv.FormatUint(rv.Uint64(), 36)
var moodID sql.NullInt64
if line.mood.id != 0 {
moodID.Int64 = int64(line.mood.id)
moodID.Valid = true
}
_, err = r.insertLine.Exec(struct {
PublicID, Animal, Text, MoodName string
Think bool
MoodID sql.NullInt64
ConversationID int
}{
publicID, line.Animal, line.Text, line.MoodName,
line.Think,
moodID,
convo.IntID,
})
if err == nil {
line.ID = publicID
return nil
}
dbErr, ok := err.(*pq.Error)
if !ok || dbErr.Code != dbErrDupUnique {
return errors.Trace(err)
}
}
return errors.New("Unable to insert a new, unique line")
}
示例11: createLoginLog
func createLoginLog(succeeded bool, remoteAddr, login string, user *User) error {
succ := 0
if succeeded {
succ = 1
}
var userId sql.NullInt64
if user != nil {
userId.Int64 = int64(user.ID)
userId.Valid = true
}
_, err := db.Exec(
"INSERT INTO login_log (`created_at`, `user_id`, `login`, `ip`, `succeeded`) "+
"VALUES (?,?,?,?,?)",
time.Now(), userId, login, remoteAddr, succ,
)
return err
}
示例12: createLoginLog
func createLoginLog(succeeded bool, remoteAddr, login string, user *User) {
succ := 0
if succeeded {
succ = 1
}
now := time.Now()
var userId sql.NullInt64
if user != nil {
userId.Int64 = int64(user.ID)
userId.Valid = true
}
go func() {
db.Exec(
"INSERT INTO login_log (`created_at`, `user_id`, `login`, `ip`, `succeeded`) "+
"VALUES (?,?,?,?,?)",
now, userId, login, remoteAddr, succ,
)
}()
if user != nil {
if succeeded {
UserIdFailures[user.ID] = 0
IpFailtures[remoteAddr] = 0
LastLoginHistory[user.ID] = [2]LastLogin{
{
Login: login,
IP: remoteAddr,
CreatedAt: now.Format("2006-01-02 15:04:05"),
},
LastLoginHistory[user.ID][0],
}
} else {
UserIdFailures[user.ID]++
IpFailtures[remoteAddr]++
}
}
}
示例13: CreateThing
func (w *DatabaseWorld) CreateThing(name string, tt ThingType, creator *Thing, parent *Thing) (thing *Thing) {
thing = NewThing()
thing.Name = name
thing.Type = tt
thing.Parent = parent.Id
var creatorId sql.NullInt64
if creator != nil && thing.Type.HasOwner() {
creatorId.Int64 = int64(creator.Id)
thing.Creator = creator.Id
thing.Owner = creator.Id
}
row := w.db.QueryRow("INSERT INTO thing (name, type, creator, owner, parent) VALUES ($1, $2, $3, $4, $5) RETURNING id, created",
thing.Name, thing.Type.String(), creatorId, creatorId, thing.Parent)
err := row.Scan(&thing.Id, &thing.Created)
if err != nil {
log.Println("Error creating a thing", name, ":", err.Error())
return nil
}
return
}
示例14: CreateNullInt64
func CreateNullInt64(v int64) sql.NullInt64 {
var ni sql.NullInt64
ni.Valid = true
ni.Int64 = v
return ni
}
示例15: handlerErrors
func handlerErrors(w http.ResponseWriter, r *http.Request) error {
r.ParseForm()
if r.URL.Path != "/debug/errors" {
return handlerNotFound(w, r)
}
tx, err := mail.DB.Begin()
if err != nil {
return err
}
defer tx.Rollback()
us, err := getUserSession(r, tx)
if err != nil {
return err
}
if r.Method == "POST" {
// User may or may not be logged in (eg: outside of site).
var nullableUserID, nullableCookieID sql.NullInt64
if us.userID != 0 {
nullableUserID.Int64 = int64(us.userID)
nullableUserID.Valid = true
selectCookieIDStmt := mail.TxStmt(sqlSelectCookieID, tx)
defer selectCookieIDStmt.Close()
err := selectCookieIDStmt.QueryRow(getCookie(r)).Scan(&nullableCookieID)
if err != nil {
return err
}
}
postError, err := getBody(r)
if err != nil {
return err
}
insertErrorStmt := mail.TxStmt(sqlInsertError, tx)
defer insertErrorStmt.Close()
_, err = insertErrorStmt.Exec(nullableUserID, nullableCookieID,
r.RequestURI, postError, r.Header.Get("Referer"),
r.Header.Get("User-Agent"))
if err != nil {
return err
}
err = tx.Commit()
if err != nil {
return err
}
errorsPage := ErrorsPage{}
errorsPage.ErrorLogged = true
return tErrors.Execute(w, errorsPage)
}
if !us.isAdmin {
errorsPage := ErrorsPage{}
errorsPage.NoAccess = true
return tErrors.Execute(newWSCollapser(&w), errorsPage)
}
selectErrorsStmt := mail.TxStmt(sqlSelectErrors, tx)
defer selectErrorsStmt.Close()
rows, err := selectErrorsStmt.Query()
if err != nil {
return err
}
defer rows.Close()
errorsPage := ErrorsPage{}
for rows.Next() {
var email sql.NullString
var url, e, referrer, userAgent string
var t time.Time
err = rows.Scan(&email, &url, &e, &referrer, &userAgent, &t)
if err != nil {
return err
}
errorsPage.Errors = append(errorsPage.Errors, ErrorItem{
email.String, url, e, referrer, userAgent, t.UTC().String()})
}
err = rows.Err() // get any error encountered during iteration
if err != nil {
return err
}
return tErrors.Execute(newWSCollapser(&w), errorsPage)
}