├── .gitignore ├── LICENSE ├── README.md ├── cassandra └── cassandra.go └── examples └── basic.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | 25 | examples -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | golang-driver 2 | ============= 3 | 4 | Golang wrapper of the DataStax/Cassandra [C/C++ driver](https://github.com/datastax/cpp-driver) 5 | 6 | Basic support for prepared statements and ad hoc queries. Lacking support for collections, but that will be remedied shortly. 7 | 8 | ### Build 9 | 10 | 1. Build and install the DataStax [C/C++ driver](https://github.com/datastax/cpp-driver) 11 | 1. Install `go get github.com/mstump/golang-driver/cassandra` 12 | 1. Run the example `go run $GOPATH/src/github.com/mstump/golang-driver/examples/basic.go` 13 | 14 | ### Example Usage 15 | 16 | ```go 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "golang-driver/cassandra" 22 | ) 23 | 24 | func main() { 25 | cluster := cassandra.NewCluster() 26 | cluster.SetContactPoints("cassandra") 27 | defer cluster.Finalize() 28 | 29 | session := cassandra.NewSession() 30 | defer session.Finalize() 31 | 32 | sessfuture := cluster.SessionConnect(session) 33 | sessfuture.Wait() 34 | defer sessfuture.Finalize() 35 | 36 | statement := cassandra.NewStatement("select cluster_name from system.local;", 0) 37 | defer statement.Finalize() 38 | 39 | stmtfuture := session.Execute(statement) 40 | stmtfuture.Wait() 41 | defer stmtfuture.Finalize() 42 | 43 | result := stmtfuture.Result() 44 | defer result.Finalize() 45 | 46 | fmt.Printf("Clusters:\r\n") 47 | for result.Next() { 48 | var clusterName string 49 | result.Scan(&clusterName) 50 | fmt.Printf("%s\n", clusterName) 51 | } 52 | 53 | fmt.Printf("DONE.\r\n") 54 | } 55 | ``` 56 | -------------------------------------------------------------------------------- /cassandra/cassandra.go: -------------------------------------------------------------------------------- 1 | package cassandra 2 | 3 | // #cgo CFLAGS: -I/usr/local/include 4 | // #cgo LDFLAGS: -lcassandra -L/usr/local/lib 5 | // #include 6 | // #include 7 | import "C" 8 | import "unsafe" 9 | import "errors" 10 | import "reflect" 11 | 12 | const ( 13 | CASS_OK = 0 14 | ) 15 | 16 | const ( 17 | CASS_ERROR_SOURCE_NONE = iota 18 | CASS_ERROR_SOURCE_LIB 19 | CASS_ERROR_SOURCE_SERVER 20 | CASS_ERROR_SOURCE_SSL 21 | CASS_ERROR_SOURCE_COMPRESSION 22 | ) 23 | 24 | const ( 25 | CASS_ERROR_LIB_BAD_PARAMS = iota 26 | CASS_ERROR_LIB_NO_STREAMS 27 | CASS_ERROR_LIB_UNABLE_TO_INIT 28 | CASS_ERROR_LIB_MESSAGE_ENCODE 29 | CASS_ERROR_LIB_HOST_RESOLUTION 30 | CASS_ERROR_LIB_UNEXPECTED_RESPONSE 31 | CASS_ERROR_LIB_REQUEST_QUEUE_FULL 32 | CASS_ERROR_LIB_NO_AVAILABLE_IO_THREAD 33 | CASS_ERROR_LIB_WRITE_ERROR 34 | CASS_ERROR_LIB_NO_HOSTS_AVAILABLE 35 | CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS 36 | CASS_ERROR_LIB_INVALID_ITEM_COUNT 37 | CASS_ERROR_LIB_INVALID_VALUE_TYPE 38 | CASS_ERROR_LIB_REQUEST_TIMED_OUT 39 | CASS_ERROR_LIB_UNABLE_TO_SET_KEYSPACE 40 | CASS_ERROR_LIB_CALLBACK_ALREADY_SET 41 | CASS_ERROR_LIB_INVALID_STATEMENT_TYPE 42 | CASS_ERROR_LIB_NAME_DOES_NOT_EXIST 43 | CASS_ERROR_LIB_UNABLE_TO_DETERMINE_PROTOCOL 44 | CASS_ERROR_LIB_NULL_VALUE 45 | CASS_ERROR_LIB_NOT_IMPLEMENTED 46 | CASS_ERROR_LIB_UNABLE_TO_CONNECT 47 | CASS_ERROR_LIB_UNABLE_TO_CLOSE 48 | CASS_ERROR_SERVER_SERVER_ERROR 49 | CASS_ERROR_SERVER_PROTOCOL_ERROR 50 | CASS_ERROR_SERVER_BAD_CREDENTIALS 51 | CASS_ERROR_SERVER_UNAVAILABLE 52 | CASS_ERROR_SERVER_OVERLOADED 53 | CASS_ERROR_SERVER_IS_BOOTSTRAPPING 54 | CASS_ERROR_SERVER_TRUNCATE_ERROR 55 | CASS_ERROR_SERVER_WRITE_TIMEOUT 56 | CASS_ERROR_SERVER_READ_TIMEOUT 57 | CASS_ERROR_SERVER_SYNTAX_ERROR 58 | CASS_ERROR_SERVER_UNAUTHORIZED 59 | CASS_ERROR_SERVER_INVALID_QUERY 60 | CASS_ERROR_SERVER_CONFIG_ERROR 61 | CASS_ERROR_SERVER_ALREADY_EXISTS 62 | CASS_ERROR_SERVER_UNPREPARED 63 | CASS_ERROR_SSL_INVALID_CERT 64 | CASS_ERROR_SSL_INVALID_PRIVATE_KEY 65 | CASS_ERROR_SSL_NO_PEER_CERT 66 | CASS_ERROR_SSL_INVALID_PEER_CERT 67 | CASS_ERROR_SSL_IDENTITY_MISMATCH 68 | ) 69 | 70 | const ( 71 | CASS_VALUE_TYPE_UNKNOWN = 0xFFFF 72 | CASS_VALUE_TYPE_CUSTOM = 0x0000 73 | CASS_VALUE_TYPE_ASCII = 0x0001 74 | CASS_VALUE_TYPE_BIGINT = 0x0002 75 | CASS_VALUE_TYPE_BLOB = 0x0003 76 | CASS_VALUE_TYPE_BOOLEAN = 0x0004 77 | CASS_VALUE_TYPE_COUNTER = 0x0005 78 | CASS_VALUE_TYPE_DECIMAL = 0x0006 79 | CASS_VALUE_TYPE_DOUBLE = 0x0007 80 | CASS_VALUE_TYPE_FLOAT = 0x0008 81 | CASS_VALUE_TYPE_INT = 0x0009 82 | CASS_VALUE_TYPE_TEXT = 0x000A 83 | CASS_VALUE_TYPE_TIMESTAMP = 0x000B 84 | CASS_VALUE_TYPE_UUID = 0x000C 85 | CASS_VALUE_TYPE_VARCHAR = 0x000D 86 | CASS_VALUE_TYPE_VARINT = 0x000E 87 | CASS_VALUE_TYPE_TIMEUUID = 0x000F 88 | CASS_VALUE_TYPE_INET = 0x0010 89 | CASS_VALUE_TYPE_LIST = 0x0020 90 | CASS_VALUE_TYPE_MAP = 0x0021 91 | CASS_VALUE_TYPE_SET = 0x0022 92 | ) 93 | 94 | const ( 95 | CASS_LOG_DISABLED = iota 96 | CASS_LOG_CRITICAL 97 | CASS_LOG_ERROR 98 | CASS_LOG_WARN 99 | CASS_LOG_INFO 100 | CASS_LOG_DEBUG 101 | CASS_LOG_TRACE 102 | ) 103 | 104 | type Cluster struct { 105 | cptr *C.struct_CassCluster_ 106 | } 107 | 108 | type Future struct { 109 | cptr *C.struct_CassFuture_ 110 | } 111 | 112 | type Session struct { 113 | cptr *C.struct_CassSession_ 114 | } 115 | 116 | type Result struct { 117 | iter *C.struct_CassIterator_ 118 | cptr *C.struct_CassResult_ 119 | } 120 | 121 | type Prepared struct { 122 | cptr *C.struct_CassPrepared_ 123 | } 124 | 125 | type Statement struct { 126 | cptr *C.struct_CassStatement_ 127 | } 128 | 129 | type Uuid struct { 130 | uuid C.struct_CassUuid_ 131 | } 132 | 133 | type UuidGenerator struct { 134 | cptr *C.struct_CassUuidGen_ 135 | } 136 | 137 | type Metrics struct { 138 | Requests struct { 139 | Min int64 140 | Max int64 141 | Mean int64 142 | Stddev int64 143 | Median int64 144 | Percentile75th int64 145 | Percentile95th int64 146 | Percentile98th int64 147 | Percentile99th int64 148 | Percentile999th int64 149 | MeanRate float64 150 | OneMinuteRate float64 151 | FiveMinuteRate float64 152 | FifteenMinuteRate float64 153 | } 154 | 155 | Stats struct { 156 | TotalConnections int64 157 | AvailableConnections int64 158 | ExceededPendingRequestsWaterMark int64 159 | ExceededWriteBytesWaterMark int64 160 | } 161 | 162 | Errors struct { 163 | ConnectionTimeouts int64 164 | PendingRequestTimeouts int64 165 | RequestTimeouts int64 166 | } 167 | } 168 | 169 | func SetLogLevel(level int32) { 170 | clevel := C.CASS_LOG_DISABLED 171 | switch level { 172 | case CASS_LOG_DISABLED: 173 | clevel = C.CASS_LOG_DISABLED 174 | case CASS_LOG_CRITICAL: 175 | clevel = C.CASS_LOG_CRITICAL 176 | case CASS_LOG_ERROR: 177 | clevel = C.CASS_LOG_ERROR 178 | case CASS_LOG_WARN: 179 | clevel = C.CASS_LOG_WARN 180 | case CASS_LOG_INFO: 181 | clevel = C.CASS_LOG_INFO 182 | case CASS_LOG_DEBUG: 183 | clevel = C.CASS_LOG_DEBUG 184 | case CASS_LOG_TRACE: 185 | clevel = C.CASS_LOG_TRACE 186 | } 187 | C.cass_log_set_level(C.CassLogLevel(clevel)) 188 | } 189 | 190 | func NewCluster() *Cluster { 191 | cluster := new(Cluster) 192 | cluster.cptr = C.cass_cluster_new() 193 | // defer cluster.Finalize() 194 | 195 | return cluster 196 | } 197 | 198 | func NewSession() *Session { 199 | session := new(Session) 200 | session.cptr = C.cass_session_new() 201 | return session 202 | } 203 | 204 | func (session *Session) Metrics() Metrics { 205 | 206 | var cmetrics C.CassMetrics 207 | C.cass_session_get_metrics(session.cptr, &cmetrics) 208 | 209 | var output Metrics 210 | output.Requests.Min = int64(cmetrics.requests.min) 211 | output.Requests.Max = int64(cmetrics.requests.max) 212 | output.Requests.Mean = int64(cmetrics.requests.mean) 213 | output.Requests.Stddev = int64(cmetrics.requests.stddev) 214 | output.Requests.Median = int64(cmetrics.requests.median) 215 | output.Requests.Percentile75th = int64(cmetrics.requests.percentile_75th) 216 | output.Requests.Percentile95th = int64(cmetrics.requests.percentile_95th) 217 | output.Requests.Percentile99th = int64(cmetrics.requests.percentile_99th) 218 | output.Requests.Percentile999th = int64(cmetrics.requests.percentile_999th) 219 | output.Requests.MeanRate = float64(cmetrics.requests.mean_rate) 220 | output.Requests.OneMinuteRate = float64(cmetrics.requests.one_minute_rate) 221 | output.Requests.FiveMinuteRate = float64(cmetrics.requests.five_minute_rate) 222 | output.Requests.FifteenMinuteRate = float64(cmetrics.requests.fifteen_minute_rate) 223 | 224 | output.Stats.TotalConnections = int64(cmetrics.stats.total_connections) 225 | output.Stats.AvailableConnections = int64(cmetrics.stats.available_connections) 226 | output.Stats.ExceededPendingRequestsWaterMark = int64(cmetrics.stats.exceeded_pending_requests_water_mark) 227 | output.Stats.ExceededWriteBytesWaterMark = int64(cmetrics.stats.exceeded_write_bytes_water_mark) 228 | 229 | output.Errors.ConnectionTimeouts = int64(cmetrics.errors.connection_timeouts) 230 | output.Errors.PendingRequestTimeouts = int64(cmetrics.errors.pending_request_timeouts) 231 | output.Errors.RequestTimeouts = int64(cmetrics.errors.request_timeouts) 232 | 233 | return output 234 | } 235 | 236 | func NewStatement(query string, param_count int) *Statement { 237 | cs := C.CString(query) 238 | defer C.free(unsafe.Pointer(cs)) 239 | 240 | statement := new(Statement) 241 | statement.cptr = C.cass_statement_new(cs, C.size_t(param_count)) 242 | return statement 243 | } 244 | 245 | func NewUuidGenerator() *UuidGenerator { 246 | generator := new(UuidGenerator) 247 | generator.cptr = C.cass_uuid_gen_new() 248 | return generator 249 | } 250 | 251 | func NewUuidGeneratorWithNode(node uint64) *UuidGenerator { 252 | generator := new(UuidGenerator) 253 | generator.cptr = C.cass_uuid_gen_new_with_node(C.cass_uint64_t(node)) 254 | return generator 255 | } 256 | 257 | func (generator *UuidGenerator) GenTime() Uuid { 258 | var uuid Uuid 259 | C.cass_uuid_gen_time(generator.cptr, &uuid.uuid) 260 | return uuid 261 | } 262 | 263 | func (generator *UuidGenerator) GenRandom() Uuid { 264 | var uuid Uuid 265 | C.cass_uuid_gen_random(generator.cptr, &uuid.uuid) 266 | return uuid 267 | } 268 | 269 | func (generator *UuidGenerator) FromTime(timestamp uint64) Uuid { 270 | var uuid Uuid 271 | C.cass_uuid_gen_from_time(generator.cptr, C.cass_uint64_t(timestamp), &uuid.uuid) 272 | return uuid 273 | } 274 | 275 | func (prepared *Prepared) Bind() *Statement { 276 | statement := new(Statement) 277 | statement.cptr = C.cass_prepared_bind(prepared.cptr) 278 | // defer statement.Finalize() 279 | return statement 280 | } 281 | 282 | func (statement *Statement) Bind(args ...interface{}) error { 283 | var err C.CassError = C.CASS_OK 284 | 285 | for i, v := range args { 286 | 287 | switch v := v.(type) { 288 | 289 | case nil: 290 | err = C.cass_statement_bind_null(statement.cptr, C.size_t(i)) 291 | 292 | case int32: 293 | err = C.cass_statement_bind_int32(statement.cptr, C.size_t(i), C.cass_int32_t(v)) 294 | 295 | case int64: 296 | err = C.cass_statement_bind_int64(statement.cptr, C.size_t(i), C.cass_int64_t(v)) 297 | 298 | case float32: 299 | err = C.cass_statement_bind_float(statement.cptr, C.size_t(i), C.cass_float_t(v)) 300 | 301 | case float64: 302 | err = C.cass_statement_bind_double(statement.cptr, C.size_t(i), C.cass_double_t(v)) 303 | 304 | case bool: 305 | if v { 306 | err = C.cass_statement_bind_bool(statement.cptr, C.size_t(i), 1) 307 | } else { 308 | err = C.cass_statement_bind_bool(statement.cptr, C.size_t(i), 0) 309 | } 310 | 311 | case string: 312 | err = bind_string(statement, i, v) 313 | 314 | case []byte: 315 | err = C.cass_statement_bind_bytes(statement.cptr, C.size_t(i), (*C.cass_byte_t)(unsafe.Pointer(&v)), C.size_t(len(v))) 316 | 317 | case Uuid: 318 | C.cass_statement_bind_uuid(statement.cptr, C.size_t(i), v.uuid) 319 | } 320 | 321 | } 322 | 323 | if err != C.CASS_OK { 324 | return errors.New(C.GoString(C.cass_error_desc(err))) 325 | } 326 | 327 | return nil 328 | } 329 | 330 | func bind_string(statement *Statement, index int, v string) C.CassError { 331 | cs := C.CString(v) 332 | defer C.free(unsafe.Pointer(cs)) 333 | return C.cass_statement_bind_string(statement.cptr, C.size_t(index), C.CString(v)) 334 | } 335 | 336 | func (cluster *Cluster) Finalize() { 337 | C.cass_cluster_free(cluster.cptr) 338 | cluster.cptr = nil 339 | } 340 | 341 | func (session *Session) Finalize() { 342 | C.cass_session_free(session.cptr) 343 | session.cptr = nil 344 | } 345 | 346 | func (future *Future) Finalize() { 347 | C.cass_future_free(future.cptr) 348 | future.cptr = nil 349 | } 350 | 351 | func (result *Result) Finalize() { 352 | C.cass_result_free(result.cptr) 353 | result.cptr = nil 354 | } 355 | 356 | func (prepared *Prepared) Finalize() { 357 | C.cass_prepared_free(prepared.cptr) 358 | prepared.cptr = nil 359 | } 360 | 361 | func (statement *Statement) Finalize() { 362 | C.cass_statement_free(statement.cptr) 363 | statement.cptr = nil 364 | } 365 | 366 | func (generator *UuidGenerator) Finalize() { 367 | C.cass_uuid_gen_free(generator.cptr) 368 | generator.cptr = nil 369 | } 370 | 371 | func (future *Future) Result() *Result { 372 | result := new(Result) 373 | result.cptr = C.cass_future_get_result(future.cptr) 374 | // defer result.Finalize() 375 | return result 376 | } 377 | 378 | func (future *Future) Prepared() *Prepared { 379 | prepared := new(Prepared) 380 | prepared.cptr = C.cass_future_get_prepared(future.cptr) 381 | // defer prepared.Finalize() 382 | return prepared 383 | } 384 | 385 | func (future *Future) Ready() bool { 386 | return C.cass_future_ready(future.cptr) == C.cass_true 387 | } 388 | 389 | func (future *Future) Wait() { 390 | C.cass_future_wait(future.cptr) 391 | } 392 | 393 | func (future *Future) WaitTimed(timeout uint64) bool { 394 | return C.cass_future_wait_timed(future.cptr, C.cass_duration_t(timeout)) == C.cass_true 395 | } 396 | 397 | func (future *Future) ErrorMessage() string { 398 | var message *C.char 399 | var message_length C.size_t 400 | C.cass_future_error_message(future.cptr, &message, &message_length) 401 | return C.GoString(message) 402 | } 403 | 404 | func (future *Future) ErrorSource() int { 405 | rc := C.cass_future_error_code(future.cptr) 406 | source := (rc >> 24) 407 | 408 | switch source { 409 | case C.CASS_ERROR_SOURCE_NONE: 410 | return CASS_OK 411 | case C.CASS_ERROR_SOURCE_LIB: 412 | return CASS_ERROR_SOURCE_LIB 413 | case C.CASS_ERROR_SOURCE_SERVER: 414 | return CASS_ERROR_SOURCE_SERVER 415 | case C.CASS_ERROR_SOURCE_SSL: 416 | return CASS_ERROR_SOURCE_SSL 417 | case C.CASS_ERROR_SOURCE_COMPRESSION: 418 | return CASS_ERROR_SOURCE_COMPRESSION 419 | } 420 | return CASS_ERROR_SOURCE_NONE 421 | } 422 | 423 | func (future *Future) ErrorCode() int { 424 | rc := C.cass_future_error_code(future.cptr) 425 | source := future.ErrorSource() 426 | 427 | if source == CASS_ERROR_SOURCE_NONE { 428 | return CASS_OK 429 | } else if source == CASS_ERROR_SOURCE_LIB { 430 | switch rc { 431 | case CASS_OK: 432 | return CASS_OK 433 | case C.CASS_ERROR_LIB_BAD_PARAMS: 434 | return CASS_ERROR_LIB_BAD_PARAMS 435 | case C.CASS_ERROR_LIB_NO_STREAMS: 436 | return CASS_ERROR_LIB_NO_STREAMS 437 | case C.CASS_ERROR_LIB_UNABLE_TO_INIT: 438 | return CASS_ERROR_LIB_UNABLE_TO_INIT 439 | case C.CASS_ERROR_LIB_MESSAGE_ENCODE: 440 | return CASS_ERROR_LIB_MESSAGE_ENCODE 441 | case C.CASS_ERROR_LIB_HOST_RESOLUTION: 442 | return CASS_ERROR_LIB_HOST_RESOLUTION 443 | case C.CASS_ERROR_LIB_UNEXPECTED_RESPONSE: 444 | return CASS_ERROR_LIB_UNEXPECTED_RESPONSE 445 | case C.CASS_ERROR_LIB_REQUEST_QUEUE_FULL: 446 | return CASS_ERROR_LIB_REQUEST_QUEUE_FULL 447 | case C.CASS_ERROR_LIB_NO_AVAILABLE_IO_THREAD: 448 | return CASS_ERROR_LIB_NO_AVAILABLE_IO_THREAD 449 | case C.CASS_ERROR_LIB_WRITE_ERROR: 450 | return CASS_ERROR_LIB_WRITE_ERROR 451 | case C.CASS_ERROR_LIB_NO_HOSTS_AVAILABLE: 452 | return CASS_ERROR_LIB_NO_HOSTS_AVAILABLE 453 | case C.CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS: 454 | return CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS 455 | case C.CASS_ERROR_LIB_INVALID_ITEM_COUNT: 456 | return CASS_ERROR_LIB_INVALID_ITEM_COUNT 457 | case C.CASS_ERROR_LIB_INVALID_VALUE_TYPE: 458 | return CASS_ERROR_LIB_INVALID_VALUE_TYPE 459 | case C.CASS_ERROR_LIB_REQUEST_TIMED_OUT: 460 | return CASS_ERROR_LIB_REQUEST_TIMED_OUT 461 | case C.CASS_ERROR_LIB_UNABLE_TO_SET_KEYSPACE: 462 | return CASS_ERROR_LIB_UNABLE_TO_SET_KEYSPACE 463 | case C.CASS_ERROR_LIB_CALLBACK_ALREADY_SET: 464 | return CASS_ERROR_LIB_UNABLE_TO_SET_KEYSPACE 465 | case C.CASS_ERROR_LIB_INVALID_STATEMENT_TYPE: 466 | return CASS_ERROR_LIB_INVALID_STATEMENT_TYPE 467 | case C.CASS_ERROR_LIB_NAME_DOES_NOT_EXIST: 468 | return CASS_ERROR_LIB_NAME_DOES_NOT_EXIST 469 | case C.CASS_ERROR_LIB_UNABLE_TO_DETERMINE_PROTOCOL: 470 | return CASS_ERROR_LIB_UNABLE_TO_DETERMINE_PROTOCOL 471 | case C.CASS_ERROR_LIB_NULL_VALUE: 472 | return CASS_ERROR_LIB_NULL_VALUE 473 | case C.CASS_ERROR_LIB_NOT_IMPLEMENTED: 474 | return CASS_ERROR_LIB_NOT_IMPLEMENTED 475 | case C.CASS_ERROR_LIB_UNABLE_TO_CONNECT: 476 | return CASS_ERROR_LIB_UNABLE_TO_CONNECT 477 | case C.CASS_ERROR_LIB_UNABLE_TO_CLOSE: 478 | return CASS_ERROR_LIB_UNABLE_TO_CLOSE 479 | } 480 | } else if source == CASS_ERROR_SOURCE_SERVER { 481 | switch rc { 482 | case C.CASS_OK: 483 | return C.CASS_OK 484 | case C.CASS_ERROR_SERVER_SERVER_ERROR: 485 | return CASS_ERROR_SERVER_SERVER_ERROR 486 | case C.CASS_ERROR_SERVER_PROTOCOL_ERROR: 487 | return CASS_ERROR_SERVER_PROTOCOL_ERROR 488 | case C.CASS_ERROR_SERVER_BAD_CREDENTIALS: 489 | return CASS_ERROR_SERVER_BAD_CREDENTIALS 490 | case C.CASS_ERROR_SERVER_UNAVAILABLE: 491 | return CASS_ERROR_SERVER_UNAVAILABLE 492 | case C.CASS_ERROR_SERVER_OVERLOADED: 493 | return CASS_ERROR_SERVER_OVERLOADED 494 | case C.CASS_ERROR_SERVER_IS_BOOTSTRAPPING: 495 | return CASS_ERROR_SERVER_IS_BOOTSTRAPPING 496 | case C.CASS_ERROR_SERVER_TRUNCATE_ERROR: 497 | return CASS_ERROR_SERVER_TRUNCATE_ERROR 498 | case C.CASS_ERROR_SERVER_WRITE_TIMEOUT: 499 | return CASS_ERROR_SERVER_WRITE_TIMEOUT 500 | case C.CASS_ERROR_SERVER_READ_TIMEOUT: 501 | return CASS_ERROR_SERVER_READ_TIMEOUT 502 | case C.CASS_ERROR_SERVER_SYNTAX_ERROR: 503 | return CASS_ERROR_SERVER_SYNTAX_ERROR 504 | case C.CASS_ERROR_SERVER_UNAUTHORIZED: 505 | return CASS_ERROR_SERVER_UNAUTHORIZED 506 | case C.CASS_ERROR_SERVER_INVALID_QUERY: 507 | return CASS_ERROR_SERVER_INVALID_QUERY 508 | case C.CASS_ERROR_SERVER_CONFIG_ERROR: 509 | return CASS_ERROR_SERVER_CONFIG_ERROR 510 | case C.CASS_ERROR_SERVER_ALREADY_EXISTS: 511 | return CASS_ERROR_SERVER_ALREADY_EXISTS 512 | case C.CASS_ERROR_SERVER_UNPREPARED: 513 | return CASS_ERROR_SERVER_UNPREPARED 514 | } 515 | } else if source == CASS_ERROR_SOURCE_SSL { 516 | switch rc { 517 | case CASS_OK: 518 | return C.CASS_OK 519 | case C.CASS_ERROR_SSL_INVALID_CERT: 520 | return CASS_ERROR_SSL_INVALID_CERT 521 | case C.CASS_ERROR_SSL_INVALID_PRIVATE_KEY: 522 | return CASS_ERROR_SSL_INVALID_PRIVATE_KEY 523 | case C.CASS_ERROR_SSL_NO_PEER_CERT: 524 | return CASS_ERROR_SSL_NO_PEER_CERT 525 | case C.CASS_ERROR_SSL_INVALID_PEER_CERT: 526 | return CASS_ERROR_SSL_INVALID_PEER_CERT 527 | case C.CASS_ERROR_SSL_IDENTITY_MISMATCH: 528 | return CASS_ERROR_SSL_IDENTITY_MISMATCH 529 | } 530 | } 531 | return CASS_ERROR_LIB_UNEXPECTED_RESPONSE 532 | } 533 | 534 | func (cluster *Cluster) SetContactPoints(contactPoints string) { 535 | contacts_cstr := C.CString(contactPoints) 536 | defer C.free(unsafe.Pointer(contacts_cstr)) 537 | C.cass_cluster_set_contact_points(cluster.cptr, contacts_cstr) 538 | } 539 | 540 | func (cluster *Cluster) SetPort(port int64) { 541 | port_cint := C.int(port) 542 | C.cass_cluster_set_port(cluster.cptr, port_cint) 543 | } 544 | 545 | func (cluster *Cluster) SetNumThreadsIo(size uint) { 546 | C.cass_cluster_set_num_threads_io(cluster.cptr, C.uint(size)) 547 | } 548 | 549 | func (cluster *Cluster) SetQueueSizeIo(size uint) { 550 | C.cass_cluster_set_queue_size_io(cluster.cptr, C.uint(size)) 551 | } 552 | 553 | func (cluster *Cluster) SetPendingRequestsLowWaterMark(size uint) { 554 | C.cass_cluster_set_pending_requests_low_water_mark(cluster.cptr, C.uint(size)) 555 | } 556 | 557 | func (cluster *Cluster) SetPendingRequestsHighWaterMark(size uint) { 558 | C.cass_cluster_set_pending_requests_high_water_mark(cluster.cptr, C.uint(size)) 559 | } 560 | 561 | func (cluster *Cluster) SetCoreConnectionsPerHost(size uint) { 562 | C.cass_cluster_set_core_connections_per_host(cluster.cptr, C.uint(size)) 563 | } 564 | 565 | func (cluster *Cluster) SetMaxConnectionsPerHost(size uint) { 566 | C.cass_cluster_set_max_connections_per_host(cluster.cptr, C.uint(size)) 567 | } 568 | 569 | func (cluster *Cluster) SessionConnect(session *Session) *Future { 570 | future := new(Future) 571 | future.cptr = C.cass_session_connect(session.cptr, cluster.cptr) 572 | return future 573 | } 574 | 575 | func (session *Session) Execute(statement *Statement) *Future { 576 | future := new(Future) 577 | future.cptr = C.cass_session_execute(session.cptr, statement.cptr) 578 | return future 579 | } 580 | 581 | func (session *Session) Prepare(statement string) *Future { 582 | cstring := C.CString(statement) 583 | future := new(Future) 584 | future.cptr = C.cass_session_prepare(session.cptr, cstring) 585 | return future 586 | } 587 | 588 | func (result *Result) RowCount() uint64 { 589 | return uint64(C.cass_result_row_count(result.cptr)) 590 | } 591 | 592 | func (result *Result) ColumnCount() uint64 { 593 | return uint64(C.cass_result_column_count(result.cptr)) 594 | } 595 | 596 | // func (result *Result) ColumnName(index uint64) string { 597 | // column_name := C.cass_result_column_name(result.cptr, C.size_t(index)) 598 | // return C.GoStringN(column_name.data, C.int(column_name.length)) 599 | // } 600 | 601 | func (result *Result) ColumnType(index uint64) int { 602 | return int(C.cass_result_column_type(result.cptr, C.size_t(index))) 603 | } 604 | 605 | func (result *Result) HasMorePages() bool { 606 | return C.cass_result_has_more_pages(result.cptr) != 0 607 | } 608 | 609 | func (result *Result) Next() bool { 610 | if result.iter == nil { 611 | result.iter = C.cass_iterator_from_result(result.cptr) 612 | } 613 | return C.cass_iterator_next(result.iter) != 0 614 | } 615 | 616 | func (result *Result) Scan(args ...interface{}) error { 617 | 618 | if result.ColumnCount() != uint64(len(args)) { 619 | errors.New("invalid argument count") 620 | } 621 | 622 | row := C.cass_iterator_get_row(result.iter) 623 | 624 | var err C.CassError = C.CASS_OK 625 | 626 | for i, v := range args { 627 | value := C.cass_row_get_column(row, C.size_t(i)) 628 | 629 | switch v := v.(type) { 630 | 631 | case *string: 632 | // var str unsafe.Pointer 633 | // var length int64 634 | // err = C.cass_value_get_string(value, &str, &length) 635 | // if err != C.CASS_OK { 636 | // return errors.New(C.GoString(C.cass_error_desc(err))) 637 | // } 638 | // *v = C.GoStringN(str, length) 639 | 640 | case *[]byte: 641 | // var b *[]byte 642 | // var l int64 643 | // err = C.cass_value_get_bytes(value, &b, &l) 644 | // if err != C.CASS_OK { 645 | // return errors.New(C.GoString(C.cass_error_desc(err))) 646 | // } 647 | // *v = C.GoBytes(unsafe.Pointer(b), l) 648 | 649 | case *int32: 650 | var i32 C.cass_int32_t 651 | err = C.cass_value_get_int32(value, &i32) 652 | if err != C.CASS_OK { 653 | return errors.New(C.GoString(C.cass_error_desc(err))) 654 | } 655 | *v = int32(i32) 656 | 657 | case *int64: 658 | var i64 C.cass_int64_t 659 | err = C.cass_value_get_int64(value, &i64) 660 | if err != C.CASS_OK { 661 | return errors.New(C.GoString(C.cass_error_desc(err))) 662 | } 663 | *v = int64(i64) 664 | 665 | case *float32: 666 | var f32 C.cass_float_t 667 | err = C.cass_value_get_float(value, &f32) 668 | if err != C.CASS_OK { 669 | return errors.New(C.GoString(C.cass_error_desc(err))) 670 | } 671 | *v = float32(f32) 672 | 673 | case *float64: 674 | var f64 C.cass_double_t 675 | err = C.cass_value_get_double(value, &f64) 676 | if err != C.CASS_OK { 677 | return errors.New(C.GoString(C.cass_error_desc(err))) 678 | } 679 | *v = float64(f64) 680 | 681 | case *bool: 682 | var b C.cass_bool_t 683 | err = C.cass_value_get_bool(value, &b) 684 | if err != C.CASS_OK { 685 | return errors.New(C.GoString(C.cass_error_desc(err))) 686 | } 687 | *v = bool(b != 0) 688 | 689 | default: 690 | return errors.New("unsupported type in Scan: " + reflect.TypeOf(v).String()) 691 | } 692 | } 693 | 694 | return nil 695 | } 696 | -------------------------------------------------------------------------------- /examples/basic.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "golang-driver/cassandra" 6 | ) 7 | 8 | func main() { 9 | cluster := cassandra.NewCluster() 10 | cluster.SetContactPoints("127.0.0.1") 11 | defer cluster.Finalize() 12 | 13 | session := cassandra.NewSession() 14 | defer session.Finalize() 15 | 16 | sessfuture := cluster.SessionConnect(session) 17 | sessfuture.Wait() 18 | defer sessfuture.Finalize() 19 | 20 | statement := cassandra.NewStatement("select cluster_name from system.local;", 0) 21 | defer statement.Finalize() 22 | 23 | stmtfuture := session.Execute(statement) 24 | stmtfuture.Wait() 25 | defer stmtfuture.Finalize() 26 | 27 | result := stmtfuture.Result() 28 | defer result.Finalize() 29 | 30 | fmt.Printf("Clusters:\r\n") 31 | for result.Next() { 32 | var clusterName string 33 | result.Scan(&clusterName) 34 | fmt.Printf("%s\n", clusterName) 35 | } 36 | 37 | fmt.Printf("DONE.\r\n") 38 | } 39 | --------------------------------------------------------------------------------