All Downloads are FREE. Search and download functionalities are using the official Maven repository.

service.database.command.QueryDatabase Maven / Gradle / Ivy

Go to download

This is a library providing an API for accessing databases via socket connections

There is a newer version: 4.0.5
Show newest version
package service.database.command;

import com.google.gson.stream.JsonWriter;
import databaseconnector.api.DatabaseConnection;
import databaseconnector.api.sql.SQLDatabaseConnection;
import databaseconnector.api.sql.SQLSchema;
import databaseconnector.api.sql.SQLTable;
import service.database.SQLDatabaseService;
import service.database.exception.DatabaseNotInitiatedException;
import service.database.exception.DatabaseRoleNotActivatedException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class QueryDatabase extends AbstractDatabaseCommand {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        setAccessControlAllowOriginHeader(req, resp);

        try {
            checkDatabaseInitiated();
            SQLDatabaseConnection databaseConnection = getDatabaseConnection();

            Pattern tablePattern = getTablePattern(req);

            boolean countOnly = false;
            if (req.getParameterMap().containsKey("count_only")){
                String value = URLDecoder.decode(req.getParameter("count_only"), StandardCharsets.UTF_8);
                if (value.equals("true")){
                    countOnly = true;
                }
            }

            resp.setStatus(200);

            Set tableNames = new HashSet<>();
            Optional schema = SQLDatabaseService.getSchema();
            assert schema.isPresent();
            schema.get().getTables().forEach(sqlTable -> {
                Matcher tableMatcher =tablePattern.matcher(sqlTable.getName());
                if (tableMatcher.matches()){
                    tableNames.add(sqlTable.getName());
                }
            });

            JsonWriter writer = new JsonWriter(new OutputStreamWriter(resp.getOutputStream(), StandardCharsets.UTF_8));
            writer.beginArray();
            writer.flush();
            boolean finalCountOnly = countOnly;
            SQLDatabaseConnection finalDatabaseConnection = databaseConnection;
            tableNames.forEach(tableName -> {
                try {
                    writer.beginObject();

                    writer.name("table").value(tableName);
                    writer.flush();
                    writer.name("columns").beginArray();
                    writer.flush();
                    {
                        Optional table = schema.get().getTables().stream().filter(sqlTable -> sqlTable.getName().equalsIgnoreCase(tableName)).findAny();
                        assert table.isPresent();
                        table.get().getColumns().forEach(column -> {
                            try {
                                writer.beginObject();
                                writer.name("name").value(column.getName());
                                writer.name("type").value(column.getDatatype());
                                writer.endObject();
                                writer.flush();
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                        });
                    }
                    writer.endArray();
                    writer.flush();

                    final BigInteger[] entries = {BigInteger.ZERO};
                    Stream rowStream = finalDatabaseConnection.read(row -> row.getTable().getName().equalsIgnoreCase(tableName));
                    writer.name("entries").beginArray();
                    writer.flush();
                    rowStream.forEach(row -> {
                        entries[0] = entries[0].add(BigInteger.ONE);
                        if (!finalCountOnly){
                            try {
                                writer.beginObject();
                                row.getColumns().forEach(column -> {
                                    Optional value = row.get(column);
                                    if (value.isPresent()){
                                        try {
                                            writer.name(column.getName()).value(value.get().get());
                                        } catch (IOException e) {
                                            throw new RuntimeException(e);
                                        }
                                    }
                                });
                                writer.endObject();
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    });
                    writer.endArray();
                    writer.name("rows").value(entries[0].toString());
                    writer.flush();

                    writer.endObject();
                    writer.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
            writer.endArray();
            writer.flush();
        } catch (DatabaseRoleNotActivatedException exception) {
            displayError(resp, 501, exception.getMessage());
        } catch (DatabaseNotInitiatedException exception) {
            displayError(resp, 500, exception.getMessage());
        }
    }

    private Pattern getTablePattern(HttpServletRequest req){
        Pattern tablePattern = Pattern.compile(".*", Pattern.CASE_INSENSITIVE);
        if (req.getParameterMap().containsKey("table")){
            String pattern = URLDecoder.decode(req.getParameter("table"), StandardCharsets.UTF_8);
            if (pattern != null){
                tablePattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
            }
        }
        return tablePattern;
    }

    public static String getCommand(){
        return "/command/database/query";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy