Index: topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java diff -u topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java:1.29 topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java:1.30 --- topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java:1.29 Wed Aug 30 14:59:04 2006 +++ topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java Thu Sep 7 09:54:31 2006 @@ -23,15 +23,16 @@ * * @author poussin * - * @version $Revision: 1.29 $ + * @version $Revision: 1.30 $ * - * Last update: $Date: 2006/08/30 14:59:04 $ by : $Author: bpoussin $ + * Last update: $Date: 2006/09/07 09:54:31 $ by : $Author: bpoussin $ */ package org.codelutin.topia.framework; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; +import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; @@ -39,10 +40,12 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; import java.io.Writer; +import java.sql.Connection; import java.sql.Statement; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -1521,6 +1524,7 @@ */ public void restore(File file) throws TopiaException { checkClosed("Ce contexte a ete ferme, impossible d'effectuer le restore"); + String sql = null; try { // decompresse file in temporary file InputStream in = new BufferedInputStream(new FileInputStream(file)); @@ -1531,36 +1535,57 @@ int magic = ((int)in.read() << 8) | b; in.reset(); - // by default we supposed that file is not compressed - File tmp = file; if (magic == GZIPInputStream.GZIP_MAGIC) { - // in fact file is compressed, use temporaly file - tmp = File.createTempFile("tmp-topia-restore", ".sql"); - tmp.deleteOnExit(); - - // decompresse file in temporary file - InputStream gzin = new GZIPInputStream(in); + in = new GZIPInputStream(in); + } - OutputStream out = new BufferedOutputStream( - new FileOutputStream(tmp)); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + Connection conn = getHibernate().connection(); + conn.setAutoCommit(false); + Statement stat = conn.createStatement(); - byte [] buffer = new byte[64 * 1024]; - int len = 0; - while ((len = gzin.read(buffer)) != -1) { - out.write(buffer, 0, len); + while((sql = reader.readLine()) != null) { + // FIXME remove next line when h2 will do the bug correction (normaly in october 2006) + // this permit database restauration + if (sql.startsWith("ALTER TABLE")) { + sql = sql.replaceAll("INDEX CONSTRAINT_INDEX_\\d*", ""); } - out.close(); - gzin.close(); + stat.execute(sql); } + stat.close(); + conn.commit(); + conn.close(); - // restore data - Statement stat = getHibernate().connection().createStatement(); - stat.execute("RUNSCRIPT FROM '" + tmp.getAbsolutePath() + "'"); - -// SQLQuery query = getHibernate().createSQLQuery("RUNSCRIPT FROM '" + file.getAbsolutePath() + "'"); -// List result = query.list(); +// // by default we supposed that file is not compressed +// File tmp = file; +// if (magic == GZIPInputStream.GZIP_MAGIC) { +// // in fact file is compressed, use temporaly file +// tmp = File.createTempFile("tmp-topia-restore", ".sql"); +// tmp.deleteOnExit(); +// +// // decompresse file in temporary file +// InputStream gzin = new GZIPInputStream(in); +// +// OutputStream out = new BufferedOutputStream( +// new FileOutputStream(tmp)); +// +// byte [] buffer = new byte[64 * 1024]; +// int len = 0; +// while ((len = gzin.read(buffer)) != -1) { +// out.write(buffer, 0, len); +// } +// out.close(); +// gzin.close(); +// } +// +// // restore data +// Statement stat = getHibernate().connection().createStatement(); +// stat.execute("RUNSCRIPT FROM '" + tmp.getAbsolutePath() + "'"); +// +//// SQLQuery query = getHibernate().createSQLQuery("RUNSCRIPT FROM '" + file.getAbsolutePath() + "'"); +//// List result = query.list(); } catch (Exception eee) { - throw new TopiaException("Can't restore", eee); + throw new TopiaException("Can't restore. Last sql instruction was:" + sql, eee); } }