use of com.ramussoft.jdbc.JDBCCallback in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method deleteElement.
@Override
public void deleteElement(final long id) {
throwExaptionIfNotCan(getAccessor().canDeleteElements(new long[] { id }), "Can not delete element.");
template.execute(new JDBCCallback() {
@Override
public Object execute(Connection connection) throws SQLException {
Transaction[] transactions = getAttributeWhatWillBeDeleted(id);
for (Transaction transaction : transactions) {
executeTransaction(transaction, connection);
}
PreparedStatement ps;
ps = connection.prepareStatement("DELETE FROM " + prefix + "formula_dependences WHERE source_element_id=?");
ps.setLong(1, id);
ps.execute();
ps.close();
ps = connection.prepareStatement("DELETE FROM " + prefix + "formulas WHERE element_id=?");
ps.setLong(1, id);
ps.execute();
ps.close();
ps = connection.prepareStatement("UPDATE " + prefix + "elements SET removed_branch_id=? WHERE ELEMENT_ID=?");
long branch = getActiveBranchId();
ps.setLong(1, branch);
ps.setLong(2, id);
ps.execute();
ps.close();
return null;
}
});
}
use of com.ramussoft.jdbc.JDBCCallback in project ramus by Vitaliy-Yakovchuk.
the class PersistentFactory method dropTables.
public void dropTables() throws SQLException {
load();
template.execute(new JDBCCallback() {
@Override
public Object execute(Connection connection) throws SQLException {
Statement st = null;
try {
st = connection.createStatement();
for (Object object : rows) {
PersistentRow row = (PersistentRow) object;
if (row.getStatus() == LOADED) {
for (PersistentField field : row.getFields()) {
if (field.getType() == PersistentField.ID) {
st.execute("DROP SEQUENCE " + field.getSequenceName(row.getTableName()) + ";");
}
}
st.execute("DROP TABLE " + row.getTableName() + ";");
st.execute("DELETE FROM " + prefix + "persistent_fields");
st.execute("DELETE FROM " + prefix + "persistents");
}
}
} finally {
if (st != null)
st.close();
}
return null;
}
});
}
use of com.ramussoft.jdbc.JDBCCallback in project ramus by Vitaliy-Yakovchuk.
the class UniversalPersistentFactory method dropTables.
@Override
public void dropTables() throws SQLException {
load();
template.execute(new JDBCCallback() {
@Override
public Object execute(Connection connection) throws SQLException {
Statement st = null;
try {
st = connection.createStatement();
for (Object object : rows) {
PersistentRow row = (PersistentRow) object;
if (row.getStatus() == LOADED) {
for (PersistentField field : row.getFields()) {
if (field.getType() == PersistentField.ID) {
st.execute("DROP SEQUENCE " + field.getSequenceName(row.getTableName()) + ";");
}
}
st.execute("DROP TABLE " + row.getTableName() + ";");
}
}
st.execute("DELETE FROM " + getPersistentFieldsTableName());
st.execute("DELETE FROM " + getPersistentClassesTableName());
} finally {
if (st != null)
st.close();
}
return null;
}
});
}
use of com.ramussoft.jdbc.JDBCCallback in project ramus by Vitaliy-Yakovchuk.
the class TableToXML method store.
public void store() throws IOException, TransformerConfigurationException, SAXException {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
th = tf.newTransformerHandler();
StreamResult result = new StreamResult(stream);
th.setResult(result);
th.startDocument();
attrs.addAttribute("", "", "generate-from-table", "CDATA", tableName);
attrs.addAttribute("", "", "generate-time", "CDATA", new Date().toString());
attrs.addAttribute("", "", "prefix", "CDATA", prefix);
startElement("table");
attrs.clear();
template.execute(new JDBCCallback() {
@Override
public Object execute(Connection connection) throws SQLException {
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM " + prefix + tableName);
ResultSetMetaData meta = rs.getMetaData();
startElement("fields");
int cc = meta.getColumnCount();
Converter[] converters = new Converter[cc];
for (int i = 0; i < cc; i++) {
attrs.addAttribute("", "", "id", "CDATA", Integer.toString(i));
attrs.addAttribute("", "", "name", "CDATA", meta.getColumnName(i + 1));
attrs.addAttribute("", "", "type", "CDATA", meta.getColumnTypeName(i + 1));
startElement("field");
attrs.clear();
endElement("field");
if ((meta.getColumnType(i + 1) == Types.BLOB) || (meta.getColumnType(i + 1) == Types.VARBINARY) || (meta.getColumnTypeName(i + 1).equalsIgnoreCase("bytea"))) {
converters[i] = new ByteAConverter();
} else if (meta.getColumnType(i + 1) == Types.CLOB) {
converters[i] = new ToStringConverter();
} else if (meta.getColumnTypeName(i + 1).equalsIgnoreCase("bool")) {
converters[i] = new BoolConverter();
} else if (meta.getColumnTypeName(i + 1).equalsIgnoreCase("timestamp")) {
converters[i] = new DateConverter();
} else {
converters[i] = new ToStringConverter();
}
}
endElement("fields");
startElement("data");
while (resultSetNext(rs)) {
startElement("row");
for (int i = 0; i < cc; i++) {
Object object;
if (converters[i] instanceof BoolConverter)
object = rs.getBoolean(i + 1);
else if (converters[i] instanceof ToStringConverter)
object = rs.getString(i + 1);
else if (converters[i] instanceof ByteAConverter)
object = rs.getBytes(i + 1);
else
object = rs.getObject(i + 1);
if (object != null) {
attrs.addAttribute("", "", "id", "CDATA", Integer.toString(i));
startElement("f");
attrs.clear();
characters(converters[i].toString(object));
endElement("f");
}
}
endElement("row");
}
endElement("data");
rs.close();
st.close();
return null;
}
});
endElement("table");
th.endDocument();
}
use of com.ramussoft.jdbc.JDBCCallback in project ramus by Vitaliy-Yakovchuk.
the class XMLToTable method load.
public void load() throws IOException, SQLException {
final Hashtable<String, Integer> positions = new Hashtable<String, Integer>();
final List<String> columns = new ArrayList<String>();
final Hashtable<String, String> ids = new Hashtable<String, String>();
final PreparedStatement ps = (PreparedStatement) template.execute(new JDBCCallback() {
@Override
public Object execute(Connection connection) throws SQLException {
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM " + prefix + tableName);
ResultSetMetaData meta = rs.getMetaData();
String colums = "";
String values = "";
int columnCount = meta.getColumnCount();
for (int i = 0; i < columnCount; i++) {
if (values.equals(""))
values += "?";
else
values += ", ?";
String cn = meta.getColumnName(i + 1);
if (colums.equals(""))
colums += cn;
else
colums += ", " + cn;
String cnLowerCase = cn.toLowerCase();
columns.add(cnLowerCase);
positions.put(cn.toLowerCase(), i + 1);
}
rs.close();
st.close();
PreparedStatement ps = connection.prepareStatement("INSERT INTO " + prefix + tableName + "(" + colums + ") VALUES(" + values + ");");
for (int i = 1; i <= columnCount; i++) if (columns.get(i - 1).equals("removed_branch_id"))
ps.setLong(i, Integer.MAX_VALUE);
else
ps.setObject(i, null);
return ps;
}
});
template.execute(new JDBCCallback() {
@Override
public Object execute(final Connection connection) throws SQLException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser;
final Hashtable<String, Converter> converters = new Hashtable<String, Converter>();
try {
parser = factory.newSAXParser();
parser.parse(stream, new DefaultHandler() {
StringBuilder sb = new StringBuilder();
private boolean initFields = true;
private boolean inRow = false;
private String id;
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
if ("data".equals(name))
initFields = false;
if ("row".equals(name)) {
inRow = true;
for (int i = 1; i <= positions.size(); i++) try {
String column = columns.get(i - 1);
if (column.endsWith("_branch_id") && !column.equals("removed_branch_id"))
ps.setLong(i, 0l);
else if (column.equals("removed_branch_id"))
ps.setObject(i, Integer.MAX_VALUE);
else
ps.setObject(i, null);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if ((inRow) && ("f".equals(name))) {
id = attributes.getValue("id");
}
if ((initFields) && ("field".equals(name))) {
Converter converter = null;
String type = attributes.getValue("type");
ids.put(attributes.getValue("id"), attributes.getValue("name"));
if ((type.equalsIgnoreCase("CLOB")) || (type.equalsIgnoreCase("CHAR")) || (type.equalsIgnoreCase("TEXT")) || (type.equalsIgnoreCase("bpchar"))) {
converter = new StringConverter();
} else if ((type.equalsIgnoreCase("INTEGER")) || (type.equalsIgnoreCase("int4")))
converter = new IntegerConverter();
else if ((type.equalsIgnoreCase("BLOB")) || (type.equalsIgnoreCase("VARBINARY")) || (type.equalsIgnoreCase("bytea"))) {
converter = new ByteAConverter();
} else if (type.equalsIgnoreCase("TIMESTAMP")) {
converter = new DateConverter();
} else if ((type.equalsIgnoreCase("LONG")) || (type.equalsIgnoreCase("BIGINT")) || (type.equalsIgnoreCase("int8"))) {
converter = new LongConverter();
} else if ((type.equalsIgnoreCase("BOOL")) || (type.equalsIgnoreCase("BOOLEAN"))) {
converter = new BooleanConverter();
} else if ((type.equalsIgnoreCase("DOUBLE")) || (type.equalsIgnoreCase("float8")))
converter = new DoubleConverter();
else {
System.err.println("ERROR: Converter not found for type " + type);
}
converters.put(attributes.getValue("id"), converter);
}
}
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
if ("row".equals(name)) {
try {
inRow = false;
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
} else if ((inRow) && ("f".equals(name))) {
String key = ids.get(id);
int i = -1;
try {
i = positions.get(key.toLowerCase());
} catch (Exception e) {
e.printStackTrace();
}
if (i > 0) {
Converter c = converters.get(id);
try {
c.fill(ps, i, sb.toString());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
sb = new StringBuilder();
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
sb.append(new String(ch, start, length));
}
public void endDocument() throws SAXException {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
});
stream.close();
}
Aggregations