package org.nutz.dao.impl.entity.macro;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.nutz.dao.entity.MappingField;
import org.nutz.dao.impl.jdbc.NutPojo;
import org.nutz.dao.sql.Pojo;
import org.nutz.dao.sql.SqlType;
import org.nutz.el.El;
import org.nutz.lang.util.Context;

public class ElFieldMacro extends NutPojo {

    /**
	 * 
	 */
	private static final long serialVersionUID = 5393288165729677800L;

	private El bin;

    private MappingField entityField;

    public SqlType getSqlType() {
        return SqlType.RUN;
    }

    public ElFieldMacro(MappingField field, String str) {
        this.entityField = field;
        this.bin = new El(str);
    }

    private ElFieldMacro() {}

    public void onAfter(Connection conn, ResultSet rs, Statement stmt) throws SQLException {
        Context context = entityField.getEntity()
                                     .wrapAsContext(getOperatingObject());
        context.set("field", entityField.getColumnName());
        context.set("view", entityField.getEntity());
        Object value = bin.eval(context);
        entityField.setValue(getOperatingObject(), value);
    }

    @Override
    public Pojo duplicate() {
        ElFieldMacro re = new ElFieldMacro();
        re.bin = bin;
        re.entityField = entityField;
        return re;
    }

    @Override
    public String forPrint() {
        return "// NOT SQL // ElFieldMacro=" + this.bin;
    }
}
