├── .gitignore ├── src ├── dbms │ ├── backend │ │ ├── parsers │ │ │ ├── json │ │ │ │ ├── Constants.properties │ │ │ │ ├── ClassTypeAdapterFactory.java │ │ │ │ ├── ClassTypeAdapter.java │ │ │ │ ├── test.java │ │ │ │ ├── ColumnAdapter.java │ │ │ │ └── JSONParser.java │ │ │ ├── protobuf │ │ │ │ ├── Constants.properties │ │ │ │ ├── ColumnsAdapterProtoBuf.java │ │ │ │ └── ProtocolBufferParser.java │ │ │ └── xml │ │ │ │ ├── schema │ │ │ │ └── dtd │ │ │ │ │ ├── DTDElementCreator.java │ │ │ │ │ ├── DTDAttributeCreator.java │ │ │ │ │ └── DTDSchemaParser.java │ │ │ │ └── Constants.properties │ │ ├── BackendParser.java │ │ └── BackendParserFactory.java │ ├── util │ │ ├── Operation.java │ │ ├── DBComparatorChain.java │ │ ├── Operator.java │ │ ├── Database.java │ │ ├── Record.java │ │ ├── Column.java │ │ └── RecordSet.java │ ├── sqlparser │ │ ├── sqlInterpreter │ │ │ ├── rules │ │ │ │ ├── DDLStatement.java │ │ │ │ ├── DMLStatement.java │ │ │ │ ├── Expression.java │ │ │ │ ├── DropDatabase.java │ │ │ │ ├── DropTable.java │ │ │ │ ├── CreateDatabase.java │ │ │ │ ├── UseDatabase.java │ │ │ │ ├── AlterDrop.java │ │ │ │ ├── CreateTable.java │ │ │ │ ├── Delete.java │ │ │ │ ├── AlterAdd.java │ │ │ │ ├── Update.java │ │ │ │ ├── InsertIntoTable.java │ │ │ │ ├── Union.java │ │ │ │ └── Select.java │ │ │ ├── Condition.java │ │ │ ├── Where.java │ │ │ ├── BooleanOperator.java │ │ │ ├── BooleanExpressionEvaluator.java │ │ │ ├── SQLPredicate.java │ │ │ └── BooleanExpression.java │ │ ├── syntax │ │ │ ├── SQLSyntax.java │ │ │ ├── UseSyntax.java │ │ │ ├── DeleteSyntax.java │ │ │ ├── DropSyntax.java │ │ │ ├── AlterSyntax.java │ │ │ ├── CreateSyntax.java │ │ │ ├── UpdateSyntax.java │ │ │ ├── InsertSyntax.java │ │ │ ├── WhereSyntax.java │ │ │ ├── SelectSyntax.java │ │ │ └── SyntaxUtil.java │ │ ├── SQLRegex.properties │ │ └── PatternsHolder.java │ ├── exception │ │ ├── DataTypeNotSupportedException.java │ │ ├── SyntaxErrorException.java │ │ ├── ReservedKeyWordException.java │ │ ├── IncorrectDataEntryException.java │ │ ├── TableNotFoundException.java │ │ ├── DatabaseNotFoundException.java │ │ ├── TypeNotSupportedException.java │ │ ├── TableAlreadyCreatedException.java │ │ └── DatabaseAlreadyCreatedException.java │ ├── test │ │ ├── SQLParserTesting │ │ │ ├── SqlParserRef.java │ │ │ └── AlterTest.java │ │ ├── OrderbyTesting.java │ │ └── Test.java │ ├── datatypes │ │ ├── DBDatatype.java │ │ ├── DBString.java │ │ ├── DBInteger.java │ │ ├── DBFloat.java │ │ ├── DBDate.java │ │ └── DatatypeFactory.java │ └── ui │ │ ├── Main.java │ │ └── Formatter.java ├── jdbc │ └── imp │ │ ├── test │ │ └── onlineTester │ │ │ ├── IntegrationTest.java │ │ │ └── TestRunner.java │ │ ├── connection │ │ ├── ConnectionAdapter.java │ │ └── DBConnection.java │ │ ├── resultSetMetaData │ │ ├── DBResultSetMetaDataImpl.java │ │ └── DBResultSetMetaData.java │ │ ├── driver │ │ └── DBDriver.java │ │ ├── ui │ │ └── Main.java │ │ └── statement │ │ ├── StatementAdapter.java │ │ └── DBStatement.java └── log4j2.properties ├── lib ├── gson-2.3.1.jar ├── log4j-api-2.6.2.jar ├── log4j-core-2.6.2.jar ├── protobuf-java-3.1.0.jar └── protobuf-java-util-3.1.0.jar ├── screenshots ├── ScreenShot_1.png └── ScreenShot_2.png ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── .classpath ├── DBMS.iml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | \bin 2 | .DS_Store 3 | .idea 4 | \JDBCLogs -------------------------------------------------------------------------------- /src/dbms/backend/parsers/json/Constants.properties: -------------------------------------------------------------------------------- 1 | extension.json=.json -------------------------------------------------------------------------------- /lib/gson-2.3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yakout/DBMS/HEAD/lib/gson-2.3.1.jar -------------------------------------------------------------------------------- /src/dbms/backend/parsers/protobuf/Constants.properties: -------------------------------------------------------------------------------- 1 | extension.protoBuf=.protobuf -------------------------------------------------------------------------------- /lib/log4j-api-2.6.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yakout/DBMS/HEAD/lib/log4j-api-2.6.2.jar -------------------------------------------------------------------------------- /lib/log4j-core-2.6.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yakout/DBMS/HEAD/lib/log4j-core-2.6.2.jar -------------------------------------------------------------------------------- /lib/protobuf-java-3.1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yakout/DBMS/HEAD/lib/protobuf-java-3.1.0.jar -------------------------------------------------------------------------------- /screenshots/ScreenShot_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yakout/DBMS/HEAD/screenshots/ScreenShot_1.png -------------------------------------------------------------------------------- /screenshots/ScreenShot_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yakout/DBMS/HEAD/screenshots/ScreenShot_2.png -------------------------------------------------------------------------------- /lib/protobuf-java-util-3.1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yakout/DBMS/HEAD/lib/protobuf-java-util-3.1.0.jar -------------------------------------------------------------------------------- /src/dbms/util/Operation.java: -------------------------------------------------------------------------------- 1 | package dbms.util; 2 | 3 | public interface Operation { 4 | > boolean apply(T o1, T o2); 5 | } 6 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/DDLStatement.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | public interface DDLStatement extends Expression { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/DMLStatement.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | public interface DMLStatement extends Expression { 4 | int getUpdateCount(); 5 | } 6 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/SQLSyntax.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public interface SQLSyntax { 6 | Pattern getPattern(); 7 | 8 | String getRegex(); 9 | } 10 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/Condition.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter; 2 | 3 | import dbms.exception.SyntaxErrorException; 4 | 5 | import java.util.Queue; 6 | 7 | public interface Condition { 8 | public Queue getPostfix() throws SyntaxErrorException; 9 | } -------------------------------------------------------------------------------- /src/dbms/exception/DataTypeNotSupportedException.java: -------------------------------------------------------------------------------- 1 | package dbms.exception; 2 | 3 | public class DataTypeNotSupportedException extends Exception { 4 | public DataTypeNotSupportedException() { 5 | super(); 6 | } 7 | 8 | public DataTypeNotSupportedException(final String message) { 9 | super(message); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/dbms/test/SQLParserTesting/SqlParserRef.java: -------------------------------------------------------------------------------- 1 | package dbms.test.SQLParserTesting; 2 | 3 | import dbms.sqlparser.SQLParser; 4 | 5 | public class SqlParserRef { 6 | 7 | private final SQLParser sqlParserObjTest = SQLParser.getInstance(); 8 | 9 | public SQLParser getSqlParserReference() { 10 | return this.sqlParserObjTest; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/Expression.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.exception.*; 4 | 5 | public interface Expression { 6 | void execute() throws DatabaseNotFoundException, 7 | TableNotFoundException, 8 | SyntaxErrorException, 9 | DataTypeNotSupportedException, 10 | TableAlreadyCreatedException, 11 | DatabaseAlreadyCreatedException, 12 | IncorrectDataEntryException; 13 | } -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | DBMS 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.sirius.nature.modelingproject 16 | org.eclipse.jdt.core.javanature 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/jdbc/imp/test/onlineTester/IntegrationTest.java: -------------------------------------------------------------------------------- 1 | package jdbc.imp.test.onlineTester; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.sql.Driver; 7 | 8 | public class IntegrationTest { 9 | 10 | public static Class getSpecifications() { 11 | return Driver.class; 12 | } 13 | 14 | @Test 15 | public void test() { 16 | Assert.assertNotNull("Failed to create Driver implemenation", (Driver) TestRunner.getImplementationInstance()); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/log4j2.properties: -------------------------------------------------------------------------------- 1 | name=PropertiesConfig 2 | appenders=file 3 | #Appender properties. 4 | appender.file.type=File 5 | appender.file.name=LOGFILE 6 | appender.file.fileName=./JDBCLogs/propertieslogs.log 7 | appender.file.layout.type=PatternLayout 8 | appender.file.layout.pattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] %c{1}:%L - %m%n 9 | rootLogger.level=debug 10 | rootLogger.appenderRefs=file 11 | rootLogger.appenderRef.file.ref=LOGFILE 12 | 13 | #appender.file.policies.type = Policies 14 | #appender.file.policies.size.size = 100MB -------------------------------------------------------------------------------- /src/dbms/exception/SyntaxErrorException.java: -------------------------------------------------------------------------------- 1 | package dbms.exception; 2 | 3 | /** 4 | * Signals a syntax error in statement. 5 | */ 6 | public class SyntaxErrorException extends Exception { 7 | private String message; 8 | 9 | public SyntaxErrorException() { 10 | super(); 11 | } 12 | 13 | public SyntaxErrorException(final String message) { 14 | super(message); 15 | this.message = message; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "Syntax Error " + message; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/dbms/exception/ReservedKeyWordException.java: -------------------------------------------------------------------------------- 1 | package dbms.exception; 2 | 3 | public class ReservedKeyWordException extends Exception { 4 | private String message; 5 | 6 | public ReservedKeyWordException() { 7 | super(); 8 | } 9 | 10 | public ReservedKeyWordException(final String message) { 11 | super(message); 12 | this.message = message; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "ReservedKeyWordException{" + 18 | "message='" + message + '\'' + 19 | '}'; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/Where.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter; 2 | 3 | import dbms.exception.SyntaxErrorException; 4 | 5 | import java.util.Queue; 6 | 7 | public class Where implements Condition { 8 | private String infix; 9 | 10 | public Where(String infix) { 11 | this.infix = infix; 12 | } 13 | 14 | @Override 15 | public Queue getPostfix() throws SyntaxErrorException { 16 | return new BooleanExpression().toPostfix(infix); 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "infix: " + infix; 22 | } 23 | } -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/BooleanOperator.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter; 2 | 3 | 4 | public class BooleanOperator { 5 | private Operator operator; 6 | 7 | public BooleanOperator(Operator operator) { 8 | this.operator = operator; 9 | } 10 | 11 | public Operator getOperator() { 12 | return operator; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "BooleanOperator{" + 18 | "operator=" + operator + 19 | '}'; 20 | } 21 | 22 | public enum Operator { 23 | And, 24 | Or 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/DropDatabase.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.exception.DatabaseNotFoundException; 5 | 6 | public class DropDatabase implements DDLStatement { 7 | private String dbName; 8 | 9 | public DropDatabase(final String dbName) { 10 | this.dbName = dbName; 11 | } 12 | 13 | public String getDbName() { 14 | return dbName; 15 | } 16 | 17 | @Override 18 | public void execute() throws DatabaseNotFoundException { 19 | BackendController.getInstance().dropDatabase(dbName); 20 | } 21 | } -------------------------------------------------------------------------------- /src/dbms/backend/parsers/json/ClassTypeAdapterFactory.java: -------------------------------------------------------------------------------- 1 | package dbms.backend.parsers.json; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.TypeAdapter; 5 | import com.google.gson.TypeAdapterFactory; 6 | import com.google.gson.reflect.TypeToken; 7 | 8 | public class ClassTypeAdapterFactory implements TypeAdapterFactory { 9 | @Override 10 | public TypeAdapter create(final Gson gson, 11 | final TypeToken typeToken) { 12 | if (!Class.class.isAssignableFrom(typeToken.getRawType())) { 13 | return null; 14 | } 15 | return (TypeAdapter) new ClassTypeAdapter(); 16 | } 17 | } -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/DropTable.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.exception.DatabaseNotFoundException; 5 | 6 | public class DropTable implements DDLStatement { 7 | private String tableName; 8 | 9 | public DropTable(final String tableName) { 10 | this.tableName = tableName; 11 | } 12 | 13 | public String getTableName() { 14 | return tableName; 15 | } 16 | 17 | @Override 18 | public void execute() throws DatabaseNotFoundException { 19 | BackendController.getInstance().dropTable(tableName); 20 | } 21 | } -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/CreateDatabase.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.exception.DatabaseAlreadyCreatedException; 5 | 6 | public class CreateDatabase implements DDLStatement { 7 | private String dbName; 8 | 9 | public CreateDatabase(final String dbName) { 10 | this.dbName = dbName; 11 | } 12 | 13 | public String getDbName() { 14 | return dbName; 15 | } 16 | 17 | @Override 18 | public void execute() throws DatabaseAlreadyCreatedException { 19 | BackendController.getInstance().createDatabase(dbName); 20 | } 21 | } -------------------------------------------------------------------------------- /src/dbms/datatypes/DBDatatype.java: -------------------------------------------------------------------------------- 1 | package dbms.datatypes; 2 | 3 | public interface DBDatatype extends Comparable { 4 | 5 | /** 6 | * Gets an equivalent primitive java object of a string value. 7 | * @param s String value. 8 | * @return Equivalent object. 9 | */ 10 | Object toObj(String s); 11 | 12 | /** 13 | * Gets the java primitive equivalent to the {@link DBDatatype}. 14 | * @return equivalent primitive java object. 15 | */ 16 | Object getValue(); 17 | 18 | /** 19 | * Gets String key that is used as an id of a data type. 20 | * @return KEY. 21 | */ 22 | String getKey(); 23 | } 24 | -------------------------------------------------------------------------------- /src/dbms/exception/IncorrectDataEntryException.java: -------------------------------------------------------------------------------- 1 | package dbms.exception; 2 | 3 | /** 4 | * Signals an incorrect data entry. 5 | */ 6 | public class IncorrectDataEntryException extends Exception { 7 | private String message; 8 | 9 | public IncorrectDataEntryException() { 10 | super(); 11 | } 12 | 13 | public IncorrectDataEntryException(final String message) { 14 | super(message); 15 | this.message = message; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "IncorrectDataEntryException{" + 21 | "message='" + message + '\'' + 22 | '}'; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/dbms/exception/TableNotFoundException.java: -------------------------------------------------------------------------------- 1 | package dbms.exception; 2 | 3 | /** 4 | * Signals an attempt to open a table that doesn't exist. 5 | */ 6 | public class TableNotFoundException extends Exception { 7 | private String message; 8 | 9 | public TableNotFoundException() { 10 | super(); 11 | } 12 | 13 | public TableNotFoundException(final String message) { 14 | super(message); 15 | this.message = message; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "TableNotFoundException{" + 21 | "message='" + message + '\'' + 22 | '}'; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/dbms/exception/DatabaseNotFoundException.java: -------------------------------------------------------------------------------- 1 | package dbms.exception; 2 | 3 | /** 4 | * Signals an attempt to open a database that doesn't exist. 5 | */ 6 | public class DatabaseNotFoundException extends Exception { 7 | private String message; 8 | 9 | public DatabaseNotFoundException() { 10 | super(); 11 | } 12 | 13 | public DatabaseNotFoundException(final String message) { 14 | super(message); 15 | this.message = message; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "DatabaseNotFoundException{" + 21 | "message='" + message + '\'' + 22 | '}'; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/dbms/exception/TypeNotSupportedException.java: -------------------------------------------------------------------------------- 1 | package dbms.exception; 2 | 3 | /** 4 | * Signals an attempt to add a non-supported type to table. 5 | */ 6 | public class TypeNotSupportedException extends Exception { 7 | private String message; 8 | 9 | public TypeNotSupportedException() { 10 | super(); 11 | } 12 | 13 | public TypeNotSupportedException(final String message) { 14 | super(message); 15 | this.message = message; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "TypeNotSupportedException{" + 21 | "message='" + message + '\'' + 22 | '}'; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/dbms/exception/TableAlreadyCreatedException.java: -------------------------------------------------------------------------------- 1 | package dbms.exception; 2 | 3 | /** 4 | * Signals an attempt to open a table that has already 5 | * been created. 6 | */ 7 | public class TableAlreadyCreatedException extends Exception { 8 | private String message; 9 | 10 | public TableAlreadyCreatedException() { 11 | super(); 12 | } 13 | 14 | public TableAlreadyCreatedException(final String message) { 15 | super(message); 16 | this.message = message; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "TableAlreadyCreatedException{" + 22 | "message='" + message + '\'' + 23 | '}'; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 6 | org.eclipse.jdt.core.compiler.compliance=1.7 7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.source=1.7 13 | -------------------------------------------------------------------------------- /src/dbms/exception/DatabaseAlreadyCreatedException.java: -------------------------------------------------------------------------------- 1 | package dbms.exception; 2 | 3 | /** 4 | * Signals an attempt to create a database when it has 5 | * already been created. 6 | */ 7 | public class DatabaseAlreadyCreatedException extends Exception { 8 | private String message; 9 | 10 | public DatabaseAlreadyCreatedException() { 11 | super(); 12 | } 13 | 14 | public DatabaseAlreadyCreatedException(final String message) { 15 | super(message); 16 | this.message = message; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "DatabaseAlreadyCreatedException{" + 22 | "message='" + message + '\'' + 23 | '}'; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/UseDatabase.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.exception.DatabaseNotFoundException; 5 | 6 | public class UseDatabase implements DDLStatement { 7 | private String dbName; 8 | 9 | /** 10 | * @param dbName sets the name of database(locally0 with the 11 | * required value. 12 | */ 13 | public UseDatabase(final String dbName) { 14 | this.dbName = dbName; 15 | } 16 | 17 | public String getDbName() { 18 | return dbName; 19 | } 20 | 21 | @Override 22 | public void execute() throws DatabaseNotFoundException { 23 | BackendController.getInstance().useDatabase(dbName); 24 | } 25 | } -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/dbms/backend/parsers/xml/schema/dtd/DTDElementCreator.java: -------------------------------------------------------------------------------- 1 | package dbms.backend.parsers.xml.schema.dtd; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.PrintWriter; 5 | 6 | class DTDElementCreator { 7 | static PrintWriter out; 8 | 9 | private DTDElementCreator() { 10 | } 11 | 12 | /** 13 | * Creates DTD element in DTD file. 14 | * @param elName defines element name. 15 | * @param property defines the behaviour of the DTD element. 16 | * @param out {@link PrintWriter} Reference on the file. 17 | * @throws FileNotFoundException if the named file is no found. 18 | */ 19 | protected static void createElement(String elName, String property, 20 | PrintWriter out) 21 | throws FileNotFoundException { 22 | out.println(""); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/jdbc/imp/connection/ConnectionAdapter.java: -------------------------------------------------------------------------------- 1 | package jdbc.imp.connection; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.backend.BackendParser; 5 | import dbms.backend.BackendParserFactory; 6 | import jdbc.imp.statement.StatementAdapter; 7 | 8 | import java.sql.SQLException; 9 | import java.sql.Statement; 10 | 11 | public class ConnectionAdapter extends DBConnection { 12 | private StatementAdapter statement; 13 | 14 | public ConnectionAdapter(String url, String databaseDir) { 15 | BackendController.getInstance().setCurrentDatabaseDir(databaseDir); 16 | BackendParserFactory.getFactory().setCurrentParser(url); 17 | } 18 | 19 | @Override 20 | public Statement createStatement() throws SQLException { 21 | statement = new StatementAdapter(this); 22 | return statement; 23 | } 24 | 25 | @Override 26 | public void close() throws SQLException { 27 | statement = null; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/UseSyntax.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class UseSyntax implements SQLSyntax { 6 | private static UseSyntax instance = null; 7 | private final String USE_REGEX = "(?i)^\\s*use\\s+(" 8 | + SyntaxUtil.DATABASE_NAME + ")" 9 | + SyntaxUtil.SEMI_COLON + "$"; 10 | private Pattern usePattern = null; 11 | 12 | private UseSyntax() { 13 | } 14 | 15 | public static UseSyntax getInstance() { 16 | if (instance == null) { 17 | instance = new UseSyntax(); 18 | } 19 | return instance; 20 | } 21 | 22 | @Override 23 | public Pattern getPattern() { 24 | if (usePattern == null) { 25 | usePattern = Pattern.compile(USE_REGEX); 26 | } 27 | return usePattern; 28 | } 29 | 30 | @Override 31 | public String getRegex() { 32 | return USE_REGEX; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/BooleanExpressionEvaluator.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter; 2 | 3 | import dbms.datatypes.DBDatatype; 4 | import dbms.exception.IncorrectDataEntryException; 5 | 6 | import java.util.Map; 7 | import java.util.Queue; 8 | 9 | public abstract class BooleanExpressionEvaluator { 10 | private Queue postfix; 11 | 12 | public BooleanExpressionEvaluator(Queue postfix) { 13 | this.postfix = postfix; 14 | } 15 | 16 | public BooleanExpressionEvaluator() { 17 | } 18 | 19 | public void setPostfix(Queue postfix) { 20 | this.postfix = postfix; 21 | } 22 | 23 | private Object fetchFromColumn(String columnName) { 24 | return null; 25 | } 26 | 27 | protected abstract boolean evaluate(Map row, 28 | Queue postfix, Map columns) 30 | throws IncorrectDataEntryException; 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/AlterDrop.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.exception.*; 5 | 6 | public class AlterDrop implements DDLStatement { 7 | private String tableName; 8 | private String columnName; 9 | 10 | public AlterDrop(String tableName, String columnName) { 11 | this.tableName = tableName; 12 | this.columnName = columnName; 13 | } 14 | 15 | public String getTableName() { 16 | return tableName; 17 | } 18 | 19 | public String getColumnName() { 20 | return columnName; 21 | } 22 | 23 | @Override 24 | public void execute() throws DatabaseNotFoundException, 25 | TableNotFoundException, SyntaxErrorException, 26 | DataTypeNotSupportedException, 27 | TableAlreadyCreatedException, DatabaseAlreadyCreatedException, 28 | IncorrectDataEntryException { 29 | BackendController.getInstance().alterDrop(tableName, columnName); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/dbms/backend/parsers/xml/schema/dtd/DTDAttributeCreator.java: -------------------------------------------------------------------------------- 1 | package dbms.backend.parsers.xml.schema.dtd; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.PrintWriter; 5 | import java.util.ResourceBundle; 6 | 7 | class DTDAttributeCreator { 8 | private static final ResourceBundle CONSTANTS = 9 | ResourceBundle.getBundle("dbms.backend.parsers.xml.Constants"); 10 | 11 | /** 12 | * Creates an attribute element in the DTD file. 13 | * @param elName element name. 14 | * @param attName attribute name. 15 | * @param state behaviour of DTD element. 16 | * @param out {@link PrintWriter} Reference on the file. 17 | * @throws FileNotFoundException if there is not file with that name. 18 | */ 19 | protected static void createElement(String elName, String attName, 20 | String state, PrintWriter out) throws 21 | FileNotFoundException { 22 | out.println(""); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/dbms/backend/parsers/xml/Constants.properties: -------------------------------------------------------------------------------- 1 | extension.schema=.xsd 2 | extension.xml=.xml 3 | extensionDTD.schema=.dtd 4 | xs.schema=xs:schema 5 | xs.element=xs:element 6 | xs.extension=xs:extension 7 | xs.complex=xs:complexType 8 | xs.sequence=xs:sequence 9 | xs.simple=xs:simpleContent 10 | xs.attr=xs:attribute 11 | nothing=0 12 | string.type=xs:string 13 | int.type=xs:integer 14 | table.element=table 15 | column.element=col 16 | optional.col=col* 17 | optional.row=row* 18 | pc.data=#PCDATA 19 | c.data=#CDATA 20 | row.element=row 21 | formDefault.attr=attributeFormDefault 22 | elementFormDefault.attr=elementFormDefault 23 | xmlns.attr=xmlns:xs 24 | name.attr=name 25 | db.attr=database 26 | rows.attr=rows 27 | type.attr=type 28 | use.attr=use 29 | maxOccurs.attr=maxOccurs 30 | minOccurs.attr=minOccurs 31 | base.attr=base 32 | formDefault.val=unqualified 33 | elementFormDefault.val=qualified 34 | xmlns.val=http://www.w3.org/2001/XMLSchema 35 | unbounded.val=unbounded 36 | optional.val=optional 37 | index.val=index 38 | default.attr=default 39 | indentation.val=4 40 | indentation={http://xml.apache.org/xslt}indent-amount 41 | req.type=#REQUIRED 42 | publicId.dtd=-//DBMS//XML DBMS 1.0//EN -------------------------------------------------------------------------------- /src/dbms/backend/parsers/json/ClassTypeAdapter.java: -------------------------------------------------------------------------------- 1 | package dbms.backend.parsers.json; 2 | 3 | import java.io.IOException; 4 | 5 | import com.google.gson.TypeAdapter; 6 | import com.google.gson.stream.JsonReader; 7 | import com.google.gson.stream.JsonToken; 8 | import com.google.gson.stream.JsonWriter; 9 | 10 | public class ClassTypeAdapter extends TypeAdapter> { 11 | 12 | @Override 13 | public Class read(JsonReader jsonReader) throws IOException { 14 | if (jsonReader.peek() == JsonToken.NULL) { 15 | jsonReader.nextNull(); 16 | return null; 17 | } 18 | Class clazz = null; 19 | try { 20 | clazz = Class.forName(jsonReader.nextString()); 21 | } catch (ClassNotFoundException exception) { 22 | throw new IOException(exception); 23 | } 24 | return clazz; 25 | } 26 | 27 | 28 | @Override 29 | public void write(JsonWriter jsonWriter, Class clazz) throws 30 | IOException { 31 | if (clazz == null) { 32 | jsonWriter.nullValue(); 33 | return; 34 | } 35 | jsonWriter.value(clazz.getName()); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/CreateTable.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.datatypes.DBDatatype; 5 | import dbms.exception.DatabaseNotFoundException; 6 | import dbms.exception.IncorrectDataEntryException; 7 | import dbms.exception.TableAlreadyCreatedException; 8 | 9 | import java.util.Map; 10 | 11 | public class CreateTable implements DDLStatement { 12 | private String tableName; 13 | private Map> columns; 14 | 15 | public CreateTable(final String tableName, final Map> columns) { 17 | this.tableName = tableName; 18 | this.columns = columns; 19 | } 20 | 21 | public String getTableName() { 22 | return tableName; 23 | } 24 | 25 | public Map> getColumns() { 26 | return columns; 27 | } 28 | 29 | @Override 30 | public void execute() throws DatabaseNotFoundException, 31 | TableAlreadyCreatedException, IncorrectDataEntryException { 32 | BackendController.getInstance().createTable(tableName, columns); 33 | } 34 | } -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/DeleteSyntax.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class DeleteSyntax implements SQLSyntax { 6 | private static DeleteSyntax instance = null; 7 | private final String DELETE_REGEX = "(?i)^\\s*delete\\s+([*]{1}\\s+)" 8 | + "?from\\s+(" 9 | + SyntaxUtil.TABLE_NAME + ")" 10 | + WhereSyntax.getInstance().getRegex(); 11 | private Pattern deletePattern = Pattern.compile(DELETE_REGEX); 12 | 13 | private DeleteSyntax() { 14 | 15 | } 16 | 17 | public static DeleteSyntax getInstance() { 18 | if (instance == null) { 19 | instance = new DeleteSyntax(); 20 | } 21 | return instance; 22 | } 23 | 24 | public static void main(String[] args) { 25 | System.out.print(getInstance().getRegex()); 26 | } 27 | 28 | @Override 29 | public Pattern getPattern() { 30 | if (deletePattern == null) { 31 | deletePattern = Pattern.compile(DELETE_REGEX); 32 | } 33 | return deletePattern; 34 | } 35 | 36 | @Override 37 | public String getRegex() { 38 | return DELETE_REGEX; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/DropSyntax.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class DropSyntax implements SQLSyntax { 6 | 7 | private static DropSyntax instance = null; 8 | private final String DROP_REGEX = "(?i)^\\s*drop\\s+(table|database)" 9 | + "{1}\\s+(" 10 | + SyntaxUtil.TABLE_NAME // OR SyntaxUtil.DATABASE_NAME 11 | + "){1}" 12 | + SyntaxUtil.SEMI_COLON 13 | + "$"; 14 | private Pattern dropPattern = Pattern.compile(DROP_REGEX); 15 | 16 | private DropSyntax() { 17 | 18 | } 19 | 20 | public static DropSyntax getInstance() { 21 | if (instance == null) { 22 | instance = new DropSyntax(); 23 | } 24 | return instance; 25 | } 26 | 27 | public static void main(String[] args) { 28 | System.out.print(getInstance().getRegex()); 29 | } 30 | 31 | @Override 32 | public Pattern getPattern() { 33 | if (dropPattern == null) { 34 | dropPattern = Pattern.compile(DROP_REGEX); 35 | } 36 | return dropPattern; 37 | } 38 | 39 | @Override 40 | public String getRegex() { 41 | return DROP_REGEX; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/Delete.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.exception.DatabaseNotFoundException; 5 | import dbms.exception.IncorrectDataEntryException; 6 | import dbms.exception.SyntaxErrorException; 7 | import dbms.exception.TableNotFoundException; 8 | import dbms.sqlparser.sqlInterpreter.Where; 9 | 10 | public class Delete implements DMLStatement { 11 | private String tableName; 12 | private Where where; 13 | private int updateCount; 14 | 15 | public Delete(String tableName) { 16 | this.tableName = tableName; 17 | } 18 | 19 | public String getTableName() { 20 | return tableName; 21 | } 22 | 23 | public Where getWhere() { 24 | return where; 25 | } 26 | 27 | public void setWhere(Where where) { 28 | this.where = where; 29 | } 30 | 31 | @Override 32 | public int getUpdateCount() { 33 | return updateCount; 34 | } 35 | 36 | @Override 37 | public void execute() throws DatabaseNotFoundException, 38 | TableNotFoundException, SyntaxErrorException, IncorrectDataEntryException { 39 | updateCount = BackendController.getInstance().delete(tableName, where); 40 | } 41 | } -------------------------------------------------------------------------------- /src/dbms/util/DBComparatorChain.java: -------------------------------------------------------------------------------- 1 | package dbms.util; 2 | 3 | import java.util.ArrayList; 4 | import java.util.BitSet; 5 | import java.util.Comparator; 6 | import java.util.List; 7 | 8 | public class DBComparatorChain implements Comparator { 9 | private final List> comparatorList; 10 | private BitSet orderingBits = null; 11 | 12 | public DBComparatorChain() { 13 | this.comparatorList = new ArrayList>(); 14 | this.orderingBits = new BitSet(); 15 | } 16 | 17 | void addComparator(Comparator RecordComparator, boolean reverse) { 18 | comparatorList.add(RecordComparator); 19 | if (reverse) orderingBits.set(size() - 1); 20 | } 21 | 22 | public int size() { 23 | return comparatorList.size(); 24 | } 25 | 26 | @Override 27 | public int compare(T o1, T o2) { 28 | int res = 0; 29 | for (int i = 0; i < size(); i++) { 30 | res = comparatorList.get(i).compare(o1, o2); 31 | if (res != 0) { 32 | if (orderingBits.get(i)) { 33 | res = res > 0 ? -1 : 1; 34 | } 35 | return res; 36 | } 37 | } 38 | return res; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/AlterSyntax.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class AlterSyntax implements SQLSyntax { 6 | private static AlterSyntax instance = null; 7 | private final String ALTER_REGEX = "(?i)^\\s*alter\\s+table\\s+(" 8 | + SyntaxUtil.TABLE_NAME + "){1}\\s+(((drop\\s+column){1}\\s+(" 9 | + SyntaxUtil.COLUMN_NAME + "){1}\\s*)|((add){1}\\s+(" 10 | + SyntaxUtil.COLUMN_NAME + "){1}\\s+" 11 | + SyntaxUtil.SUPPORTED_DATA_TYPES + "))" 12 | + SyntaxUtil.SEMI_COLON + "$"; 13 | private Pattern alterPattern = null; 14 | 15 | private AlterSyntax() { 16 | 17 | } 18 | 19 | public static AlterSyntax getInstance() { 20 | if (instance == null) { 21 | instance = new AlterSyntax(); 22 | } 23 | return instance; 24 | } 25 | 26 | public static void main(String[] args) { 27 | System.out.println(getInstance().getRegex()); 28 | } 29 | 30 | @Override 31 | public Pattern getPattern() { 32 | if (alterPattern == null) { 33 | alterPattern = Pattern.compile(ALTER_REGEX); 34 | } 35 | return alterPattern; 36 | } 37 | 38 | @Override 39 | public String getRegex() { 40 | return ALTER_REGEX; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/CreateSyntax.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class CreateSyntax implements SQLSyntax { 6 | private static CreateSyntax instance = null; 7 | private final String CREATE_REGEX = "(?i)^\\s*create\\s+((database\\s+(" 8 | + SyntaxUtil.DATABASE_NAME + ")){1}|(table\\s+(" 9 | + SyntaxUtil.TABLE_NAME + ")\\s*[(]\\s*(\\s*" 10 | + SyntaxUtil.COLUMN_NAME + "\\s+" 11 | + SyntaxUtil.SUPPORTED_DATA_TYPES + "\\s*(\\s*,\\s*" 12 | + SyntaxUtil.COLUMN_NAME + "\\s+" 13 | + SyntaxUtil.SUPPORTED_DATA_TYPES + "\\s*)*)\\s*[)]){1})" 14 | + SyntaxUtil.SEMI_COLON + "$"; 15 | private Pattern createPattern = Pattern.compile(CREATE_REGEX); 16 | 17 | private CreateSyntax() { 18 | 19 | } 20 | 21 | public static CreateSyntax getInstance() { 22 | if (instance == null) { 23 | instance = new CreateSyntax(); 24 | } 25 | return instance; 26 | } 27 | 28 | public static void main(String[] args) { 29 | System.out.println(getInstance().getRegex()); 30 | } 31 | 32 | @Override 33 | public Pattern getPattern() { 34 | if (createPattern == null) { 35 | createPattern = Pattern.compile(CREATE_REGEX); 36 | } 37 | return createPattern; 38 | } 39 | 40 | @Override 41 | public String getRegex() { 42 | return CREATE_REGEX; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/dbms/datatypes/DBString.java: -------------------------------------------------------------------------------- 1 | package dbms.datatypes; 2 | 3 | public class DBString implements DBDatatype { 4 | 5 | /** 6 | * Key identifier to DBString. 7 | */ 8 | public static final String KEY = "String"; 9 | 10 | static { 11 | DatatypeFactory.getFactory().register(KEY, DBString.class); 12 | } 13 | 14 | private String value = null; 15 | 16 | public DBString() { 17 | } 18 | 19 | public DBString(String value) { 20 | this.value = value; 21 | } 22 | 23 | @Override 24 | public Object toObj(String s) { 25 | return s; 26 | } 27 | 28 | @Override 29 | public int compareTo(DBDatatype data) { 30 | return value.compareTo((String) data.getValue()); 31 | } 32 | 33 | @Override 34 | public String getValue() { 35 | return value; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return value; 41 | } 42 | 43 | @Override 44 | 45 | public String getKey() { 46 | return KEY; 47 | } 48 | 49 | @Override 50 | public boolean equals(Object o) { 51 | if (this == o) return true; 52 | if (o == null || getClass() != o.getClass()) return false; 53 | 54 | DBString dbString = (DBString) o; 55 | return value != null ? value.equals(dbString.value) : 56 | dbString.value == null; 57 | } 58 | 59 | @Override 60 | public int hashCode() { 61 | return value != null ? value.hashCode() : 0; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/SQLRegex.properties: -------------------------------------------------------------------------------- 1 | rule.regex=(?i)^\\s*(select|drop|insert|update|delete|create|where|use|alter).* 2 | where.regex=(\\s+where\\s+(TRUE|([(]\\s*)*\\s*(\\w+)\\s*(>|<|>=|<=|=|!=){1}\\s*('\\w+'|\\w+|"\\w+")\\s*(\\s*[)])*\\s*(\\s+(and|or)(\\s*([(]\\s*)*\\s*(\\w+)\\s*(>|<|>=|<=|=|!=){1}\\s*(\\w+|'\\w+'|"\\w+")\\s*(\\s*[)])*\\s*))*))?\\s*;\\s*$ 3 | select.regex=(?i)^\\s*select\\s+(?:distinct\\s+)?((\\w+\\s*(\\s*,\\s*\\w+)*)|(\\*))\\s+from\\s+(\\w+) 4 | drop.regex=(?i)^\\s*drop\\s+(table|database){1}\\s+(\\w+){1}\\s*;\\s*$ 5 | insert.regex=(?i)^\\s*insert\\s+into\\s+(\\w+){1}\\s+(?:[(]\\s*(\\w+\\s*(\\s*,\\s*\\w+)*)\\s*[)])?\\s*values\\s*[(]\\s*(('(?:\\s*\\w+\\s*)*'|\\d+|"(?:\\s*\\w+\\s*)*"){1}\\s*(\\s*,\\s*('(?:\\s*\\w+\\s*)*'|\\d+|"(?:\\s*\\w+\\s*)*"){1})*)\\s*[)]\\s*;\\s*$ 6 | create.regex=(?i)^\\s*create\\s+((database\\s+(\\w+)){1}|(table\\s+(\\w+)\\s*[(]\\s*(\\s*\\w+\\s+(int|varchar|date|float){1}\\s*(\\s*,\\s*\\w+\\s+(int|varchar|date|float){1}\\s*)*)\\s*[)]){1})\\s*;\\s*$ 7 | update.regex=(?i)^\\s*update\\s+(\\w+){1}\\s+set\\s+(\\w+\\s*=\\s*('\\w+'|\\w+|"\\w+"){1}\\s*(\\s*,\\s*\\w+\\s*=\\s*('\\w+'|\\w+|"\\w+"){1})*) 8 | delete.regex=(?i)^\\s*delete\\s+([*]{1}\\s+)?from\\s+(\\w+) 9 | alter.regex=(?i)^\\s*alter\\s+table\\s+(\\w+){1}\\s+(((drop\\s+column){1}\\s+(\\w+){1}\\s*)|((add){1}\\s+(\\w+){1}\\s+(int|varchar|date|float){1}))\\s*;\\s*$ 10 | use.database.regex=(?i)^\\s*use\\s+(\\w+)\\s*;\\s* 11 | orderby.regex=(\\s+(?i)(?:order\\s+by)(?-i)\\s+(\\w+\\s*(\\s+ASC|\\s+DESC)?\\s*(\\s*,\\s*\\w+\\s*(?:\\s+ASC|\\s+DESC)?\\s*)*))? -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/UpdateSyntax.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class UpdateSyntax implements SQLSyntax { 6 | private static UpdateSyntax instance = null; 7 | private final String VALUE_FORMAT = "(" 8 | + SyntaxUtil.MULTIPLE_WORDS_SINGLE_QUOTES + "|" 9 | + SyntaxUtil.NUMBER_FORMAT + "|" 10 | + SyntaxUtil.DATE_FORMAT + "|" 11 | + SyntaxUtil.COLUMN_NAME + ")"; 12 | private final String UPDATE_REGEX = "(?i)^\\s*update\\s+(" 13 | + SyntaxUtil.TABLE_NAME + "){1}\\s+set\\s+(" 14 | + SyntaxUtil.COLUMN_NAME + "\\s*=\\s*" 15 | + VALUE_FORMAT + "\\s*(\\s*,\\s*" 16 | + SyntaxUtil.COLUMN_NAME + "\\s*=\\s*" 17 | + VALUE_FORMAT + ")*)" 18 | + WhereSyntax.getInstance().getRegex(); 19 | private Pattern updatePattern = null; 20 | 21 | private UpdateSyntax() { 22 | } 23 | 24 | public static UpdateSyntax getInstance() { 25 | if (instance == null) { 26 | instance = new UpdateSyntax(); 27 | } 28 | return instance; 29 | } 30 | 31 | public static void main(String[] args) { 32 | System.out.println(getInstance().getRegex()); 33 | } 34 | 35 | @Override 36 | public Pattern getPattern() { 37 | if (updatePattern == null) { 38 | updatePattern = Pattern.compile(UPDATE_REGEX); 39 | } 40 | return updatePattern; 41 | } 42 | 43 | @Override 44 | public String getRegex() { 45 | return UPDATE_REGEX; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/dbms/util/Operator.java: -------------------------------------------------------------------------------- 1 | package dbms.util; 2 | 3 | 4 | public enum Operator implements Operation { 5 | GreaterThan { 6 | @Override 7 | public > boolean apply(T dataType1, T 8 | dataType2) { 9 | return dataType1 != null && dataType1.compareTo(dataType2) > 0; 10 | } 11 | }, 12 | SmallerThan { 13 | @Override 14 | public > boolean apply(T dataType1, T 15 | dataType2) { 16 | return dataType1 != null && dataType1.compareTo(dataType2) < 0; 17 | } 18 | }, 19 | GreaterThanOrEqual { 20 | @Override 21 | public > boolean apply(T dataType1, T 22 | dataType2) { 23 | return dataType1 != null && dataType1.compareTo(dataType2) >= 0; 24 | } 25 | }, 26 | SmallerThanOrEqual { 27 | @Override 28 | public > boolean apply(T dataType1, T 29 | dataType2) { 30 | return dataType1 != null && dataType1.compareTo(dataType2) <= 0; 31 | } 32 | }, 33 | Equal { 34 | @Override 35 | public > boolean apply(T dataType1, T 36 | dataType2) { 37 | return dataType1 != null && dataType1.compareTo(dataType2) == 0; 38 | } 39 | }, 40 | NotEqual { 41 | @Override 42 | public > boolean apply(T dataType1, T 43 | dataType2) { 44 | return dataType1 != null && dataType1.compareTo(dataType2) != 0; 45 | } 46 | }; 47 | } 48 | -------------------------------------------------------------------------------- /src/dbms/datatypes/DBInteger.java: -------------------------------------------------------------------------------- 1 | package dbms.datatypes; 2 | 3 | 4 | public class DBInteger implements DBDatatype { 5 | 6 | /** 7 | * Key identifier to DBInteger. 8 | */ 9 | public static final String KEY = "Integer"; 10 | 11 | static { 12 | DatatypeFactory.getFactory().register(KEY, DBInteger.class); 13 | } 14 | 15 | private Integer value; 16 | 17 | public DBInteger() { 18 | } 19 | 20 | public DBInteger(final Integer value) { 21 | this.value = value; 22 | } 23 | 24 | @Override 25 | public Object toObj(final String s) { 26 | try { 27 | return Integer.parseInt(s); 28 | } catch (Exception e) { 29 | return null; 30 | } 31 | } 32 | 33 | @Override 34 | public int compareTo(final DBDatatype data) { 35 | return value.compareTo((Integer) data.getValue()); 36 | } 37 | 38 | @Override 39 | public Integer getValue() { 40 | return value; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return value.toString(); 46 | } 47 | 48 | @Override 49 | public String getKey() { 50 | return KEY; 51 | } 52 | 53 | @Override 54 | public boolean equals(final Object o) { 55 | if (this == o) return true; 56 | if (o == null || getClass() != o.getClass()) return false; 57 | 58 | DBInteger dbInteger = (DBInteger) o; 59 | 60 | return value != null ? value.equals(dbInteger.value) 61 | : dbInteger.value == null; 62 | } 63 | 64 | @Override 65 | public int hashCode() { 66 | return value != null ? value.hashCode() : 0; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/AlterAdd.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.datatypes.DBDatatype; 5 | import dbms.exception.DataTypeNotSupportedException; 6 | import dbms.exception.DatabaseAlreadyCreatedException; 7 | import dbms.exception.DatabaseNotFoundException; 8 | import dbms.exception.IncorrectDataEntryException; 9 | import dbms.exception.SyntaxErrorException; 10 | import dbms.exception.TableAlreadyCreatedException; 11 | import dbms.exception.TableNotFoundException; 12 | 13 | public class AlterAdd implements DDLStatement { 14 | private String tableName; 15 | private String columnName; 16 | private Class dataType; 17 | 18 | public AlterAdd(final String tableName, final String columnName, 19 | Class dataType) { 20 | this.tableName = tableName; 21 | this.columnName = columnName; 22 | this.dataType = dataType; 23 | } 24 | 25 | public String getTableName() { 26 | return tableName; 27 | } 28 | 29 | public String getColumnName() { 30 | return columnName; 31 | } 32 | 33 | public Class getDataType() { 34 | return dataType; 35 | } 36 | 37 | @Override 38 | public void execute() throws DatabaseNotFoundException, 39 | TableNotFoundException, SyntaxErrorException, 40 | DataTypeNotSupportedException, TableAlreadyCreatedException, 41 | DatabaseAlreadyCreatedException, IncorrectDataEntryException { 42 | BackendController.getInstance().alterAdd(tableName, columnName, 43 | dataType); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/dbms/datatypes/DBFloat.java: -------------------------------------------------------------------------------- 1 | package dbms.datatypes; 2 | 3 | public class DBFloat implements DBDatatype { 4 | 5 | /** 6 | * Key identifier to DBString. 7 | */ 8 | public static final String KEY = "Float"; 9 | 10 | static { 11 | DatatypeFactory.getFactory().register(KEY, DBFloat.class); 12 | } 13 | 14 | private Float value; 15 | 16 | public DBFloat() { 17 | 18 | } 19 | 20 | /** 21 | * @param value {@link Float} sets the local value to the given value. 22 | */ 23 | public DBFloat(final Float value) { 24 | this.value = value; 25 | } 26 | 27 | @Override 28 | public Object toObj(final String s) { 29 | try { 30 | return Float.parseFloat(s); 31 | } catch (Exception e) { 32 | return null; 33 | } 34 | } 35 | 36 | @Override 37 | public int compareTo(final DBDatatype data) { 38 | return value.compareTo((Float) data.getValue()); 39 | } 40 | 41 | @Override 42 | public Float getValue() { 43 | return value; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return value.toString(); 49 | } 50 | 51 | @Override 52 | public String getKey() { 53 | return KEY; 54 | } 55 | 56 | @Override 57 | public boolean equals(final Object o) { 58 | if (this == o) return true; 59 | if (o == null || getClass() != o.getClass()) return false; 60 | 61 | DBFloat dbFloat = (DBFloat) o; 62 | 63 | return value != null ? value.equals(dbFloat.value) 64 | : dbFloat.value == null; 65 | } 66 | 67 | @Override 68 | public int hashCode() { 69 | return value != null ? value.hashCode() : 0; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/dbms/datatypes/DBDate.java: -------------------------------------------------------------------------------- 1 | package dbms.datatypes; 2 | 3 | import java.sql.Date; 4 | 5 | public class DBDate implements DBDatatype { 6 | 7 | /** 8 | * Key identifier to DBDate. 9 | */ 10 | public static final String KEY = "Date"; 11 | 12 | static { 13 | DatatypeFactory.getFactory().register(KEY, DBDate.class); 14 | } 15 | 16 | private Date value; 17 | 18 | public DBDate() { 19 | 20 | } 21 | 22 | /** 23 | * @param value {@link Date} sets the the local date to the fiven value. 24 | */ 25 | public DBDate(final Date value) { 26 | this.value = value; 27 | } 28 | 29 | @Override 30 | public Object toObj(final String s) { 31 | try { 32 | return Date.valueOf(s); 33 | } catch (Exception e) { 34 | return null; 35 | } 36 | } 37 | 38 | @Override 39 | public int compareTo(final DBDatatype data) { 40 | return value.compareTo((Date) data.getValue()); 41 | } 42 | 43 | @Override 44 | public Date getValue() { 45 | return value; 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return value.toString(); 51 | } 52 | 53 | @Override 54 | public String getKey() { 55 | return KEY; 56 | } 57 | 58 | @Override 59 | public boolean equals(final Object o) { 60 | if (this == o) return true; 61 | if (o == null || getClass() != o.getClass()) return false; 62 | 63 | DBDate dbDate = (DBDate) o; 64 | return value != null ? value.equals(dbDate.value) 65 | : dbDate.value == null; 66 | } 67 | 68 | @Override 69 | public int hashCode() { 70 | return value != null ? value.hashCode() : 0; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/InsertSyntax.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | 6 | public class InsertSyntax implements SQLSyntax { 7 | private static InsertSyntax instance = null; 8 | private final String COLUMNS_FORMAT = "(?:[(]\\s*(" 9 | + SyntaxUtil.COLUMN_NAME + "\\s*(\\s*,\\s*" 10 | + SyntaxUtil.COLUMN_NAME + ")*)\\s*[)])?"; 11 | private final String VALUE_FORMAT = "(" 12 | + SyntaxUtil.MULTIPLE_WORDS_SINGLE_QUOTES 13 | + "|" + SyntaxUtil.MULTIPLE_WORDS_DOUBLE_QUOTES 14 | + "|" + SyntaxUtil.NUMBER_FORMAT 15 | + "|" + SyntaxUtil.DATE_FORMAT + ")"; 16 | private final String VALUES_FORMAT = "[(]\\s*(" 17 | + VALUE_FORMAT + "\\s*(\\s*,\\s*" 18 | + VALUE_FORMAT + ")*)\\s*[)]"; 19 | private final String insertRegex = "(?i)^\\s*insert\\s+into\\s+(" 20 | + SyntaxUtil.TABLE_NAME + ")\\s*" 21 | + COLUMNS_FORMAT + "\\s*values\\s*" 22 | + VALUES_FORMAT 23 | + SyntaxUtil.SEMI_COLON + "$"; 24 | private Pattern insertPattern = null; 25 | 26 | private InsertSyntax() { 27 | } 28 | 29 | public static InsertSyntax getInstance() { 30 | if (instance == null) { 31 | instance = new InsertSyntax(); 32 | } 33 | return instance; 34 | } 35 | 36 | public static void main(String[] args) { 37 | System.out.println(getInstance().getRegex()); 38 | } 39 | 40 | @Override 41 | public Pattern getPattern() { 42 | if (insertPattern == null) { 43 | insertPattern = Pattern.compile(insertRegex); 44 | } 45 | return insertPattern; 46 | } 47 | 48 | @Override 49 | public String getRegex() { 50 | return insertRegex; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/WhereSyntax.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class WhereSyntax implements SQLSyntax { 6 | public static final String SUPPORTED_OPERATORS = "(>|<|>=|<=|=|!=){1}"; 7 | public static final String VALUE_FORMAT = "(" 8 | + SyntaxUtil.DATE_FORMAT 9 | + "|" + SyntaxUtil.NUMBER_FORMAT 10 | + "|" + SyntaxUtil.MULTIPLE_WORDS_SINGLE_QUOTES 11 | + "|" + SyntaxUtil.MULTIPLE_WORDS_DOUBLE_QUOTES 12 | + "|" + SyntaxUtil.COLUMN_NAME + ")"; 13 | private static WhereSyntax instance = null; 14 | private final String BOOLEAN_OPERATORS = "(and|or)"; 15 | private final String WHERE_REGEX = "(\\s+where\\s+(TRUE|([(]\\s*)*\\s*(" 16 | + SyntaxUtil.COLUMN_NAME + ")\\s*" 17 | + SUPPORTED_OPERATORS + "\\s*" 18 | + VALUE_FORMAT + "\\s*(\\s*[)])*\\s*(\\s+" 19 | + BOOLEAN_OPERATORS + "(\\s*([(]\\s*)*\\s*(" 20 | + SyntaxUtil.COLUMN_NAME + ")\\s*" 21 | + SUPPORTED_OPERATORS + "\\s*" 22 | + VALUE_FORMAT + "\\s*(\\s*[)])*\\s*))*))?" 23 | + SyntaxUtil.SEMI_COLON 24 | + "$"; 25 | private Pattern wherePattern = null; 26 | 27 | private WhereSyntax() { 28 | } 29 | 30 | public static WhereSyntax getInstance() { 31 | if (instance == null) { 32 | instance = new WhereSyntax(); 33 | } 34 | return instance; 35 | } 36 | 37 | public static void main(String[] args) { 38 | System.out.print(getInstance().getRegex()); 39 | } 40 | 41 | @Override 42 | public Pattern getPattern() { 43 | if (wherePattern == null) { 44 | wherePattern = Pattern.compile(WHERE_REGEX); 45 | } 46 | return wherePattern; 47 | } 48 | 49 | @Override 50 | public String getRegex() { 51 | return WHERE_REGEX; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/SelectSyntax.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class SelectSyntax implements SQLSyntax { 6 | private static SelectSyntax instance = null; 7 | private final String COLUMNS_FORMAT = "((" 8 | + SyntaxUtil.COLUMN_NAME + "\\s*(\\s*,\\s*" 9 | + SyntaxUtil.COLUMN_NAME + ")*)|(\\*))"; 10 | private final String ORDERBY_COLUMNS_FORMAT = "(" 11 | + SyntaxUtil.COLUMN_NAME + "\\s*(\\s+ASC|\\s+DESC)?\\s*(\\s*,\\s*" 12 | + SyntaxUtil.COLUMN_NAME + "\\s*(?:\\s+ASC|\\s+DESC)?\\s*)*)"; 13 | private final String ORDERBY_REGEX = "(\\s+(?i)(?:order\\s+by)(?-i)\\s+" 14 | + ORDERBY_COLUMNS_FORMAT + ")?"; 15 | private final String SELECT_REGEX = "(?i)^\\s*select\\s+(distinct\\s+)?" 16 | + COLUMNS_FORMAT + "\\s+from\\s+(" 17 | + SyntaxUtil.TABLE_NAME + ")" 18 | + ORDERBY_REGEX 19 | + WhereSyntax.getInstance().getRegex(); 20 | private final String UNION_REGEX = SELECT_REGEX.replaceAll("(\\^|\\$)", "") 21 | + "\\s+((union|union\\s+all)\\s+" 22 | + SELECT_REGEX.replaceAll("(\\^|\\$)", "") + ")*$"; 23 | private Pattern selectPattern = null; 24 | 25 | private SelectSyntax() { 26 | } 27 | 28 | public static SelectSyntax getInstance() { 29 | if (instance == null) { 30 | instance = new SelectSyntax(); 31 | } 32 | return instance; 33 | } 34 | 35 | public static void main(String[] args) { 36 | System.out.println(getInstance().UNION_REGEX); 37 | } 38 | 39 | @Override 40 | public Pattern getPattern() { 41 | if (selectPattern == null) { 42 | selectPattern = Pattern.compile(SELECT_REGEX); 43 | } 44 | return selectPattern; 45 | } 46 | 47 | @Override 48 | public String getRegex() { 49 | return SELECT_REGEX; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/dbms/util/Database.java: -------------------------------------------------------------------------------- 1 | package dbms.util; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * Representation of a database. 7 | */ 8 | public class Database { 9 | /** 10 | * Name of database. 11 | */ 12 | private String name = null; 13 | 14 | /** 15 | * {@link java.util.List} list of tables inside database. 16 | */ 17 | private ArrayList tables = null; 18 | 19 | /** 20 | * Constructor of database. 21 | * @param name Name of the database. 22 | */ 23 | public Database(String name) { 24 | this.name = name; 25 | tables = new ArrayList
(); 26 | } 27 | 28 | /** 29 | * Creates a new table inside this database. 30 | * @param name Name of the table. 31 | * @return A new table that is contained inside this database. 32 | */ 33 | public Table createTable(String name) { 34 | Table table = new Table(name, this); 35 | tables.add(table); 36 | return table; 37 | } 38 | 39 | /** 40 | * Adds a new {@link Table} to this database. 41 | * @param table Table to be added. 42 | */ 43 | public void addTable(Table table) { 44 | tables.add(table); 45 | } 46 | 47 | /** 48 | * Gets table that has given name inside this database. 49 | * @param tableName Name of table to be found. 50 | * @return {@link Table} table that has given name, if no table 51 | * is found with the given name it returns null. 52 | */ 53 | public Table getTable(String tableName) { 54 | for (Table table : tables) { 55 | if (table.getName().equals(tableName)) { 56 | return table; 57 | } 58 | } 59 | return null; 60 | } 61 | 62 | /** 63 | * Gets database name. 64 | */ 65 | public String getName() { 66 | return name; 67 | } 68 | 69 | /** 70 | * Sets database name. 71 | * @param name Database name. 72 | */ 73 | public void setName(String name) { 74 | this.name = name; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/Update.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.datatypes.DBDatatype; 5 | import dbms.exception.DatabaseNotFoundException; 6 | import dbms.exception.IncorrectDataEntryException; 7 | import dbms.exception.SyntaxErrorException; 8 | import dbms.exception.TableNotFoundException; 9 | import dbms.sqlparser.sqlInterpreter.Where; 10 | 11 | import java.util.Map; 12 | 13 | public class Update implements DMLStatement { 14 | private String tableName; 15 | private Map values; 16 | private Map columns; 17 | private Where where; 18 | private int updateCount; 19 | 20 | public Update(final String tableName, final Map values, final Map columns) { 22 | this.tableName = tableName; 23 | this.values = values; 24 | this.columns = columns; 25 | } 26 | 27 | /** 28 | * @return {@link Where} the where statement. 29 | */ 30 | public Where getWhere() { 31 | return where; 32 | } 33 | 34 | /** 35 | * @param where {@link Where} sets the where statement. 36 | */ 37 | public void setWhere(Where where) { 38 | this.where = where; 39 | } 40 | 41 | /** 42 | * @return {@link Map} the new values of data. 43 | */ 44 | public Map getValues() { 45 | return values; 46 | } 47 | 48 | /** 49 | * @return {@link Map} the old columns (before updation). 50 | */ 51 | public Map getColumns() { 52 | return columns; 53 | } 54 | 55 | public String getTableName() { 56 | return tableName; 57 | } 58 | 59 | @Override 60 | public int getUpdateCount() { 61 | return updateCount; 62 | } 63 | 64 | @Override 65 | public void execute() throws DatabaseNotFoundException, 66 | TableNotFoundException, SyntaxErrorException, 67 | IncorrectDataEntryException { 68 | updateCount = BackendController.getInstance().update( 69 | tableName, values, columns, where); 70 | } 71 | } -------------------------------------------------------------------------------- /DBMS.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /src/jdbc/imp/resultSetMetaData/DBResultSetMetaDataImpl.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | 6 | package jdbc.imp.resultSetMetaData; 7 | 8 | 9 | import dbms.datatypes.DBDatatype; 10 | import dbms.datatypes.DBDate; 11 | import dbms.datatypes.DBFloat; 12 | import dbms.datatypes.DBInteger; 13 | import dbms.datatypes.DBString; 14 | import jdbc.imp.resultSet.DBResultSetImpl; 15 | 16 | import java.sql.SQLException; 17 | import java.sql.Types; 18 | 19 | /** 20 | * Result Set Metadata implamentation. 21 | */ 22 | public final class DBResultSetMetaDataImpl extends DBResultSetMetaData { 23 | /** 24 | * Reference to re{@link DBResultSetImpl} that has this metadata. 25 | */ 26 | DBResultSetImpl resultSet = null; 27 | 28 | /** 29 | * Constructs a new metadata. 30 | * @param resultSet 31 | */ 32 | public DBResultSetMetaDataImpl(DBResultSetImpl resultSet) { 33 | this.resultSet = resultSet; 34 | } 35 | 36 | @Override 37 | public int getColumnCount() throws SQLException { 38 | return this.resultSet.getRecordSet().getColumnList().size(); 39 | } 40 | 41 | @Override 42 | public String getColumnLabel(int column) throws SQLException { 43 | return getColumnName(column); 44 | } 45 | 46 | @Override 47 | public String getColumnName(int column) throws SQLException { 48 | return resultSet.getRecordSet().getColumnList().get(column - 1) 49 | .getKey(); 50 | } 51 | 52 | @Override 53 | public int getColumnType(int column) throws SQLException { 54 | Class datatype = resultSet 55 | .getRecordSet().getColumnList().get(column - 1) 56 | .getValue(); 57 | if (datatype == DBInteger.class) { 58 | return Types.INTEGER; 59 | } else if (datatype == DBString.class) { 60 | return Types.VARCHAR; 61 | } else if (datatype == DBFloat.class) { 62 | return Types.FLOAT; 63 | } else if (datatype == DBDate.class) { 64 | return Types.DATE; 65 | } 66 | throw new SQLException(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/InsertIntoTable.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.backend.BackendController; 4 | import dbms.datatypes.DBDatatype; 5 | import dbms.exception.DatabaseNotFoundException; 6 | import dbms.exception.IncorrectDataEntryException; 7 | import dbms.exception.TableNotFoundException; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Collection; 11 | import java.util.Iterator; 12 | import java.util.Map; 13 | 14 | public class InsertIntoTable implements DMLStatement { 15 | private String tableName; 16 | private Map entryMap; 17 | private int updateCount; 18 | private boolean insertWithNoColumns = false; 19 | 20 | public InsertIntoTable(final String tableName, 21 | final Map entryMap) { 22 | this.tableName = tableName; 23 | this.entryMap = entryMap; 24 | } 25 | 26 | public void insertWithNoColumns(final boolean insertWithNoColumns) { 27 | this.insertWithNoColumns = insertWithNoColumns; 28 | } 29 | 30 | public String getTableName() { 31 | return tableName; 32 | } 33 | 34 | public Map getEntryMap() { 35 | return entryMap; 36 | } 37 | 38 | @Override 39 | public int getUpdateCount() { 40 | return updateCount; 41 | } 42 | 43 | @Override 44 | public void execute() throws DatabaseNotFoundException, 45 | TableNotFoundException, IncorrectDataEntryException { 46 | if (insertWithNoColumns) { 47 | Collection entries = new ArrayList<>(); 48 | Iterator> it 49 | = entryMap.entrySet().iterator(); 50 | while (it.hasNext()) { 51 | Map.Entry pair = (Map.Entry) it.next(); 52 | entries.add((DBDatatype) pair.getValue()); 53 | } 54 | updateCount = BackendController.getInstance() 55 | .insertIntoTable(tableName, entries); 56 | return; 57 | } 58 | updateCount = BackendController.getInstance() 59 | .insertIntoTable(tableName, entryMap); 60 | } 61 | } -------------------------------------------------------------------------------- /src/dbms/backend/BackendParser.java: -------------------------------------------------------------------------------- 1 | package dbms.backend; 2 | 3 | import java.io.File; 4 | 5 | import dbms.exception.DatabaseAlreadyCreatedException; 6 | import dbms.exception.DatabaseNotFoundException; 7 | import dbms.exception.TableAlreadyCreatedException; 8 | import dbms.exception.TableNotFoundException; 9 | import dbms.util.Database; 10 | import dbms.util.Table; 11 | 12 | public abstract class BackendParser { 13 | public static void createDatabase(Database database) 14 | throws DatabaseAlreadyCreatedException { 15 | File workspace = new File(BackendController.getInstance() 16 | .getCurrentDatabaseDir()); 17 | if (!workspace.exists()) { 18 | workspace.mkdirs(); 19 | } 20 | File databaseDir = new File(workspace, 21 | database.getName()); 22 | if (!databaseDir.exists()) { 23 | databaseDir.mkdirs(); 24 | } else { 25 | throw new DatabaseAlreadyCreatedException(); 26 | } 27 | } 28 | 29 | public static void dropDatabase(Database database) 30 | throws DatabaseNotFoundException { 31 | File databaseDir = new File(BackendController.getInstance() 32 | .getCurrentDatabaseDir() 33 | + File.separator + database.getName()); 34 | if (databaseDir.exists()) { 35 | String[] files = databaseDir.list(); 36 | for (String fileName : files) { 37 | new File(databaseDir.getPath(), fileName).delete(); 38 | } 39 | databaseDir.delete(); 40 | } else { 41 | throw new DatabaseNotFoundException(); 42 | } 43 | } 44 | 45 | public abstract BackendParser getParser(); 46 | 47 | public abstract void loadTable(Table table) 48 | throws TableNotFoundException, DatabaseNotFoundException; 49 | 50 | public abstract void writeToFile(Table table) 51 | throws TableNotFoundException, DatabaseNotFoundException; 52 | 53 | public abstract void createTable(Table table) 54 | throws DatabaseNotFoundException, TableAlreadyCreatedException; 55 | 56 | public abstract void dropTable(Table table) 57 | throws DatabaseNotFoundException; 58 | } 59 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/syntax/SyntaxUtil.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.syntax; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | /** 6 | * Utility class that stores regex that is frequently used by 7 | * sql statements in {@link dbms.sqlparser.syntax} package. 8 | */ 9 | public class SyntaxUtil { 10 | /** 11 | * Regex for column name. 12 | */ 13 | public static final String COLUMN_NAME = "\\w+"; 14 | /** 15 | * Regex for tatabase name. 16 | */ 17 | public static final String DATABASE_NAME = "\\w+"; 18 | /** 19 | * Regex for table name. 20 | */ 21 | public static final String TABLE_NAME = "\\w+"; 22 | 23 | /** 24 | * Regex to support spaces in values between double quotes. 25 | * TODO: support 'afloatis 0.3' 26 | */ 27 | public static final String MULTIPLE_WORDS_DOUBLE_QUOTES = "\"" 28 | + "(?:\\s*\\w+\\s*)*\""; 29 | /** 30 | * Regex to support spaces in values between single quotes. 31 | */ 32 | public static final String MULTIPLE_WORDS_SINGLE_QUOTES = "'" 33 | + "(?:\\s*\\w+\\s*)*'"; 34 | 35 | /** 36 | * Regex for date format. 37 | */ 38 | public static final String DATE_FORMAT = "'\\d+-\\d+-\\d+'"; 39 | /** 40 | * Regex for number format. 41 | */ 42 | public static final String NUMBER_FORMAT = "-?[0-9]\\d*(?:\\.\\d+)?"; 43 | 44 | /** 45 | * Regex to support semi-colon in sql statement. 46 | * To remove support for semi-colon just replace "\s*\;\s*" with "\s*" 47 | */ 48 | // public static final String SEMI_COLON = "\\s*\\;\\s*"; 49 | public static final String SEMI_COLON = "\\s*"; 50 | 51 | /** 52 | * The supported data types. 53 | */ 54 | public static final String SUPPORTED_DATA_TYPES = "" 55 | + "(int|varchar|date|float)"; 56 | 57 | /** 58 | * Regex for the supported sql statements (rules). 59 | */ 60 | public static final String RULE_REGEX = "(?i)^\\s*(.*(union\\s+all|union)" 61 | + "|select|" + 62 | "drop|insert|update|delete|create|where|use|alter).*"; 63 | 64 | /** 65 | * Regex for rule The supported data types. 66 | */ 67 | public static final Pattern RULE_PATTERN = Pattern.compile(RULE_REGEX); 68 | } 69 | -------------------------------------------------------------------------------- /src/dbms/backend/parsers/json/test.java: -------------------------------------------------------------------------------- 1 | package dbms.backend.parsers.json; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.Map; 5 | 6 | import dbms.backend.BackendController; 7 | import dbms.backend.parsers.xml.XMLParser; 8 | import dbms.datatypes.DBDatatype; 9 | import dbms.datatypes.DBInteger; 10 | import dbms.datatypes.DBString; 11 | import dbms.datatypes.DatatypeFactory; 12 | import dbms.exception.DatabaseAlreadyCreatedException; 13 | import dbms.exception.DatabaseNotFoundException; 14 | import dbms.exception.IncorrectDataEntryException; 15 | import dbms.exception.SyntaxErrorException; 16 | import dbms.exception.TableAlreadyCreatedException; 17 | import dbms.exception.TableNotFoundException; 18 | import dbms.util.RecordSet; 19 | 20 | 21 | public class test { 22 | 23 | private final static BackendController JSONParserConc 24 | = BackendController.getInstance(); 25 | 26 | public static void main(String[] args) { 27 | try { 28 | JSONParserConc.createDatabase("mine"); 29 | } catch (DatabaseAlreadyCreatedException e) { 30 | e.printStackTrace(); 31 | } 32 | LinkedHashMap> passMap = new LinkedHashMap<>(); 33 | passMap.put("column_1", DBInteger.class); 34 | passMap.put("column_2", DBString.class); 35 | try { 36 | JSONParserConc.createTable("table11", passMap); 37 | } catch (DatabaseNotFoundException | TableAlreadyCreatedException 38 | | IncorrectDataEntryException e) { 39 | e.printStackTrace(); 40 | } 41 | Map entriesMap = new LinkedHashMap(); 42 | entriesMap.put("column_1", DatatypeFactory.convertToDataType(550)); 43 | entriesMap.put("column_2", DatatypeFactory.convertToDataType("KHalED")); 44 | try { 45 | JSONParserConc.insertIntoTable("table11", entriesMap); 46 | RecordSet rs = JSONParserConc.select("table11", null, null); 47 | } catch (DatabaseNotFoundException | TableNotFoundException 48 | | IncorrectDataEntryException e) { 49 | e.printStackTrace(); 50 | } catch (SyntaxErrorException e) { 51 | e.printStackTrace(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/dbms/sqlparser/sqlInterpreter/rules/Union.java: -------------------------------------------------------------------------------- 1 | package dbms.sqlparser.sqlInterpreter.rules; 2 | 3 | import dbms.exception.*; 4 | import dbms.ui.Formatter; 5 | import dbms.util.RecordSet; 6 | 7 | import java.util.Iterator; 8 | import java.util.List; 9 | 10 | public class Union implements DMLStatement { 11 | private List selects, final boolean removeDuplicates) { 17 | this.selects = selects; 18 | this.removeDuplicates = removeDuplicates; 19 | } 20 | 21 | @Override 22 | public int getUpdateCount() { 23 | return updateCount; 24 | } 25 | 26 | public RecordSet getRecordSet() { 27 | return recordSet; 28 | } 29 | 30 | @Override 31 | public void execute() throws DatabaseNotFoundException, 32 | TableNotFoundException, 33 | SyntaxErrorException, DataTypeNotSupportedException, 34 | TableAlreadyCreatedException, DatabaseAlreadyCreatedException, 35 | IncorrectDataEntryException { 36 | 37 | Iterator