Last active
September 20, 2023 15:27
-
-
Save hugosenari/a1cbace7feeee4d115767f188fac1603 to your computer and use it in GitHub Desktop.
Nim cassandra cli example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requires: cassandra wrapper https://github.com/yglukhov/cassandra | |
# | |
# Expect a query from stdin | |
# printf "SELECT cql_version FROM system.local"|./cascli | |
# | |
# Fist parameter is config file name, default is clascli.ini | |
# Example: | |
# [cassandra] | |
# ips=127.0.0.1 | |
# user=cass_user | |
# pass=cass_pass | |
# | |
# Second paramater is section in config (default is cassandra) | |
# Example: | |
# [dev] | |
# ips=127.0.0.1 | |
# user=cass_user | |
# pass=cass_pass | |
# [prd] | |
# ips=127.0.0.1 | |
# user=cass_user | |
# pass=cass_pass | |
# | |
# printf "SELECT cql_version FROM system.local"|./cascli bla.ini dev | |
# | |
import std/asyncdispatch | |
import std/os | |
import std/parsecfg | |
import std/strutils | |
import std/terminal | |
import cassandra | |
import cassandra/bindings | |
iterator rows(r: Result): Row {.inline.} = | |
let it = r.o.cass_iterator_from_result | |
while it.cass_iterator_next == cass_true: | |
let o = it.cass_iterator_get_row | |
yield Row(o: o) | |
proc uid2str(v: Value): string = | |
var uidval: CassUuid | |
discard v.o.cass_value_get_uuid(addr uidval) | |
var uidcstr = cast[cstring](create(uint8, CASS_UUID_STRING_LENGTH)) | |
uidval.cass_uuid_string uidcstr | |
result = $uidcstr | |
uidcstr.dealloc | |
proc main() {.async.} = | |
if stdin.isATTY: | |
echo """Excpect a CQL command from stdin""" | |
quit 1 | |
let configFile = | |
if paramCount() >= 2: | |
1.paramStr | |
else: | |
"cascli.ini" | |
let configSection = | |
if paramCount() >= 3: | |
2.paramStr | |
else: | |
"cassandra" | |
let cfg = configFile.loadConfig | |
let cluster = newCluster() | |
let session = newSession() | |
# Add contact points | |
cluster.setContactPoints(cfg.getSectionValue(configSection, "ips")) | |
cluster.setCredentials( cfg.getSectionValue(configSection, "user"), | |
cfg.getSectionValue(configSection, "pass")) | |
# Provide the cluster object as configuration to connect the session | |
discard await session.connect(cluster) | |
let query = stdin.readAll | |
let statement = newStatement(query) | |
let res = await session.execute(statement) | |
let cols = cass_result_column_count(res.o).uint | |
var headers = newSeq[string]() | |
var c: cstring | |
var sz: csize_t | |
for i in 0 ..< cols: | |
discard cass_result_column_name(res.o, i, cast[cstringArray](addr c), addr sz) | |
headers.add($c) | |
echo headers.join(",") | |
for r in res.rows(): | |
var line = newSeq[string]() | |
for i in 0 ..< cols: | |
let v = Value(o: r.o.cass_row_get_column(i.csize_t)) | |
if v.o.cass_value_is_null == cass_true: | |
line.add("null") | |
continue | |
case v.kind | |
of CASS_VALUE_TYPE_TEXT, CASS_VALUE_TYPE_VARCHAR: | |
line.add('"' & $v & '"') | |
of CASS_VALUE_TYPE_UUID: | |
line.add(v.uid2str) | |
else: | |
line.add($v) | |
echo line.join(",") | |
when isMainModule: | |
waitFor main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment