<commit_before>package org.oskari.statistics.user;

import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * MyBatis Mapper for the SQL table oskari_user_indicator for thematic map user indicators.
 * The database currently supports multiple years/regionsets per indicator, but these queries do not so the API
 * will change a bit in the future.
 */
public interface UserIndicatorMapper {

    // TODO: support multiple year/regionsets
    @Select("SELECT" +
            "    i.id," +
            "    i.user_id," +
            "    i.title," +
            "    i.source," +
            "    i.description," +
            "    i.published," +
            "    d.year," +
            "    d.regionset_id " +
            "FROM" +
            "    oskari_user_indicator i" +
            "    LEFT JOIN oskari_user_indicator_data d ON i.id = d.indicator_id " +
            "WHERE" +
            "    i.user_id =  #{userId} " +
            "ORDER BY i.id, d.regionset_id, d.year")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "regionsetId", column = "regionset_id")
    })
    @ResultType(UserIndicatorDataRow.class)
    List<UserIndicatorDataRow> findByUser(long userId);

    // TODO: support multiple year/regionsets
    @Select("SELECT" +
            "    i.id," +
            "    i.user_id," +
            "    i.title," +
            "    i.source," +
            "    i.description," +
            "    i.published," +
            "    d.year," +
            "    d.regionset_id " +
            "FROM" +
            "    oskari_user_indicator i" +
            "    LEFT JOIN oskari_user_indicator_data d ON i.id = d.indicator_id " +
            "WHERE" +
            "    i.id = #{id} " +
            "ORDER BY i.id, d.regionset_id, d.year")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "regionsetId", column = "regionset_id")
    })
    @ResultType(UserIndicatorDataRow.class)
    List<UserIndicatorDataRow> findById(long id);


    @Select("SELECT data FROM oskari_user_indicator_data" +
            " WHERE indicator_id = #{indicator} AND regionset_id = #{regionset} AND year = #{year}")
    String getData(@Param("indicator") long indicator, @Param("regionset") long regionset, @Param("year") int year);


    @Delete("delete from oskari_user_indicator where id = #{id} AND user_id = #{userId}")
    int delete(@Param("id") long id, @Param("userId") long userId);

    @Delete("delete from oskari_user_indicator where user_id = #{userId}")
    void deleteByUser(long userId);

/*
    <statement id="insert" parameterMap="UserIndicatorInsertParameter" resultClass="int">
    insert into oskari_user_indicator (
            user_id,
            title,
            source,
            description,
            published)
    values (?, ?, ?, ?, ?)
    <!-- TODO: data should be inserted into oskari_user_indicator_data table
    Should be upgraded to MyBatis!
            -->
    </statement>


    <update id="update" parameterMap="UserIndicatorUpdateParameter">
    update oskari_user_indicator set
    user_id = ?,
    title = ?,
    source = ?,
    description = ?,
    published = ?
    where id = ?
    <!-- TODO: data should be inserted into oskari_user_indicator_data table
    Should be upgraded to MyBatis!
            -->
            */
}<commit_msg>Add insert/update/delete sqls to UserIndicator MyBatis mapper<commit_after>package org.oskari.statistics.user;

import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * MyBatis Mapper for the SQL table oskari_user_indicator for thematic map user indicators.
 * The database currently supports multiple years/regionsets per indicator, but these queries do not so the API
 * will change a bit in the future.
 */
public interface UserIndicatorMapper {

    // TODO: support multiple year/regionsets
    @Select("SELECT" +
            "    i.id," +
            "    i.user_id," +
            "    i.title," +
            "    i.source," +
            "    i.description," +
            "    i.published," +
            "    d.year," +
            "    d.regionset_id " +
            "FROM" +
            "    oskari_user_indicator i" +
            "    LEFT JOIN oskari_user_indicator_data d ON i.id = d.indicator_id " +
            "WHERE" +
            "    i.user_id =  #{userId} " +
            "ORDER BY i.id, d.regionset_id, d.year")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "regionsetId", column = "regionset_id")
    })
    @ResultType(UserIndicatorDataRow.class)
    List<UserIndicatorDataRow> findByUser(long userId);

    // TODO: support multiple year/regionsets
    @Select("SELECT" +
            "    i.id," +
            "    i.user_id," +
            "    i.title," +
            "    i.source," +
            "    i.description," +
            "    i.published," +
            "    d.year," +
            "    d.regionset_id " +
            "FROM" +
            "    oskari_user_indicator i" +
            "    LEFT JOIN oskari_user_indicator_data d ON i.id = d.indicator_id " +
            "WHERE" +
            "    i.id = #{id} " +
            "ORDER BY i.id, d.regionset_id, d.year")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "regionsetId", column = "regionset_id")
    })
    @ResultType(UserIndicatorDataRow.class)
    List<UserIndicatorDataRow> findById(long id);


    @Select("SELECT data FROM oskari_user_indicator_data" +
            " WHERE indicator_id = #{indicator} AND regionset_id = #{regionset} AND year = #{year}")
    String getData(@Param("indicator") long indicator, @Param("regionset") long regionset, @Param("year") int year);


    @Delete("delete from oskari_user_indicator where id = #{id} AND user_id = #{userId}")
    int delete(@Param("id") long id, @Param("userId") long userId);

    @Delete("delete from oskari_user_indicator where user_id = #{userId}")
    void deleteByUser(long userId);

    @Insert("INSERT INTO oskari_user_indicator_data"
            + " (indicator_id, regionset_id, year, data)"
            + " VALUES (#{indicator}, #{regionset}, #{year}, #{data})")
    void addData(@Param("indicator") long indicator, @Param("regionset") long regionset, @Param("year") int year, @Param("data") String data);

    @Delete("DELETE FROM oskari_user_indicator_data" +
            " WHERE indicator_id = #{indicator} AND regionset_id = #{regionset} AND year = #{year}")
    void deleteData(@Param("indicator") long indicator, @Param("regionset") long regionset, @Param("year") int year);

    @Insert("INSERT INTO oskari_user_indicator"
            + " (user_id, title, source, description, published)"
            + " VALUES (#{userId}, #{title}, #{source}, #{description}, #{published})")
    void addIndicator(UserIndicatorDataRow row);

    @Update("update oskari_user_indicator set" +
            "    title = #{title}," +
            "    source = #{source}," +
            "    description = #{description}," +
            "    published = #{published}" +
            "    where id = #{id} AND user_id = #{userId}")
    void updateIndicator(UserIndicatorDataRow row);
}
{'fraction_non_alphanumeric': 0.12953296703296704, 'fraction_numerical': 0.0, 'mean_word_length': 2.2018469656992083, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3406974', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2204, 'n_words': 503}