<commit_before>package com.perpetumobile.bit.orm.record.field;

import java.nio.ByteBuffer;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.apache.cassandra.thrift.Column;

import com.perpetumobile.bit.orm.record.exception.FieldUnsupportedOperationException;
import com.perpetumobile.bit.util.Util;


/**
 * 
 *  @author <NAME>
 */
abstract public class Field {
	public static final String NAME_SPACE_DELIMITER = ".";
	
	protected String fieldName = null;
	private ByteBuffer bbFieldName = null;
	protected boolean isSet = false;
	protected long timestamp = 0;
	
	public Field(String fieldName) {
		this.fieldName = fieldName;
	}
	
	public Field(ByteBuffer fieldName) {
		this.bbFieldName = fieldName;
	}
	
	abstract public boolean equalValue(Field f);
	public boolean equals(Object obj) {
		Field f = (Field)obj;
		if(getFieldName().equals(f.getFieldName())) {
			return equalValue(f);
		}
		return false;
	}
	
	public String getFieldName() {
		if(fieldName == null && bbFieldName != null) {
			fieldName = Util.toString(bbFieldName, FieldConfig.CHARSET_NAME);
		}
		return fieldName;
	}
	
	public ByteBuffer getByteBufferFieldName() {
		if(bbFieldName == null && fieldName != null) {
			bbFieldName = Util.toByteBuffer(fieldName, FieldConfig.CHARSET_NAME);
		}
		return bbFieldName;
	}
	
	public long getTimestamp() {
		return timestamp;
	}
	
