Search in sources :

Example 6 with JDBCCallback

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;
        }
    });
}
Also used : Transaction(com.ramussoft.common.persistent.Transaction) SQLException(java.sql.SQLException) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection) FindObject(com.ramussoft.common.attribute.FindObject) PreparedStatement(java.sql.PreparedStatement)

Example 7 with JDBCCallback

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;
        }
    });
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) SQLException(java.sql.SQLException) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Statement(java.sql.Statement) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection)

Example 8 with JDBCCallback

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;
        }
    });
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) SQLException(java.sql.SQLException) PersistentRow(com.ramussoft.common.persistent.PersistentRow) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection)

Example 9 with JDBCCallback

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();
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) SQLException(java.sql.SQLException) Statement(java.sql.Statement) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection) Date(java.util.Date) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet)

Example 10 with JDBCCallback

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();
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) SAXException(org.xml.sax.SAXException) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Hashtable(java.util.Hashtable) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) IOException(java.io.IOException) SQLException(java.sql.SQLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) ParseException(java.text.ParseException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

JDBCCallback (com.ramussoft.jdbc.JDBCCallback)14 Connection (java.sql.Connection)14 PreparedStatement (java.sql.PreparedStatement)12 SQLException (java.sql.SQLException)10 FindObject (com.ramussoft.common.attribute.FindObject)5 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)5 Transaction (com.ramussoft.common.persistent.Transaction)4 ArrayList (java.util.ArrayList)4 Attribute (com.ramussoft.common.Attribute)3 Qualifier (com.ramussoft.common.Qualifier)2 PersistentField (com.ramussoft.common.persistent.PersistentField)2 PersistentRow (com.ramussoft.common.persistent.PersistentRow)2 User (com.ramussoft.net.common.User)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 Hashtable (java.util.Hashtable)2 Element (com.ramussoft.common.Element)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1