// create a DB
String dbname = props.getProperty("db", DEFAULT_DB);
ClassLoader classLoader = CommandLine.class.getClassLoader();
DB db = null;
try {
Class dbclass = classLoader.loadClass(dbname);
db = (DB) dbclass.newInstance();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
db.setProperties(props);
try {
db.init();
} catch (DataStoreException e) {
e.printStackTrace();
System.exit(0);
}
System.out.println("Connected.");
// main loop
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (;;) {
// get user input
System.out.print("> ");
String input = null;
try {
input = br.readLine();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
if (input.compareTo("") == 0) {
continue;
}
if (input.compareTo("help") == 0) {
help();
continue;
}
if (input.compareTo("quit") == 0) {
break;
}
String[] tokens = input.split(" ");
long st = System.currentTimeMillis();
// handle commands
if (tokens[0].compareTo("table") == 0) {
if (tokens.length == 1) {
System.out.println("Using table \"" + table + "\"");
} else if (tokens.length == 2) {
table = tokens[1];
System.out.println("Using table \"" + table + "\"");
} else {
System.out.println("Error: syntax is \"table tablename\"");
}
} else if (tokens[0].compareTo("read") == 0) {
if (tokens.length == 1) {
System.out
.println("Error: syntax is \"read keyname [field1 field2 ...]\"");
} else {
Set<String> fields = null;
if (tokens.length > 2) {
fields = new HashSet<String>();
for (int i = 2; i < tokens.length; i++) {
fields.add(tokens[i]);
}
}
HashMap<String, String> result = new HashMap<String, String>();
int ret = db.read(table, tokens[1], fields, result);
System.out.println("Return code: " + ret);
for (Map.Entry<String, String> ent : result.entrySet()) {
System.out.println(ent.getKey() + "=" + ent.getValue());
}
}
} else if (tokens[0].compareTo("scan") == 0) {
if (tokens.length < 3) {
System.out
.println("Error: syntax is \"scan keyname scanlength [field1 field2 ...]\"");
} else {
Set<String> fields = null;
if (tokens.length > 3) {
fields = new HashSet<String>();
for (int i = 3; i < tokens.length; i++) {
fields.add(tokens[i]);
}
}
Vector<HashMap<String, String>> results = new Vector<HashMap<String, String>>();
int ret = db.scan(table, tokens[1],
Integer.parseInt(tokens[2]), fields, results);
System.out.println("Return code: " + ret);
int record = 0;
if (results.size() == 0) {
System.out.println("0 records");
} else {
System.out.println("--------------------------------");
}
for (HashMap<String, String> result : results) {
System.out.println("Record " + (record++));
for (Map.Entry<String, String> ent : result.entrySet()) {
System.out.println(ent.getKey() + "="
+ ent.getValue());
}
System.out.println("--------------------------------");
}
}
} else if (tokens[0].compareTo("update") == 0) {
if (tokens.length < 3) {
System.out
.println("Error: syntax is \"update keyname name1=value1 [name2=value2 ...]\"");
} else {
HashMap<String, String> values = new HashMap<String, String>();
for (int i = 2; i < tokens.length; i++) {
String[] nv = tokens[i].split("=");
values.put(nv[0], nv[1]);
}
int ret = db.update(table, tokens[1], values);
System.out.println("Return code: " + ret);
}
} else if (tokens[0].compareTo("insert") == 0) {
if (tokens.length < 3) {
System.out
.println("Error: syntax is \"insert keyname name1=value1 [name2=value2 ...]\"");
} else {
HashMap<String, String> values = new HashMap<String, String>();
for (int i = 2; i < tokens.length; i++) {
String[] nv = tokens[i].split("=");
values.put(nv[0], nv[1]);
}
int ret = db.insert(table, tokens[1], values);
System.out.println("Return code: " + ret);
}
} else if (tokens[0].compareTo("delete") == 0) {
if (tokens.length != 2) {
System.out.println("Error: syntax is \"delete keyname\"");
} else {
int ret = db.delete(table, tokens[1]);
System.out.println("Return code: " + ret);
}
} else {
System.out.println("Error: unknown command \"" + tokens[0] + "\"");
}