Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 11

import java.io.

BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class DeleteQuery {


private String tableName = null;
private ArrayList<String> conditions = new ArrayList<String>();
private String[] words;

public DeleteQuery(String query) {


query = anullerEspacesDoubles(query);
words = query.split(" ");
extractTableName();
extractConditions();

public void extractTableName(){


tableName = words[2].split(">")[1];
}

public String getTableName(){


return this.tableName;
}

public void extractConditions(){


if(words.length >4) {
String[] part = Arrays.copyOfRange(words, 4, words.length);
words[4] = String.join(" ", part);
String cond = words[4];
conditions.add(cond);
}
}

public ArrayList<String> getConditions(){


return this.conditions;
}

public String anullerEspacesDoubles(String s) {


String res = s.trim();
while(res.contains(" ")) {
res = res.replace(" ", " ");
}
return res;
}
}
This code defines a DeleteQuery class that can be used to represent a delete query.
The constructor takes a query string as input and extracts the table name and
conditions from it. The extractTableName method is used to extract the table name,
and the extractConditions method is used to extract the conditions specified in the
query. The getTableName and getConditions methods can be used to retrieve the table
name and conditions respectively. The anullerEspacesDoubles method is used to
remove any extra spaces in the query string.

To use this class, you can do the following:

String query = "DELETE FROM >table_name< WHERE condition";


DeleteQuery deleteQuery = new DeleteQuery(query);
String tableName = deleteQuery.getTableName();
ArrayList<String> conditions = deleteQuery.getConditions();
This will create a DeleteQuery object and extract the table name and conditions
from the query string. The table name can be accessed using the getTableName
method, and the conditions can be accessed using the getConditions method.

String query = "DELETE FROM >table_name< WHERE condition";


DeleteQuery deleteQuery = new DeleteQuery(query);
String tableName = deleteQuery.getTableName();
ArrayList<String> conditions = deleteQuery.getConditions();

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class DeleteQuery {


private String tableName;
private String primaryKey;
private String primaryKeyValue;
private String[] words;

public DeleteQuery(String query) {


query = removeExtraSpaces(query);
words = query.split(" ");
extractTableName();
extractPrimaryKey();
extractPrimaryKeyValue();
}

public void extractTableName() {


tableName = words[2].split(">")[1];
}

public String getTableName() {


return this.tableName;
}

public void extractPrimaryKey() {


primaryKey = words[4].split("=")[0];
}

public String getPrimaryKey() {


return this.primaryKey;
}

public void extractPrimaryKeyValue() {


primaryKeyValue = words[4].split("=")[1];
}

public String getPrimaryKeyValue() {


return this.primaryKeyValue;
}

public String removeExtraSpaces(String s) {


String res = s.trim();
while (res.contains(" ")) {
res = res.replace(" ", " ");
}
return res;
}

public void deleteRecord() {


ArrayList<String[]> records = new ArrayList<>();
String line;

try (BufferedReader reader = new BufferedReader(new FileReader(tableName +


".csv"))) {
while ((line = reader.readLine()) != null) {
String[] values = line.split(",");
if (!values[0].equals(primaryKeyValue)) {
records.add(values);
}
}
} catch (IOException e) {
e.printStackTrace();
}

try (BufferedWriter writer = new BufferedWriter(new FileWriter(tableName +


".csv"))) {
for (String[] record : records) {
String lineToWrite = String.join(",", record);
writer.write(lineToWrite);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String query = "DELETE FROM >table_name< WHERE primary_key=primary_key_value";
DeleteQuery deleteQuery = new DeleteQuery(query);
deleteQuery.deleteRecord();
}

delete >tb1 [name="ayoub"]

public void extractTableCols() {


File file = new File("C:\\Users\\USER\\Desktop\\sgbdtest\\"+ tableName
+"_meta.csv"); //"+query.getTableName().get(0)+"
try (BufferedReader reader = new BufferedReader(new FileReader(file)))
{
String line;
String[] Tline;

while ((line = reader.readLine()) != null) {


Tline = line.split(";");
if(Tline.length == 3)
tableCols.add(new Attribut(Tline[0], Tline[1], true));
else
tableCols.add(new Attribut(Tline[0], Tline[1], false));
}

} catch (IOException e) {
// Handle any errors that occurred while reading the file
e.printStackTrace();
}
}

public class Attribut {


private String nom;
private String type;
private boolean isPrimaryKey;
public Attribut(String nom, String type, boolean isPrimaryKey) {
super();
this.nom = nom;
this.type = type;
this.isPrimaryKey = isPrimaryKey;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isPrimaryKey() {
return isPrimaryKey;
}
public void setPrimaryKey(boolean isPrimaryKey) {
this.isPrimaryKey = isPrimaryKey;
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class DeleteQuery {


private String tableName;
private String column;
private String columnValue;
private String[] words;
private ArrayList<Attribut> tableCols = new ArrayList<Attribut>();

public DeleteQuery(String query) {


query = removeExtraSpaces(query);
words = query.split(" ");
extractTableName();
extractColumn();
extractColumnValue();
extractTableCols();
}

public void extractTableName() {


tableName = words[2];
}

public String getTableName() {


return this.tableName;
}

public void extractColumn() {


column = words[4].split("=")[0];
}

public String getColumn() {


return this.column;
}

public void extractTableCols() {


File file2 = new File("C:\\Users\\Ayoub\\sgbdtest\\"+ tableName
+"_meta.csv"); //"+query.getTableName().get(0)+"
try (BufferedReader reader2 = new BufferedReader(new FileReader(file2))) {
String line2;
String[] Tline;

while ((line2 = reader2.readLine()) != null) {


Tline = line2.split(";");
if(Tline.length == 3)
tableCols.add(new Attribut(Tline[0], Tline[1], true));
else
tableCols.add(new Attribut(Tline[0], Tline[1], false));
}

} catch (IOException e) {
// Handle any errors that occurred while reading the file
e.printStackTrace();
}
}

public int indColumn(String colName) {


for(int i = 0 ; i < tableCols.size() ; i++) {
if(tableCols.get(i).getNom().equals(colName))
return i;
}
return -1;
}

public void extractColumnValue() {


columnValue = words[4].split("=")[1];
}

public String getColumnValue() {


return this.columnValue;
}

public String removeExtraSpaces(String s) {


String res = s.trim();
while (res.contains(" ")) {
res = res.replace(" ", " ");
}
return res;
}

public void deleteRecord() {


ArrayList<String[]> records = new ArrayList<>();
String line;

try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\


Ayoub\\sgbdtest\\" + tableName + ".csv
public void deleteRecord() {
ArrayList<String[]> records = new ArrayList<>();
String line;

try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\


Ayoub\\sgbdtest\\" + tableName + ".csv"))) {
while ((line = reader.readLine()) != null) {
String[] values = line.split(",");
records.add(values);
}
} catch (IOException e) {
e.printStackTrace();
}

int index = -1;


for (int i = 0; i < records.size(); i++) {
String[] record = records.get(i);
if (record[indColumn(column)].equals(columnValue)) {
index = i;
break;
}
}

if (index != -1) {
records.remove(index);
try (BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\
Users\\Ayoub\\sgbdtest\\" + tableName + ".csv"))) {
for (String[] record : records) {
String lineToWrite = String.join(",", record);
writer.write(lineToWrite);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

public String getOperator() {


return words[3];
}
}

public static String contientOpComparaison(String cdt) {


char c;

for(int i = 0 ; i < cdt.length() ; i++) {


c = cdt.charAt(i);
if(c == '=') {
if((i < cdt.length() - 1) && (cdt.charAt(i+1) == '=')) {
return "" + c + cdt.charAt(i+1);
}
} else if((c == '>') || c == '<') {
if((i < cdt.length() - 1) && ((cdt.charAt(i+1) == '='))) {
return "" + c + cdt.charAt(i+1);
} else {
return c + "";
}
} else if(c == '!') {
if((i < cdt.length() - 1) && ((cdt.charAt(i+1) == '='))) {
return "" + c + cdt.charAt(i+1);
}
}
}

return "";
}

public int compareString(String right, String left, String operator) {


if((right.charAt(0) == '\'') && right.charAt(right.length()-1) == '\'')
{
right = new String(Arrays.copyOfRange(right.toCharArray(), 1,
right.length()-1));
} else {
System.out.println("erreur");
return -1;
}

switch(operator) {
case "==":
if(right.equals(left)) {
return 1;
} else {
return 0;
}

case "!=":
if(!right.equals(left)) {
return 1;
} else {
return 0;
}
case "<":
if(left.compareTo(right) < 0) {
return 1;
} else {
return 0;
}
case ">":
if(left.compareTo(right) > 0) {
return 1;
} else {
return 0;
}
case "<=":
if(left.compareTo(right) <= 0) {
return 1;
} else {
return 0;
}
case ">=":
if(left.compareTo(right) >= 0) {
return 1;
} else {
return 0;
}
default:
System.out.println("Erreur");
return -1;
}
}

public boolean isInteger( String input ) { //Pass in string


try { //Try to make the input into an integer
Integer.parseInt( input );
return true; //Return true if it works
}
catch( Exception e ) {
return false; //If it doesn't work return false
}
}

public boolean isFloat( String input ) { //Pass in string


try { //Try to make the input into an flaot
Float.parseFloat( input );
return true; //Return true if it works
}
catch( Exception e ) {
return false; //If it doesn't work return false
}
}

public int compareInt(String rights, String lefts, String operator) {


int left = Integer.parseInt(lefts);
int right;
if(isInteger(rights)) {
right = Integer.parseInt(rights);
} else {
System.out.println("erreur");
return -1;
}

switch(operator) {
case "==":
if(right == left) {
return 1;
} else {
return 0;
}

case "!=":
if(right != left) {
return 1;
} else {
return 0;
}
case "<":
if(left < right) {
return 1;
} else {
return 0;
}
case ">":
if(left > right) {
return 1;
} else {
return 0;
}
case "<=":
if(left <= right) {
return 1;
} else {
return 0;
}
case ">=":
if(left >= right) {
return 1;
} else {
return 0;
}
default:
System.out.println("Erreur");
return -1;
}
}

public int compareFloat(String rights, String lefts, String operator) {


float left = Float.parseFloat(lefts);
float right;
if(isInteger(rights)) {
right = Float.parseFloat(rights);
} else {
System.out.println("erreur");
return -1;
}

switch(operator) {
case "==":
if(right == left) {
return 1;
} else {
return 0;
}

case "!=":
if(right != left) {
return 1;
} else {
return 0;
}
case "<":
if(left < right) {
return 1;
} else {
return 0;
}
case ">":
if(left > right) {
return 1;
} else {
return 0;
}
case "<=":
if(left <= right) {
return 1;
} else {
return 0;
}
case ">=":
if(left >= right) {
return 1;
} else {
return 0;
}
default:
System.out.println("Erreur");
return -1;
}
}

You might also like