<commit_before>package org.voovan.db.recorder;

import org.voovan.db.JdbcOperate;
import org.voovan.db.recorder.annotation.NotInsert;
import org.voovan.db.recorder.annotation.NotUpdate;
import org.voovan.tools.log.Logger;
import org.voovan.tools.reflect.annotation.NotSerialization;

import javax.sql.DataSource;
import java.util.List;


/**
 * 类文字命名
 *
 * @author: helyho
 * walletbridge Framework.
 * WebSite: https://github.com/helyho/walletbridge
 * Licence: Apache v2 License
 */
public class Dao<T> {
    public Class<T> clazz;
    @NotUpdate
    @NotInsert
    @NotSerialization
    public transient javax.sql.DataSource dataSource;

    public Class<T> getClazz() {
        return clazz;
    }

    public void setClazz(Class<T> clazz) {
        this.clazz = clazz;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public boolean insert() {
        Recorder recorder = new Recorder(new JdbcOperate(dataSource));
        return recorder.insert((T)this) == 1;
    }

    public boolean update(String ... dataFields) {
        Recorder recorder = new Recorder(new JdbcOperate(dataSource));
        Query query = Query.newInstance().data(dataFields);
        return recorder.update((T)this, query) == 1;
    }
<commit_msg>add: Dao 新增 beginUpdate 和 endUpdate 方法,自动判断需要更新的属性<commit_after>package org.voovan.db.recorder;

import org.voovan.db.JdbcOperate;
import org.voovan.db.recorder.annotation.NotInsert;
import org.voovan.db.recorder.annotation.NotUpdate;
import org.voovan.db.recorder.exception.RecorderException;
import org.voovan.tools.log.Logger;
import org.voovan.tools.reflect.TReflect;
import org.voovan.tools.reflect.annotation.NotSerialization;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 类文字命名
 *
 * @author: helyho
 * walletbridge Framework.
 * WebSite: https://github.com/helyho/walletbridge
 * Licence: Apache v2 License
 */
public class Dao<T> {
    public static String[] emptyStringArray = new String[0];

    @NotUpdate
    @NotInsert
    @NotSerialization
    public transient DataSource dataSource;

    @NotUpdate
    @NotInsert
    @NotSerialization
    public transient Map<String, Object> originMap;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public boolean insert() {
        Recorder recorder = new Recorder(new JdbcOperate(dataSource));
        return recorder.insert((T)this) == 1;
    }

    public void beginUpdate() {
        try {
            originMap = TReflect.getMapfromObject(this);
        } catch (ReflectiveOperationException e) {
            throw new RecorderException("beginUpdate failed", e);
        }
    }

    public void endUpdate() {
        try {
            Map<String, Object> currentMap = TReflect.getMapfromObject(this);

            List<String> modifyField = new ArrayList<String>();
            for(Map.Entry<String, Object> entry : currentMap.entrySet()) {
                Object originFieldValue = originMap.get(entry.getKey());
                Object currentFieldValue = entry.getValue();

                if(originFieldValue==null && currentFieldValue==null) {
                    continue;
                } else if(originFieldValue!=null && !originFieldValue.equals(currentFieldValue)) {
                    modifyField.add(entry.getKey());
                } else if(currentFieldValue!=null && !currentFieldValue.equals(originFieldValue)) {
                    modifyField.add(entry.getKey());
                }
            }

            update(modifyField.toArray(emptyStringArray));
            originMap = null;
        } catch (ReflectiveOperationException e) {
            throw new RecorderException("endUpdate failed", e);
        }
    }

    public boolean update(String ... dataFields) {
        Recorder recorder = new Recorder(new JdbcOperate(dataSource));
        Query query = Query.newInstance().data(dataFields);
        return recorder.update((T)this, query) == 1;
    }

{'fraction_non_alphanumeric': 0.10626638074815345, 'fraction_numerical': 0.0016678579938050988, 'mean_word_length': 2.865561694290976, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '1457529', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1215, 'n_words': 250}