Skip to content

Commit 0347788

Browse files
close to having working cql statements
1 parent 4a92cb6 commit 0347788

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/main/java/com/datastax/cdm/CassandraDatasetManager.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,6 @@ void install(String name) throws IOException, InterruptedException, GitAPIExcept
225225
{
226226
Session session = cluster.connect();
227227

228-
// String createKeyspace = "DROP KEYSPACE IF EXISTS " + config.keyspace +
229-
// "; CREATE KEYSPACE " + config.keyspace +
230-
// " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}";
231-
232228
StringBuilder createKeyspace = new StringBuilder();
233229
createKeyspace.append(" CREATE KEYSPACE ")
234230
.append(config.keyspace)
@@ -251,6 +247,8 @@ void install(String name) throws IOException, InterruptedException, GitAPIExcept
251247

252248
Session session = cluster2.connect(config.keyspace);
253249

250+
this.session = session;
251+
254252
for(String table: config.tables) {
255253
String dataFile = dataPath + table + ".csv";
256254
Iterable<CSVRecord> records = openCSV(dataFile);
@@ -278,8 +276,10 @@ void install(String name) throws IOException, InterruptedException, GitAPIExcept
278276

279277
for(CSVRecord record: records) {
280278
// generate a CQL statement
281-
String cql = generateCQL(table, record,
282-
fieldlist, types);
279+
String cql = generateCQL(table,
280+
record,
281+
fieldlist,
282+
types);
283283
session.execute(cql);
284284
}
285285
}
@@ -302,9 +302,23 @@ String generateCQL(String table,
302302
HashSet needs_quotes = new HashSet();
303303
needs_quotes.add("text");
304304
needs_quotes.add("datetime");
305+
needs_quotes.add("map");
306+
needs_quotes.add("list");
307+
needs_quotes.add("udt");
308+
needs_quotes.add("set");
305309

306310
StringBuilder query = new StringBuilder("INSERT INTO ");
307311
query.append(table);
312+
query.append("(");
313+
314+
StringJoiner sjfields = new StringJoiner(", ");
315+
for(Field f: fields) {
316+
sjfields.add(f.name);
317+
}
318+
query.append(sjfields.toString());
319+
320+
query.append(") VALUES (");
321+
query.append(")");
308322

309323
return query.toString();
310324
}

test/java/com/datastax/cdm/CassandraDatasetManagerTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.IOException;
1111
import java.util.ArrayList;
1212
import java.util.HashMap;
13+
import java.util.StringJoiner;
1314

1415
/**
1516
* Created by jhaddad on 9/5/16.
@@ -24,12 +25,18 @@ public void testCQLStatementGeneration() throws IOException {
2425

2526
HashMap types = new HashMap();
2627
ArrayList<Field> fieldList = new ArrayList<Field>();
28+
fieldList.add(new Field("id", "uuid"));
29+
fieldList.add(new Field("avg", "float"));
30+
fieldList.add(new Field("cash", "decimal"));
31+
fieldList.add(new Field("intmap", "map"));
32+
fieldList.add(new Field("num", "int"));
33+
fieldList.add(new Field("ts", "timeuuid"));
34+
2735
String query = c.generateCQL("whatever",
2836
r,
2937
fieldList,
3038
types);
31-
32-
assertThat(query, containsString("INSERT INTO whatever"));
39+
assertThat(query, containsString("INSERT INTO whatever(id, avg, cash, intmap, num, ts) VALUES"));
3340

3441
}
3542

0 commit comments

Comments
 (0)