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

org.xblackcat.sjpu.storage.impl.SqlAnnotatedBuilder Maven / Gradle / Ivy

Go to download

Service for generating DB access logic in simple way via interfaces and annotations

There is a newer version: 2.0
Show newest version
package org.xblackcat.sjpu.storage.impl;

import org.xblackcat.sjpu.storage.ann.QueryType;
import org.xblackcat.sjpu.storage.ann.Sql;
import org.xblackcat.sjpu.storage.consumer.IRowSetConsumer;
import org.xblackcat.sjpu.storage.converter.builder.ConverterInfo;
import org.xblackcat.sjpu.storage.typemap.TypeMapper;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

/**
 * 11.03.13 13:18
 *
 * @author xBlackCat
 */
class SqlAnnotatedBuilder extends ASelectAnnotatedBuilder {
    public SqlAnnotatedBuilder(TypeMapper typeMapper, Map, Class> rowSetConsumers) {
        super(Sql.class, typeMapper, rowSetConsumers);
    }

    @Override
    protected QueryType getQueryType(Method m) {
        final String sql = getAnnotation(m).value();

        final QueryType type;
        {
            final Matcher matcher = AHBuilderUtils.FIRST_WORD_SQL.matcher(sql);
            if (matcher.find()) {
                final String word = matcher.group(1);
                if ("select".equalsIgnoreCase(word)) {
                    type = QueryType.Select;
                } else if ("insert".equalsIgnoreCase(word)) {
                    type = QueryType.Insert;
                } else if ("update".equalsIgnoreCase(word)) {
                    type = QueryType.Update;
                } else {
                    type = QueryType.Other;
                }
            } else {
                type = QueryType.Other;
            }
        }
        return type;
    }

    @Override
    protected List appendDefineSql(StringBuilder body, ConverterInfo info, Method m) {
        final String sql = getAnnotation(m).value();
        return SqlStringUtils.appendSqlWithParts(body, sql, info.getSqlParts());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy