1 module cassandra.cassandradb; 2 3 public import cassandra.client; 4 5 import cassandra.cql.connection; 6 7 8 /** 9 Connects to a Cassandra instance. 10 */ 11 CassandraClient connectCassandraDB(string host, ushort port = Connection.defaultPort) 12 { 13 return new CassandraClient(host, port); 14 } 15 16 17 unittest { 18 auto cassandra = connectCassandraDB("127.0.0.1", 9042); 19 20 /*auto opts = cassandra.requestOptions(); 21 foreach (opt, values; opts) { 22 log(opt, values); 23 }*/ 24 25 CassandraKeyspace ks; 26 try ks = cassandra.getKeyspace("twissandra"); 27 catch (Exception e) ks = cassandra.createKeyspace("twissandra"); 28 29 static struct UserTableEntry { 30 import std.bigint; 31 32 string user_name; 33 string password; 34 string gender; 35 string session_token; 36 string state; 37 BigInt birth_year; 38 } 39 40 try { 41 log("CREATE TABLE "); 42 auto res = ks.query(`CREATE TABLE users ( 43 user_name varchar, 44 password varchar, 45 gender varchar, 46 session_token varchar, 47 state varchar, 48 birth_year bigint, 49 PRIMARY KEY (user_name) 50 )`, Consistency.any); 51 log("created table %s %s %s %s", res.kind, res.keyspace, res.lastchange, res.table); 52 } catch (Exception e) { log(e.msg); } 53 54 try { 55 log("INSERT"); 56 ks.query(`INSERT INTO users 57 (user_name, password) 58 VALUES ('jsmith', 'ch@ngem3a')`, Consistency.any); 59 log("inserted"); 60 } catch (Exception e) { log(e.msg); assert(false); } 61 62 try { 63 log("SELECT"); 64 auto res = ks.query(`SELECT * FROM users WHERE user_name='jsmith'`, Consistency.one); 65 log("select resulted in %s", res.toString()); 66 import std.typecons : Tuple; 67 while (!res.empty) { 68 UserTableEntry entry; 69 res.readRow(entry); 70 log("ROW: %s", entry); 71 } 72 } catch (Exception e) { log(e.msg); assert(false); } 73 74 static struct AllTypesEntry { 75 import std.bigint; 76 import std.datetime; 77 import std.uuid; 78 79 string user_name; 80 long birth_year; 81 string ascii_col; //@ascii 82 ubyte[] blob_col; 83 bool booleant_col; 84 bool booleanf_col; 85 Decimal decimal_col; 86 double double_col; 87 float float_col; 88 ubyte[] inet_col; 89 int int_col; 90 string[] list_col; 91 string[string] map_col; 92 string[] set_col; 93 string text_col; 94 SysTime timestamp_col; 95 UUID uuid_col; 96 UUID timeuuid_col; 97 BigInt varint_col; 98 } 99 100 try { 101 log("CREATE TABLE "); 102 auto res = ks.query(`CREATE TABLE alltypes ( 103 user_name varchar, 104 birth_year bigint, 105 ascii_col ascii, 106 blob_col blob, 107 booleant_col boolean, 108 booleanf_col boolean, 109 decimal_col decimal, 110 double_col double, 111 float_col float, 112 inet_col inet, 113 int_col int, 114 list_col list<varchar>, 115 map_col map<varchar,varchar>, 116 set_col set<varchar>, 117 text_col text, 118 timestamp_col timestamp, 119 uuid_col uuid, 120 timeuuid_col timeuuid, 121 varint_col varint, 122 123 PRIMARY KEY (user_name) 124 )`, Consistency.any); 125 log("created table %s %s %s %s", res.kind, res.keyspace, res.lastchange, res.table); 126 } catch (Exception e) { log(e.msg); } 127 128 try { 129 log("INSERT into alltypes"); 130 ks.query(`INSERT INTO alltypes (user_name,birth_year,ascii_col,blob_col,booleant_col, booleanf_col,decimal_col,double_col,float_col,inet_col,int_col,list_col,map_col,set_col,text_col,timestamp_col,uuid_col,timeuuid_col,varint_col) 131 VALUES ('bob@domain.com', 7777777777, 132 'someasciitext', 0x2020202020202020202020202020, 133 True, False, 134 123.456, 8.5, 9.44, '127.0.0.1', 999, 135 ['li1','li2','li3'], {'blurg':'blarg'}, { 'kitten', 'cat', 'pet' }, 136 'some text col value', 'now', aaaaaaaa-eeee-cccc-9876-dddddddddddd, 137 now(), 138 -9494949449 139 )`, Consistency.any); 140 log("inserted"); 141 } catch (Exception e) { log(e.msg); assert(false); } 142 143 144 try { 145 log("PREPARE INSERT into alltypes"); 146 auto stmt = ks.prepare(`INSERT INTO alltypes (user_name,birth_year, ascii_col, blob_col, booleant_col, booleanf_col, decimal_col, double_col 147 , float_col, inet_col, int_col, list_col, map_col, set_col, text_col, timestamp_col 148 , uuid_col, timeuuid_col, varint_col)` 149 ` VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`); 150 log("prepared stmt: %s", stmt); 151 alias long Bigint; 152 alias double Double; 153 alias float Float; 154 alias int InetAddress4; 155 alias ubyte[16] InetAddress16; 156 alias long Timestamp; 157 158 ks.execute(stmt, "rory", cast(Bigint)1378218642, "mossesf asciiiteeeext", 0x898989898989 159 , true, false, cast(long)999, cast(double)8.88, cast(float)7.77, cast(int)2130706433 160 , 66666, ["thr","for"], ["key1": "value1"], ["one","two", "three"], "some more text«»" 161 , cast(Timestamp)0x0000021212121212, "\xaa\xaa\xaa\xaa\xee\xee\xcc\xcc\x98\x76\xdd\xdd\xdd\xdd\xdd\xdd" 162 , "\xb3\x8b\x6d\xb0\x14\xcc\x11\xe3\x81\x03\x9d\x48\x04\xae\x88\xb3", long.max); 163 } catch (Exception e) { log(e.msg); assert(false); } // list should be: [0, 2, 0, 3, 111, 110, 101, 0, 3, 116, 119, 111] 164 165 try { 166 log("SELECT from alltypes"); 167 auto res = ks.query(`SELECT * FROM alltypes`, Consistency.one); 168 log("select resulted in %s", res.toString()); 169 170 while (!res.empty) { 171 AllTypesEntry entry; 172 res.readRow(entry); 173 log("ROW: %s", entry); 174 } 175 } catch (Exception e) { log(e.msg); assert(false); } 176 177 178 //cassandra.close(); 179 log("done. exiting"); 180 }