	void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}
	
	abstract protected int bindImpl(ResultSet rs, int index) throws SQLException;
	public int bind(ResultSet rs, int index) throws SQLException {
		isSet = true;
		timestamp = Util.currentTimeMicros();
		return bindImpl(rs, index);
	}
	
	protected void bindImpl(Column column) throws Exception {
		setByteBufferFieldValueImpl(column.value);
	}
	public void bind(Column column) throws Exception {
		isSet = true;
		timestamp = column.timestamp;
		bindImpl(column);
	}
	
	abstract public int setPreparedStatementParameter(PreparedStatement stmt, int index) throws SQLException;
	
	public boolean isSet() {
		return isSet;
	}
	
	public boolean isSet(String nameSpace) {
		boolean result = false;
		if(isSet) {
			int index = fieldName.indexOf(NAME_SPACE_DELIMITER);
			if(index == -1) {
				result = true;
			} else {
				String fieldNameSpace = fieldName.substring(0, index);
				if(fieldNameSpace.equals(nameSpace)) {
					result = true;
				}
			}
		}
		return result;
	}
	
	public boolean isAutoIncrement() {
		return false;
	}
	
	public String getSQLFieldValue() {
		return getFieldValue();
	}
	
	public String getJSONFieldValue() {
		StringBuilder buf = new StringBuilder();
		buf.append("\"");
		// TODO: perform json escaping		
		buf.append(getFieldValue());
		buf.append("\"");
		return buf.toString();
	}
	
	// ByteBufferField interface
	abstract public ByteBuffer getByteBufferFieldValue(); 
	abstract void setByteBufferFieldValueImpl(ByteBuffer fieldValue); 
	public void setByteBufferFieldValue(ByteBuffer fieldValue) {
		if(fieldValue != null) {
			setByteBufferFieldValueImpl(fieldValue);
			isSet = true;
			timestamp = Util.currentTimeMicros();
		}
	}
	
	// StringField interface
	abstract public String getFieldValue();
	abstract protected void setFieldValueImpl(String fieldValue);
	public void setFieldValue(String fieldValue) {
		if(fieldValue != null) {
			setFieldValueImpl(fieldValue);
			isSet = true;
			timestamp = Util.currentTimeMicros();
		}
	}
	
	// IntField interface
	public int getIntFieldValue() {<commit_msg>handle null and escape string in getJSONFieldValue method<commit_after>package com.perpetumobile.bit.orm.record.field;

import java.nio.ByteBuffer;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.apache.cassandra.thrift.Column;
import org.json.simple.JSONObject;

import com.perpetumobile.bit.orm.record.exception.FieldUnsupportedOperationException;
import com.perpetumobile.bit.util.Util;


/**
 * 
 *  @author <NAME>
 */
abstract public class Field {
	public static final String NAME_SPACE_DELIMITER = ".";
	
	protected String fieldName = null;
	private ByteBuffer bbFieldName = null;
	protected boolean isSet = false;
	protected long timestamp = 0;
	
	public Field(String fieldName) {
		this.fieldName = fieldName;
	}
	
	public Field(ByteBuffer fieldName) {
		this.bbFieldName = fieldName;
	}
	
	abstract public boolean equalValue(Field f);
	public boolean equals(Object obj) {
		Field f = (Field)obj;
		if(getFieldName().equals(f.getFieldName())) {
			return equalValue(f);
		}
		return false;
	}
	
	public String getFieldName() {
		if(fieldName == null && bbFieldName != null) {
			fieldName = Util.toString(bbFieldName, FieldConfig.CHARSET_NAME);
		}
		return fieldName;
	}
	
	public ByteBuffer getByteBufferFieldName() {
		if(bbFieldName == null && fieldName != null) {
			bbFieldName = Util.toByteBuffer(fieldName, FieldConfig.CHARSET_NAME);
		}
		return bbFieldName;
	}
	
	public long getTimestamp() {
		return timestamp;
	}
	
	void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}
	
	abstract protected int bindImpl(ResultSet rs, int index) throws SQLException;
	public int bind(ResultSet rs, int index) throws SQLException {
		isSet = true;
		timestamp = Util.currentTimeMicros();
		return bindImpl(rs, index);
	}
	
	protected void bindImpl(Column column) throws Exception {
		setByteBufferFieldValueImpl(column.value);
	}
	public void bind(Column column) throws Exception {
		isSet = true;
		timestamp = column.timestamp;
		bindImpl(column);
	}
	
	abstract public int setPreparedStatementParameter(PreparedStatement stmt, int index) throws SQLException;
	
	public boolean isSet() {
		return isSet;
	}
	
	public boolean isSet(String nameSpace) {
		boolean result = false;
		if(isSet) {
			int index = fieldName.indexOf(NAME_SPACE_DELIMITER);
			if(index == -1) {
				result = true;
			} else {
				String fieldNameSpace = fieldName.substring(0, index);
				if(fieldNameSpace.equals(nameSpace)) {
					result = true;
				}
			}
		}
		return result;
	}
	
	public boolean isAutoIncrement() {
		return false;
	}
	
	public String getSQLFieldValue() {
		return getFieldValue();
	}
	
	public String getJSONFieldValue() {
		StringBuilder buf = new StringBuilder();
		String value = getFieldValue();
		if(value != null) {
			buf.append("\"");
			buf.append(JSONObject.escape(getFieldValue()));
			buf.append("\"");
		} else {
			buf.append("null");
		}	
		return buf.toString();
	}
	
	// ByteBufferField interface
	abstract public ByteBuffer getByteBufferFieldValue(); 
	abstract void setByteBufferFieldValueImpl(ByteBuffer fieldValue); 
	public void setByteBufferFieldValue(ByteBuffer fieldValue) {
		if(fieldValue != null) {
			setByteBufferFieldValueImpl(fieldValue);
			isSet = true;
			timestamp = Util.currentTimeMicros();
		}
	}
	
	// StringField interface
	abstract public String getFieldValue();
	abstract protected void setFieldValueImpl(String fieldValue);
	public void setFieldValue(String fieldValue) {
		if(fieldValue != null) {
			setFieldValueImpl(fieldValue);
			isSet = true;
			timestamp = Util.currentTimeMicros();
		}
	}
	
	// IntField interface
	public int getIntFieldValue() {
{'fraction_non_alphanumeric': 0.10330064908161857, 'fraction_numerical': 0.0008286148322054964, 'mean_word_length': 4.364444444444445, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7387162', 'n_tokens_mistral': 2457, 'n_tokens_neox': 2200, 'n_words': 534}