262 lines
6.7 KiB
Java
262 lines
6.7 KiB
Java
import java.sql.*;
|
|
import java.util.ArrayList;
|
|
|
|
public class DBConnector
|
|
{
|
|
private String mappingSchema = "Relation text, TID int4, Col text, CID text, HID text";
|
|
private String compSchema = "CID text, HID text, WID text, Value int";
|
|
private String worldSchema = "CID text, WID text";
|
|
|
|
private String mappingAttrs = "Relation, TID, Col, CID, HID";
|
|
private String compAttrs = "CID, HID, WID, Value";
|
|
private String worldAttrs = "CID, WID";
|
|
|
|
private static int id = 1;
|
|
private Connection dbConnection;
|
|
private Statement sqlStatement;
|
|
|
|
private String templateRelName;
|
|
private String mappingRelName;
|
|
private String compRelName;
|
|
private String worldRelName;
|
|
|
|
public double worldCount;
|
|
|
|
public DBConnector(String aDatabase, String aUser, String aPassword,
|
|
String aTemplateRelName, String aMappingRelName, String aCompRelName,
|
|
String aWorldRelName)
|
|
{
|
|
templateRelName = aTemplateRelName;
|
|
mappingRelName = aMappingRelName;
|
|
compRelName = aCompRelName;
|
|
worldRelName = aWorldRelName;
|
|
worldCount = 1;
|
|
init(aDatabase, aUser, aPassword);
|
|
}
|
|
|
|
private void init(String aDatabase, String aUser, String aPassword)
|
|
{
|
|
try
|
|
{
|
|
Class.forName("org.postgresql.Driver"); //load the driver
|
|
|
|
String connString = "jdbc:postgresql:" + aDatabase;
|
|
//System.out.println("Connecting to " + connString);
|
|
dbConnection = DriverManager.getConnection(connString,
|
|
aUser, aPassword);
|
|
sqlStatement = dbConnection.createStatement();
|
|
|
|
// Create mapping relation
|
|
createTable(mappingRelName, mappingSchema);
|
|
|
|
// Create component relation
|
|
createTable(compRelName, compSchema);
|
|
|
|
// Create world relation
|
|
//createTable(worldRelName, worldSchema);
|
|
}
|
|
catch (SQLException e)
|
|
{
|
|
System.err.println("Could not connect to the database!");
|
|
e.printStackTrace();
|
|
return;
|
|
}
|
|
catch (ClassNotFoundException e)
|
|
{
|
|
System.err.println("Could not load database driver!");
|
|
return;
|
|
}
|
|
}
|
|
|
|
public void closeConnection()
|
|
{
|
|
try
|
|
{
|
|
sqlStatement.close();
|
|
dbConnection.close();
|
|
}
|
|
catch (SQLException e)
|
|
{
|
|
// Ignore
|
|
return;
|
|
}
|
|
}
|
|
|
|
private void createTable(String aName, String aSchema)
|
|
throws SQLException
|
|
{
|
|
String sql = "CREATE TABLE " + aName + " (" + aSchema + ");";
|
|
System.out.println(sql);
|
|
//sqlStatement.executeUpdate(sql);
|
|
}
|
|
private String getValue(String aRelName, int aTid, String aColumnName)
|
|
throws SQLException
|
|
{
|
|
String sql = "SELECT " + aColumnName + " FROM " + aRelName + " WHERE tid = " + aTid;
|
|
ResultSet rs = sqlStatement.executeQuery(sql);
|
|
if (rs.next())
|
|
{
|
|
if (rs.getObject(1) == null)
|
|
{
|
|
return "-1";
|
|
}
|
|
return rs.getString(1);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private void setHole(int aTid, String aColumnName, ArrayList aHoleValues,
|
|
StringBuffer aRBuffer, StringBuffer aFBuffer, StringBuffer aCBuffer)
|
|
throws SQLException
|
|
{
|
|
// Get current value of the specified column
|
|
String value = getValue(templateRelName, aTid, aColumnName);
|
|
|
|
if (value == null || value.equals("-1"))
|
|
{
|
|
// //System.err.print("+");
|
|
return;
|
|
}
|
|
|
|
|
|
// Add the real value of the column if it is not among the generated values
|
|
if (!aHoleValues.contains(value))
|
|
{
|
|
aHoleValues.add(value);
|
|
}
|
|
|
|
|
|
// Create a hole in the template relation
|
|
String sql = "UPDATE " + templateRelName + " SET " + aColumnName + " = -1 " +
|
|
" WHERE tid = " + aTid + ";";
|
|
aRBuffer.append(sql);
|
|
|
|
// Insert an entry for the new hole in the mapping relation
|
|
String cid = "c" + id;
|
|
String hid = "h" + id;
|
|
String mappingEntry = templateRelName + "\t" + aTid + "\t" +
|
|
aColumnName + "\t" + cid + "\t" + hid + "\n";
|
|
|
|
//insertTuple(mappingRelName, mappingAttrs, mappingEntry, aFStatement);
|
|
aFBuffer.append(mappingEntry);
|
|
id++;
|
|
|
|
// Insert values for the new hole in the component relation
|
|
insertHoleValues(cid, hid, aColumnName, aHoleValues, aCBuffer);
|
|
}
|
|
|
|
private void insertHoleValues(String aCid, String aHid, String aColumnName, ArrayList aValues, StringBuffer aBuffer)
|
|
throws SQLException
|
|
{
|
|
|
|
for (int i = 0; i < aValues.size(); ++i)
|
|
{
|
|
String wid = "w" + (i + 1);
|
|
String compEntry = aCid + "\t" + aHid + "\t"
|
|
+ wid + "\t" + aValues.get(i).toString() + "\n";
|
|
aBuffer.append(compEntry);
|
|
|
|
//insertTuple(compRelName, compAttrs, compEntry, aBuffer);
|
|
}
|
|
}
|
|
|
|
private void insertTuple(String aRelName, String aSchema, String aValues, Statement aStatement)
|
|
throws SQLException
|
|
{
|
|
String sql = "INSERT INTO " + aRelName + " (" + aSchema +
|
|
") VALUES (" + aValues + ")";
|
|
aStatement.addBatch(sql);
|
|
}
|
|
|
|
private int getRelSize(String aRelName)
|
|
{
|
|
ResultSet rs;
|
|
try
|
|
{
|
|
rs = sqlStatement.executeQuery("SELECT count(*) from " + aRelName);
|
|
}
|
|
catch (SQLException e)
|
|
{
|
|
System.err.println("Could not execute statement!");
|
|
return 0;
|
|
}
|
|
|
|
try
|
|
{
|
|
rs.next();
|
|
return (new Integer(rs.getString(1))).intValue();
|
|
}
|
|
catch (SQLException e)
|
|
{
|
|
System.err.println("Error getting the result!");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public int getTemplateRelSize()
|
|
{
|
|
return getRelSize(templateRelName);
|
|
}
|
|
|
|
public void introduceNoise(ArrayList aHoles)
|
|
throws SQLException
|
|
{
|
|
StringBuffer rBuffer = new StringBuffer();
|
|
StringBuffer fBuffer = new StringBuffer();
|
|
StringBuffer cBuffer = new StringBuffer();
|
|
|
|
int size = aHoles.size();
|
|
int step = 1000;
|
|
//System.err.println(size);
|
|
|
|
int i = 0;
|
|
for (; i < size;)
|
|
{
|
|
fBuffer.append("COPY " + mappingRelName + "(" + mappingAttrs + ") FROM stdin;\n");
|
|
cBuffer.append("COPY " + compRelName + " FROM stdin;\n");
|
|
int n = i + step;
|
|
if (n > size)
|
|
{
|
|
n = size;
|
|
}
|
|
int j = i;
|
|
for (; j < n; ++j)
|
|
{
|
|
Hole holeInfo = (Hole) aHoles.get(j);
|
|
setHole(holeInfo.tid, holeInfo.columnName, holeInfo.values, rBuffer, fBuffer, cBuffer);
|
|
}
|
|
//System.err.print(j + " ");
|
|
|
|
fBuffer.append("\\.");
|
|
cBuffer.append("\\.");
|
|
|
|
System.out.println(rBuffer.toString());
|
|
System.out.flush();
|
|
System.out.println(fBuffer.toString());
|
|
System.out.flush();
|
|
System.out.println(cBuffer.toString());
|
|
System.out.flush();
|
|
//sqlStatement.executeUpdate(fBuffer.toString());
|
|
//sqlStatement.executeUpdate(cBuffer.toString());
|
|
|
|
rBuffer.delete(0, rBuffer.length());
|
|
fBuffer.delete(0, fBuffer.length());
|
|
cBuffer.delete(0, cBuffer.length());
|
|
|
|
i += step;
|
|
}
|
|
|
|
//System.err.println(i);
|
|
// rBuffer.close();
|
|
// fStatement.close();
|
|
// cStatement.close();
|
|
}
|
|
|
|
public void createWorldTable() throws SQLException
|
|
{
|
|
|
|
String sql = "CREATE TABLE " + worldRelName + " AS SELECT DISTINCT cid, wid FROM " + compRelName + ";";
|
|
System.out.println(sql);
|
|
}
|
|
}
|