package org.kairosdb.datastore.h2.orm;
import org.agileclick.genorm.runtime.*;
import java.util.ArrayList;
import java.util.List;
/**
This class has been automatically generated by GenORMous. This file
should not be modified.
*/
public class MetricTag_base extends GenOrmRecord
{
protected static final Logger s_logger = LoggerFactory.getLogger(MetricTag.class.getName());
public static final String COL_METRIC_ID = "metric_id";
public static final String COL_TAG_NAME = "tag_name";
public static final String COL_TAG_VALUE = "tag_value";
//Change this value to true to turn on warning messages
private static final boolean WARNINGS = false;
private static final String SELECT = "SELECT this.\"metric_id\", this.\"tag_name\", this.\"tag_value\" ";
private static final String FROM = "FROM metric_tag this ";
private static final String WHERE = "WHERE ";
private static final String KEY_WHERE = "WHERE \"metric_id\" = ? AND \"tag_name\" = ? AND \"tag_value\" = ?";
public static final String TABLE_NAME = "metric_tag";
public static final int NUMBER_OF_COLUMNS = 3;
private static final String s_fieldEscapeString = "\"";
public static final GenOrmFieldMeta METRIC_ID_FIELD_META = new GenOrmFieldMeta("metric_id", "string", 0, true, true);
public static final GenOrmFieldMeta TAG_NAME_FIELD_META = new GenOrmFieldMeta("tag_name", "string", 1, true, true);
public static final GenOrmFieldMeta TAG_VALUE_FIELD_META = new GenOrmFieldMeta("tag_value", "string", 2, true, true);
//===========================================================================
public static MetricTagFactoryImpl factory = new MetricTagFactoryImpl();
public static interface MetricTagFactory extends GenOrmRecordFactory
{
public boolean delete(String metricId, String tagName, String tagValue);
public MetricTag find(String metricId, String tagName, String tagValue);
public MetricTag findOrCreate(String metricId, String tagName, String tagValue);
/**
*/
public ResultSet getByTag(String tagName, String tagValue);/**
*/
public ResultSet getByMetric(String metricId);
}
public static class MetricTagFactoryImpl //Inherit interfaces
implements MetricTagFactory
{
public static final String CREATE_SQL = "CREATE CACHED TABLE metric_tag (\n \"metric_id\" VARCHAR NOT NULL,\n \"tag_name\" VARCHAR NOT NULL,\n \"tag_value\" VARCHAR NOT NULL,\n PRIMARY KEY (\"metric_id\", \"tag_name\", \"tag_value\"),\n CONSTRAINT metric_tag_metric_id_fkey FOREIGN KEY (\"metric_id\")\n REFERENCES metric (\"id\") ON DELETE CASCADE,\n CONSTRAINT metric_tag_tag_name_fkey FOREIGN KEY (\"tag_name\", \"tag_value\")\n REFERENCES tag (\"name\", \"value\") \n )";
private ArrayList<GenOrmFieldMeta> m_fieldMeta;
private ArrayList<GenOrmConstraint> m_foreignKeyConstraints;
protected MetricTagFactoryImpl()
{
m_fieldMeta = new ArrayList<GenOrmFieldMeta>();
m_fieldMeta.add(METRIC_ID_FIELD_META);
m_fieldMeta.add(TAG_NAME_FIELD_META);
m_fieldMeta.add(TAG_VALUE_FIELD_META);
m_foreignKeyConstraints = new ArrayList<GenOrmConstraint>();
m_foreignKeyConstraints.add(new GenOrmConstraint("metric", "metric_tag_metric_id_fkey", "CONSTRAINT metric_tag_metric_id_fkey FOREIGN KEY (\"metric_id\")\n REFERENCES metric (\"id\") ON DELETE CASCADE"));
m_foreignKeyConstraints.add(new GenOrmConstraint("tag", "metric_tag_tag_name_fkey", "CONSTRAINT metric_tag_tag_name_fkey FOREIGN KEY (\"tag_name\", \"tag_value\")\n REFERENCES tag (\"name\", \"value\")"));
}
protected MetricTag newMetricTag(java.sql.ResultSet rs)
{
MetricTag rec = new MetricTag();
((MetricTag_base)rec).initialize(rs);
return ((MetricTag)GenOrmDataSource.getGenOrmConnection().getUniqueRecord(rec));
}
//---------------------------------------------------------------------------
/**
Returns a list of the feild meta for the class that this is a factory of
*/
public List<GenOrmFieldMeta> getFields()
{
return (m_fieldMeta);
}
//---------------------------------------------------------------------------
/**
Returns a list of foreign key constraints
*/
public List<GenOrmConstraint> getForeignKeyConstraints()
{
return (m_foreignKeyConstraints);
}
//---------------------------------------------------------------------------
/**
Returns the SQL create statement for this table
*/
public String getCreateStatement()
{
return (CREATE_SQL);
}
//---------------------------------------------------------------------------
/**
Creates a new entry with the specified primary keys.
*/
public MetricTag create(String metricId, String tagName, String tagValue)
{
MetricTag rec = new MetricTag();
rec.m_isNewRecord = true;
((MetricTag_base)rec).setMetricId(metricId);
((MetricTag_base)rec).setTagName(tagName);
((MetricTag_base)rec).setTagValue(tagValue);
return ((MetricTag)GenOrmDataSource.getGenOrmConnection().getUniqueRecord(rec));
}
//---------------------------------------------------------------------------
/**
Creates a new entry that is empty
*/
public MetricTag createRecord()
{
MetricTag rec = new MetricTag();
rec.m_isNewRecord = true;
return ((MetricTag)GenOrmDataSource.getGenOrmConnection().getUniqueRecord(rec));
}
//---------------------------------------------------------------------------
/**
If the table has a primary key that has a key generator this method will
return a new table entry with a generated primary key.
@return MetricTag with generated primary key
*/
public MetricTag createWithGeneratedKey()
{
throw new UnsupportedOperationException("MetricTag does not support a generated primary key");
}
//---------------------------------------------------------------------------
/**
A generic api for finding a record.
@param keys This must match the primary key for this record. If the
record has multiple primary keys this parameter must be of type Object[]
where each element is the corresponding key.
@return MetricTag or null if no record is found
*/
public MetricTag findRecord(Object keys)
{
Object[] kArr = (Object[])keys;
return (find((String)kArr[0], (String)kArr[1], (String)kArr[2]));
}
//---------------------------------------------------------------------------
/**
Deletes the record with the specified primary keys.
The point of this api is to prevent a hit on the db to see if the record
is there. This call will add a record to the next transaction that is
marked for delete.
@return Returns true if the record was previous created and existed
either in the transaction cache or the db.
*/
public boolean delete(String metricId, String tagName, String tagValue)
{
boolean ret = false;
MetricTag rec = new MetricTag();
((MetricTag_base)rec).initialize(metricId, tagName, tagValue);
GenOrmConnection con = GenOrmDataSource.getGenOrmConnection();
MetricTag cachedRec = (MetricTag)con.getCachedRecord(rec.getRecordKey());
if (cachedRec != null)
{
ret = true;
cachedRec.delete();
}
else
{
rec = (MetricTag)con.getUniqueRecord(rec); //This adds the record to the cache
rec.delete();
ret = rec.flush();
rec.setIgnored(true); //So the system does not try to delete it again at commmit
}
return (ret);
}
//---------------------------------------------------------------------------
/**
Find the record with the specified primary keys
@return MetricTag or null if no record is found
*/
public MetricTag find(String metricId, String tagName, String tagValue)
{
MetricTag rec = new MetricTag();
//Create temp object and look in cache for it
((MetricTag_base)rec).initialize(metricId, tagName, tagValue);
rec = (MetricTag)GenOrmDataSource.getGenOrmConnection().getCachedRecord(rec.getRecordKey());
java.sql.PreparedStatement genorm_statement = null;
java.sql.ResultSet genorm_rs = null;
if (rec == null)
{
try
{
//No cached object so look in db
genorm_statement = GenOrmDataSource.prepareStatement(SELECT+FROM+KEY_WHERE);
genorm_statement.setString(1, metricId);
genorm_statement.setString(2, tagName);
genorm_statement.setString(3, tagValue);
s_logger.debug(genorm_statement.toString());
genorm_rs = genorm_statement.executeQuery();
if (genorm_rs.next())
rec = newMetricTag(genorm_rs);
}
catch (java.sql.SQLException sqle)
{
throw new GenOrmException(sqle);
}
finally
{
try
{
if (genorm_rs != null)
genorm_rs.close();
if (genorm_statement != null)
genorm_statement.close();
}
catch (java.sql.SQLException sqle2)
{
throw new GenOrmException(sqle2);
}
}
}
return (rec);
}
//---------------------------------------------------------------------------
/**
This is the same as find except if the record returned is null a new one
is created with the specified primary keys
@return A new or existing record.
*/
public MetricTag findOrCreate(String metricId, String tagName, String tagValue)
{
MetricTag rec = find(metricId, tagName, tagValue);
if (rec == null)
rec = create(metricId, tagName, tagValue);
return (rec);
}
//---------------------------------------------------------------------------
/**
Convenience method for selecting records. Ideally this should not be use,
instead a custom query for this table should be used.
@param where sql where statement.
*/
public ResultSet select(String where)
{
return (select(where, null));
}
//---------------------------------------------------------------------------
/**
Convenience method for selecting records. Ideally this should not be use,
instead a custom query for this table should be used.
@param where sql where statement.
@param orderBy sql order by statement
*/
public ResultSet select(String where, String orderBy)
{
ResultSet rs = null;
java.sql.Statement stmnt = null;
try
{
stmnt = GenOrmDataSource.createStatement();
StringBuilder sb = new StringBuilder();
sb.append(SELECT);
sb.append(FROM);
if (where != null)
{
sb.append(WHERE);
sb.append(where);
}
if (orderBy != null)
{
sb.append(" ");
sb.append(orderBy);
}
String query = sb.toString();
rs = new SQLResultSet(stmnt.executeQuery(query), query, stmnt);
}
catch (java.sql.SQLException sqle)
{
try
{
if (stmnt != null)
stmnt.close();
}
catch (java.sql.SQLException sqle2) { }
throw new GenOrmException(sqle);
}
return (rs);
}
//---------------------------------------------------------------------------
/**
*/
public ResultSet getByTag(String tagName, String tagValue)
{
String query = SELECT+"FROM metric_tag this WHERE this.\"tag_name\" = ? AND this.\"tag_value\" = ?";
java.sql.PreparedStatement genorm_statement = null;
try
{
genorm_statement = GenOrmDataSource.prepareStatement(query);
genorm_statement.setString(1, tagName);genorm_statement.setString(2, tagValue);
s_logger.debug(genorm_statement.toString());
ResultSet rs = new SQLResultSet(genorm_statement.executeQuery(), query, genorm_statement);
return (rs);
}
catch (java.sql.SQLException sqle)
{
try
{
if (genorm_statement != null)
genorm_statement.close();
}
catch (java.sql.SQLException sqle2) { }
if (s_logger.isDebug())
sqle.printStackTrace();
throw new GenOrmException(sqle);
}
}
//---------------------------------------------------------------------------
/**
*/
public ResultSet getByMetric(String metricId)
{
String query = SELECT+"FROM metric_tag this WHERE this.\"metric_id\" = ?";
java.sql.PreparedStatement genorm_statement = null;
try
{
genorm_statement = GenOrmDataSource.prepareStatement(query);
genorm_statement.setString(1, metricId);
s_logger.debug(genorm_statement.toString());
ResultSet rs = new SQLResultSet(genorm_statement.executeQuery(), query, genorm_statement);
return (rs);
}
catch (java.sql.SQLException sqle)
{
try
{
if (genorm_statement != null)
genorm_statement.close();
}
catch (java.sql.SQLException sqle2) { }
if (s_logger.isDebug())
sqle.printStackTrace();
throw new GenOrmException(sqle);
}
}
//---------------------------------------------------------------------------
/**
Calls all query methods with test parameters.
*/
public void testQueryMethods()
{
ResultSet rs;
}
}
//===========================================================================
public static interface ResultSet extends GenOrmResultSet
{
public ArrayList<MetricTag> getArrayList(int maxRows);
public ArrayList<MetricTag> getArrayList();
public MetricTag getRecord();
public MetricTag getOnlyRecord();
}
//===========================================================================
private static class SQLResultSet
implements ResultSet
{
private java.sql.ResultSet m_resultSet;
private java.sql.Statement m_statement;
private String m_query;
private boolean m_onFirstResult;
//------------------------------------------------------------------------
protected SQLResultSet(java.sql.ResultSet resultSet, String query, java.sql.Statement statement)
{
m_resultSet = resultSet;
m_statement = statement;
m_query = query;
m_onFirstResult = false;
}
//------------------------------------------------------------------------
/**
Closes any underlying java.sql.Result set and java.sql.Statement
that was used to create this results set.
*/
public void close()
{
try
{
m_resultSet.close();
m_statement.close();
}
catch (java.sql.SQLException sqle)
{
throw new GenOrmException(sqle);
}
}
//------------------------------------------------------------------------
/**
Returns the reults as an ArrayList of Record objects.
The Result set is closed within this call
@param maxRows if the result set contains more than this param
then an exception is thrown
*/
public ArrayList<MetricTag> getArrayList(int maxRows)
{
ArrayList<MetricTag> results = new ArrayList<MetricTag>();
int count = 0;
try
{
if (m_onFirstResult)
{
count ++;
results.add(factory.newMetricTag(m_resultSet));
}
while (m_resultSet.next() && (count < maxRows))
{
count ++;
results.add(factory.newMetricTag(m_resultSet));
}
if (m_resultSet.next())
throw new GenOrmException("Bound of "+maxRows+" is too small for query ["+m_query+"]");
}
catch (java.sql.SQLException sqle)
{
sqle.printStackTrace();
throw new GenOrmException(sqle);
}
close();
return (results);
}
//------------------------------------------------------------------------
/**
Returns the reults as an ArrayList of Record objects.
The Result set is closed within this call
*/
public ArrayList<MetricTag> getArrayList()
{
ArrayList<MetricTag> results = new ArrayList<MetricTag>();
try
{
if (m_onFirstResult)
results.add(factory.newMetricTag(m_resultSet));
while (m_resultSet.next())
results.add(factory.newMetricTag(m_resultSet));
}
catch (java.sql.SQLException sqle)
{
sqle.printStackTrace();
throw new GenOrmException(sqle);
}
close();
return (results);
}
//------------------------------------------------------------------------
/**
Returns the underlying java.sql.ResultSet object
*/
public java.sql.ResultSet getResultSet()
{
return (m_resultSet);
}
//------------------------------------------------------------------------
/**
Returns the current record in the result set
*/
public MetricTag getRecord()
{
return (factory.newMetricTag(m_resultSet));
}
//------------------------------------------------------------------------
/**
This call expects only one record in the result set. If multiple records
are found an excpetion is thrown.
The ResultSet object is automatically closed by this call.
*/
public MetricTag getOnlyRecord()
{
MetricTag ret = null;
try
{
if (m_resultSet.next())
ret = factory.newMetricTag(m_resultSet);
if (m_resultSet.next())
throw new GenOrmException("Multiple rows returned in call from MetricTag.getOnlyRecord");
}
catch (java.sql.SQLException sqle)
{
throw new GenOrmException(sqle);
}
close();
return (ret);
}
//------------------------------------------------------------------------
/**
Returns true if there is another record in the result set.
*/
public boolean next()
{
boolean ret = false;
m_onFirstResult = true;
try
{
ret = m_resultSet.next();
}
catch (java.sql.SQLException sqle)
{
throw new GenOrmException(sqle);
}
return (ret);
}
}
//===========================================================================
private GenOrmString m_metricId;
private GenOrmString m_tagName;
private GenOrmString m_tagValue;
private List<GenOrmRecordKey> m_foreignKeys;
public List<GenOrmRecordKey> getForeignKeys() { return (m_foreignKeys); }
//---------------------------------------------------------------------------
/**
*/
public String getMetricId() { return (m_metricId.getValue()); }
public MetricTag setMetricId(String data)
{
boolean changed = m_metricId.setValue(data);
//Add the now dirty record to the transaction only if it is not previously dirty
if (changed)
{
if (m_dirtyFlags.isEmpty())
GenOrmDataSource.getGenOrmConnection().addToTransaction(this);
m_dirtyFlags.set(METRIC_ID_FIELD_META.getDirtyFlag());
if (m_isNewRecord) //Force set the prev value
m_metricId.setPrevValue(data);
}
return ((MetricTag)this);
}
//---------------------------------------------------------------------------
/**
*/
public String getTagName() { return (m_tagName.getValue()); }
public MetricTag setTagName(String data)
{
boolean changed = m_tagName.setValue(data);
//Add the now dirty record to the transaction only if it is not previously dirty
if (changed)
{
if (m_dirtyFlags.isEmpty())
GenOrmDataSource.getGenOrmConnection().addToTransaction(this);
m_dirtyFlags.set(TAG_NAME_FIELD_META.getDirtyFlag());
if (m_isNewRecord) //Force set the prev value
m_tagName.setPrevValue(data);
}
return ((MetricTag)this);
}
//---------------------------------------------------------------------------
/**
*/
public String getTagValue() { return (m_tagValue.getValue()); }
public MetricTag setTagValue(String data)
{
boolean changed = m_tagValue.setValue(data);
//Add the now dirty record to the transaction only if it is not previously dirty
if (changed)
{
if (m_dirtyFlags.isEmpty())
GenOrmDataSource.getGenOrmConnection().addToTransaction(this);
m_dirtyFlags.set(TAG_VALUE_FIELD_META.getDirtyFlag());
if (m_isNewRecord) //Force set the prev value
m_tagValue.setPrevValue(data);
}
return ((MetricTag)this);
}
//---------------------------------------------------------------------------
public Metric getMetricRef()
{
return (Metric.factory.find(m_metricId.getValue()));
}
//--------------------------------------------------------------------------
public MetricTag setMetricRef(Metric table)
{
//We add the record to the transaction if one of the key values change
if (m_metricId.setValue(table.getId()))
{
if ((m_dirtyFlags.isEmpty()) && (GenOrmDataSource.getGenOrmConnection() != null))
GenOrmDataSource.getGenOrmConnection().addToTransaction(this);
m_dirtyFlags.set(METRIC_ID_FIELD_META.getDirtyFlag());
}
return ((MetricTag)this);
}
//---------------------------------------------------------------------------
public Tag getTagRef()
{
return (Tag.factory.find(m_tagName.getValue(), m_tagValue.getValue()));
}
//--------------------------------------------------------------------------
public MetricTag setTagRef(Tag table)
{
//We add the record to the transaction if one of the key values change
if (m_tagName.setValue(table.getName()))
{
if ((m_dirtyFlags.isEmpty()) && (GenOrmDataSource.getGenOrmConnection() != null))
GenOrmDataSource.getGenOrmConnection().addToTransaction(this);
m_dirtyFlags.set(TAG_NAME_FIELD_META.getDirtyFlag());
}
if (m_tagValue.setValue(table.getValue()))
{
if ((m_dirtyFlags.isEmpty()) && (GenOrmDataSource.getGenOrmConnection() != null))
GenOrmDataSource.getGenOrmConnection().addToTransaction(this);
m_dirtyFlags.set(TAG_VALUE_FIELD_META.getDirtyFlag());
}
return ((MetricTag)this);
}
//---------------------------------------------------------------------------
protected void initialize(String metricId, String tagName, String tagValue)
{
m_metricId.setValue(metricId);
m_metricId.setPrevValue(metricId);
m_tagName.setValue(tagName);
m_tagName.setPrevValue(tagName);
m_tagValue.setValue(tagValue);
m_tagValue.setPrevValue(tagValue);
}
//---------------------------------------------------------------------------
protected void initialize(java.sql.ResultSet rs)
{
try
{
if (s_logger.isDebug())
{
java.sql.ResultSetMetaData meta = rs.getMetaData();
for (int I = 1; I <= meta.getColumnCount(); I++)
{
s_logger.debug("Reading - "+meta.getColumnName(I) +" : "+rs.getString(I));
}
}
m_metricId.setValue(rs, 1);
m_tagName.setValue(rs, 2);
m_tagValue.setValue(rs, 3);
}
catch (java.sql.SQLException sqle)
{
throw new GenOrmException(sqle);
}
}
//---------------------------------------------------------------------------
/*package*/ MetricTag_base()
{
super(TABLE_NAME);
m_logger = s_logger;
m_foreignKeys = new ArrayList<GenOrmRecordKey>();
m_dirtyFlags = new java.util.BitSet(NUMBER_OF_COLUMNS);
m_metricId = new GenOrmString(METRIC_ID_FIELD_META);
addField(m_metricId);
m_tagName = new GenOrmString(TAG_NAME_FIELD_META);
addField(m_tagName);
m_tagValue = new GenOrmString(TAG_VALUE_FIELD_META);
addField(m_tagValue);
GenOrmRecordKey foreignKey;
foreignKey = new GenOrmRecordKey("metric");
foreignKey.addKeyField("id", m_metricId);
m_foreignKeys.add(foreignKey);
foreignKey = new GenOrmRecordKey("tag");
foreignKey.addKeyField("name", m_tagName);
foreignKey.addKeyField("value", m_tagValue);
m_foreignKeys.add(foreignKey);
}
//---------------------------------------------------------------------------
@Override
public GenOrmConnection getGenOrmConnection()
{
return (GenOrmDataSource.getGenOrmConnection());
}
//---------------------------------------------------------------------------
@Override
public String getFieldEscapeString()
{
return (s_fieldEscapeString);
}
//---------------------------------------------------------------------------
@Override
public void setMTS()
{
}
//---------------------------------------------------------------------------
@Override
public void setCTS()
{
}
//---------------------------------------------------------------------------
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("metric_id=\"");
sb.append(m_metricId.getValue());
sb.append("\" ");
sb.append("tag_name=\"");
sb.append(m_tagName.getValue());
sb.append("\" ");
sb.append("tag_value=\"");
sb.append(m_tagValue.getValue());
sb.append("\" ");
return (sb.toString().trim());
}
//===========================================================================
}