Skip to content

Commit 4a92cb6

Browse files
working to generate query
1 parent c47a950 commit 4a92cb6

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
//import com.datastax.loader.CqlDelimLoadTask;
2121

2222
import java.io.*;
23+
import java.lang.reflect.Array;
2324
import java.math.BigDecimal;
2425
import java.net.MalformedURLException;
2526
import java.net.URL;
@@ -35,6 +36,10 @@ public class CassandraDatasetManager {
3536
private Map<String, Dataset> datasets;
3637
private Session session;
3738

39+
CassandraDatasetManager() {
40+
41+
}
42+
3843
CassandraDatasetManager(Map<String, Dataset> datasets) {
3944
this.datasets = datasets;
4045
}
@@ -262,19 +267,19 @@ void install(String name) throws IOException, InterruptedException, GitAPIExcept
262267

263268
HashMap types = new HashMap();
264269

270+
ArrayList<Field> fieldlist = new ArrayList<>();
271+
265272
for(ColumnMetadata c: columns) {
266273
fields.add(c.getName());
267-
types.put(c.getName(), c.getType().getName().toString());
274+
String ftype = c.getType().getName().toString();
275+
types.put(c.getName(), ftype);
276+
fieldlist.add(new Field(c.getName(), ftype));
268277
}
269278

270-
PreparedStatement p = session.prepare(insert_query.toString());
271-
HashSet needs_quotes = new HashSet();
272-
needs_quotes.add("text");
273-
needs_quotes.add("datetime");
274-
275279
for(CSVRecord record: records) {
276280
// generate a CQL statement
277-
String cql = generateCQL(record, types);
281+
String cql = generateCQL(table, record,
282+
fieldlist, types);
278283
session.execute(cql);
279284
}
280285
}
@@ -289,8 +294,19 @@ Iterable<CSVRecord> openCSV(String path) throws IOException {
289294
return records;
290295
}
291296

292-
String generateCQL(CSVRecord record, HashMap<String, String> types) {
293-
return "";
297+
String generateCQL(String table,
298+
CSVRecord record,
299+
ArrayList<Field> fields,
300+
HashMap<String, String> types) {
301+
302+
HashSet needs_quotes = new HashSet();
303+
needs_quotes.add("text");
304+
needs_quotes.add("datetime");
305+
306+
StringBuilder query = new StringBuilder("INSERT INTO ");
307+
query.append(table);
308+
309+
return query.toString();
294310
}
295311

296312
void update() throws IOException {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.datastax.cdm;
2+
3+
/**
4+
* Created by jhaddad on 9/5/16.
5+
*/
6+
public class Field {
7+
public String name;
8+
public String type;
9+
10+
Field(String name, String type) {
11+
this.name = name;
12+
this.type = type;
13+
}
14+
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
package com.datastax.cdm;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.hamcrest.CoreMatchers.containsString;
5+
import static org.junit.Assert.assertThat;
46

57
import org.apache.commons.csv.CSVRecord;
68
import org.junit.Test;
79

10+
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
814
/**
915
* Created by jhaddad on 9/5/16.
1016
*/
1117
public class CassandraDatasetManagerTest {
1218

1319
@Test
14-
public void testCQLStatementGeneration() {
20+
public void testCQLStatementGeneration() throws IOException {
21+
CassandraDatasetManager c = new CassandraDatasetManager();
22+
Iterable<CSVRecord> records = c.openCSV("data/alltypes.csv");
23+
CSVRecord r = records.iterator().next();
24+
25+
HashMap types = new HashMap();
26+
ArrayList<Field> fieldList = new ArrayList<Field>();
27+
String query = c.generateCQL("whatever",
28+
r,
29+
fieldList,
30+
types);
31+
32+
assertThat(query, containsString("INSERT INTO whatever"));
1533

1634
}
1735

0 commit comments

Comments
 (0)