本文整理汇总了Golang中database/sql.NullString.String方法的典型用法代码示例。如果您正苦于以下问题:Golang NullString.String方法的具体用法?Golang NullString.String怎么用?Golang NullString.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类database/sql.NullString
的用法示例。
在下文中一共展示了NullString.String方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
db, err := sql.Open("postgres", "user=postgres password=pass dbname=mecab sslmode=disable")
checkErr(err)
//データの検索
//rows, err := db.Query("SELECT * FROM words LIMIT 3")
rows, err := db.Query("SELECT * FROM words WHERE first_char IS NULL")
checkErr(err)
for rows.Next() {
var surface string
var original string
var reading string
var first_c sql.NullString
var last_c sql.NullString
err = rows.Scan(&surface, &original, &reading, &first_c, &last_c)
checkErr(err)
/*
fmt.Println(surface)
fmt.Println(original)
fmt.Println(reading)
fmt.Println(reading[0:3])
fmt.Println(reading[len(reading)-3 : len(reading)])
fmt.Println(first_c)
fmt.Println(last_c)
*/
first_c.String = reading[0:3]
last_c.String = reading[len(reading)-3 : len(reading)]
//データの更新
stmt, err := db.Prepare("update words set first_char=$1, last_char=$2 where original=$3")
checkErr(err)
first_c.Valid = true
last_c.Valid = true
_, err = stmt.Exec(first_c.String, last_c.String, original)
checkErr(err)
/*
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(affect)
*/
}
db.Close()
fmt.Println("finish")
}
示例2: 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
}
示例3: checkStringForNull
func checkStringForNull(eventStr string, event *sql.NullString) {
if len(eventStr) == 0 {
event.Valid = false
} else {
event.String = eventStr
event.Valid = true
}
}
示例4: main
func main() {
c := configFromFile()
s := slack.New(c.SlackToken)
db, err := sql.Open("mysql", c.Datasource)
if err != nil {
log.Fatal(err)
}
defer db.Close()
err = updateChannels(s, db)
if err != nil {
log.Fatal(err)
}
err = updateUsers(s, db)
if err != nil {
log.Fatal(err)
}
rows, err := db.Query("SELECT id, name, latest FROM channels")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var id string
var name string
var latest sql.NullString
if err := rows.Scan(&id, &name, &latest); err != nil {
log.Fatal(err)
}
fmt.Printf("import history: #%s\n", name)
if !latest.Valid {
latest.String = ""
}
l, err := addChannelHistory(s, db, id, latest.String)
if err != nil {
log.Fatal(err)
}
if l != "" {
_, err = db.Exec(`UPDATE channels SET latest = ? WHERE id = ?`, l, id)
if err != nil {
log.Fatal(err)
}
}
}
}
示例5: Value
func (h Hstore) Value() (driver.Value, error) {
hstore := hstore.Hstore{Map: map[string]sql.NullString{}}
if len(h) == 0 {
return nil, nil
}
for key, value := range h {
var s sql.NullString
if value != nil {
s.String = *value
s.Valid = true
}
hstore.Map[key] = s
}
return hstore.Value()
}
示例6: CreateContainer
func (db *SQLDB) CreateContainer(container Container, ttl time.Duration) (SavedContainer, error) {
if !(isValidCheckID(container.ContainerIdentifier) || isValidStepID(container.ContainerIdentifier)) {
return SavedContainer{}, ErrInvalidIdentifier
}
tx, err := db.conn.Begin()
if err != nil {
return SavedContainer{}, err
}
defer tx.Rollback()
checkSource, err := json.Marshal(container.CheckSource)
if err != nil {
return SavedContainer{}, err
}
envVariables, err := json.Marshal(container.EnvironmentVariables)
if err != nil {
return SavedContainer{}, err
}
user := container.User
interval := fmt.Sprintf("%d second", int(ttl.Seconds()))
if container.PipelineName != "" && container.PipelineID == 0 {
// containers that belong to some pipeline must be identified by pipeline ID not name
return SavedContainer{}, errors.New("container metadata must include pipeline ID")
}
var pipelineID sql.NullInt64
if container.PipelineID != 0 {
pipelineID.Int64 = int64(container.PipelineID)
pipelineID.Valid = true
}
var resourceID sql.NullInt64
if container.ResourceID != 0 {
resourceID.Int64 = int64(container.ResourceID)
resourceID.Valid = true
}
var resourceTypeVersion string
if container.ResourceTypeVersion != nil {
resourceTypeVersionBytes, err := json.Marshal(container.ResourceTypeVersion)
if err != nil {
return SavedContainer{}, err
}
resourceTypeVersion = string(resourceTypeVersionBytes)
}
var buildID sql.NullInt64
if container.BuildID != 0 {
buildID.Int64 = int64(container.BuildID)
buildID.Valid = true
}
workerName := container.WorkerName
if workerName == "" {
workerName = container.WorkerName
}
var attempts sql.NullString
if len(container.Attempts) > 0 {
attemptsBlob, err := json.Marshal(container.Attempts)
if err != nil {
return SavedContainer{}, err
}
attempts.Valid = true
attempts.String = string(attemptsBlob)
}
var imageResourceSource sql.NullString
if container.ImageResourceSource != nil {
marshaled, err := json.Marshal(container.ImageResourceSource)
if err != nil {
return SavedContainer{}, err
}
imageResourceSource.String = string(marshaled)
imageResourceSource.Valid = true
}
var imageResourceType sql.NullString
if container.ImageResourceType != "" {
imageResourceType.String = container.ImageResourceType
imageResourceType.Valid = true
}
_, err = tx.Exec(`
INSERT INTO containers (handle, resource_id, step_name, pipeline_id, build_id, type, worker_name, expires_at, ttl, check_type, check_source, plan_id, working_directory, env_variables, attempts, stage, image_resource_type, image_resource_source, process_user, resource_type_version)
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW() + $8::INTERVAL, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)`,
container.Handle,
resourceID,
container.StepName,
pipelineID,
buildID,
container.Type.String(),
workerName,
interval,
//.........这里部分代码省略.........
示例7: FuncListBankQuestion2
//查看题库题目,不显示题目答案
func FuncListBankQuestion2(bankId int64, parse common.ListParam, aid int64) ([]QUESTION, common.Pagiation, error) {
rs := []QUESTION{}
args := []interface{}{bankId}
args = append(args, aid)
// answerFilter := ""
// if aid!=0{
// answerFilter = " and a.answer_key=?"
// }
statusFilter := ""
//1答对,0答错,-1未答
if parse.Status != "" {
statusFilter = " and a.add1=?"
s := parse.Status
if parse.Status == "-1" {
statusFilter = " and a.status=?"
s = "N"
}
args = append(args, s)
}
typeFilter := ""
if parse.Type != 0 {
typeFilter = " and q.type=?"
args = append(args, parse.Type)
}
if parse.Pa.Pn < 1 || parse.Pa.Ps < 1 {
parse.Pa.Pn = 1
parse.Pa.Ps = 10
}
start := (parse.Pa.Pn - 1) * parse.Pa.Ps
end := parse.Pa.Ps
conn := dbMgr.DbConn()
tx, err := conn.Begin()
if err != nil {
return rs, parse.Pa, err
}
sql_ := fmt.Sprintf(`select SQL_CALC_FOUND_ROWS q.TID, q.NAME, q.BANK_ID, q.PID, q.TAG, q.DESCRIPTION, q.DURATION, q.SCORE, q.Q_ANALYZE, q.TYPE,q.Q_OPTION, q.UID, q.UNAME,q2b.score,q2b.ext,q2b.tid,q2b.seq,q.desc2,a.tid,a.status,a.content,a.paper_snapshot,a.answer_snapshot from
ebs_answer a left join ebs_question q on q.tid=a.qid left join ebs_q2b q2b on q2b.tid=a.q2b_id and q2b.bank_id=?
where a.answer_key=? %s order by q2b.seq asc limit %d,%d`, statusFilter+typeFilter, start, end)
if aid == 0 {
sql_ = fmt.Sprintf(`select SQL_CALC_FOUND_ROWS q.TID, q.NAME, q.BANK_ID, q.PID, q.TAG, q.DESCRIPTION, q.DURATION, q.SCORE, q.Q_ANALYZE, q.TYPE,q.Q_OPTION, q.UID, q.UNAME,q2b.score,q2b.ext,q2b.tid,q2b.seq,q.desc2,a.tid,a.status,a.content,a.paper_snapshot,a.answer_snapshot
from ebs_question q join ebs_q2b q2b on q2b.bank_id=? and q2b.q_id=q.tid left join ebs_answer a on a.answer_key=? and a.q2b_id=q2b.tid where 1=1 %s order by q2b.seq asc limit %d,%d`, statusFilter+typeFilter, start, end)
}
log.D("%v", sql_)
if rows, err := tx.Query(sql_, args...); err != nil {
tx.Commit()
return rs, parse.Pa, err
} else {
columns := []string{"TID", "NAME", "BANK_ID", "PID", "TAG", "DESCRIPTION", "DURATION", "SCORE", "Q_ANALYZE", "TYPE",
"Q_OPTION", "UID", "UNAME", "SCORE"}
for rows.Next() {
vals := []interface{}{}
b := QUESTION{}
for _, v := range columns {
(&b).GetFieldByColumn(v, &vals)
}
var ext, desc2, aStatus, aContent, paperSnapshot, answerSnapshot sql.NullString
var q2bId sql.NullInt64
var q2bSeq, aIdRes sql.NullInt64
vals = append(vals, &ext, &q2bId, &q2bSeq, &desc2, &aIdRes, &aStatus, &aContent, &paperSnapshot, &answerSnapshot)
if err := rows.Scan(vals...); err != nil {
log.E("scan err:%v", err)
}
qParse := QUESTION{}
json.Unmarshal([]byte(paperSnapshot.String), &qParse)
if qParse.Tid != nil {
log.D("qParse:%v", qParse.ToString())
b = qParse
bb, _ := json.Marshal(qParse.Ext)
ext.String = string(bb)
}
b.Q2bId = q2bId.Int64
b.Q2bSeq = q2bSeq.Int64
if b.TYPE != nil {
if *b.TYPE == 35 {
b.DESCRIPTION = &desc2.String
}
}
if aid != 0 {
b.Extra = map[string]interface{}{"aid": aIdRes.Int64, "aStatus": aStatus.String, "aContent": aContent.String}
}
if ext.String != "" {
extParse := EXT{}
json.Unmarshal([]byte(ext.String), &extParse)
b.SCORE = &extParse.Score
b.DURATION = &extParse.Duration
b.Ext = extParse
}
rs = append(rs, b)
}
}
if err := tx.QueryRow(`select FOUND_ROWS()`).Scan(&parse.Pa.Total); err != nil {
tx.Commit()
return rs, parse.Pa, err
}
//.........这里部分代码省略.........
示例8: CreateContainer
func (db *SQLDB) CreateContainer(container Container, ttl time.Duration) (Container, error) {
if !isValidID(container.ContainerIdentifier) {
return Container{}, ErrInvalidIdentifier
}
tx, err := db.conn.Begin()
if err != nil {
return Container{}, err
}
checkSource, err := json.Marshal(container.CheckSource)
if err != nil {
return Container{}, err
}
envVariables, err := json.Marshal(container.EnvironmentVariables)
if err != nil {
return Container{}, err
}
user := container.User
interval := fmt.Sprintf("%d second", int(ttl.Seconds()))
var pipelineID sql.NullInt64
if container.PipelineName != "" {
pipeline, err := db.GetPipelineByTeamNameAndName(atc.DefaultTeamName, container.PipelineName)
if err != nil {
return Container{}, fmt.Errorf("failed to find pipeline: %s", err.Error())
}
pipelineID.Int64 = int64(pipeline.ID)
pipelineID.Valid = true
}
var resourceID sql.NullInt64
if container.ResourceID != 0 {
resourceID.Int64 = int64(container.ResourceID)
resourceID.Valid = true
}
var buildID sql.NullInt64
if container.BuildID != 0 {
buildID.Int64 = int64(container.BuildID)
buildID.Valid = true
}
workerName := container.WorkerName
if workerName == "" {
workerName = container.WorkerName
}
var attempts sql.NullString
if len(container.Attempts) > 0 {
attemptsBlob, err := json.Marshal(container.Attempts)
if err != nil {
return Container{}, err
}
attempts.Valid = true
attempts.String = string(attemptsBlob)
}
var imageResourceSource sql.NullString
if container.ImageResourceSource != nil {
marshaled, err := json.Marshal(container.ImageResourceSource)
if err != nil {
return Container{}, err
}
imageResourceSource.String = string(marshaled)
imageResourceSource.Valid = true
}
var imageResourceType sql.NullString
if container.ImageResourceType != "" {
imageResourceType.String = container.ImageResourceType
imageResourceType.Valid = true
}
defer tx.Rollback()
_, err = tx.Exec(`
INSERT INTO containers (handle, resource_id, step_name, pipeline_id, build_id, type, worker_name, expires_at, check_type, check_source, plan_id, working_directory, env_variables, attempts, stage, image_resource_type, image_resource_source, process_user)
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW() + $8::INTERVAL, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)`,
container.Handle,
resourceID,
container.StepName,
pipelineID,
buildID,
container.Type.String(),
workerName,
interval,
container.CheckType,
checkSource,
string(container.PlanID),
container.WorkingDirectory,
envVariables,
attempts,
string(container.Stage),
imageResourceType,
imageResourceSource,
//.........这里部分代码省略.........
示例9: FindContainerByIdentifier
func (db *SQLDB) FindContainerByIdentifier(id ContainerIdentifier) (Container, bool, error) {
err := deleteExpired(db)
if err != nil {
return Container{}, false, err
}
var imageResourceSource sql.NullString
if id.ImageResourceSource != nil {
marshaled, err := json.Marshal(id.ImageResourceSource)
if err != nil {
return Container{}, false, err
}
imageResourceSource.String = string(marshaled)
imageResourceSource.Valid = true
}
var imageResourceType sql.NullString
if id.ImageResourceType != "" {
imageResourceType.String = id.ImageResourceType
imageResourceType.Valid = true
}
var containers []Container
selectQuery := `
SELECT ` + containerColumns + `
FROM containers c ` + containerJoins + `
`
conditions := []string{}
params := []interface{}{}
if isValidCheckID(id) {
checkSourceBlob, err := json.Marshal(id.CheckSource)
if err != nil {
return Container{}, false, err
}
conditions = append(conditions, "resource_id = $1")
params = append(params, id.ResourceID)
conditions = append(conditions, "check_type = $2")
params = append(params, id.CheckType)
conditions = append(conditions, "check_source = $3")
params = append(params, checkSourceBlob)
conditions = append(conditions, "stage = $4")
params = append(params, string(id.Stage))
} else if isValidStepID(id) {
conditions = append(conditions, "build_id = $1")
params = append(params, id.BuildID)
conditions = append(conditions, "plan_id = $2")
params = append(params, string(id.PlanID))
conditions = append(conditions, "stage = $3")
params = append(params, string(id.Stage))
} else {
return Container{}, false, ErrInvalidIdentifier
}
if imageResourceSource.Valid && imageResourceType.Valid {
conditions = append(conditions, fmt.Sprintf("image_resource_source = $%d", len(params)+1))
params = append(params, imageResourceSource.String)
conditions = append(conditions, fmt.Sprintf("image_resource_type = $%d", len(params)+1))
params = append(params, imageResourceType.String)
} else {
conditions = append(conditions, "image_resource_source IS NULL")
conditions = append(conditions, "image_resource_type IS NULL")
}
selectQuery += "WHERE " + strings.Join(conditions, " AND ")
rows, err := db.conn.Query(selectQuery, params...)
if err != nil {
return Container{}, false, err
}
for rows.Next() {
container, err := scanContainer(rows)
if err != nil {
return Container{}, false, nil
}
containers = append(containers, container)
}
switch len(containers) {
case 0:
return Container{}, false, nil
case 1:
return containers[0], true, nil
default:
return Container{}, false, ErrMultipleContainersFound
}
}
示例10: CreateNullString
func CreateNullString(s string) sql.NullString {
var ns sql.NullString
ns.Valid = true
ns.String = s
return ns
}
示例11: GenerateTestSuiteHandler
func GenerateTestSuiteHandler(w http.ResponseWriter, r *http.Request) {
DontCache(&w)
// Generate a test suite based on the category IDs given by the user: of the
// category IDs that are live and have a route back to the "root" of the
// test suite hierarchy based on the category IDs given, this generates the
// JavaScript code necessary to add the categories and tests to the test
// framework running in the user's browser
// Sanity-check for given category IDs: it must be a comma-delimited list of
// integers, or * (which denotes all live categories in the category table)
categoryIDs := mux.Vars(r)["catids"]
if categoryIDs == "*" {
// It's enough just to find the top-level categories here: the recursive
// query we run below will find all of the live child categories we also
// need to include in the test suite execution
if err := db.QueryRow("SELECT '{' || array_to_string(array_agg(id),',') || '}' AS categories FROM category WHERE parent IS NULL AND live = true").Scan(&categoryIDs); err != nil {
log.Panicf("Unable to get list of top-level categories: %s\n", err)
}
} else if regexp.MustCompile("^[0-9]+(,[0-9]+)*$").MatchString(categoryIDs) {
categoryIDs = "{" + categoryIDs + "}"
} else {
log.Panic("Malformed category IDs supplied")
}
w.Header().Set("Content-Type", "text/javascript")
fmt.Fprintln(w, "// Automatically-generated BrowserAudit test suite\n")
// The table returned by this query is sorted by the order in which the
// categories and then tests are to be executed; the hierarchy is correct by
// the time it gets here, and so can be printed line-by-line with no further
// processing of the resulting table
// Suggestion for using WITH RECURSIVE courtesy of:
// http://blog.timothyandrew.net/blog/2013/06/24/recursive-postgres-queries/
// (note: WITH RECURSIVE is Postgres-specific)
rows, err := db.Query(`
WITH RECURSIVE touched_parent_categories AS (
SELECT unnest($1::int[]) AS id
UNION
SELECT category.parent AS id FROM category, touched_parent_categories tpc WHERE tpc.id = category.id AND category.parent IS NOT NULL
), touched_child_categories AS (
SELECT unnest($1::int[]) AS id
UNION
SELECT category.id FROM category, touched_child_categories tcc WHERE category.parent = tcc.id
), touched_categories AS (
SELECT id FROM touched_parent_categories
UNION
SELECT id FROM touched_child_categories
), hierarchy AS (
(
SELECT 'c' as type, id, title, description, NULL::test_behaviour AS behaviour, NULL::varchar AS test_function, NULL::smallint AS timeout, parent, array[execute_order] AS execute_order
FROM category
WHERE parent IS NULL AND live = true AND id IN (SELECT id FROM touched_categories)
) UNION (
SELECT e.type, e.id, e.title, e.description, e.behaviour, e.test_function, e.timeout, e.parent, (h.execute_order || e.execute_order)
FROM (
SELECT 't' as type, id, title, NULL::varchar AS description, behaviour, test_function, timeout, parent, execute_order
FROM test
WHERE live = true AND parent IN (SELECT id FROM touched_categories)
UNION
SELECT 'c' as type, id, title, description, NULL::test_behaviour AS behaviour, NULL::varchar AS test_function, NULL::smallint AS timeout, parent, execute_order
FROM category
WHERE live = true AND id IN (SELECT id FROM touched_categories)
) e, hierarchy h
WHERE e.parent = h.id AND h.type = 'c'
)
)
SELECT type, id, title, description, behaviour, test_function, timeout, parent FROM hierarchy ORDER BY execute_order`, categoryIDs)
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var rowType string
var id int
var title string
var description sql.NullString
var behaviour sql.NullString
var testFunctionInvocation sql.NullString
var timeoutNullable sql.NullInt64
var parentNullable sql.NullInt64
// NULL description -> empty string (only the case for categories, which
// doesn't matter because they aren't written out for categories in the
// JavaScript below)
description.String = ""
// NULL behaviour -> empty string (only the case for categories, which
// doesn't matter because they aren't written out for categories in the
// JavaScript below)
behaviour.String = ""
// NULL test_function -> empty string (only the case for
// categories, which doesn't matter because they aren't written out for
// categories in the JavaScript below)
testFunctionInvocation.String = ""
if err := rows.Scan(&rowType, &id, &title, &description, &behaviour, &testFunctionInvocation, &timeoutNullable, &parentNullable); err != nil {
//.........这里部分代码省略.........
示例12: setString
func setString(s *sql.NullString, v string) {
if v != "" {
s.Valid = true
s.String = v
}
}