Skip to content
Snippets Groups Projects
Commit d8060542 authored by tjc's avatar tjc
Browse files

write back updates to dao

git-svn-id: svn+ssh://svn.internal.sanger.ac.uk/repos/svn/pathsoft/artemis/trunk@4168 ee4ac58c-ac51-4696-9907-e4b3aa274f04
parent de03241c
No related branches found
No related tags found
No related merge requests found
......@@ -24,10 +24,8 @@
package uk.ac.sanger.artemis.chado;
import java.util.Properties;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import java.util.Enumeration;
import java.util.StringTokenizer;
/**
......@@ -43,18 +41,20 @@ public class ChadoTransaction
public static final int INSERT = 2;
/** delete statement */
public static final int DELETE = 3;
/** properties store */
protected Hashtable properties;
private List properties;
/** old properties store */
protected Hashtable constraint;
protected List constraint;
/** type of statement */
protected int type;
/** feature uniquename */
protected String uniquename;
/** chado table for this transaction */
protected String chadoTable;
/** schema */
private String schema;
/** feature id */
private int feature_id;
/**
*
......@@ -71,6 +71,24 @@ public class ChadoTransaction
this.chadoTable = chadoTable;
}
/**
*
*
*/
public void setSchema(final String schema)
{
this.schema = schema;
}
/**
*
*
*/
public String getSchema()
{
return schema;
}
/**
*
* @return transaction type
......@@ -111,130 +129,122 @@ public class ChadoTransaction
public void addProperty(String name, String value)
{
if(properties == null)
properties = new Hashtable();
properties.put(name, value);
properties = new Vector();
properties.add(name+"="+value);
}
/**
*
* Set a constraint on this transaction.
*
*/
public void setConstraint(String name, String value)
{
if(constraint == null)
constraint = new Hashtable();
constraint.put(name, value);
constraint = new Vector();
constraint.add(name+"="+"\'"+value+"\'");
}
public String[] getSqlQuery(String schema)
/**
*
* Get properties to this transaction that will be changed.
*
*/
public List getProperties()
{
StringBuffer sqlBuff = new StringBuffer();
return properties;
}
public void setProperties(List properties)
{
this.properties = properties;
}
if(type == UPDATE)
/**
*
* Get properties to this transaction that will be changed.
*
*/
public List getPropertiesName()
{
List propertiesName = new Vector();
String property;
int index;
for(int i=0; i<properties.size();i++)
{
sqlBuff.append("UPDATE "+schema+"."+chadoTable);
sqlBuff.append(" SET ");
String name;
String value;
Enumeration enum_prop = properties.keys();
while(enum_prop.hasMoreElements())
{
name = (String)enum_prop.nextElement();
value = (String)properties.get(name);
sqlBuff.append(name+"="+value);
if(enum_prop.hasMoreElements())
sqlBuff.append(" , ");
}
sqlBuff.append(" FROM "+schema+".feature");
sqlBuff.append(" WHERE "+schema+".feature.feature_id="+
schema+"."+chadoTable+".feature_id AND (");
StringTokenizer tok = new StringTokenizer(uniquename,",");
while(tok.hasMoreTokens())
{
sqlBuff.append(" "+schema+"."+"feature.uniquename='" + tok.nextToken()+"' ");
if(tok.hasMoreTokens())
sqlBuff.append("OR");
}
sqlBuff.append(")");
if(constraint != null)
{
Enumeration enum_constraint = constraint.keys();
while(enum_constraint.hasMoreElements())
{
name = (String)enum_constraint.nextElement();
value = (String)constraint.get(name);
sqlBuff.append(" AND ");
// looks like specifying table, so include schema
if(name.indexOf(".") > -1)
sqlBuff.append(schema+".");
sqlBuff.append(name+"="+value);
}
}
property = (String)properties.get(i);
index = property.indexOf("=");
propertiesName.add(property.substring(0,index));
}
else if(type == INSERT)
return propertiesName;
}
/**
*
* Get properties to this transaction that will be changed.
*
*/
public List getPropertiesValue()
{
List propertiesValue = new Vector();
String property;
int index;
for(int i=0; i<properties.size();i++)
{
StringTokenizer tok = new StringTokenizer(uniquename,",");
while(tok.hasMoreTokens())
{
sqlBuff.append("INSERT INTO "+schema+"."+chadoTable);
StringBuffer sqlKeys = new StringBuffer();
StringBuffer sqlValues = new StringBuffer();
sqlKeys.append("feature_id , ");
sqlValues.append("(SELECT feature_id FROM "+schema+".feature WHERE uniquename='"+
tok.nextToken()+"') , ");
String name;
Enumeration enum_prop = properties.keys();
while(enum_prop.hasMoreElements())
{
name = (String)enum_prop.nextElement();
sqlKeys.append(name);
sqlValues.append((String)properties.get(name));
if(enum_prop.hasMoreElements())
{
sqlKeys.append(" , ");
sqlValues.append(" , ");
}
}
sqlBuff.append(" ( "+sqlKeys.toString()+" ) ");
sqlBuff.append(" values ");
sqlBuff.append(" ( "+sqlValues.toString()+" )");
sqlBuff.append("\t");
}
property = (String)properties.get(i);
index = property.indexOf("=");
propertiesValue.add(property.substring(index+1));
}
else if(type == DELETE)
return propertiesValue;
}
/**
*
* Get constraints on this transaction.
*
*/
public List getConstraint()
{
return constraint;
}
/**
*
* Get uniquenames of features.
*
*/
public List getUniquename()
{
Vector names = new Vector();
StringTokenizer tok = new StringTokenizer(uniquename,",");
while(tok.hasMoreTokens())
{
StringTokenizer tok = new StringTokenizer(uniquename,",");
while(tok.hasMoreTokens())
{
sqlBuff.append("DELETE FROM "+schema+"."+chadoTable+" WHERE ");
String name;
String value;
Enumeration enum_constraint = constraint.keys();
while(enum_constraint.hasMoreElements())
{
name = (String)enum_constraint.nextElement();
value = (String)constraint.get(name);
sqlBuff.append(name+"="+value+" AND ");
}
sqlBuff.append("feature_id=(SELECT feature_id FROM "+schema+".feature WHERE uniquename='"+
tok.nextToken()+"')");
sqlBuff.append("\t");
}
names.add(tok.nextToken());
}
String sql = sqlBuff.toString();
StringTokenizer tok = new StringTokenizer(sql,"\t");
final int count = tok.countTokens();
String[] sql_array = new String[count];
for(int i=0; i<count; i++)
sql_array[i] = tok.nextToken();
return sql_array;
return names;
// String str_names[] = new String[names.size()];
// for(int i=0; i<str_names.length; i++)
// str_names[i] = (String)names.get(i);
// return str_names;
}
public void setFeature_id(final int feature_id)
{
this.feature_id = feature_id;
}
public int getFeature_id()
{
return feature_id;
}
}
......@@ -685,9 +685,20 @@ public class DatabaseDocument extends Document
Connection conn = getConnection();
int row = 0;
ChadoDAO dao = getDAO();
for(int i = 0; i < sql.size(); i++)
{
ChadoTransaction tsn = (ChadoTransaction) sql.get(i);
if(tsn.getType() == ChadoTransaction.UPDATE)
dao.updateAttributes(schema, tsn);
else if(tsn.getType() == ChadoTransaction.INSERT)
dao.insertAttributes(schema, tsn);
else if(tsn.getType() == ChadoTransaction.DELETE)
dao.deleteAttributes(schema, tsn);
/*
String[] sql_array = tsn.getSqlQuery(schema);
for(int j = 0; j < sql_array.length; j++)
......@@ -697,6 +708,7 @@ public class DatabaseDocument extends Document
Statement st = conn.createStatement();
row += st.executeUpdate(sql_array[j]);
}
*/
}
conn.close();
......
......@@ -27,6 +27,7 @@ package uk.ac.sanger.ibatis;
import java.sql.*;
import java.io.*;
import java.util.List;
import uk.ac.sanger.artemis.chado.ChadoTransaction;
public interface ChadoDAO
{
......@@ -106,4 +107,34 @@ public interface ChadoDAO
*/
public List getCvterm()
throws SQLException;
//
// WRITE BACK
//
/**
*
* @param schema schema to update.
*
*/
public void updateAttributes
(final String schema, final ChadoTransaction tsn)
throws SQLException;
/**
*
* @param schema schema to update.
*
*/
public void insertAttributes
(final String schema, final ChadoTransaction tsn)
throws SQLException;
/**
*
* @param schema schema to update.
*
*/
public void deleteAttributes
(final String schema, final ChadoTransaction tsn)
throws SQLException;
}
......@@ -25,8 +25,11 @@
package uk.ac.sanger.ibatis;
import com.ibatis.sqlmap.client.SqlMapClient;
import uk.ac.sanger.artemis.chado.ChadoTransaction;
import java.util.List;
import java.util.Vector;
import java.util.Enumeration;
import java.sql.*;
import javax.swing.JPasswordField;
......@@ -183,5 +186,66 @@ public class IBatisDAO implements ChadoDAO
return (Cvterm)sqlMap.queryForObject("getCvterm", cvterm);
}
//
// WRITE BACK
//
/**
*
* @param schema schema to update.
*
*/
public void updateAttributes
(final String schema, final ChadoTransaction tsn)
throws SQLException
{
SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance();
tsn.setSchema(schema);
sqlMap.update("updateAttributes", tsn);
}
/**
*
* @param schema schema to update.
*
*/
public void insertAttributes
(final String schema, final ChadoTransaction tsn)
throws SQLException
{
SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance();
tsn.setSchema(schema);
// get the feature id's
List feature_ids = sqlMap.queryForList("getFeatureID", tsn);
for(int i=0; i<feature_ids.size(); i++)
{
tsn.setFeature_id( ((Integer)feature_ids.get(i)).intValue() );
sqlMap.insert("insertAttributes", tsn);
}
}
/**
*
* @param schema schema to update.
*
*/
public void deleteAttributes
(final String schema, final ChadoTransaction tsn)
throws SQLException
{
SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance();
tsn.setSchema(schema);
// get the feature id's
List feature_ids = sqlMap.queryForList("getFeatureID", tsn);
for(int i=0; i<feature_ids.size(); i++)
{
tsn.setFeature_id( ((Integer)feature_ids.get(i)).intValue() );
sqlMap.delete("deleteAttributes", tsn);
}
}
}
......@@ -29,6 +29,9 @@ import java.sql.*;
import java.io.*;
import java.util.List;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import uk.ac.sanger.artemis.chado.ChadoTransaction;
public class JdbcDAO
implements ChadoDAO
......@@ -323,6 +326,156 @@ public class JdbcDAO
return cvterms;
}
//
// WRITE
//
/**
*
* @param schema schema to update.
*
*/
public void updateAttributes
(final String schema, final ChadoTransaction tsn)
throws SQLException
{
StringBuffer sqlBuff = new StringBuffer();
String chadoTable = tsn.getChadoTable();
sqlBuff.append("UPDATE "+schema+"."+chadoTable);
sqlBuff.append(" SET ");
List properties = tsn.getProperties();
for(int i=0; i<properties.size(); i++)
{
sqlBuff.append((String)properties.get(i));
if(i < properties.size()-1)
sqlBuff.append(" , ");
}
sqlBuff.append(" FROM "+schema+".feature");
sqlBuff.append(" WHERE "+schema+".feature.feature_id="+
schema+"."+chadoTable+".feature_id AND (");
List uniquename = tsn.getUniquename();
for(int i=0; i<uniquename.size(); i++)
{
sqlBuff.append(" "+schema+"."+"feature.uniquename='" +
(String)uniquename.get(i) +"' ");
if(i < uniquename.size()-1)
sqlBuff.append("OR");
}
sqlBuff.append(")");
List constraints = tsn.getConstraint();
if(constraints != null)
{
for(int i=0; i<constraints.size(); i++)
{
sqlBuff.append(" AND ");
// looks like specifying table, so include schema
String constraint = (String)constraints.get(i);
int index;
if( (index = constraint.indexOf(".")) > -1 &&
constraint.indexOf("=") > index)
sqlBuff.append(schema+".");
sqlBuff.append(constraint);
}
}
Statement st = conn.createStatement();
st.executeUpdate(new String(sqlBuff));
}
/**
*
* @param schema schema to update.
*
*/
public void insertAttributes
(final String schema, final ChadoTransaction tsn)
throws SQLException
{
StringBuffer sqlBuff;
List uniquename = tsn.getUniquename();
String chadoTable = tsn.getChadoTable();
for(int i=0; i<uniquename.size(); i++)
{
sqlBuff = new StringBuffer();
sqlBuff.append("INSERT INTO "+schema+"."+chadoTable);
StringBuffer sqlKeys = new StringBuffer();
StringBuffer sqlValues = new StringBuffer();
sqlKeys.append("feature_id , ");
sqlValues.append("(SELECT feature_id FROM "+schema+".feature WHERE uniquename='"+
(String)uniquename.get(i)+"') , ");
String name;
String property;
List propertiesName = tsn.getPropertiesName();
List propertiesValue = tsn.getPropertiesValue();
for(int j=0; j<propertiesName.size(); j++)
{
name = (String)propertiesName.get(j);
sqlKeys.append(name);
sqlValues.append((String)propertiesValue.get(j));
if(j < propertiesName.size()-1)
{
sqlKeys.append(" , ");
sqlValues.append(" , ");
}
}
sqlBuff.append(" ( "+sqlKeys.toString()+" ) ");
sqlBuff.append(" values ");
sqlBuff.append(" ( "+sqlValues.toString()+" )");
appendToLogFile(new String(sqlBuff), sqlLog);
Statement st = conn.createStatement();
st.executeUpdate(new String(sqlBuff));
}
}
/**
*
* @param schema schema to update.
*
*/
public void deleteAttributes
(final String schema, final ChadoTransaction tsn)
throws SQLException
{
StringBuffer sqlBuff;
List uniquename = tsn.getUniquename();
String chadoTable = tsn.getChadoTable();
for(int i=0; i<uniquename.size(); i++)
{
sqlBuff = new StringBuffer();
sqlBuff.append("DELETE FROM "+schema+"."+chadoTable+" WHERE ");
String name;
String value;
List constraints = tsn.getConstraint();
for(int j=0; j<constraints.size(); j++)
sqlBuff.append((String)constraints.get(j)+" AND ");
sqlBuff.append("feature_id=(SELECT feature_id FROM "+
schema+".feature WHERE uniquename='"+
(String)uniquename.get(i)+"')");
appendToLogFile(new String(sqlBuff), sqlLog);
Statement st = conn.createStatement();
st.executeUpdate(new String(sqlBuff));
}
}
/**
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment