Exemplo n.º 1
0
Arquivo: mysql.go Projeto: hhkbp2/yabf
func (self *MysqlDB) Insert(table string, key string, values yabf.KVMap) yabf.StatusType {
	statement, args := self.createInsertStat(table, key, values)
	stmt, err := self.db.Prepare(statement)
	if err != nil {
		return yabf.StatusBadRequest
	}
	_, err = stmt.Exec(args...)
	if err != nil {
		yabf.Errorf("fail to insert table: %s, key: %s, error: %s", table, key, err)
		return yabf.StatusError
	}
	return yabf.StatusOK
}
Exemplo n.º 2
0
Arquivo: mysql.go Projeto: hhkbp2/yabf
func (self *MysqlDB) Delete(table string, key string) yabf.StatusType {
	statement := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", table, self.primaryKey)
	stmt, err := self.db.Prepare(statement)
	if err != nil {
		return yabf.StatusBadRequest
	}
	_, err = stmt.Exec(key)
	if err != nil {
		if err == sql.ErrNoRows {
			return yabf.StatusNotFound
		}
		yabf.Errorf("fail to delete table: %s, key: %s, error: %s", table, key, err)
		return yabf.StatusError
	}
	return yabf.StatusOK
}
Exemplo n.º 3
0
Arquivo: mysql.go Projeto: hhkbp2/yabf
func (self *MysqlDB) Update(table string, key string, values yabf.KVMap) yabf.StatusType {
	statement, args := self.createUpdateStat(table, values)
	stmt, err := self.db.Prepare(statement)
	if err != nil {
		return yabf.StatusBadRequest
	}
	_, err = stmt.Exec(args...)
	if err != nil {
		if err == sql.ErrNoRows {
			return yabf.StatusNotFound
		}
		yabf.Errorf("fail to update table: %s, key: %s, error: %s", table, key, err)
		return yabf.StatusError
	}
	return yabf.StatusOK
}
Exemplo n.º 4
0
Arquivo: mysql.go Projeto: hhkbp2/yabf
func (self *MysqlDB) Read(table string, key string, fields []string) (yabf.KVMap, yabf.StatusType) {
	statement := self.createReadStat(table, fields, 0)
	stmt, err := self.db.Prepare(statement)
	if err != nil {
		return nil, yabf.StatusBadRequest
	}
	rows, err := stmt.Query(key)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, yabf.StatusNotFound
		}
		yabf.Errorf("fail to read table: %s, key: %s, error: %s", table, key, err)
		return nil, yabf.StatusError
	}
	columns, err := rows.Columns()
	if err != nil {
		return nil, yabf.StatusError
	}
	length := len(columns)
	if (len(fields) != 0) && (length != len(fields)) {
		return nil, yabf.StatusUnexpectedState
	}
	if !rows.Next() {
		if rows.Err() != nil {
			return nil, yabf.StatusError
		}
		return nil, yabf.StatusNotFound
	}
	results := make([][]byte, length)
	toScan := make([]interface{}, length)
	for i, _ := range results {
		toScan[i] = &results[i]
	}
	err = rows.Scan(toScan...)
	if err != nil {
		return nil, yabf.StatusError
	}
	ret := make(yabf.KVMap)
	for i := 0; i < length; i++ {
		ret[columns[i]] = results[i]
	}
	return ret, yabf.StatusOK
}
Exemplo n.º 5
0
Arquivo: mysql.go Projeto: hhkbp2/yabf
func (self *MysqlDB) Scan(table string, startKey string, recordCount int64, fields []string) ([]yabf.KVMap, yabf.StatusType) {
	statement := self.createReadStat(table, fields, recordCount)
	stmt, err := self.db.Prepare(statement)
	if err != nil {
		return nil, yabf.StatusBadRequest
	}
	rows, err := stmt.Query(startKey, recordCount)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, yabf.StatusNotFound
		}
		yabf.Errorf("fail to scan table: %s, start key: %s, record count: %d, error: %s", table, startKey, recordCount, err)
		return nil, yabf.StatusError
	}
	columns, err := rows.Columns()
	if err != nil {
		return nil, yabf.StatusError
	}
	length := len(columns)
	if (len(fields) != 0) && (length != len(fields)) {
		return nil, yabf.StatusUnexpectedState
	}
	ret := make([]yabf.KVMap, 0, recordCount)
	for rows.Next() {
		results := make([][]byte, length)
		toScan := make([]interface{}, length)
		for i, _ := range results {
			toScan[i] = &results[i]
		}
		err = rows.Scan(toScan...)
		if err != nil {
			return nil, yabf.StatusError
		}
		m := make(yabf.KVMap)
		for i := 0; i < length; i++ {
			m[columns[i]] = results[i]
		}
	}
	return ret, yabf.StatusOK
}