示例#1
0
// Execute an sql 'exec' that returns 'insert ids' (api is stupid)
func (self *sql) exec(query string, columns []string, values ...[]interface{}) (*rowset, error) {
	var output gsql.Result = nil
	var err error = nil

	// Pass params over
	if len(values) > 0 {
		output, err = self.db.Exec(query, values[0]...)
	} else {
		output, err = self.db.Exec(query)
	}
	if err != nil {
		return nil, utils.Fail(1, "Error perfroming explicit SQL query \"%s\": %s", query, err.Error())
	}

	// Process output
	var rtn = newRowset(nil).(*rowset)
	var id, id_err = output.LastInsertId()
	if id_err == nil {
		var record = map[string]interface{}{}
		for _, c := range columns {
			record[c] = id
		}
		rtn.Values.PushBack(record)
	}

	return rtn, err
}
示例#2
0
// Execute a query and return a rowset
func (self *sql) Execute(query *nsql.Query) (nsql.Rowset, error) {
	switch {
	case query.Type == nsql.INSERT:
		return self.Insert(query)
	case query.Type == nsql.SELECT:
		return self.Select(query)
	}
	return nil, utils.Fail(1, "Invalid query type; not supported")
}
示例#3
0
// Execute an sql 'query' that returns 'rows'
func (self *sql) query(query string, values ...[]interface{}) (*rowset, error) {
	var rows *gsql.Rows = nil
	var err error = nil

	// Pass params over
	if len(values) > 0 {
		rows, err = self.db.Query(query, values[0]...)
	} else {
		rows, err = self.db.Query(query)
	}
	if err != nil {
		return nil, utils.Fail(1, "Error perfroming explicit SQL query \"%s\": %s", query, err.Error())
	}

	// Generate output
	var rtn = newRowset(rows).(*rowset)
	return rtn, err
}