Changeset 4503
- Timestamp:
- Sep 10, 2008, 2:43:46 PM (15 years ago)
- Location:
- trunk/src/core/net/sf/basedb/core
- Files:
-
- 6 added
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/net/sf/basedb/core/AbstractBatcher.java
r4479 r4503 28 28 import java.lang.ref.WeakReference; 29 29 import java.sql.SQLException; 30 import java.sql.Statement;31 30 32 31 import net.sf.basedb.core.dbengine.DbEngine; 32 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 33 33 34 34 /** … … 198 198 try 199 199 { 200 Statement s = HibernateUtil.getConnection(getDbControl().getHibernateSession()).createStatement(); 201 s.executeUpdate(sql); 202 s.close(); 200 HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), new ExecuteUpdateWork(sc, sql)); 203 201 } 204 202 catch (SQLException ex) -
trunk/src/core/net/sf/basedb/core/AbstractSqlQuery.java
r4479 r4503 26 26 package net.sf.basedb.core; 27 27 28 import net.sf.basedb.core.hibernate.ResultSetWork; 28 29 import net.sf.basedb.core.query.SqlQuery; 29 30 import net.sf.basedb.core.query.QueryType; … … 102 103 throws BaseException 103 104 { 104 inttotalCount = -1;105 long totalCount = -1; 105 106 // Get query string 106 107 String countSql = getCountQuery(dc, true); 107 108 // Parse SQL and replace named parameters with ? 108 List<String> countParameterOrder = new LinkedList<String>(); 109 final List<String> countParameterOrder = new LinkedList<String>(); 110 final AbstractSqlQuery query = this; 109 111 countSql = parseParameters(countSql, countParameterOrder); 110 112 try 111 113 { 112 Connection c = HibernateUtil.getConnection(dc.getHibernateSession());113 114 if (debugSqlEnabled) logSql.debug("Executing count query: " + countSql); 114 final PreparedStatement ps = c.prepareStatement(countSql);115 setParameters(ps, countParameterOrder);116 115 117 ResultSet result = QueryExecutor.executeQuery(ps, dc.getSessionControl()); 118 119 result.next(); 120 totalCount = result.getInt(1); 121 result.close(); 122 ps.close(); 123 } 124 catch (InterruptedException ex) 125 { 126 throw new SignalException("Aborted by user", ex); 116 totalCount = HibernateUtil.doJdbcWork(dc.getHibernateSession(), 117 new ResultSetWork<Long>(dc.getSessionControl(), countSql) 118 { 119 @Override 120 protected void setParameters(PreparedStatement ps) 121 throws SQLException 122 { 123 query.setParameters(ps, countParameterOrder); 124 } 125 @Override 126 protected Long getResult(ResultSet rs) 127 throws SQLException 128 { 129 return rs.next() ? rs.getLong(1) : 0; 130 } 131 } 132 ); 127 133 } 128 134 catch (SQLException ex) -
trunk/src/core/net/sf/basedb/core/Application.java
r4483 r4503 29 29 import net.sf.basedb.core.data.ClientData; 30 30 import net.sf.basedb.core.data.SchemaVersionData; 31 import net.sf.basedb.core.hibernate.JdbcWork; 31 32 import net.sf.basedb.core.authentication.Authenticator; 32 33 import net.sf.basedb.util.FileUtil; … … 50 51 import java.util.regex.Pattern; 51 52 import java.net.InetAddress; 53 import java.sql.Connection; 52 54 import java.sql.DatabaseMetaData; 55 import java.sql.SQLException; 53 56 54 57 … … 297 300 { 298 301 session = HibernateUtil.newSession(); 299 DatabaseMetaData meta = HibernateUtil.getConnection(session).getMetaData(); 300 databaseVersionNumber = meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion(); 302 HibernateUtil.doJdbcWork(session, 303 new JdbcWork<String>() 304 { 305 @Override 306 public String getResult() 307 { 308 return null; 309 } 310 @Override 311 public void execute(Connection connection) 312 throws SQLException 313 { 314 DatabaseMetaData meta = connection.getMetaData(); 315 databaseVersionNumber = meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion(); 316 } 317 }); 301 318 } 302 319 catch (Throwable th) -
trunk/src/core/net/sf/basedb/core/FeatureBatcher.java
r4479 r4503 31 31 import net.sf.basedb.core.data.ReporterData; 32 32 import net.sf.basedb.core.data.WellData; 33 import net.sf.basedb.core. signal.SignalException;33 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 34 34 35 35 import java.sql.PreparedStatement; … … 327 327 { 328 328 if (debugSqlEnabled) logSql.debug("Deleting features: " + sql); 329 PreparedStatement ps = HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(sql); 330 ps.setInt(1, arrayDesign.getId()); 331 QueryExecutor.executeUpdate(ps, getSessionControl()); 329 HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 330 new ExecuteUpdateWork(getSessionControl(), sql) 331 { 332 @Override 333 public void setParameters(PreparedStatement ps) 334 throws SQLException 335 { 336 ps.setInt(1, arrayDesign.getId()); 337 } 338 339 } 340 ); 332 341 arrayDesignData.setHasFeatures(false); 333 }334 catch (InterruptedException ex)335 {336 throw new SignalException("Aborted by user.", ex);337 342 } 338 343 catch (SQLException ex) -
trunk/src/core/net/sf/basedb/core/FilterBatcher.java
r4479 r4503 26 26 package net.sf.basedb.core; 27 27 28 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 28 29 import net.sf.basedb.core.query.Expressions; 29 30 import net.sf.basedb.core.query.Selects; 30 import net.sf.basedb.core.signal.SignalException;31 31 32 32 import java.util.List; … … 287 287 @throws BaseException If there is an error 288 288 */ 289 public int insert( AbstractSqlQuery query)289 public int insert(final AbstractSqlQuery query) 290 290 throws BaseException 291 291 { … … 313 313 314 314 // Holds the names of the parameters in the query in the order they appear 315 List<String> parameterOrder = new LinkedList<String>();315 final List<String> parameterOrder = new LinkedList<String>(); 316 316 317 317 // Parse out named parameters … … 323 323 try 324 324 { 325 PreparedStatement ps = HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql); 326 query.setParameters(ps, parameterOrder); 327 rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl()); 328 ps.close(); 325 rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 326 new ExecuteUpdateWork(getSessionControl(), insertSql) 327 { 328 @Override 329 public void setParameters(PreparedStatement ps) 330 throws SQLException 331 { 332 query.setParameters(ps, parameterOrder); 333 } 334 335 } 336 ); 329 337 bytes += rowsInserted * bytesPerRow; 330 338 totalInsertCount += rowsInserted; 331 }332 catch (InterruptedException ex)333 {334 throw new SignalException("Aborted by user.", ex);335 339 } 336 340 catch (SQLException ex) -
trunk/src/core/net/sf/basedb/core/HibernateUtil.java
r4479 r4503 42 42 import net.sf.basedb.core.dbengine.TableInfo.IndexInfo; 43 43 import net.sf.basedb.core.hibernate.EntityQueryWrapper; 44 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 45 import net.sf.basedb.core.hibernate.JdbcWork; 46 import net.sf.basedb.core.hibernate.MultiUpdateWork; 47 import net.sf.basedb.core.hibernate.TableExistsWork; 44 48 import net.sf.basedb.core.query.QueryType; 45 49 import net.sf.basedb.util.XMLUtil; 46 50 51 import java.util.ArrayList; 47 52 import java.util.Arrays; 48 53 import java.util.HashMap; … … 540 545 { 541 546 session = HibernateUtil.newSession(); 542 DatabaseMetaData metaData = HibernateUtil.getConnection(session).getMetaData(); 547 isEmpty = HibernateUtil.doJdbcWork(session, 548 new JdbcWork<Boolean>() 549 { 550 private boolean isEmpty = true; 551 @Override 552 public Boolean getResult() 553 { 554 return isEmpty; 555 } 556 @Override 557 public void execute(Connection connection) 558 throws SQLException 559 { 560 DatabaseMetaData metaData = connection.getMetaData(); 561 Iterator<PersistentClass> classes = cfg.getClassMappings(); 562 while (classes.hasNext() && isEmpty) 563 { 564 PersistentClass pClass = classes.next(); 565 Table t = pClass.getTable(); 566 ResultSet tables = metaData.getTables(t.getCatalog(), t.getSchema(), t.getName(), new String[] { "TABLE" } ); 567 if (tables.next()) 568 { 569 isEmpty = false; 570 log.error("Table '"+t.getName()+"' already exists; install aborted"); 571 } 572 tables.close(); 573 } 574 } 575 } 576 ); 543 577 544 Iterator<PersistentClass> classes = cfg.getClassMappings();545 while (classes.hasNext() && isEmpty)546 {547 PersistentClass pClass = classes.next();548 Table t = pClass.getTable();549 ResultSet tables = metaData.getTables(t.getCatalog(), t.getSchema(), t.getName(), new String[] { "TABLE" } );550 if (tables.next())551 {552 isEmpty = false;553 log.error("Table '"+t.getName()+"' already exists; install aborted");554 }555 tables.close();556 }557 578 HibernateUtil.close(session); 558 579 session = null; … … 600 621 session = HibernateUtil.newSession(); 601 622 tx = HibernateUtil.newTransaction(session); 602 Connection connection = HibernateUtil.getConnection(session);603 623 String tableName = table.getTableName(db); 604 624 605 if (!tableExists(tableName, connection))625 if (!tableExists(tableName, session)) 606 626 { 607 627 Table hibernateTable = newDynamicMapping(db, table); … … 612 632 String quotedSchema = quote(dynamicSchema); 613 633 634 List<String> statements = new ArrayList<String>(); 635 614 636 String sql = hibernateTable.sqlCreateString(dialect, null, quotedCatalog, quotedSchema); 615 637 sql = dbEngine.makeSafeCreateTable(sql, dynamicCatalog, dynamicSchema, tableName); 616 638 logSql.debug(sql); 639 statements.add(sql); 617 640 618 Statement s = connection.createStatement();619 s.executeUpdate(sql);620 621 641 Iterator i = hibernateTable.getIndexIterator(); 622 642 while (i.hasNext()) … … 625 645 sql = ii.sqlCreateString(dialect, null, quotedCatalog, quotedSchema); 626 646 logSql.debug(sql); 627 s .executeUpdate(sql);647 statements.add(sql); 628 648 } 629 s.close();649 HibernateUtil.doJdbcWork(session, new MultiUpdateWork(statements)); 630 650 } 631 651 HibernateUtil.commit(tx); … … 676 696 tx = HibernateUtil.newTransaction(session); 677 697 678 Connection connection = HibernateUtil.getConnection(session); 679 if (tableExists(table.getTableName(db), connection)) 698 if (tableExists(table.getTableName(db), session)) 680 699 { 681 700 Table hibernateTable = newDynamicMapping(db, table); … … 685 704 String sql = hibernateTable.sqlDropString(dialect, dynamicCatalog, dynamicSchema); 686 705 logSql.debug(sql); 687 688 Statement s = connection.createStatement(); 689 s.executeUpdate(sql); 690 s.close(); 706 HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql)); 691 707 } 692 708 HibernateUtil.commit(tx); … … 731 747 session = HibernateUtil.newSession(); 732 748 tx = HibernateUtil.newTransaction(session); 733 hasTable = tableExists(table.getTableName(db), HibernateUtil.getConnection(session));749 hasTable = tableExists(table.getTableName(db), session); 734 750 HibernateUtil.commit(tx); 735 751 } … … 756 772 Check if table in the dynamic database exists. 757 773 @param tableName The name of the table 758 @param c The JDBC connection774 @param session The Hibernate session 759 775 @see #virtualTableExists(VirtualDb, VirtualTable) 760 776 */ 761 private static boolean tableExists(String tableName, Connection c)777 private static boolean tableExists(String tableName, Session session) 762 778 throws SQLException 763 779 { 764 ResultSet tables = null; 765 boolean hasTable = false; 766 try 767 { 768 DatabaseMetaData metaData = c.getMetaData(); 769 tables = metaData.getTables(Application.getDynamicCatalog(), 770 Application.getDynamicSchema(), tableName, null); 771 hasTable = tables.next(); 772 } 773 finally 774 { 775 if (tables != null) tables.close(); 776 } 777 return hasTable; 780 return HibernateUtil.doJdbcWork(session, 781 new TableExistsWork(Application.getDynamicCatalog(), Application.getDynamicSchema(), tableName)); 778 782 } 779 783 … … 864 868 // Important: we may get into problem with Keys otherwise which do their work 865 869 // in separate transactions 866 HibernateUtil.getConnection(session).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 870 HibernateUtil.doJdbcWork(session, 871 new JdbcWork<Object>() 872 { 873 @Override 874 public Object getResult() 875 { 876 return null; 877 } 878 @Override 879 public void execute(Connection connection) 880 throws SQLException 881 { 882 connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 883 } 884 } 885 ); 867 886 return session; 868 887 } … … 912 931 /** 913 932 Get the underlying JDBC connection from the Hibernate session. 914 TODO - Hibernate 3.3 will probably have a replacement for 915 the Session.connection() method. Update this method and other 916 code that is using it. See 933 Hibernate 3.3 have implemented a partial replacement for the 934 Session.connection() method that can be used if the connection 935 or any derived object such as a PreparedStatement or ResultSet is 936 not needed after the connection has been used. Such code should 937 use {@link #doJdbcWork(Session, JdbcWork)} instead. 938 <p> 939 Other code may continue using this method until the Hibernate 940 team develops a full replacement. 941 See 917 942 <a href="http://forum.hibernate.org/viewtopic.php?t=974518"> 918 943 http://forum.hibernate.org/viewtopic.php?t=974518</a> 919 944 @since 2.4 945 @see #doJdbcWork(Session, JdbcWork) 920 946 */ 921 947 @SuppressWarnings("deprecation") … … 930 956 throw new BaseException(ex); 931 957 } 958 } 959 960 /** 961 Execute some arbitrary JDBC code using the same 962 database connection as the Hibernate session. This method 963 should be used instead of {@link #getConnection(Session)} 964 wherever possible. 965 966 @param session The Hibernate session to use 967 @param work The work implementation 968 @return Determined by the work implementation 969 @throws SQLException In case there is an 970 SQL error 971 @since 2.9 972 */ 973 static <R> R doJdbcWork(Session session, JdbcWork<R> work) 974 throws SQLException 975 { 976 try 977 { 978 session.doWork(work); 979 } 980 catch (HibernateException ex) 981 { 982 if (ex.getCause() instanceof SQLException) 983 { 984 throw (SQLException)ex.getCause(); 985 } 986 throw ex; 987 } 988 return work.getResult(); 932 989 } 933 990 … … 1739 1796 @param dropIndexes If true, all indexes will be dropped 1740 1797 */ 1741 public static void dynamicDbIndexes(boolean verbose, boolean silent,boolean dropIndexes)1742 { 1743 if (silent) verbose = false;1798 public static void dynamicDbIndexes(boolean verbose, final boolean silent, final boolean dropIndexes) 1799 { 1800 final boolean isVerbose = verbose && !silent; 1744 1801 Session session = newSession(); 1745 Connection connection = HibernateUtil.getConnection(session);1746 DatabaseMetaData metaData = null;1747 List<Table> tables = new LinkedList<Table>();1748 1802 1749 1803 try 1750 1804 { 1751 metaData = connection.getMetaData(); 1752 ResultSet result = metaData.getTables(Application.getDynamicCatalog(), 1753 Application.getDynamicSchema(), null, null); 1754 1755 while (result.next()) 1756 { 1757 String tableName = result.getString("TABLE_NAME"); 1758 String tableCatalog = result.getString("TABLE_CAT"); 1759 String tableSchema = result.getString("TABLE_SCHEM"); 1760 Table table = new Table(); 1761 table.setName(tableName); 1762 table.setCatalog(tableCatalog); 1763 table.setSchema(tableSchema); 1764 tables.add(table); 1765 } 1766 } 1767 catch (SQLException ex) 1768 { 1769 throw new BaseException(ex); 1770 } 1771 1772 for (Table table : tables) 1773 { 1774 1775 if (!silent) 1776 { 1777 System.out.println("================="); 1778 System.out.println("Table : " + table.getName()); 1779 } 1780 if (verbose) 1781 { 1782 System.out.println("Catalog : " + table.getCatalog()); 1783 System.out.println("Schema : " + table.getSchema()); 1784 } 1785 1786 Map<Set<String>, String> indexedColumns = new HashMap<Set<String>, String>(); 1787 Map<Set<String>, String> uniqueColumns = new HashMap<Set<String>, String>(); 1788 1789 String pkName = null; 1790 Set<String> fkNames = new HashSet<String>(); 1791 1792 if (verbose) 1793 { 1794 System.out.println("Database information"); 1795 System.out.println("--------------------"); 1796 } 1797 1798 TableInfo tiDb = null; 1799 try 1800 { 1801 tiDb = new TableInfo(table, metaData); 1802 } 1803 catch (SQLException ex) 1804 { 1805 throw new BaseException(ex); 1806 } 1807 1808 if (tiDb != null) 1809 { 1810 if (verbose) 1805 HibernateUtil.doJdbcWork(session, 1806 new JdbcWork<Object>() 1811 1807 { 1812 // Write all columns1813 for (ColumnInfo ci : tiDb.getColumns())1808 @Override 1809 public Object getResult() 1814 1810 { 1815 System.out.println(" Column : " + ci);1811 return null; 1816 1812 } 1817 1818 // Write primary key 1819 System.out.println(" Primary key : " + tiDb.getPrimaryKey()); 1820 1821 // Write foreign keys 1822 for (ForeignKeyInfo fk : tiDb.getForeignKeys()) 1813 @Override 1814 public void execute(Connection connection) 1815 throws SQLException 1823 1816 { 1824 System.out.println(" Foreign key : " + fk); 1825 } 1826 } 1827 1828 // Write indexes and unique constraints 1829 for (IndexInfo ii : tiDb.getIndexes()) 1830 { 1831 if (!silent) 1832 { 1833 if (ii.isUnique()) 1817 DatabaseMetaData metaData = null; 1818 List<Table> tables = new LinkedList<Table>(); 1819 1820 metaData = connection.getMetaData(); 1821 ResultSet result = metaData.getTables(Application.getDynamicCatalog(), 1822 Application.getDynamicSchema(), null, null); 1823 1824 while (result.next()) 1834 1825 { 1835 System.out.println(" Unique : " + ii); 1826 String tableName = result.getString("TABLE_NAME"); 1827 String tableCatalog = result.getString("TABLE_CAT"); 1828 String tableSchema = result.getString("TABLE_SCHEM"); 1829 Table table = new Table(); 1830 table.setName(tableName); 1831 table.setCatalog(tableCatalog); 1832 table.setSchema(tableSchema); 1833 tables.add(table); 1836 1834 } 1837 else 1835 1836 for (Table table : tables) 1838 1837 { 1839 System.out.println(" Index : " + ii); 1840 } 1841 } 1842 1843 boolean safeToDrop = tiDb.safeToDrop(ii); 1844 1845 String dropSql = dbEngine.getDropIndexSql( 1846 table.getCatalog(), table.getSchema(), table.getName(), 1847 ii.getName(), ii.isUnique()); 1848 boolean actionDrop = dropIndexes && safeToDrop; 1849 1850 if (!silent) 1851 { 1852 System.out.println(" Safe drop : " + safeToDrop); 1853 System.out.println(" DROP-SQL : " + dropSql); 1854 System.out.println(" Actions : " + (actionDrop ? "DROP " : "")); 1855 } 1856 1857 if (actionDrop ) 1858 { 1859 Statement st = null; 1860 try 1861 { 1862 st = connection.createStatement(); 1863 if (actionDrop) 1838 1839 if (!silent) 1864 1840 { 1865 log.info("Dropping index: + " + dropSql);1866 st.executeUpdate(dropSql);1841 System.out.println("================="); 1842 System.out.println("Table : " + table.getName()); 1867 1843 } 1868 connection.commit(); 1869 st.close(); 1870 st = null; 1871 } 1872 catch (SQLException ex) 1873 { 1874 log.error("Exception", ex); 1875 throw new BaseException(ex); 1876 } 1877 finally 1878 { 1879 if (st != null) 1844 if (isVerbose) 1880 1845 { 1881 try 1846 System.out.println("Catalog : " + table.getCatalog()); 1847 System.out.println("Schema : " + table.getSchema()); 1848 } 1849 1850 Map<Set<String>, String> indexedColumns = new HashMap<Set<String>, String>(); 1851 Map<Set<String>, String> uniqueColumns = new HashMap<Set<String>, String>(); 1852 1853 String pkName = null; 1854 Set<String> fkNames = new HashSet<String>(); 1855 1856 if (isVerbose) 1857 { 1858 System.out.println("Database information"); 1859 System.out.println("--------------------"); 1860 } 1861 1862 TableInfo tiDb = null; 1863 try 1864 { 1865 tiDb = new TableInfo(table, metaData); 1866 } 1867 catch (SQLException ex) 1868 { 1869 throw new BaseException(ex); 1870 } 1871 1872 if (tiDb != null) 1873 { 1874 if (isVerbose) 1882 1875 { 1883 st.close(); 1876 // Write all columns 1877 for (ColumnInfo ci : tiDb.getColumns()) 1878 { 1879 System.out.println(" Column : " + ci); 1880 } 1881 1882 // Write primary key 1883 System.out.println(" Primary key : " + tiDb.getPrimaryKey()); 1884 1885 // Write foreign keys 1886 for (ForeignKeyInfo fk : tiDb.getForeignKeys()) 1887 { 1888 System.out.println(" Foreign key : " + fk); 1889 } 1884 1890 } 1885 catch (Throwable t) 1891 1892 // Write indexes and unique constraints 1893 for (IndexInfo ii : tiDb.getIndexes()) 1886 1894 { 1887 log.error("Exception", t); 1895 if (!silent) 1896 { 1897 if (ii.isUnique()) 1898 { 1899 System.out.println(" Unique : " + ii); 1900 } 1901 else 1902 { 1903 System.out.println(" Index : " + ii); 1904 } 1905 } 1906 1907 boolean safeToDrop = tiDb.safeToDrop(ii); 1908 1909 String dropSql = dbEngine.getDropIndexSql( 1910 table.getCatalog(), table.getSchema(), table.getName(), 1911 ii.getName(), ii.isUnique()); 1912 boolean actionDrop = dropIndexes && safeToDrop; 1913 1914 if (!silent) 1915 { 1916 System.out.println(" Safe drop : " + safeToDrop); 1917 System.out.println(" DROP-SQL : " + dropSql); 1918 System.out.println(" Actions : " + (actionDrop ? "DROP " : "")); 1919 } 1920 1921 if (actionDrop ) 1922 { 1923 Statement st = null; 1924 try 1925 { 1926 st = connection.createStatement(); 1927 if (actionDrop) 1928 { 1929 log.info("Dropping index: + " + dropSql); 1930 st.executeUpdate(dropSql); 1931 } 1932 connection.commit(); 1933 st.close(); 1934 st = null; 1935 } 1936 catch (SQLException ex) 1937 { 1938 log.error("Exception", ex); 1939 throw new BaseException(ex); 1940 } 1941 finally 1942 { 1943 if (st != null) 1944 { 1945 try 1946 { 1947 st.close(); 1948 } 1949 catch (Throwable t) 1950 { 1951 log.error("Exception", t); 1952 } 1953 } 1954 } 1955 } 1956 1888 1957 } 1958 } 1959 1960 if (!silent) 1961 { 1962 System.out.println("================="); 1963 System.out.println(""); 1889 1964 } 1890 1965 } 1891 1966 } 1892 1893 1967 } 1894 } 1895 1896 if (!silent) 1897 { 1898 System.out.println("================="); 1899 System.out.println(""); 1900 } 1968 ); 1969 } 1970 catch (SQLException ex) 1971 { 1972 throw new BaseException(ex); 1901 1973 } 1902 1974 } … … 1915 1987 */ 1916 1988 @SuppressWarnings("unchecked") 1917 public static void dbIndexes(boolean verbose, boolean silent, boolean dropIndexes, boolean updateIndexes)1918 {1919 if (silent) verbose = false;1920 Iterator<Table> tables = (Iterator<Table>)cfg.getTableMappings();1989 public static void dbIndexes(boolean verbose, final boolean silent, 1990 final boolean dropIndexes, final boolean updateIndexes) 1991 { 1992 final boolean isVerbose = verbose && !silent; 1921 1993 Session session = newSession(); 1922 Connection connection = HibernateUtil.getConnection(session);1923 DatabaseMetaData metaData = null;1924 1994 1925 1995 try 1926 1996 { 1927 metaData = connection.getMetaData(); 1928 } 1929 catch (SQLException ex) 1930 { 1931 throw new BaseException(ex); 1932 } 1933 1934 while (tables.hasNext()) 1935 { 1936 Table table = tables.next(); 1937 if (!silent) 1938 { 1939 System.out.println("================="); 1940 System.out.println("Table : " + table.getName()); 1941 } 1942 if (verbose) 1943 { 1944 System.out.println("Catalog : " + table.getCatalog()); 1945 System.out.println("Schema : " + table.getSchema()); 1946 } 1947 1948 Map<Set<String>, String> indexedColumns = new HashMap<Set<String>, String>(); 1949 Map<Set<String>, String> uniqueColumns = new HashMap<Set<String>, String>(); 1950 1951 String pkName = null; 1952 Set<String> fkNames = new HashSet<String>(); 1953 1954 if (verbose) 1955 { 1956 System.out.println("Database information"); 1957 System.out.println("--------------------"); 1958 } 1959 1960 TableInfo tiDb = null; 1961 try 1962 { 1963 tiDb = new TableInfo(table, metaData); 1964 } 1965 catch (SQLException ex) 1966 { 1967 throw new BaseException(ex); 1968 } 1969 1970 if (verbose && tiDb != null) 1971 { 1972 // Write all columns 1973 for (ColumnInfo ci : tiDb.getColumns()) 1997 HibernateUtil.doJdbcWork(session, 1998 new JdbcWork<Object>() 1974 1999 { 1975 System.out.println(" Column : " + ci); 1976 } 1977 1978 // Write primary key 1979 System.out.println(" Primary key : " + tiDb.getPrimaryKey()); 1980 1981 // Write foreign keys 1982 for (ForeignKeyInfo fk : tiDb.getForeignKeys()) 1983 { 1984 System.out.println(" Foreign key : " + fk); 1985 } 1986 1987 // Write indexes and unique constraints 1988 for (IndexInfo ii : tiDb.getIndexes()) 1989 { 1990 if (ii.isUnique()) 2000 @Override 2001 public Object getResult() 1991 2002 { 1992 System.out.println(" Unique : " + ii);2003 return null; 1993 2004 } 1994 else 2005 2006 @Override 2007 public void execute(Connection connection) 2008 throws SQLException 1995 2009 { 1996 System.out.println(" Index : " + ii); 1997 } 1998 } 1999 } 2000 2001 if (verbose) 2002 { 2003 System.out.println("Hibernate information"); 2004 System.out.println("---------------------"); 2005 } 2006 2007 TableInfo tiHib = new TableInfo(table, dialect); 2008 2009 if (verbose) 2010 { 2011 // Write all columns 2012 for (ColumnInfo ci : tiHib.getColumns()) 2013 { 2014 System.out.println(" Column : " + ci); 2015 } 2016 2017 // Write primary key 2018 System.out.println(" Primary key : " + tiHib.getPrimaryKey()); 2019 2020 // Write foreign keys 2021 for (ForeignKeyInfo fk : tiHib.getForeignKeys()) 2022 { 2023 System.out.println(" Foreign key : " + fk); 2024 } 2025 } 2026 2027 // Write indexes and unique constraints 2028 for (IndexInfo ii : tiHib.getIndexes()) 2029 { 2030 if (!silent) 2031 { 2032 if (ii.isUnique()) 2033 { 2034 System.out.println(" Unique : " + ii); 2035 } 2036 else 2037 { 2038 System.out.println(" Index : " + ii); 2039 } 2040 } 2010 Iterator<Table> tables = (Iterator<Table>)cfg.getTableMappings(); 2011 DatabaseMetaData metaData = connection.getMetaData(); 2012 while (tables.hasNext()) 2013 { 2014 Table table = tables.next(); 2015 if (!silent) 2016 { 2017 System.out.println("================="); 2018 System.out.println("Table : " + table.getName()); 2019 } 2020 if (isVerbose) 2021 { 2022 System.out.println("Catalog : " + table.getCatalog()); 2023 System.out.println("Schema : " + table.getSchema()); 2024 } 2041 2025 2042 String dbName = tiDb.findIndexName(ii.getName(), ii.getColumns()); 2043 boolean exists = dbName != null; 2044 boolean safeToDrop = exists && tiDb.safeToDrop(ii); 2045 2046 String dropSql = exists ? 2047 dbEngine.getDropIndexSql(table.getCatalog(), table.getSchema(), 2048 table.getName(), dbName, ii.isUnique()) : ""; 2049 String createSql = 2050 dbEngine.getCreateIndexSql(table.getCatalog(), table.getSchema(), 2051 table.getName(), table.getName() + "_" + ii.getName(), 2052 ii.getColumns(), ii.isUnique()); 2053 boolean actionDrop = dropIndexes && exists && safeToDrop; 2054 boolean actionCreate = updateIndexes && (actionDrop || !exists); 2055 2056 if (!silent) 2057 { 2058 System.out.println(" Exists : " + exists + (exists ? "(" + dbName + ")" : "")); 2059 System.out.println(" Safe drop : " + safeToDrop); 2060 System.out.println(" DROP-SQL : " + dropSql); 2061 System.out.println(" CREATE-SQL : " + createSql); 2062 System.out.println(" Actions : " + (actionDrop ? "DROP " : "") + 2063 (actionCreate ? "CREATE" : "")); 2064 } 2065 if (actionDrop || actionCreate) 2066 { 2067 Statement st = null; 2068 try 2069 { 2070 st = connection.createStatement(); 2071 if (actionDrop) 2072 { 2073 log.info("Dropping index: + " + dropSql); 2074 st.executeUpdate(dropSql); 2075 } 2076 if (actionCreate) 2077 { 2078 log.info("Creating index: + " + createSql); 2079 st.executeUpdate(createSql); 2080 } 2081 connection.commit(); 2082 st.close(); 2083 st = null; 2084 } 2085 catch (SQLException ex) 2086 { 2087 log.error("Exception", ex); 2088 throw new BaseException(ex); 2089 } 2090 finally 2091 { 2092 if (st != null) 2093 { 2026 Map<Set<String>, String> indexedColumns = new HashMap<Set<String>, String>(); 2027 Map<Set<String>, String> uniqueColumns = new HashMap<Set<String>, String>(); 2028 2029 String pkName = null; 2030 Set<String> fkNames = new HashSet<String>(); 2031 2032 if (isVerbose) 2033 { 2034 System.out.println("Database information"); 2035 System.out.println("--------------------"); 2036 } 2037 2038 TableInfo tiDb = null; 2094 2039 try 2095 2040 { 2096 st.close();2041 tiDb = new TableInfo(table, metaData); 2097 2042 } 2098 catch ( Throwable t)2043 catch (SQLException ex) 2099 2044 { 2100 log.error("Exception", t); 2045 throw new BaseException(ex); 2046 } 2047 2048 if (isVerbose && tiDb != null) 2049 { 2050 // Write all columns 2051 for (ColumnInfo ci : tiDb.getColumns()) 2052 { 2053 System.out.println(" Column : " + ci); 2054 } 2055 2056 // Write primary key 2057 System.out.println(" Primary key : " + tiDb.getPrimaryKey()); 2058 2059 // Write foreign keys 2060 for (ForeignKeyInfo fk : tiDb.getForeignKeys()) 2061 { 2062 System.out.println(" Foreign key : " + fk); 2063 } 2064 2065 // Write indexes and unique constraints 2066 for (IndexInfo ii : tiDb.getIndexes()) 2067 { 2068 if (ii.isUnique()) 2069 { 2070 System.out.println(" Unique : " + ii); 2071 } 2072 else 2073 { 2074 System.out.println(" Index : " + ii); 2075 } 2076 } 2077 } 2078 2079 if (isVerbose) 2080 { 2081 System.out.println("Hibernate information"); 2082 System.out.println("---------------------"); 2083 } 2084 2085 TableInfo tiHib = new TableInfo(table, dialect); 2086 2087 if (isVerbose) 2088 { 2089 // Write all columns 2090 for (ColumnInfo ci : tiHib.getColumns()) 2091 { 2092 System.out.println(" Column : " + ci); 2093 } 2094 2095 // Write primary key 2096 System.out.println(" Primary key : " + tiHib.getPrimaryKey()); 2097 2098 // Write foreign keys 2099 for (ForeignKeyInfo fk : tiHib.getForeignKeys()) 2100 { 2101 System.out.println(" Foreign key : " + fk); 2102 } 2103 } 2104 2105 // Write indexes and unique constraints 2106 for (IndexInfo ii : tiHib.getIndexes()) 2107 { 2108 if (!silent) 2109 { 2110 if (ii.isUnique()) 2111 { 2112 System.out.println(" Unique : " + ii); 2113 } 2114 else 2115 { 2116 System.out.println(" Index : " + ii); 2117 } 2118 } 2119 2120 String dbName = tiDb.findIndexName(ii.getName(), ii.getColumns()); 2121 boolean exists = dbName != null; 2122 boolean safeToDrop = exists && tiDb.safeToDrop(ii); 2123 2124 String dropSql = exists ? 2125 dbEngine.getDropIndexSql(table.getCatalog(), table.getSchema(), 2126 table.getName(), dbName, ii.isUnique()) : ""; 2127 String createSql = 2128 dbEngine.getCreateIndexSql(table.getCatalog(), table.getSchema(), 2129 table.getName(), table.getName() + "_" + ii.getName(), 2130 ii.getColumns(), ii.isUnique()); 2131 boolean actionDrop = dropIndexes && exists && safeToDrop; 2132 boolean actionCreate = updateIndexes && (actionDrop || !exists); 2133 2134 if (!silent) 2135 { 2136 System.out.println(" Exists : " + exists + (exists ? "(" + dbName + ")" : "")); 2137 System.out.println(" Safe drop : " + safeToDrop); 2138 System.out.println(" DROP-SQL : " + dropSql); 2139 System.out.println(" CREATE-SQL : " + createSql); 2140 System.out.println(" Actions : " + (actionDrop ? "DROP " : "") + 2141 (actionCreate ? "CREATE" : "")); 2142 } 2143 if (actionDrop || actionCreate) 2144 { 2145 Statement st = null; 2146 try 2147 { 2148 st = connection.createStatement(); 2149 if (actionDrop) 2150 { 2151 log.info("Dropping index: + " + dropSql); 2152 st.executeUpdate(dropSql); 2153 } 2154 if (actionCreate) 2155 { 2156 log.info("Creating index: + " + createSql); 2157 st.executeUpdate(createSql); 2158 } 2159 connection.commit(); 2160 st.close(); 2161 st = null; 2162 } 2163 catch (SQLException ex) 2164 { 2165 log.error("Exception", ex); 2166 throw new BaseException(ex); 2167 } 2168 finally 2169 { 2170 if (st != null) 2171 { 2172 try 2173 { 2174 st.close(); 2175 } 2176 catch (Throwable t) 2177 { 2178 log.error("Exception", t); 2179 } 2180 } 2181 } 2182 } 2183 2184 } 2185 2186 if (!silent) 2187 { 2188 System.out.println("================="); 2189 System.out.println(""); 2101 2190 } 2102 2191 } 2192 2103 2193 } 2194 2104 2195 } 2105 2106 } 2196 ); 2197 } 2198 catch (SQLException ex) 2199 { 2107 2200 2108 if (!silent) 2109 { 2110 System.out.println("================="); 2111 System.out.println(""); 2112 } 2113 } 2201 } 2202 2114 2203 } 2115 2204 -
trunk/src/core/net/sf/basedb/core/MappingBatcher.java
r4479 r4503 27 27 28 28 import net.sf.basedb.core.data.RawData; 29 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 29 30 import net.sf.basedb.core.query.Expressions; 30 31 import net.sf.basedb.core.query.Selects; 31 import net.sf.basedb.core.signal.SignalException;32 32 33 33 import java.util.List; … … 227 227 @throws BaseException If there is an error 228 228 */ 229 public int insert( AbstractSqlQuery query)229 public int insert(final AbstractSqlQuery query) 230 230 throws BaseException 231 231 { … … 246 246 247 247 // Holds the names of the parameters in the query in the order they appear 248 List<String> parameterOrder = new LinkedList<String>();248 final List<String> parameterOrder = new LinkedList<String>(); 249 249 250 250 // Parse out named parameters … … 255 255 try 256 256 { 257 PreparedStatement ps = HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql); 258 query.setParameters(ps, parameterOrder); 259 rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl()); 260 ps.close(); 257 rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 258 new ExecuteUpdateWork(getSessionControl(), insertSql) 259 { 260 @Override 261 public void setParameters(PreparedStatement ps) 262 throws SQLException 263 { 264 query.setParameters(ps, parameterOrder); 265 } 266 } 267 ); 261 268 bytes += rowsInserted * bytesPerRow; 262 269 totalInsertCount += rowsInserted; 263 }264 catch (InterruptedException ex)265 {266 throw new SignalException("Aborted by user.", ex);267 270 } 268 271 catch (SQLException ex) -
trunk/src/core/net/sf/basedb/core/PositionBatcher.java
r4479 r4503 27 27 28 28 import net.sf.basedb.core.data.ReporterData; 29 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 29 30 import net.sf.basedb.core.query.Expressions; 30 31 import net.sf.basedb.core.query.Selects; 31 import net.sf.basedb.core.signal.SignalException;32 32 33 33 import java.util.List; … … 223 223 @throws BaseException If there is an error 224 224 */ 225 public int insert( AbstractSqlQuery query)225 public int insert(final AbstractSqlQuery query) 226 226 throws BaseException 227 227 { … … 242 242 243 243 // Holds the names of the parameters in the query in the order they appear 244 List<String> parameterOrder = new LinkedList<String>();244 final List<String> parameterOrder = new LinkedList<String>(); 245 245 246 246 // Parse out named parameters … … 251 251 try 252 252 { 253 PreparedStatement ps = HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql); 254 query.setParameters(ps, parameterOrder); 255 rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl()); 256 ps.close(); 253 rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 254 new ExecuteUpdateWork(getSessionControl(), insertSql) 255 { 256 @Override 257 public void setParameters(PreparedStatement ps) 258 throws SQLException 259 { 260 query.setParameters(ps, parameterOrder); 261 } 262 } 263 ); 257 264 bytes += rowsInserted * bytesPerRow; 258 265 totalInsertCount += rowsInserted; 259 }260 catch (InterruptedException ex)261 {262 throw new SignalException("Aborted by user.", ex);263 266 } 264 267 catch (SQLException ex) -
trunk/src/core/net/sf/basedb/core/PositionExtraValueBatcher.java
r4479 r4503 26 26 package net.sf.basedb.core; 27 27 28 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 28 29 import net.sf.basedb.core.query.Expressions; 29 30 import net.sf.basedb.core.query.Selects; 30 import net.sf.basedb.core.signal.SignalException;31 31 32 32 import java.util.List; … … 317 317 @throws BaseException If there is an error 318 318 */ 319 public int insert( AbstractSqlQuery query)319 public int insert(final AbstractSqlQuery query) 320 320 throws BaseException 321 321 { … … 349 349 350 350 // Holds the names of the parameters in the query in the order they appear 351 List<String> parameterOrder = new LinkedList<String>();351 final List<String> parameterOrder = new LinkedList<String>(); 352 352 353 353 // Parse out named parameters … … 358 358 try 359 359 { 360 PreparedStatement ps = HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql); 361 query.setParameters(ps, parameterOrder); 362 rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl()); 363 ps.close(); 360 rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 361 new ExecuteUpdateWork(getSessionControl(), insertSql) 362 { 363 @Override 364 public void setParameters(PreparedStatement ps) 365 throws SQLException 366 { 367 query.setParameters(ps, parameterOrder); 368 } 369 } 370 ); 364 371 bytes += rowsInserted * bytesPerRow; 365 372 totalInsertCount += rowsInserted; 366 }367 catch (InterruptedException ex)368 {369 throw new SignalException("Aborted by user.", ex);370 373 } 371 374 catch (SQLException ex) -
trunk/src/core/net/sf/basedb/core/QueryExecutor.java
r4479 r4503 59 59 @base.modified $Date: 2007-08-17 09:18:29 +0200 (fr, 17 aug 2007) $ 60 60 */ 61 class QueryExecutor61 public class QueryExecutor 62 62 { 63 63 … … 88 88 @throws InterruptedException If this thread was interrupted 89 89 */ 90 static ResultSet executeQuery(PreparedStatement ps, SessionControl sc)90 public static ResultSet executeQuery(PreparedStatement ps, SessionControl sc) 91 91 throws InterruptedException 92 92 { … … 102 102 @throws InterruptedException If this thread was interrupted 103 103 */ 104 static int executeUpdate(PreparedStatement ps, SessionControl sc)104 public static int executeUpdate(PreparedStatement ps, SessionControl sc) 105 105 throws InterruptedException 106 106 { … … 119 119 @since 2.7 120 120 */ 121 static <T> ScrollIterator<T> loadIterator(Class<T> clazz, Query query,121 public static <T> ScrollIterator<T> loadIterator(Class<T> clazz, Query query, 122 122 StatelessSession session, SessionControl sc) 123 123 throws InterruptedException … … 139 139 @since 2.7 140 140 */ 141 static <T> T loadData(Class<T> clazz, Query query,141 public static <T> T loadData(Class<T> clazz, Query query, 142 142 StatelessSession session, SessionControl sc) 143 143 throws InterruptedException -
trunk/src/core/net/sf/basedb/core/RawDataBatcher.java
r4479 r4503 31 31 import net.sf.basedb.core.data.FeatureData; 32 32 import net.sf.basedb.core.data.ReporterData; 33 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 33 34 import net.sf.basedb.core.signal.SignalException; 34 35 import net.sf.basedb.core.signal.ThreadSignalHandler; … … 39 40 import java.util.Map; 40 41 import java.util.Set; 41 import java.sql.Statement;42 42 import java.sql.SQLException; 43 43 … … 634 634 { 635 635 if (debugSqlEnabled) logSql.debug("Deleting raw data: " + sql); 636 Statement s = HibernateUtil.getConnection(getDbControl().getHibernateSession()).createStatement(); 637 s.executeUpdate(sql); 636 HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 637 new ExecuteUpdateWork(getSessionControl(), sql) 638 ); 638 639 rawBioAssayData.setSpots(0); 639 640 rawBioAssayData.setBytes(0); -
trunk/src/core/net/sf/basedb/core/SpotBatcher.java
r4479 r4503 26 26 package net.sf.basedb.core; 27 27 28 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 28 29 import net.sf.basedb.core.query.Expressions; 29 30 import net.sf.basedb.core.query.Selects; 30 import net.sf.basedb.core.signal.SignalException;31 31 32 32 import java.util.List; … … 304 304 @throws BaseException If there is an error 305 305 */ 306 public int insert( AbstractSqlQuery query)306 public int insert(final AbstractSqlQuery query) 307 307 throws BaseException 308 308 { … … 330 330 331 331 // Holds the names of the parameters in the query in the order they appear 332 List<String> parameterOrder = new LinkedList<String>();332 final List<String> parameterOrder = new LinkedList<String>(); 333 333 334 334 // Parse out named parameters … … 339 339 try 340 340 { 341 PreparedStatement ps = HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql); 342 query.setParameters(ps, parameterOrder); 343 rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl()); 344 ps.close(); 341 rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 342 new ExecuteUpdateWork(getSessionControl(), insertSql) 343 { 344 @Override 345 public void setParameters(PreparedStatement ps) 346 throws SQLException 347 { 348 query.setParameters(ps, parameterOrder); 349 } 350 } 351 ); 345 352 bytes += rowsInserted * bytesPerRow; 346 353 totalInsertCount += rowsInserted; 347 }348 catch (InterruptedException ex)349 {350 throw new SignalException("Aborted by user.", ex);351 354 } 352 355 catch (SQLException ex) -
trunk/src/core/net/sf/basedb/core/SpotExtraValueBatcher.java
r4479 r4503 26 26 package net.sf.basedb.core; 27 27 28 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 28 29 import net.sf.basedb.core.query.Expressions; 29 30 import net.sf.basedb.core.query.Selects; 30 import net.sf.basedb.core.signal.SignalException;31 31 32 32 import java.util.List; … … 306 306 @throws BaseException If there is an error 307 307 */ 308 public int insert( AbstractSqlQuery query)308 public int insert(final AbstractSqlQuery query) 309 309 throws BaseException 310 310 { … … 338 338 339 339 // Holds the names of the parameters in the query in the order they appear 340 List<String> parameterOrder = new LinkedList<String>();340 final List<String> parameterOrder = new LinkedList<String>(); 341 341 342 342 // Parse out named parameters … … 347 347 try 348 348 { 349 PreparedStatement ps = HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql); 350 query.setParameters(ps, parameterOrder); 351 rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl()); 352 ps.close(); 349 rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 350 new ExecuteUpdateWork(getSessionControl(), insertSql) 351 { 352 @Override 353 public void setParameters(PreparedStatement ps) 354 throws SQLException 355 { 356 query.setParameters(ps, parameterOrder); 357 } 358 } 359 ); 353 360 bytes += rowsInserted * bytesPerRow; 354 361 totalInsertCount += rowsInserted; 355 }356 catch (InterruptedException ex)357 {358 throw new SignalException("Aborted by user.", ex);359 362 } 360 363 catch (SQLException ex) -
trunk/src/core/net/sf/basedb/core/Update.java
r4479 r4503 25 25 package net.sf.basedb.core; 26 26 27 import java.sql.Connection;28 import java.sql.DatabaseMetaData;29 27 import java.sql.SQLException; 30 import java.sql.Statement;31 28 import java.util.Arrays; 32 29 import java.util.Collections; … … 60 57 import net.sf.basedb.core.dbengine.DbEngine; 61 58 import net.sf.basedb.core.dbengine.TableInfo; 59 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 60 import net.sf.basedb.core.hibernate.TableInfoWork; 62 61 63 62 /** … … 1141 1140 try 1142 1141 { 1143 Connection connection = HibernateUtil.getConnection(session);1144 DatabaseMetaData metaData = null;1145 1142 TableInfo helpInfo = null; 1146 1143 try 1147 1144 { 1148 metaData = connection.getMetaData(); 1149 Table helpTable = new Table("HelpTexts"); 1150 helpInfo = new TableInfo(helpTable, metaData); 1145 helpInfo = HibernateUtil.doJdbcWork(session, 1146 new TableInfoWork(new Table("HelpTexts"))); 1151 1147 } 1152 1148 catch (SQLException ex) … … 1163 1159 try 1164 1160 { 1165 Statement st = connection.createStatement();1166 1161 log.info("Dropping index: " + sql); 1167 st.executeUpdate(sql); 1168 st.close(); 1162 HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql)); 1169 1163 } 1170 1164 catch (SQLException ex) … … 1322 1316 1323 1317 // Copy existing parameters 1324 Connection c = HibernateUtil.getConnection(session);1325 1318 try 1326 1319 { 1327 Statement s = c.createStatement();1328 1320 String sql = PredefinedQuery.getQueryString("COPY_PLUGINCONFIGURATION_PARAMETERS"); 1329 1321 /* … … 1333 1325 FROM `PluginConfigurationValues` 1334 1326 */ 1335 s.executeUpdate(sql);1327 HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql)); 1336 1328 1337 1329 sql = PredefinedQuery.getQueryString("DELETE_UNVERSIONED_PLUGINCONFIGURATION_PARAMETERS"); … … 1339 1331 DELETE FROM `PluginConfigurationValues` 1340 1332 */ 1341 s.executeUpdate(sql);1333 HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql)); 1342 1334 } 1343 1335 catch (SQLException ex) … … 1377 1369 { 1378 1370 tx = HibernateUtil.newTransaction(session); 1379 1380 Connection c = HibernateUtil.getConnection(session);1381 1371 try 1382 1372 { 1383 Statement s = c.createStatement();1384 1385 1373 // Change discriminator for file values 1386 1374 String sql = PredefinedQuery.getQueryString("CHANGE_FILE_DISCRIMINATOR"); … … 1390 1378 WHERE `discriminator` = 9 1391 1379 */ 1392 s.executeUpdate(sql);1380 HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql)); 1393 1381 1394 1382 sql = PredefinedQuery.getQueryString("COPY_FILE_PARAMETERS"); … … 1398 1386 FROM `FileValues` 1399 1387 */ 1400 s.executeUpdate(sql);1388 HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql)); 1401 1389 1402 1390 // Delete old file values … … 1405 1393 DELETE FROM `FileValues` 1406 1394 */ 1407 s.executeUpdate(sql);1395 HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql)); 1408 1396 } 1409 1397 catch (SQLException ex) … … 2204 2192 try 2205 2193 { 2206 Connection connection = HibernateUtil.getConnection(session);2207 DatabaseMetaData metaData = null;2208 2194 TableInfo info = null; 2209 2195 try 2210 2196 { 2211 metaData = connection.getMetaData(); 2212 Table table = new Table("Features"); 2213 info = new TableInfo(table, metaData); 2197 info = HibernateUtil.doJdbcWork(session, 2198 new TableInfoWork(new Table("Features"))); 2214 2199 } 2215 2200 catch (SQLException ex) … … 2227 2212 try 2228 2213 { 2229 Statement st = connection.createStatement();2230 2214 log.info("Dropping index: " + sql); 2231 st.executeUpdate(sql); 2232 st.close(); 2215 HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql)); 2233 2216 } 2234 2217 catch (SQLException ex) -
trunk/src/core/net/sf/basedb/core/VirtualDb.java
r4479 r4503 28 28 import net.sf.basedb.core.data.VirtualDbData; 29 29 import net.sf.basedb.core.data.SharedData; 30 import net.sf.basedb.core.signal.SignalException; 31 32 import java.sql.Connection; 33 import java.sql.PreparedStatement; 30 import net.sf.basedb.core.hibernate.ExecuteUpdateWork; 31 34 32 import java.sql.SQLException; 35 33 … … 331 329 throws BaseException 332 330 { 333 Connection c = HibernateUtil.getConnection(getDbControl().getHibernateSession());334 331 int rowsDeleted = 0; 335 332 for (VirtualTable table : VirtualTable.values()) … … 339 336 String tableName = table.getQualifiedTableName(this); 340 337 String sql = "DELETE FROM " + tableName + " WHERE "+ 341 HibernateUtil.quote("cube")+" = ?"; 342 PreparedStatement delete = null; 338 HibernateUtil.quote("cube")+" = " + cubeNo; 343 339 try 344 340 { 345 341 if (debugSqlEnabled) logSql.debug(sql); 346 delete = c.prepareStatement(sql); 347 delete.setShort(1, cubeNo); 348 rowsDeleted += QueryExecutor.executeUpdate(delete, getSessionControl()); 349 } 350 catch (InterruptedException ex) 351 { 352 throw new SignalException("Aborted by user.", ex); 342 rowsDeleted += HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 343 new ExecuteUpdateWork(getSessionControl(), sql)); 353 344 } 354 345 catch (SQLException ex) … … 357 348 throw new BaseException(ex); 358 349 } 359 finally360 {361 try362 {363 if (delete != null) delete.close();364 }365 catch (SQLException ex)366 {}367 }368 350 } 369 351 } … … 383 365 { 384 366 int rowsDeleted = 0; 385 Connection c = HibernateUtil.getConnection(getDbControl().getHibernateSession());386 367 if (HibernateUtil.virtualTableExists(this, VirtualTable.SPOT)) 387 368 { 388 369 String tableName = VirtualTable.SPOT.getQualifiedTableName(this); 389 370 String sql = "DELETE FROM " + tableName + " WHERE "+ 390 HibernateUtil.quote("cube")+" = ? AND "+ 391 HibernateUtil.quote("layer")+" = ?"; 392 PreparedStatement delete = null; 371 HibernateUtil.quote("cube")+" = " + cubeNo + " AND "+ 372 HibernateUtil.quote("layer")+" = " + layerNo; 393 373 try 394 374 { 395 375 if (debugSqlEnabled) logSql.debug(sql); 396 delete = c.prepareStatement(sql); 397 delete.setShort(1, cubeNo); 398 delete.setShort(2, layerNo); 399 rowsDeleted += QueryExecutor.executeUpdate(delete, getSessionControl()); 400 } 401 catch (InterruptedException ex) 402 { 403 throw new SignalException("Aborted by user.", ex); 376 rowsDeleted += HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 377 new ExecuteUpdateWork(getSessionControl(), sql)); 404 378 } 405 379 catch (SQLException ex) … … 407 381 logSql.error(sql, ex); 408 382 throw new BaseException(ex); 409 }410 finally411 {412 try413 {414 if (delete != null) delete.close();415 }416 catch (SQLException ex)417 {}418 383 } 419 384 } … … 433 398 { 434 399 int rowsDeleted = 0; 435 Connection c = HibernateUtil.getConnection(getDbControl().getHibernateSession());436 400 if (HibernateUtil.virtualTableExists(this, VirtualTable.FILTER)) 437 401 { 438 402 String tableName = VirtualTable.FILTER.getQualifiedTableName(this); 439 403 String sql = "DELETE FROM " + tableName + " WHERE "+ 440 HibernateUtil.quote("cube")+" = ?AND "+441 HibernateUtil.quote("filter")+" = ?";442 PreparedStatement delete = null;404 HibernateUtil.quote("cube")+" = " + cubeNo + " AND "+ 405 HibernateUtil.quote("filter")+" = " + filterNo; 406 443 407 try 444 408 { 445 409 if (debugSqlEnabled) logSql.debug(sql); 446 delete = c.prepareStatement(sql); 447 delete.setShort(1, cubeNo); 448 delete.setShort(2, filterNo); 449 rowsDeleted += QueryExecutor.executeUpdate(delete, getSessionControl()); 450 } 451 catch (InterruptedException ex) 452 { 453 throw new SignalException("Aborted by user.", ex); 410 rowsDeleted += HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 411 new ExecuteUpdateWork(getSessionControl(), sql)); 454 412 } 455 413 catch (SQLException ex) … … 457 415 logSql.error(sql, ex); 458 416 throw new BaseException(ex); 459 }460 finally461 {462 try463 {464 if (delete != null) delete.close();465 }466 catch (SQLException ex)467 {}468 417 } 469 418 } … … 484 433 { 485 434 int rowsDeleted = 0; 486 Connection c = HibernateUtil.getConnection(getDbControl().getHibernateSession());435 //Connection c = HibernateUtil.getConnection(getDbControl().getHibernateSession()); 487 436 if (HibernateUtil.virtualTableExists(this, extraTable)) 488 437 { 489 438 String tableName = extraTable.getQualifiedTableName(this); 490 String sql = "DELETE FROM " + tableName + " WHERE "+491 HibernateUtil.quote("cube")+" = ? AND "+492 HibernateUtil.quote("extra")+" = ?";493 PreparedStatement delete = null;439 final String sql = "DELETE FROM " + tableName + " WHERE "+ 440 HibernateUtil.quote("cube")+" = " + cubeNo + " AND " + 441 HibernateUtil.quote("extra")+" = " + extraNo; 442 494 443 try 495 444 { 496 445 if (debugSqlEnabled) logSql.debug(sql); 497 delete = c.prepareStatement(sql); 498 delete.setShort(1, cubeNo); 499 delete.setShort(2, extraNo); 500 rowsDeleted += QueryExecutor.executeUpdate(delete, getSessionControl()); 501 } 502 catch (InterruptedException ex) 503 { 504 throw new SignalException("Aborted by user.", ex); 446 rowsDeleted += HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), 447 new ExecuteUpdateWork(getSessionControl(), sql)); 505 448 } 506 449 catch (SQLException ex) … … 509 452 throw new BaseException(ex); 510 453 } 511 finally512 {513 try514 {515 if (delete != null) delete.close();516 }517 catch (SQLException ex)518 {}519 }520 454 } 521 455 return rowsDeleted;
Note: See TracChangeset
for help on using the changeset viewer.