Documentation ¶
Overview ¶
Example (SqlConnector) ¶
// Example shows how to use OpenDB Connector // Normal NewConnector to localhost would look like: // connector := cql.NewConnector("127.0.0.1") // For testing, need to use additional variables connector := cql.NewConnector(cql.TestHostValid) db := sql.OpenDB(connector) // If you would like change some of the ClusterConfig options // https://godoc.org/github.com/gocql/gocql#ClusterConfig // Can do a type cast to get to them cqlConnector := connector.(*cql.CqlConnector) cqlConnector.ClusterConfig.Timeout = cql.TimeoutValid cqlConnector.ClusterConfig.ConnectTimeout = cql.ConnectTimeoutValid if cql.EnableAuthentication { passwordAuthenticator := gocql.PasswordAuthenticator{ Username: cql.Username, Password: cql.Password, } cqlConnector.ClusterConfig.Authenticator = passwordAuthenticator } ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second) defer cancel() rows, err := db.QueryContext(ctx, "select cql_version from system.local") if err != nil { fmt.Println("QueryContext error is not nil:", err) return } if !rows.Next() { fmt.Println("no Next rows") return } dest := make([]interface{}, 1) destPointer := make([]interface{}, 1) destPointer[0] = &dest[0] err = rows.Scan(destPointer...) if err != nil { fmt.Println("Scan error is not nil:", err) return } if len(dest) != 1 { fmt.Println("len dest != 1") return } data, ok := dest[0].(string) if !ok { fmt.Println("dest type not string") return } if len(data) < 3 { fmt.Println("data string len too small") return } if rows.Next() { fmt.Println("has Next rows") return } err = rows.Close() if err != nil { fmt.Println("Close error is not nil:", err) return } err = rows.Err() if err != nil { fmt.Println("Err error is not nil:", err) return } err = db.Close() if err != nil { fmt.Println("Close error is not nil:", err) return } fmt.Println("received cql_version from system.local")
Output: received cql_version from system.local
Example (SqlSelect) ¶
// Example shows how to do a basic select openString := cql.TestHostValid + "?timeout=" + cql.TimeoutValidString + "&connectTimeout=" + cql.ConnectTimeoutValidString if cql.EnableAuthentication { openString += "&username=" + cql.Username + "&password=" + cql.Password } // A normal simple Open to localhost would look like: // db, err := sql.Open("cql", "127.0.0.1") // For testing, need to use additional variables db, err := sql.Open("cql", openString) if err != nil { fmt.Printf("Open error is not nil: %v", err) return } if db == nil { fmt.Println("db is nil") return } ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second) defer cancel() rows, err := db.QueryContext(ctx, "select cql_version from system.local") if err != nil { fmt.Println("QueryContext error is not nil:", err) return } if !rows.Next() { fmt.Println("no Next rows") return } dest := make([]interface{}, 1) destPointer := make([]interface{}, 1) destPointer[0] = &dest[0] err = rows.Scan(destPointer...) if err != nil { fmt.Println("Scan error is not nil:", err) return } if len(dest) != 1 { fmt.Println("len dest != 1") return } data, ok := dest[0].(string) if !ok { fmt.Println("dest type not string") return } if len(data) < 3 { fmt.Println("data string len too small") return } if rows.Next() { fmt.Println("has Next rows") return } err = rows.Close() if err != nil { fmt.Println("Close error is not nil:", err) return } err = rows.Err() if err != nil { fmt.Println("Err error is not nil:", err) return } err = db.Close() if err != nil { fmt.Println("Close error is not nil:", err) return } fmt.Println("received cql_version from system.local")
Output: received cql_version from system.local
Example (SqlStatement) ¶
// Example shows how to use database statement openString := cql.TestHostValid + "?timeout=" + cql.TimeoutValidString + "&connectTimeout=" + cql.ConnectTimeoutValidString if cql.EnableAuthentication { openString += "&username=" + cql.Username + "&password=" + cql.Password } // A normal simple Open to localhost would look like: // db, err := sql.Open("cql", "127.0.0.1") // For testing, need to use additional variables db, err := sql.Open("cql", openString) if err != nil { fmt.Printf("Open error is not nil: %v", err) return } if db == nil { fmt.Println("db is nil") return } ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second) stmt, err := db.PrepareContext(ctx, "select cql_version from system.local") cancel() if err != nil { fmt.Println("PrepareContext error is not nil:", err) return } if stmt == nil { fmt.Println("stmt is nil") return } ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) defer cancel() rows, err := stmt.QueryContext(ctx) if err != nil { fmt.Println("QueryContext error is not nil:", err) return } if !rows.Next() { fmt.Println("no Next rows") return } dest := make([]interface{}, 1) destPointer := make([]interface{}, 1) destPointer[0] = &dest[0] err = rows.Scan(destPointer...) if err != nil { fmt.Println("Scan error is not nil:", err) return } if len(dest) != 1 { fmt.Println("len dest != 1") return } data, ok := dest[0].(string) if !ok { fmt.Println("dest type not string") return } if len(data) < 3 { fmt.Println("data string len too small") return } if rows.Next() { fmt.Println("has Next rows") return } err = rows.Close() if err != nil { fmt.Println("Close error is not nil:", err) return } err = rows.Err() if err != nil { fmt.Println("Err error is not nil:", err) return } err = db.Close() if err != nil { fmt.Println("Close error is not nil:", err) return } fmt.Println("received cql_version from system.local")
Output: received cql_version from system.local
Index ¶
- Variables
- func ClusterConfigToConfigString(clusterConfig *gocql.ClusterConfig) string
- func ConfigStringToClusterConfig(configString string) (*gocql.ClusterConfig, error)
- func DurationToDuration(cqlDuration gocql.Duration) time.Duration
- func InterfaceToDuration(aInterface interface{}) time.Duration
- func NewClusterConfig(hosts ...string) *gocql.ClusterConfig
- func NewConnector(hosts ...string) driver.Connector
- type CqlConnector
- type CqlDriverStruct
- type CqlStmt
- func (cqlStmt *CqlStmt) Close() error
- func (cqlStmt *CqlStmt) ColumnConverter(index int) driver.ValueConverter
- func (cqlStmt *CqlStmt) Exec(args []driver.Value) (driver.Result, error)
- func (cqlStmt *CqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)
- func (cqlStmt *CqlStmt) NumInput() int
- func (cqlStmt *CqlStmt) Query(args []driver.Value) (driver.Rows, error)
- func (cqlStmt *CqlStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotSupported is returned for any method that is not supported ErrNotSupported = fmt.Errorf("not supported") // ErrNotImplementedYet is returned for any method that is not implemented yet ErrNotImplementedYet = fmt.Errorf("not implemented yet") // ErrQueryIsNil is returned when a query is nil ErrQueryIsNil = fmt.Errorf("query is nil") // ErrNamedValuesNotSupported is returned when values are named. Named values are not supported. ErrNamedValuesNotSupported = fmt.Errorf("named values not supported") // ErrOrdinalOutOfRange is returned when values ordinal is out of range ErrOrdinalOutOfRange = fmt.Errorf("ordinal out of range") // CqlDriver is the sql driver CqlDriver = &CqlDriverStruct{ Logger: log.New(os.Stderr, "cql ", log.Ldate|log.Ltime|log.LUTC|log.Lshortfile), } )
var DbConsistency = map[gocql.Consistency]string{ gocql.Any: "any", gocql.One: "one", gocql.Two: "two", gocql.Three: "three", gocql.Quorum: "quorum", gocql.All: "all", gocql.LocalQuorum: "localQuorum", gocql.EachQuorum: "eachQuorum", gocql.LocalOne: "localOne", }
DbConsistency maps gocql consistency levels to string
var DbConsistencyLevels = map[string]gocql.Consistency{ "any": gocql.Any, "one": gocql.One, "two": gocql.Two, "three": gocql.Three, "quorum": gocql.Quorum, "all": gocql.All, "localQuorum": gocql.LocalQuorum, "eachQuorum": gocql.EachQuorum, "localOne": gocql.LocalOne, }
DbConsistencyLevels maps string to gocql consistency levels
Functions ¶
func ClusterConfigToConfigString ¶
func ClusterConfigToConfigString(clusterConfig *gocql.ClusterConfig) string
ClusterConfigToConfigString converts a gocql ClusterConfig to a config string https://godoc.org/github.com/gocql/gocql#ClusterConfig
func ConfigStringToClusterConfig ¶
func ConfigStringToClusterConfig(configString string) (*gocql.ClusterConfig, error)
ConfigStringToClusterConfig converts a config string to a gocql ClusterConfig
func DurationToDuration ¶
DurationToDuration converts gocql.Duration type to time.Duration. Does not check for overflow
func InterfaceToDuration ¶
InterfaceToDuration converts an interface of gocql.Duration type to time.Duration. Does not check for overflow. Returns 0 if interface is not gocql.Duration
func NewClusterConfig ¶
func NewClusterConfig(hosts ...string) *gocql.ClusterConfig
NewClusterConfig returns a new gocql ClusterConfig
func NewConnector ¶
NewConnector returns a new database connector
Types ¶
type CqlConnector ¶
type CqlConnector struct { // Logger is used to log connection ping errors Logger *log.Logger // ClusterConfig is used for changing config options // https://godoc.org/github.com/gocql/gocql#ClusterConfig ClusterConfig *gocql.ClusterConfig }
CqlConnector is the sql driver connector
func (*CqlConnector) Driver ¶
func (cqlConnector *CqlConnector) Driver() driver.Driver
Driver returns the cql driver
type CqlDriverStruct ¶
CqlDriverStruct is the sql driver
func (*CqlDriverStruct) Open ¶
func (cqlDriver *CqlDriverStruct) Open(configString string) (driver.Conn, error)
Open returns a new database connection
func (*CqlDriverStruct) OpenConnector ¶
func (cqlDriver *CqlDriverStruct) OpenConnector(configString string) (driver.Connector, error)
OpenConnector returns a new database connector
type CqlStmt ¶
type CqlStmt struct { // CqlQuery is used for changing query options // https://godoc.org/github.com/gocql/gocql#Query // This will only work if Go sql every gives access to the driver CqlQuery *gocql.Query }
CqlStmt is the sql driver statement
func (*CqlStmt) ColumnConverter ¶
func (cqlStmt *CqlStmt) ColumnConverter(index int) driver.ValueConverter
ColumnConverter provides driver ValueConverter for statement
func (*CqlStmt) ExecContext ¶
func (cqlStmt *CqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)
ExecContext executes a statement with context
func (*CqlStmt) QueryContext ¶
func (cqlStmt *CqlStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)
QueryContext queries a statement with context