Exemple #1
0
// Prepare returns a prepared statement, bound to this connection
func (c *connection) Prepare(query string) (driver.Stmt, error) {

	// Allocate the statement handle
	var stmtHandle odbc.SQLHandle
	ret := odbc.SQLAllocHandle(odbc.SQL_HANDLE_STMT, c.handle, &stmtHandle)
	if isError(ret) {
		return nil, errorConnection(c.handle)
	}

	// Set the query timeout
	ret = odbc.SQLSetStmtAttr(stmtHandle, odbc.SQL_ATTR_QUERY_TIMEOUT, odbc.SQLPOINTER(queryTimeout.Seconds()), odbc.SQL_IS_INTEGER)
	if isError(ret) {
		return nil, errorStatement(stmtHandle, query)
	}

	// Get the statement descriptor table
	var stmtDescHandle odbc.SQLHandle
	ret = odbc.SQLGetStmtAttr(stmtHandle, odbc.SQL_ATTR_APP_PARAM_DESC, uintptr(unsafe.Pointer(&stmtDescHandle)), 0, nil)
	if isError(ret) {
		return nil, errorConnection(c.handle)
	}

	// Parse query options
	queryOptions, err := parseQueryOptions(query)
	if err != nil {
		return nil, err
	}

	// Remove query options from SQL query
	query = removeOptions(query)

	// Create new statement
	stmt := &statement{handle: stmtHandle, stmtDescHandle: stmtDescHandle, sqlStmt: query, conn: c, queryOptions: queryOptions}

	// Add to map of statements owned by the connection
	c.statements[stmt] = true

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

	return stmt, nil
}
Exemple #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
}