Example #1
0
func (db *defaultUserDBImpl) Create(user *models.User) error {
	q, _ := db.ps.Get("insert_user.sql")
	q, args, err := sqlx.Named(q, user)
	if err != nil {
		return sqlErr(err, q)
	}
	return sqlErr(db.QueryRow(db.Rebind(q), args...).Scan(&user.ID), q)
}
Example #2
0
func (db *defaultPodcastDBImpl) Create(pc *models.Podcast) error {
	q, _ := db.ps.Get("insert_podcast.sql")
	q, args, err := sqlx.Named(q, pc)
	if err != nil {
		return sqlErr(err, q)
	}
	return sqlErr(db.QueryRow(db.Rebind(q), args...).Scan(&pc.ID), q)
}
Example #3
0
func (db *UserDBWriter) Create(user *models.User) error {
	q := `INSERT INTO users(name, email, password)
VALUES (:name, :email, :password) RETURNING id`
	q, args, err := sqlx.Named(q, user)
	if err != nil {
		return dbErr(err, q)
	}
	return dbErr(db.QueryRowx(db.Rebind(q), args...).Scan(&user.ID), q)
}
Example #4
0
func (db *defaultChannelDBImpl) Create(ch *models.Channel) error {

	q, _ := db.ps.Get("upsert_channel.sql")

	q, args, err := sqlx.Named(q, ch)
	if err != nil {
		return sqlErr(err, q)
	}

	return sqlErr(db.QueryRowx(db.Rebind(q), args...).Scan(&ch.ID), q)
}
Example #5
0
func BenchmarkSqlxNamed(b *testing.B) {
	type testStruct struct {
		Foo string `db:"foo"`
		Bar string `db:"bar"`
		Baz int    `db:"baz"`
	}

	for i := 0; i < b.N; i++ {
		sqlx.Named("SELECT * FROM foo WHERE foo=:foo AND bar=:bar AND baz=:baz", testStruct{Foo: "foo", Bar: "bar", Baz: 42})
	}
}
Example #6
0
func ParseQueryStruct(q string, d interface{}, db *sqlx.DB) (string, []interface{}, error) {
	errors := make([]error, 2)
	query, args, err := sqlx.Named(q, d)
	errors = append(errors, err)
	query, args, err = sqlx.In(query, args...)
	errors = append(errors, err)
	query = db.Rebind(query)
	for _, e := range errors {
		if e != nil {
			return "", nil, e
		}
	}
	return query, args, nil
}
Example #7
0
func (db *PodcastDBWriter) Create(pc *models.Podcast) error {
	q := `SELECT insert_podcast(
    :channel_id, 
    :guid,
    :title, 
    :description, 
    :enclosure_url, 
    :source,
    :pub_date
)`
	q, args, err := sqlx.Named(q, pc)
	if err != nil {
		return dbErr(err, q)
	}
	return dbErr(db.QueryRowx(db.Rebind(q), args...).Scan(&pc.ID), q)
}
Example #8
0
func (db *ChannelDBWriter) Create(ch *models.Channel) error {

	q := `SELECT upsert_channel (
:url, 
:title, 
:description, 
:image, 
:categories, 
:website
)`

	q, args, err := sqlx.Named(q, ch)
	if err != nil {
		return dbErr(err, q)
	}

	return dbErr(db.QueryRowx(db.Rebind(q), args...).Scan(&ch.ID), q)
}
Example #9
0
File: cards.go Project: fmr/kanban
func (db *defaultCardDB) Create(card *models.Card) error {
	var maxOrder int64
	if err := db.Get(&maxOrder, "select max(ordering) from cards"); err != nil {
		maxOrder = 0
	}
	card.Ordering = maxOrder + 1
	sql := "insert into cards(ordering, name) values (:ordering, :name)"
	query, args, err := sqlx.Named(sql, card)
	if err != nil {
		return err
	}
	result, err := db.Exec(query, args...)
	if err != nil {
		return err
	}

	card.ID, err = result.LastInsertId()
	if err != nil {
		return err
	}
	card.Tasks = make([]models.Task, 0)
	return nil
}
Example #10
0
func sqlxNamedRegexpReplaceQueryComment(query string, args interface{}) (string, []interface{}, error) {
	return sqlx.Named(regexpReplaceQueryComment(query), args)
}
Example #11
0
func sqlxNamed(query string, args interface{}) (string, []interface{}, error) {
	return sqlx.Named(query, args)
}
Example #12
0
func (self *Reportd) getIndexData(c *gin.Context) gin.H {
	db := self.db
	data := gin.H{"PageTitle": "Report", "ReportTime": time.Now().Format("2006-01-02 15:04:05")}

	var args []interface{}                    // sql query args
	var params = make(map[string]interface{}) // url parameters

	for k, v := range c.Request.URL.Query() {
		params[k] = v
	}

	var results = []Result{}
	for _, v := range conf.Reports {
		sqlStr := v.Sql
		var err error

		if v.Params {
			sqlStr, args, err = sqlx.Named(v.Sql, params)
			if err != nil {
				log.Println(err)
			}
			sqlStr, args, err = sqlx.In(sqlStr, args...)
			if err != nil {
				log.Println(err)
			}
			sqlStr = db.Rebind(sqlStr)
		}

		rows, err := db.Queryx(sqlStr, args...)
		if err != nil {
			log.Println(err)
		}
		defer rows.Close()

		cols, _ := rows.Columns()
		result := Result{Title: v.Title, Columns: cols}

		for rows.Next() {
			obj, err := rows.SliceScan()
			for i, item := range obj {
				if item == nil {
					obj[i] = nil
				} else {
					t := reflect.TypeOf(item).Kind().String()

					if t == "slice" {
						obj[i] = string(item.([]byte))
					}
				}
			}

			if err == nil {
				result.Rows = append(result.Rows, obj)
			}
		}

		results = append(results, result)
	}

	data["Results"] = results
	return data
}
Example #13
0
// Save saves/replaces a given node.
func (ns NodeStore) Save(n entities.Node) error {
	vars, err := n.Vars.Serialize()
	if err != nil {
		return err
	}

	// setup transaction
	tx, err := ns.db.Beginx()
	defer func() {
		if err != nil {
			tx.Rollback()
			return
		}
		err = tx.Commit()
	}()

	// save node
	node := &Node{
		Name: n.Name,
		Vars: string(vars),
	}

	result, err := tx.NamedExec(`INSERT OR REPLACE INTO
		node(name, vars)
		VALUES(:name, :vars);`, node)

	// save relations to roles if required
	if len(n.Roles) > 0 {
		nodeID, err := result.LastInsertId()
		if err != nil {
			return err
		}

		data := struct {
			NodeID int64    `db:"nid"`
			Roles  []string `db:"roles"`
		}{
			NodeID: nodeID,
			Roles:  n.Roles,
		}

		query, args, err := sqlx.Named(`INSERT OR IGNORE INTO
								   node_role(role_id, node_id, id)
				            SELECT r.id AS role_id,
				                   :nid AS node_id,
			                       :nid || '-' || r.ID AS id
		                      FROM role r
							  WHERE r.name IN (:roles);`, data)
		if err != nil {
			return err
		}

		query, args, err = sqlx.In(query, args...)
		if err != nil {
			return err
		}

		query = tx.Rebind(query)
		_, err = tx.Exec(query, args...)
	}

	return err
}