Ejemplo n.º 1
0
func (stmt *statement) Exec(args []driver.Value) (driver.Result, error) {
	//Clear any existing bind values
	stmt.bindValues = make([]interface{}, len(args)+1)

	//Bind the parameters
	bindParameters, err := stmt.convertToBindParameters(args)
	if err != nil {
		return nil, err
	}
	stmt.bindParameters(bindParameters)

	//If rows is not nil, close rows and set to nil
	if stmt.rows != nil {
		stmt.rows.Close()
		stmt.rows = nil
	}

	//Execute SQL statement
	sqlStmtSqlPtr := (*odbc.SQLCHAR)(unsafe.Pointer(syscall.StringToUTF16Ptr(stmt.sqlStmt)))
	ret := odbc.SQLExecDirect(stmt.handle, sqlStmtSqlPtr, odbc.SQL_NTS)
	if isError(ret) {
		return nil, errorStatement(stmt.handle, fmt.Sprintf("SQL Stmt: %v\n Bind Values: %v", stmt.sqlStmt, stmt.formatBindValues()))
	}

	return driver.ResultNoRows, nil
}
Ejemplo n.º 2
0
func (stmt *statement) Query(args []driver.Value) (driver.Rows, error) {
	//Clear any existing bind values
	stmt.bindValues = make([]interface{}, len(args)+1)

	//Bind the parameters
	bindParameters, err := stmt.convertToBindParameters(args)
	if err != nil {
		return nil, err
	}
	stmt.bindParameters(bindParameters)

	//If rows is not nil, close rows and set to nil
	if stmt.rows != nil {
		stmt.rows.Close()
		stmt.rows = nil
	}

	//Execute SQL statement
	sqlStmtSqlPtr := (*odbc.SQLCHAR)(unsafe.Pointer(syscall.StringToUTF16Ptr(stmt.sqlStmt)))
	ret := odbc.SQLExecDirect(stmt.handle, sqlStmtSqlPtr, odbc.SQL_NTS)
	if isError(ret) {
		return nil, errorStatement(stmt.handle, fmt.Sprintf("SQL Stmt: %v\nBind Values: %v", stmt.sqlStmt, stmt.formatBindValues()))
	}

	//Get row descriptor handle
	var descRowHandle odbc.SQLHandle
	ret = odbc.SQLGetStmtAttr(stmt.handle, odbc.SQL_ATTR_APP_ROW_DESC, uintptr(unsafe.Pointer(&descRowHandle)), 0, nil)
	if isError(ret) {
		return nil, errorStatement(stmt.handle, fmt.Sprintf("SQL Stmt: %v\nBind Values: %v", stmt.sqlStmt, stmt.formatBindValues()))
	}

	//Check to see if the query option ResultSetNum was passed and if so, iterate through result sets
	optionValue, optionFound := getOptionValue(stmt.queryOptions, ResultSetNum)
	if optionFound {
		for counter, resultSetNum := 0, int(optionValue.(float64)); counter < resultSetNum; counter++ {
			ret := odbc.SQLMoreResults(stmt.handle)
			if isError(ret) {
				return nil, errorStatement(stmt.handle, fmt.Sprintf("SQL Stmt: %v", stmt.sqlStmt))
			}
		}
	} else {
		//If query option ResultSetNum was not passed, iterate through result sets until at least one column is found
		for {
			var numColumns odbc.SQLSMALLINT
			ret := odbc.SQLNumResultCols(stmt.handle, &numColumns)
			if isError(ret) {
				return nil, errorStatement(stmt.handle, fmt.Sprintf("SQL Stmt: %v", stmt.sqlStmt))
			}
			if numColumns > 0 {
				break
			} else {
				ret := odbc.SQLMoreResults(stmt.handle)
				if isError(ret) {
					return nil, errorStatement(stmt.handle, fmt.Sprintf("SQL Stmt: %v", stmt.sqlStmt))
				}
			}
		}
	}

	//Get definition of result columns
	resultColumnDefs, ret := buildResultColumnDefinitions(stmt.handle, stmt.sqlStmt)
	if isError(ret) {
		return nil, errorStatement(stmt.handle, fmt.Sprintf("SQL Stmt: %v\nBind Values: %v", stmt.sqlStmt, stmt.formatBindValues()))
	}

	//Make a slice of the column names
	columnNames := make([]string, len(resultColumnDefs))
	for index, resultCol := range resultColumnDefs {
		columnNames[index] = fmt.Sprint(resultCol.Name)
	}

	//Create rows
	stmt.rows = &rows{handle: stmt.handle, descHandle: descRowHandle, isBeforeFirst: true, resultColumnDefs: resultColumnDefs, resultColumnNames: columnNames, sqlStmt: stmt.sqlStmt}

	//Add a finalizer
	runtime.SetFinalizer(stmt.rows, (*rows).Close)

	return stmt.rows, nil
}