tried to insert a database dump import method to the phpBB3 import function. Reason: imports or large database dumps are cannot be handled with phpMyAdmin and this should be an easy way to the database dumps into a mySQL database where it can be exported again with the phpBB3 content integration adapter. Completion or removal of this function stub will follow before next main release.
git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6065 6c8d7289-2bf4-0310-a012-ef5d649a1542pull/1/head
parent
945777aa80
commit
f348190566
@ -0,0 +1,99 @@
|
|||||||
|
// DatabaseConnection.java
|
||||||
|
// (C) 2009 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||||||
|
// first published 11.06.2009 on http://yacy.net
|
||||||
|
//
|
||||||
|
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
|
||||||
|
// $LastChangedRevision: 1986 $
|
||||||
|
// $LastChangedBy: orbiter $
|
||||||
|
//
|
||||||
|
// LICENSE
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
package de.anomic.content.dao;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
public class DatabaseConnection {
|
||||||
|
|
||||||
|
private Connection connection;
|
||||||
|
|
||||||
|
public DatabaseConnection(final String dbType, String host, int port, String dbname, String user, String pw) throws SQLException {
|
||||||
|
String dbDriverStr = null, dbConnStr = null;
|
||||||
|
if (dbType.equalsIgnoreCase("mysql")) {
|
||||||
|
dbDriverStr = "com.mysql.jdbc.Driver";
|
||||||
|
dbConnStr = "jdbc:mysql://" + host + ":" + port + "/" + dbname;
|
||||||
|
} else if (dbType.equalsIgnoreCase("pgsql")) {
|
||||||
|
dbDriverStr = "org.postgresql.Driver";
|
||||||
|
dbConnStr = "jdbc:postgresql://" + host + ":" + port + "/" + dbname;
|
||||||
|
} else throw new IllegalArgumentException();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Class.forName(dbDriverStr).newInstance();
|
||||||
|
} catch (final Exception e) {
|
||||||
|
throw new SQLException("Unable to load the jdbc driver: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.connection = DriverManager.getConnection(dbConnStr, user, pw);
|
||||||
|
} catch (final Exception e) {
|
||||||
|
throw new SQLException("Unable to establish a database connection: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAutoCommit(boolean b) {
|
||||||
|
try {
|
||||||
|
this.connection.setAutoCommit(b);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int count(String tablename) throws SQLException {
|
||||||
|
Statement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
stmt = this.connection.createStatement();
|
||||||
|
rs = stmt.executeQuery("select count(*) from " + tablename);
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getInt(1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
if (rs != null) try {rs.close();} catch (SQLException e) {}
|
||||||
|
if (stmt != null) try {stmt.close();} catch (SQLException e) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
if (connection != null) {
|
||||||
|
try {
|
||||||
|
connection.close();
|
||||||
|
connection = null;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Statement statement() throws SQLException {
|
||||||
|
return this.connection.createStatement();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
// PhpBB3Dao.java
|
||||||
|
// (C) 2009 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||||||
|
// first published 26.05.2009 on http://yacy.net
|
||||||
|
//
|
||||||
|
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
|
||||||
|
// $LastChangedRevision: 1986 $
|
||||||
|
// $LastChangedBy: orbiter $
|
||||||
|
//
|
||||||
|
// LICENSE
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
package de.anomic.content.dao;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
import de.anomic.kelondro.util.FileUtils;
|
||||||
|
|
||||||
|
public class ImportDump {
|
||||||
|
|
||||||
|
private DatabaseConnection conn = null;
|
||||||
|
|
||||||
|
public ImportDump(
|
||||||
|
String dbType,
|
||||||
|
String host,
|
||||||
|
int port,
|
||||||
|
String dbname,
|
||||||
|
String user,
|
||||||
|
String pw) throws Exception {
|
||||||
|
this.conn = new DatabaseConnection(dbType, host, port, dbname, user, pw);
|
||||||
|
this.conn.setAutoCommit(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void imp(File dump) throws SQLException {
|
||||||
|
Statement statement = null;
|
||||||
|
//int maxBatch = 1048576;
|
||||||
|
try {
|
||||||
|
statement = conn.statement();
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
FileUtils.copy(dump, baos);
|
||||||
|
|
||||||
|
String s = new String(baos.toByteArray(), "UTF-8");
|
||||||
|
int batchSize = 0;
|
||||||
|
int p, q;
|
||||||
|
String t;
|
||||||
|
loop: while (s.length() > 0) {
|
||||||
|
p = s.indexOf("INSERT INTO", 1);
|
||||||
|
q = s.indexOf("CREATE TABLE", 1);
|
||||||
|
if (q >= 0 && q < p) p = q;
|
||||||
|
if (p < 0) {
|
||||||
|
// finalize process
|
||||||
|
statement.executeBatch();
|
||||||
|
System.out.println(s);
|
||||||
|
statement.addBatch(s);
|
||||||
|
statement.executeBatch();
|
||||||
|
break loop;
|
||||||
|
}
|
||||||
|
t = s.substring(0, p);
|
||||||
|
s = s.substring(p);
|
||||||
|
//if (batchSize + t.length() >= maxBatch) {
|
||||||
|
statement.executeBatch();
|
||||||
|
batchSize = 0;
|
||||||
|
//}
|
||||||
|
System.out.println(t);
|
||||||
|
statement.addBatch(t);
|
||||||
|
batchSize += t.length();
|
||||||
|
}
|
||||||
|
statement.executeBatch();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new SQLException(e.getMessage());
|
||||||
|
} finally {
|
||||||
|
if (statement != null) try {statement.close();} catch (SQLException e) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void finalize() throws Throwable {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
this.conn.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue