Documentation ¶
Overview ¶
Package odbc3-go provides a thin wrapper around the ODBC API.
The acronym ODBC refers to Open Database Connectivity, and the API provides a abstract and low-level interface to different types of database servers. Drivers exist for most major databases types, which means that the API can provide an agnostic interface to most databases.
This package provide a thin wrapper around the ODBC API. This means that most functions exist with nearly the same parameters and return values as in the underlying C API. Therefore, documentation and tutorials for ODBC should also be useful in understanding this package. However, some modifications in the API are present. These modifications exist to ensure type-safety, whereas the C API frequently requires unsafe casts.
On linux and other unix-like systems, this package uses cgo. To build this package, you will need to have the necessary development files (i.e. headers) installed on your system. You will also need a compiler.
On windows, this package uses the syscall interface. No development files are required.
Index ¶
- Constants
- Variables
- type CKind
- type CompletionType
- type Connection
- func (c *Connection) AutoCommit() (bool, error)
- func (c *Connection) Connect(server, username, authentication string) error
- func (c *Connection) DriverConnect(dsn string) error
- func (c *Connection) EndTransaction(completion CompletionType) error
- func (c *Connection) ExecDirect(sql string) (*Statement, error)
- func (c *Connection) Free() error
- func (c *Connection) GetAttrInt(field FieldIdentifier) (int32, error)
- func (c *Connection) GetAttrUint(field FieldIdentifier) (uint32, error)
- func (c *Connection) NewStatement() (*Statement, error)
- func (c *Connection) Prepare(sql string) (*Statement, error)
- func (c *Connection) SetAutoCommit(value bool) error
- type ConnectionPooling
- type ConnectionPoolingMatch
- type DataSource
- type Date
- type Driver
- type DriverCompletion
- type Environment
- func (e *Environment) Connect(server, username, authentication string) (*Connection, error)
- func (e *Environment) ConnectionPooling() (ConnectionPooling, error)
- func (e *Environment) ConnectionPoolingMatch() (ConnectionPoolingMatch, error)
- func (e *Environment) DataSources() ([]DataSource, error)
- func (e *Environment) DriverConnect(dsn string) (*Connection, error)
- func (e *Environment) Drivers() ([]Driver, error)
- func (e *Environment) EndTransaction(completion CompletionType) error
- func (e *Environment) Free() error
- func (e *Environment) NewConnection() (*Connection, error)
- func (e *Environment) SetConnectionPooling(value ConnectionPooling) error
- func (e *Environment) SetConnectionPoolingMatch(value ConnectionPoolingMatch) error
- type Error
- type FetchDirection
- type FieldIdentifier
- type HandleType
- type InputOutputType
- type Kind
- type Statement
- func (s *Statement) BindParameter(index int, value interface{}) error
- func (s *Statement) BindParameterBinary(index int, value []byte) error
- func (s *Statement) BindParameterBinaryString(index int, value string) error
- func (s *Statement) BindParameterBytes(index int, value []byte) error
- func (s *Statement) BindParameterDate(index int, value *Date) error
- func (s *Statement) BindParameterFloat64(index int, value *float64) error
- func (s *Statement) BindParameterInt32(index int, value *int32) error
- func (s *Statement) BindParameterInt64(index int, value *int64) error
- func (s *Statement) BindParameterNull(index int) error
- func (s *Statement) BindParameterString(index int, value string) error
- func (s *Statement) BindParameterTime(index int, value *Time) error
- func (s *Statement) BindParameterTimestamp(index int, value *Timestamp) error
- func (s *Statement) Cancel() error
- func (s *Statement) ColAttributeNumeric(index int, field FieldIdentifier) (uint32, error)
- func (s *Statement) ColAttributeString(index int, field FieldIdentifier) (string, error)
- func (s *Statement) ExecDirect(sql string) error
- func (s *Statement) Execute() error
- func (s *Statement) Fetch() (ok bool, err error)
- func (s *Statement) FetchScroll(orientation FetchDirection, offset int32) (ok bool, err error)
- func (s *Statement) Free() error
- func (s *Statement) GetData(index int) (interface{}, error)
- func (s *Statement) GetDataBool(index int) (value bool, ok bool, err error)
- func (s *Statement) GetDataBytes(index int) ([]byte, error)
- func (s *Statement) GetDataFloat64(index int) (value float64, isnull bool, err error)
- func (s *Statement) GetDataInt32(index int) (value int32, ok bool, err error)
- func (s *Statement) GetDataInt64(index int) (value int64, ok bool, err error)
- func (s *Statement) GetDataString(index int) (value string, ok bool, err error)
- func (s *Statement) GetDataTimestamp(index int) (value Timestamp, ok bool, err error)
- func (s *Statement) NumParams() (int, error)
- func (s *Statement) NumResultCols() (int, error)
- func (s *Statement) Prepare(sql string) error
- func (s *Statement) RowCount() (int32, error)
- type Time
- type Timestamp
Examples ¶
Constants ¶
const ( HANDLE_ENV = HandleType(1) HANDLE_DBC = HandleType(2) HANDLE_STMT = HandleType(3) HANDLE_DESC = HandleType(4) )
const ( UNKNOWN_TYPE = Kind(0) CHAR = Kind(1) NUMERIC = Kind(2) DECIMAL = Kind(3) INTEGER = Kind(4) SMALLINT = Kind(5) FLOAT = Kind(6) REAL = Kind(7) DOUBLE = Kind(8) DATETIME = Kind(9) DATE = Kind(9) TIME = Kind(10) TIMESTAMP = Kind(11) VARCHAR = Kind(12) TYPE_DATE = Kind(91) TYPE_TIME = Kind(92) TYPE_TIMESTAMP = Kind(93) BINARY = Kind(-2) VARBINARY = Kind(-3) BIGINT = Kind(-5) TINYINT = Kind(-6) BIT = Kind(-7) WCHAR = Kind(-8) WVARCHAR = Kind(-9) )
const ( C_CHAR = CKind(CHAR) C_DOUBLE = CKind(DOUBLE) C_DATE = CKind(DATE) C_TIME = CKind(TIME) C_TIMESTAMP = CKind(TIMESTAMP) C_BIT = CKind(BIT) C_WCHAR = CKind(WCHAR) C_TYPE_DATE = CKind(TYPE_DATE) C_TYPE_TIME = CKind(TYPE_TIME) C_TYPE_TIMESTAMP = CKind(TYPE_TIMESTAMP) C_BINARY = CKind(BINARY) C_DEFAULT = CKind(99) C_SLONG = CKind(-16) C_SBIGINT = CKind(-25) )
const ( DESC_AUTO_UNIQUE_VALUE = FieldIdentifier(11) DESC_BASE_COLUMN_NAME = FieldIdentifier(22) DESC_BASE_TABLE_NAME = FieldIdentifier(23) DESC_CASE_SENSITIVE = FieldIdentifier(12) DESC_COUNT = FieldIdentifier(1001) DESC_TYPE = FieldIdentifier(1002) DESC_LENGTH = FieldIdentifier(1003) DESC_OCTET_LENGTH_PTR = FieldIdentifier(1004) DESC_PRECISION = FieldIdentifier(1005) DESC_SCALE = FieldIdentifier(1006) DESC_DATETIME_INTERVAL_CODE = FieldIdentifier(1007) DESC_NULLABLE = FieldIdentifier(1008) DESC_INDICATOR_PTR = FieldIdentifier(1009) DESC_DATA_PTR = FieldIdentifier(1010) DESC_NAME = FieldIdentifier(1011) DESC_UNNAMED = FieldIdentifier(1012) DESC_OCTET_LENGTH = FieldIdentifier(1013) DESC_ALLOC_TYPE = FieldIdentifier(1099) )
const ( PARAM_TYPE_UNKNOWN = InputOutputType(0) PARAM_INPUT = InputOutputType(1) PARAM_INPUT_OUTPUT = InputOutputType(2) RESULT_COL = InputOutputType(3) PARAM_OUTPUT = InputOutputType(4) RETURN_VALUE = InputOutputType(5) )
const ( FETCH_NEXT = FetchDirection(1) FETCH_FIRST = FetchDirection(2) FETCH_LAST = FetchDirection(3) FETCH_PRIOR = FetchDirection(4) FETCH_ABSOLUTE = FetchDirection(5) FETCH_RELATIVE = FetchDirection(6) )
const ( CP_OFF = ConnectionPooling(0) CP_ONE_PER_DRIVER = ConnectionPooling(1) CP_ONE_PER_HENV = ConnectionPooling(2) CP_DEFAULT = CP_OFF )
const ( CP_STRICT_MATCH = ConnectionPoolingMatch(0) CP_RELAXED_MATCH = ConnectionPoolingMatch(1) CP_MATCH_DEFAULT = CP_STRICT_MATCH )
const ( COMMIT = CompletionType(0) ROLLBACK = CompletionType(1) )
const ( DRIVER_NOPROMPT = DriverCompletion(0) DRIVER_COMPLETE = DriverCompletion(1) DRIVER_PROMPT = DriverCompletion(2) DRIVER_COMPLETE_REQUIRED = DriverCompletion(3) )
const ( BUFFER_SIZE = 10 * 1024 INFO_BUFFER_LEN = 256 )
Variables ¶
var ( // This error is returned if a call to NewEnvironment fails. ErrAllocHandleFailed = errors.New("Could not allocate new SQL handle.") // The data type in the query is not supported. May be returned by the method GetData. ErrUnsupportedOdbcType = errors.New("Unsupported type in database.") // The data type cannot be used to bind a parameter. May be return by the method BindParameter. ErrUnsupportedBindType = errors.New("Unsupported type in bind parameter.") )
Functions ¶
This section is empty.
Types ¶
type CKind ¶
type CKind int16
A CKind represents the specific data type of a parameter or column used externally (i.e. in the calling code). Most users of this package should not need to use these parameters.
type CompletionType ¶
type CompletionType int16
A CompletionType specifies how a transaction in terminated.
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
A Connection represents a connection to a data source.
func (*Connection) AutoCommit ¶
func (c *Connection) AutoCommit() (bool, error)
AutoCommit determines if the connection automatically commits each statement when executed.
This function wraps the C API function SQLGetConnectAttr.
func (*Connection) Connect ¶
func (c *Connection) Connect(server, username, authentication string) error
Connect creates a connection to a data source.
This function wraps the C API function SQLConnect
func (*Connection) DriverConnect ¶
func (c *Connection) DriverConnect(dsn string) error
DriverConnect creates a connection to a data source.
This function wraps the C API function SQLDriverConnect
func (*Connection) EndTransaction ¶
func (c *Connection) EndTransaction(completion CompletionType) error
EndTransaction either performs either a commit or a rollback of the current transaction.
This function wraps the C API function SQLEndTran.
func (*Connection) ExecDirect ¶
func (c *Connection) ExecDirect(sql string) (*Statement, error)
ExecDirect is a helper function that allocates a new statement, and then executes the provided SQL statement. Note that the SQL statement is execute directly, which means that there is no opportunity to bind parameters.
See the method ExecDirect of the type Statement.
func (*Connection) Free ¶
func (c *Connection) Free() error
Free releases all resources associated with the environment.
This function wraps the C API function SQLFreeHandle.
func (*Connection) GetAttrInt ¶
func (c *Connection) GetAttrInt(field FieldIdentifier) (int32, error)
GetAttrInt retrieves a attribute describing the connection if that value can be represented as an int32.
This function wraps the C API function SQLGetConnectAttr.
func (*Connection) GetAttrUint ¶
func (c *Connection) GetAttrUint(field FieldIdentifier) (uint32, error)
GetAttrInt retrieves a attribute describing the connection if that value can be represented as an uint32.
This function wraps the C API function SQLGetConnectAttr.
func (*Connection) NewStatement ¶
func (c *Connection) NewStatement() (*Statement, error)
func (*Connection) Prepare ¶
func (c *Connection) Prepare(sql string) (*Statement, error)
Prepare is a helper function that allocates a new statement, and then prepares the provided SQL statement. Use the returned Statement to bind parameters and then execute the query.
See the method Prepare of the type Statement.
func (*Connection) SetAutoCommit ¶
func (c *Connection) SetAutoCommit(value bool) error
SetAutoCommit determines if the connection automatically commits each statement when executed.
This function wraps the C API function SQLGetConnectAttr.
type ConnectionPooling ¶
type ConnectionPooling uint32
A ConnectionPool specifies how an environment handles connection pooling.
type ConnectionPoolingMatch ¶
type ConnectionPoolingMatch uint32
A ConnectionPoolMatch specifies how strictly an environment matches connections.
type DataSource ¶
type DataSource struct { // Data source name Server string // A description of the driver associated with the source Description string }
A DataSource struct contains information about an ODBC data source installed on the system.
type Driver ¶
type Driver struct { // Description of the driver. Name string // A list of driver attributes separated by semi-colons Attr string }
A Driver struct contains information about an ODBC driver installed on the system.
type DriverCompletion ¶
type DriverCompletion uint16
A DriverCompletion specifies how a driver should prompt for missing information in a DSN string
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
An Environment represents a connection to an ODBC driver.
This function calls the SQLAllocHandle to create the environment. It also call SQLEnvSetAttr to set the version of ODBC to version 3.
func NewEnvironment ¶
func NewEnvironment() (*Environment, error)
Example ¶
// Allocate a new environement. env, _ := NewEnvironment() // Ensure that the environment is released when the function returns. defer env.Free()
Output:
func (*Environment) Connect ¶
func (e *Environment) Connect(server, username, authentication string) (*Connection, error)
Connect is a helper function that allocates a connection, and then attempts to connect to a data source.
func (*Environment) ConnectionPooling ¶
func (e *Environment) ConnectionPooling() (ConnectionPooling, error)
ConnectionPooling returns a value indicating how the environment handles connection pooling.
This member wraps behaviour of calling SQLGetEnvAttr.
func (*Environment) ConnectionPoolingMatch ¶
func (e *Environment) ConnectionPoolingMatch() (ConnectionPoolingMatch, error)
ConnectionPoolingMatchs returns a value indicating how the environment matches different connection handles to determine if they can be interchanged.
This member wraps the C API function SQLGetEnvAttr.
func (*Environment) DataSources ¶
func (e *Environment) DataSources() ([]DataSource, error)
Drivers returns a description of the data sources installed on the system.
This function wraps the C API function SQLDataSources.
func (*Environment) DriverConnect ¶
func (e *Environment) DriverConnect(dsn string) (*Connection, error)
DriverConnect is a helper function that allocates a connection, and then attempts to connect to a data source using a DSN.
Example ¶
// Allocate a new environement. env, _ := NewEnvironment() // Ensure that the environment is released when the function returns. defer env.Free() // Connect to a data source using a known DSN dbc, _ := env.DriverConnect(DSN) // Ensure that the connection is released defer dbc.Free()
Output:
func (*Environment) Drivers ¶
func (e *Environment) Drivers() ([]Driver, error)
Drivers returns a description of the drivers installed on the system.
This function wraps the C API function SQLDrivers.
func (*Environment) EndTransaction ¶
func (e *Environment) EndTransaction(completion CompletionType) error
EndTransaction either performs either a commit or a rollback of the current transaction.
This function wraps the C API function SQLEndTran.
func (*Environment) Free ¶
func (e *Environment) Free() error
Free releases all resources associated with the environment.
This function wraps the C API function SQLFreeHandle.
func (*Environment) NewConnection ¶
func (e *Environment) NewConnection() (*Connection, error)
Example ¶
// Allocate a new environement. env, _ := NewEnvironment() // Ensure that the environment is released when the function returns. defer env.Free() // Allocate a new connection dbc, _ := env.NewConnection() // Ensure that the connection is released defer dbc.Free() // Connect to a datasource using a known DSN _ = dbc.DriverConnect(DSN)
Output:
func (*Environment) SetConnectionPooling ¶
func (e *Environment) SetConnectionPooling(value ConnectionPooling) error
SetConnectionPooling modifies how the environment handles connection pooling.
This member wraps behaviour of calling SQLSetEnvAttr.
func (*Environment) SetConnectionPoolingMatch ¶
func (e *Environment) SetConnectionPoolingMatch(value ConnectionPoolingMatch) error
SetConnectionPoolingMatch modifies how the environment matches different connection handles to determine if they can be interchanged
This member wraps behaviour of calling SQLSetEnvAttr.
type Error ¶
type Error struct { // A five-character SQLSTATE code SQLState string // A native error code, which is specific to the data source NativeError int // A user readable description of the error. ErrorMessage string }
An Error contains information about a ODBC access failure.
This type is used to wrap information returned from SQLGetDiagRec.
type FetchDirection ¶
type FetchDirection uint16
A FetchDirection is used to control record seeking in calls to FetchScroll.
type FieldIdentifier ¶
type FieldIdentifier uint16
A FieldIdentifier is used to select various attributes that can be reported when using the member function ColAttributeNumeric or ColAttributeString.
type HandleType ¶
type HandleType int16
A HandleType enumerates the different type of handle used by the ODBC API. Most users should not need to use this type.
type InputOutputType ¶
type InputOutputType int16
type Kind ¶
type Kind int16
A Kind represents the specific data type of a parameter or column used in internally by an SQL query. Most users of this package should not need to use these parameters.
type Statement ¶
type Statement struct {
// contains filtered or unexported fields
}
A Statement represents a database query, either in progress or in preparation.
func (*Statement) BindParameter ¶
BindParameter connects a value in Go to a parameter marker in an SQL statement.
This function bind the actual memory location to the parameter. Therefore, it does not accept, for example, a value of type int64, but will accept a value that is a pointer to an int64. That memory location must live as long as the parameter is bound. Failure to follow this rule will result in invalid memory accesses.
This restriction that the underlying memory must exist also applies to slices of bytes and to strings. However, for those values, the memory backing the value is used, so points are not expected.
The following types are accepted for the value: nil, *int32, *int64, *float64, []byte, string, *odbc3.Date, *odbc3.Time, and *odbc3.Timestamp.
This function wraps the C API function SQLBindParameter.
func (*Statement) BindParameterBinary ¶
BindParameterBinary connects a slice of bytes to a parameter marker in an SQL statement. The value has type BINARY in the SQL statement.
Please look at the documentation for BindParameter for further instructions.
This function wraps the C API function SQLBindParameter.
func (*Statement) BindParameterBinaryString ¶
BindParameterBinaryString connects a string value in Go to a parameter marker in an SQL statement. The value has type BINARY in the SQL statement.
Please look at the documentation for BindParameter for further instructions.
This function wraps the C API function SQLBindParameter.
func (*Statement) BindParameterBytes ¶
BindParameterBytes connects a slice of bytes to a parameter marker in an SQL statement. The value has type CHAR in the SQL statement.
Please look at the documentation for BindParameter for further instructions.
This function wraps the C API function SQLBindParameter.
func (*Statement) BindParameterDate ¶
BindParameterDate connects a odbc3.Date value in Go to a parameter marker in an SQL statement. The value has type DATE in the SQL statement.
Please look at the documentation for BindParameter for further instructions.
This function wraps the C API function SQLBindParameter.
func (*Statement) BindParameterFloat64 ¶
BindParameterFloat64 connects a float64 value in Go to a parameter marker in an SQL statement. The value has type DOUBLE in the SQL statement.
Please look at the documentation for BindParameter for further instructions.
This function wraps the C API function SQLBindParameter.
func (*Statement) BindParameterInt32 ¶
BindParameterInt32 connects an int32 value in Go to a parameter marker in an SQL statement. The value has type INTEGER in the SQL statement.
Please look at the documentation for BindParameter for further instructions.
This function wraps the C API function SQLBindParameter.
func (*Statement) BindParameterInt64 ¶
BindParameterInt64 connects an int64 value in Go to a parameter marker in an SQL statement. The value has type BIGINT in the SQL statement.
Please look at the documentation for BindParameter for further instructions.
This function wraps the C API function SQLBindParameter.
func (*Statement) BindParameterNull ¶
BindParamaterNull binds the value NULL to a parameter marker in an SQL statement.
This function wraps the C API function SQLBindParameter.
Example ¶
// Allocate a new environment. env, _ := NewEnvironment() // Ensure that the environment is released when the function returns. defer env.Free() // Allocate a new connection dbc, _ := env.DriverConnect(DSN) // Ensure that the connection is released. defer dbc.Free() // Prepare an SQL statement. // Note that the question marks indicates a parameter to be bound at a later date. stmt, _ := dbc.Prepare("SELECT ?;") defer stmt.Free() // Bind a NULL value to the one open parameter. _ = stmt.BindParameterNull(1) // Execute the statement _ = stmt.Execute() // Once executed, we can determine how many columns exist in the result. nr, _ := stmt.NumResultCols() fmt.Println("Number of columns is: ", nr) // Let's fetch our row of data ok, _ := stmt.Fetch() if !ok { fmt.Println("Oops. How did this happen?") } result, err := stmt.GetData(1) if err != nil { fmt.Println("There was some problem with fetching or converting the data.", err) } fmt.Println("Value is: ", result /* should be a nil interface, which represents NULL */)
Output: Number of columns is: 1 Value is: <nil>
func (*Statement) BindParameterString ¶
BindParameterString connects a string value in Go to a parameter marker in an SQL statement. The value has type CHAR in the SQL statement.
Please look at the documentation for BindParameter for further instructions.
This function wraps the C API function SQLBindParameter.
func (*Statement) BindParameterTime ¶
BindParameterTime connects a odbc3.Time value in Go to a parameter marker in an SQL statement. The value has type TIME in the SQL statement.
Please look at the documentation for BindParameter for further instructions.
This function wraps the C API function SQLBindParameter.
func (*Statement) BindParameterTimestamp ¶
BindParameterTimestamp connects a odbc3.Timestamp value in Go to a parameter marker in an SQL statement. The value has type TIMESTAMP in the SQL statement.
Please look at the documentation for BindParameter for further instructions.
This function wraps the C API function SQLBindParameter.
func (*Statement) Cancel ¶
Cancel cancels processing of the statement
This function wraps the C API function SQLCancel.
func (*Statement) ColAttributeNumeric ¶
func (s *Statement) ColAttributeNumeric(index int, field FieldIdentifier) (uint32, error)
ColAttributeString returns information about attributes of a column, but only when that attribute is a number.
This function wraps the C API function SQLColAttribute.
func (*Statement) ColAttributeString ¶
func (s *Statement) ColAttributeString(index int, field FieldIdentifier) (string, error)
ColAttributeString returns information about attributes of a column, but only when that attribute is a string.
This function wraps the C API function SQLColAttribute.
func (*Statement) ExecDirect ¶
ExecDirection prepares and then immediately executes the SQL statement.
This function wraps the C API function SQLExecDirect.
func (*Statement) Execute ¶
Execute runs the already prepared SQL statement. Values from bound parameters are used as to complete the statement.
This function wraps the C API function SQLExecute.
func (*Statement) Fetch ¶
Fetch obtains the next row of the dataset.
This function wraps the C API function SQLFetch.
func (*Statement) FetchScroll ¶
func (s *Statement) FetchScroll(orientation FetchDirection, offset int32) (ok bool, err error)
FetchScroll obtains the requested row of the dataset.
This function wraps the C API function SQLFetchScroll.
func (*Statement) Free ¶
Free releases all resources associated with the statement.
This function wraps the C API function SQLFreeHandle.
func (*Statement) GetData ¶
GetData retrieves data from a single column.
This function determines the type of the SQL value of the requested column, and coerces that value into the nearest Go type.
If the SQL data is NULL, than this function will return a nil interface.
This function wraps the C API function SQLGetData.
func (*Statement) GetDataBool ¶
GetDataBool retrieves data from a single column, and attempts to coerce that value into an bool.
This function wraps the C API function SQLGetData.
func (*Statement) GetDataBytes ¶
GetDataBytes retrieves data from a single column, and attempts to coerce that value into a slice of bytes.
If the SQL data is NULL, than the byte slice will be nil.
This function wraps the C API function SQLGetData.
func (*Statement) GetDataFloat64 ¶
GetDataFloat64 retrieves data from a single column, and attempts to coerce that value into an float64.
If the SQL data is null, this function will return 0. Additionally, the second return value will be true.
This function wraps the C API function SQLGetData.
func (*Statement) GetDataInt32 ¶
GetDataInt32 retrieves data from a single column, and attempts to coerce that value into an int32.
If the SQL data is null, this function will return 0. Additionally, the second return value will be true.
This function wraps the C API function SQLGetData.
func (*Statement) GetDataInt64 ¶
GetDataInt64 retrieves data from a single column, and attempts to coerce that value into an int64.
If the SQL data is null, this function will return 0. Additionally, the second return value will be true.
This function wraps the C API function SQLGetData.
Example ¶
// Allocate a new environment. env, _ := NewEnvironment() // Ensure that the environment is released when the function returns. defer env.Free() // Allocate a new connection dbc, _ := env.DriverConnect(DSN) // Ensure that the connection is released. defer dbc.Free() // Prepare and execute an SQL statement. stmt, _ := dbc.ExecDirect("SELECT 123456789;") defer stmt.Free() // Once executed, we can determine how many columns exist in the result. nr, _ := stmt.NumResultCols() fmt.Println("Number of columns is: ", nr) // Let's fetch our row of data ok, _ := stmt.Fetch() if !ok { fmt.Println("Oops. How did this happen?") } result, ok, err := stmt.GetDataInt64(1) if err != nil { fmt.Println("There was some problem with fetching or converting the data.", err) } else if !ok { fmt.Println("Somehow, our literal number become NULL.") } else { fmt.Println("Value is: ", result) }
Output: Number of columns is: 1 Value is: 123456789
func (*Statement) GetDataString ¶
GetDataString retrieves data from a single column, and attempts to coerce that value into an string.
This function wraps the C API function SQLGetData.
func (*Statement) GetDataTimestamp ¶
GetDataTimestamp retrieves data from a single column, and attempts to coerce that value into an time.Time.
This function wraps the C API function SQLGetData.
func (*Statement) NumParams ¶
NumParams returns the number of parameters required for the SQL statement.
This function wraps the C API function SQLNumParams.
Example ¶
// Allocate a new environement. env, _ := NewEnvironment() // Ensure that the environment is released when the function returns. defer env.Free() // Allocate a new connection dbc, _ := env.DriverConnect(DSN) // Ensure that the connection is released defer dbc.Free() // Prepare an SQL statement. // Note that the question marks indicates a parameter to be bound at a later date. stmt, _ := dbc.Prepare("SELECT * FROM atable WHERE c1=?;") defer stmt.Free() np, _ := stmt.NumParams() fmt.Println("Number of parameters is: ", np)
Output: Number of parameters is: 1
func (*Statement) NumResultCols ¶
NumResultCols returns the number of columns resulting from the SQL statement.
This function wraps the C API function SQLNumResultCols.
Example ¶
// Allocate a new environement. env, _ := NewEnvironment() // Ensure that the environment is released when the function returns. defer env.Free() // Allocate a new connection dbc, _ := env.DriverConnect(DSN) // Ensure that the connection is released defer dbc.Free() // Prepare an SQL statement. stmt, _ := dbc.Prepare("SELECT c1, c2 FROM atable;") defer stmt.Free() nr, _ := stmt.NumResultCols() fmt.Println("Number of columns is: ", nr)
Output: Number of columns is: 2