Ejemplo n.º 1
0
func (b *Backend) Inspect(db *sql.DB, m driver.Model) (*sql.Table, error) {
	var database string
	if err := db.QueryRow("SELECT DATABASE() FROM DUAL").Scan(&database); err != nil {
		return nil, err
	}
	return b.SqlBackend.Inspect(db, m, database)
}
Ejemplo n.º 2
0
func (b *Backend) HasIndex(db *sql.DB, m driver.Model, idx *index.Index, name string) (bool, error) {
	var exists int
	err := db.QueryRow("SELECT 1 FROM pg_class WHERE relname = $1 AND relkind = 'i'", name).Scan(&exists)
	if err == sql.ErrNoRows {
		err = nil
	}
	return exists != 0, err
}
Ejemplo n.º 3
0
func (b *Backend) Insert(db *sql.DB, m driver.Model, query string, args ...interface{}) (driver.Result, error) {
	fields := m.Fields()
	if fields.AutoincrementPk {
		q := query + " RETURNING " + fields.MNames[fields.PrimaryKey]
		var id int64
		err := db.QueryRow(q, args...).Scan(&id)
		// We need to perform a "real" insert to find the real error, so
		// just let the code fall to the Exec at the end of the function
		// if there's an error.
		if err == nil {
			return insertResult(id), nil
		}
	}
	return db.Exec(query, args...)
}