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