Last active
August 29, 2015 13:57
-
-
Save NithishKolli/9870420 to your computer and use it in GitHub Desktop.
Sql Query Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 | |
Students | |
Name Rollno* Hall Mobile | |
Kush 10MF100021 Azad 944641684 | |
Nithish 10MA20028 Azad 95426546 | |
Mandula 10MF10013 Azad 7501654369 | |
Ajit 10MA20014 RP 7797256805 | |
Kai 10NA300214 MS 9800165458 | |
Departments | |
Name* Departments | |
Kush Mech | |
Nithish Maths | |
Ajit Maths | |
Mandula Mech |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class SQLquery { | |
int noOfTables; | |
Table[] tables; | |
String row; | |
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); | |
void readFile() { | |
int tempTableCount = 0; | |
try { | |
BufferedReader br = new BufferedReader(new FileReader("input.txt")); | |
noOfTables = Integer.parseInt(br.readLine()); | |
tables = new Table[noOfTables]; | |
while (tempTableCount < noOfTables) { | |
tables[tempTableCount] = new Table(br.readLine()); | |
tables[tempTableCount].columnNames = br.readLine().split(" "); | |
int i = 0; | |
while ((row = br.readLine()) != null && row.contains(" ")) { | |
br.mark(1000); | |
tables[tempTableCount].tableData[i] = row.split(" "); | |
tables[tempTableCount].noOfColumns = tables[tempTableCount].tableData[i].length; | |
i++; | |
} | |
tables[tempTableCount].noOfRows = i; | |
if (row != null) { | |
br.reset(); | |
} | |
tables[tempTableCount].getKey(); | |
tempTableCount++; | |
} | |
br.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
void writeFile() { | |
try { | |
BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt")); | |
bw.write(noOfTables); | |
bw.newLine(); | |
for (int i = 0; i < noOfTables; i++) { | |
bw.write(tables[i].tableName); | |
bw.newLine(); | |
bw.write(tables[i].columnNames[1]); | |
bw.newLine(); | |
} | |
bw.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
void input() { | |
String query; | |
try { | |
do { | |
System.out.println("Enter the SQL query in caps"); | |
query = br1.readLine(); | |
if (query.contains("INSERT")) { | |
insertInto(query); | |
} else if (query.contains("DELETE")) | |
deleteFrom(query); | |
else if (query.contains("SELECT")) | |
selectWhere(query); | |
else if (query.contains("DISPLAY")) | |
displayTable(query); | |
else if (query.contains("JOIN")) | |
joinTables(query); | |
else if (query.contains("CROSSPRODUCT")) | |
crossProduct(query); | |
} while (!query.equalsIgnoreCase("EXIT")); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
void insertInto(String query) { | |
// Syntax is INSERT INTO table_name VALUES (value1,value2,value3) | |
String tableName = query.substring(12); | |
tableName = tableName.substring(0, tableName.indexOf(" ")); | |
try { | |
String[] values = query.substring(query.indexOf("(") + 1, | |
query.indexOf(")")).split(","); | |
for (int j = 0; j < noOfTables; j++) { | |
if (tableName.equalsIgnoreCase(tables[j].tableName)) { | |
for (int i = 0; i < tables[j].columnNames.length; i++) { | |
tables[j].tableData[(tables[j].noOfRows)][i] = values[i]; | |
} | |
tables[j].noOfRows++; | |
printTable(j); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
void deleteFrom(String query) { | |
// Syntax is DELETE FROM table_name WHERE columnname=some_value | |
String value = null, columnid = null; | |
String tableName = query.substring(12); | |
tableName = tableName.substring(0, tableName.indexOf(" ")); | |
columnid = query.substring(12 + tableName.length() + 1 + 5 + 1, | |
query.indexOf("=")); | |
value = query.substring(query.indexOf("=") + 1); | |
try { | |
for (int i = 0; i < noOfTables; i++) { | |
if (tableName.equalsIgnoreCase(tables[i].tableName)) { | |
for (int j = 0; j < tables[i].noOfRows; j++) { | |
if (tables[i].tableData[j][tables[i] | |
.getColumnId(columnid)].equalsIgnoreCase(value)) { | |
tables[i].tableData = removeRow( | |
tables[i].tableData, j, tables[i].noOfRows, | |
tables[i].noOfColumns); | |
tables[i].noOfRows--; | |
} | |
} | |
printTable(i); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
void printTable(int id) { | |
for (int i = 0; i < tables[id].noOfRows; i++) { | |
for (int j = 0; j < tables[id].noOfColumns; j++) { | |
System.out.print(tables[0].tableData[i][j] + " "); | |
} | |
System.out.println(""); | |
} | |
} | |
void selectWhere(String query) { | |
// Syntax SELECT WHERE FROM tablename id=value | |
String temp = query.substring(query.indexOf("FROM") + 5); | |
String temp1 = temp.substring(temp.indexOf(" ") + 1); | |
String tableName = temp.substring(0, temp.indexOf(" ")); | |
String id = temp1.substring(0, temp1.indexOf("=")); | |
String value = temp1.substring(temp1.indexOf("=") + 1); | |
int p = 0, q = 0; | |
for (int i = 0; i < noOfTables; i++) { | |
if (tables[i].tableName.equalsIgnoreCase(tableName)) { | |
p = i; | |
for (int j = 0; j < tables[i].columnNames.length; j++) { | |
if (tables[i].columnNames[j].equalsIgnoreCase(id)) { | |
q = j; | |
} | |
} | |
} | |
} | |
for (int i = 0; i < tables[p].noOfRows; i++) { | |
if (tables[p].tableData[i][q].equalsIgnoreCase(value)) { | |
for (int j = 0; j < tables[p].noOfColumns; j++) { | |
System.out.print(tables[p].tableData[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
} | |
void displayTable(String query) { | |
// Syntax is DISPLAY tablename | |
String tableName = query.substring(query.indexOf(" ") + 1); | |
for (int i = 0; i < noOfTables; i++) { | |
if (tables[i].tableName.equalsIgnoreCase(tableName)) { | |
printTable(i); | |
break; | |
} | |
} | |
} | |
void joinTables(String query) { | |
// JOIN table1 table2 | |
String tableName1, tableName2, temp; | |
int p = 0, q = 0, r = 0, s = 0; | |
String[][] tempa = new String[100][20]; | |
temp = query.substring(5); | |
tableName1 = temp.substring(0, temp.indexOf(" ")); | |
tableName2 = temp.substring(temp.indexOf(" ") + 1); | |
for (int i = 0; i < noOfTables; i++) { | |
if (tables[i].tableName.equalsIgnoreCase(tableName1)) { | |
p = i; | |
} | |
if (tables[i].tableName.equalsIgnoreCase(tableName2)) { | |
q = i; | |
} | |
} | |
for (int i = 0; i < tables[p].columnNames.length; i++) { | |
for (int j = 0; j < tables[q].columnNames.length; j++) { | |
if (tables[p].columnNames[i] | |
.equalsIgnoreCase(tables[q].columnNames[j])) { | |
r = i; | |
s = j; | |
break; | |
} | |
} | |
} | |
for (int i = 0; i < tables[p].noOfRows; i++) { | |
for (int j = 0; j < tables[q].noOfRows; j++) { | |
for (int k = 0; k < tables[p].noOfColumns | |
+ tables[q].noOfColumns; k++) { | |
if (k < tables[p].noOfColumns) { | |
tempa[(tables[q].noOfRows * i) + j][k] = tables[p].tableData[i][k]; | |
} else { | |
tempa[(tables[q].noOfRows * i) + j][k] = tables[q].tableData[j][k | |
- tables[p].noOfColumns]; | |
} | |
} | |
} | |
} | |
for (int i = 0; i < (tables[p].noOfRows * tables[q].noOfRows); i++) { | |
if (tempa[i][r].equals(tempa[i][s + tables[p].noOfColumns])) { | |
for (int j = 0; j < tables[p].noOfColumns | |
+ tables[q].noOfColumns; j++) { | |
System.out.print(tempa[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
} | |
void crossProduct(String query) { | |
// Syntax is CROSSPRODUCT table1 table2 | |
String tableName1, tableName2, temp; | |
int p = 0, q = 0; | |
String[][] tempa = new String[100][20]; | |
temp = query.substring(13); | |
tableName1 = temp.substring(0, temp.indexOf(" ")); | |
tableName2 = temp.substring(temp.indexOf(" ") + 1); | |
for (int i = 0; i < noOfTables; i++) { | |
if (tables[i].tableName.equalsIgnoreCase(tableName1)) { | |
p = i; | |
} | |
if (tables[i].tableName.equalsIgnoreCase(tableName2)) { | |
q = i; | |
} | |
} | |
for (int i = 0; i < tables[p].noOfRows; i++) { | |
for (int j = 0; j < tables[q].noOfRows; j++) { | |
for (int k = 0; k < tables[p].noOfColumns | |
+ tables[q].noOfColumns; k++) { | |
if (k < tables[p].noOfColumns) { | |
tempa[(tables[q].noOfRows * i) + j][k] = tables[p].tableData[i][k]; | |
} else { | |
tempa[(tables[q].noOfRows * i) + j][k] = tables[q].tableData[j][k | |
- tables[p].noOfColumns]; | |
} | |
} | |
} | |
} | |
for (int i = 0; i < (tables[p].noOfRows * tables[q].noOfRows); i++) { | |
for (int j = 0; j < tables[p].noOfColumns + tables[q].noOfColumns; j++) { | |
System.out.print(tempa[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
String[][] removeRow(String[][] a, int rowid, int rowsize, int columnsize) { | |
String[][] temp = new String[100][10]; | |
int p = 0; | |
try { | |
for (int i = 0; i < rowsize; i++) { | |
if (i != rowid) { | |
for (int j = 0; j < columnsize; j++) { | |
temp[p][j] = a[i][j]; | |
} | |
p++; | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return temp; | |
} | |
public static void main(String args[]) { | |
SQLquery sq = new SQLquery(); | |
sq.readFile(); | |
sq.input(); | |
} | |
} | |
class Table { | |
String tableName; | |
String[] columnNames; | |
String key; | |
int keyValue; | |
String[][] tableData = new String[100][10]; | |
int noOfRows; | |
int noOfColumns; | |
Table(String tableName) { | |
this.tableName = tableName; | |
} | |
void getKey() { | |
for (int i = 0; i < columnNames.length; i++) { | |
if (columnNames[i].contains("*")) { | |
key = columnNames[i].replace("*", ""); | |
keyValue = i; | |
} | |
} | |
} | |
int getColumnId(String id) { | |
int temp = 0; | |
for (int i = 0; i < columnNames.length; i++) { | |
if (columnNames[i].equalsIgnoreCase(id)) { | |
temp = i; | |
} | |
} | |
return temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment