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

net.java.ao.sql.SqlUtils Maven / Gradle / Ivy

Go to download

This is the full Active Objects library, if you don't know which one to use, you probably want this one.

There is a newer version: 6.1.1
Show newest version
package net.java.ao.sql;

import com.google.common.base.Function;
import net.java.ao.Common;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkState;

public final class SqlUtils
{
    public static final Pattern WHERE_CLAUSE = Pattern.compile("(\\w+)(?=\\s*((=|!=|>|<|<>|<=|>=|(? processor)
    {
        final Matcher matcher = WHERE_CLAUSE.matcher(where);
        final StringBuffer sb = new StringBuffer();
        while (matcher.find())
        {
            matcher.appendReplacement(sb, processor.apply(matcher.group()));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String processOnClause(String on, Function processor)
    {
        final Matcher matcher = ON_CLAUSE.matcher(on);
        checkState(matcher.matches());
        final StringBuilder sb = new StringBuilder();
        if (matcher.group(1) != null)
        {
            sb.append(matcher.group(1)).append(".");
            if (matcher.group(2) != null)
            {
                sb.append(processor.apply(matcher.group(2))).append(".");
            }
        }
        else if (matcher.group(2) != null)
        {
            sb.append(matcher.group(2)).append(".");
        }
        sb.append(processor.apply(matcher.group(3)));
        sb.append(matcher.group(4));
        if (matcher.group(5) != null)
        {
            sb.append(matcher.group(5)).append(".");
            if (matcher.group(6) != null)
            {
                sb.append(processor.apply(matcher.group(6))).append(".");
            }
        }
        else if (matcher.group(6) != null)
        {
            sb.append(matcher.group(6)).append(".");
        }
        sb.append(processor.apply(matcher.group(7)));
        return sb.toString();
    }
    
    public static String processGroupByClause(String groupBy, Function processor)
    {
        final Matcher matcher = GROUP_BY_CLAUSE.matcher(groupBy);
        final StringBuffer sb = new StringBuffer();
        while (matcher.find())
        {
            final String group = matcher.group();
            if (group.contains("."))
            {
                final int dotIndexAlmost = group.lastIndexOf(".") + 1;
                matcher.appendReplacement(sb, group.substring(0, dotIndexAlmost) + processor.apply(group.substring(dotIndexAlmost, group.length())));
            }
            else
            {
                matcher.appendReplacement(sb, processor.apply(group));
            }
        }
        matcher.appendTail(sb);
        return sb.toString();

    }

    public static void closeQuietly(Connection connection)
    {
        Common.closeQuietly(connection);
    }

    public static void closeQuietly(Statement statement)
    {
        Common.closeQuietly(statement);
    }

    public static void closeQuietly(ResultSet resultSet)
    {
        Common.closeQuietly(resultSet);
    }

    public static void closeQuietly(Statement st, Connection c)
    {
        closeQuietly(st);
        closeQuietly(c);
    }

    public static void closeQuietly(ResultSet rs, Statement st, Connection c)
    {
        closeQuietly(rs);
        closeQuietly(st, c);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy