How can I use JDBC to copy schema from one database to another without using Apache DDLUtils?
Asked Answered
F

1

1

I have a database in MySQL, and I want to programatically create all the same tables and fields in FileMaker Pro. I can do this myself using JDBC, but I was hoping that there was already a library that would do this.

I looked into DDLUtils from Apache, but I'm not able to figure out how to build it (it uses Maven for a build system and I get a fatal error when I try to build it).

Forereach answered 21/11, 2011 at 0:57 Comment(2)
There is a potentially better answer here, but it involves DDLUtils which didn't work for the OP: #11878534Curtin
Furthermore, there should be no need to build DdlUtils yourself as the download page has an option to download a pre-built JAR file.Eisen
F
2

I wrote the code myself, didn't turn out to be that hard. This is for generating the schema to MySQL; I haven't tested for FileMaker but it should be very similar.

    package com.prosc.db;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA. User: jesse Date: 11/21/11 Time: 1:34 PM
 */
public abstract class SchemaGenerator {
    private static final Logger log = Logger.getLogger( SchemaGenerator.class.getName() );

    public String createDatabaseSql( String databasename ) {
        return "CREATE DATABASE " + databasename;
    }

    /** This should return the String required to create a new table
     * @param tableName The name of the table in the source database. This is also used as the name of the table to generate.
     * @param metaData A JDBC metadata object from the source database (where the schema is being created from, not where it's being written to)
     * */
    public String createTableSql( DatabaseMetaData metaData, String tableName ) throws SQLException {
        ResultSet columnsMetadata = metaData.getColumns( null, null, tableName, null );
        Set<String> pkNames = new HashSet<String>(3);

        try {
            ResultSet pkResultSet = metaData.getPrimaryKeys( null, null, tableName );
            while( pkResultSet.next() ) {
                pkNames.add( pkResultSet.getString( 4 ) );
            }
        } catch( Exception e ) {
            log.log( Level.WARNING, "Could not determine primary keys; will need to be manually configured", e );
        }

        StringBuffer sql = new StringBuffer();
        sql.append( "CREATE TABLE " + tableName + " (" );
        String delim = "";
        while( columnsMetadata.next() ) {
            sql.append( delim );
            delim = ", ";

            String columnName = columnsMetadata.getString( 4 );
            String sqlTypename = columnsMetadata.getString(6);
            int typeCode = columnsMetadata.getInt( 5 );
            String targetTypename = targetTypename( sqlTypename, typeCode );
            if( targetTypename == null ) {
                log.info( "Unknown typename for type code " + typeCode + "; SQL type name is " + sqlTypename );
                continue;
            }
            int columnSize = columnsMetadata.getInt( 7 );
            boolean nullsProhibited = "NO".equalsIgnoreCase( columnsMetadata.getString(18) );
            int precision = columnsMetadata.getInt( 9 );
            appendFieldCreationClause( sql, columnName, targetTypename, columnSize, precision, nullsProhibited, pkNames.contains( columnName ), metaData.getIdentifierQuoteString(), columnsMetadata );
        }
        sql.append( ")" );
        return sql.toString();
    }

    public void appendFieldCreationClause( StringBuffer buffer, String columnName, String targetTypename, int columnSize, int precision, boolean nullsProhibited, boolean isPrimaryKey, String identifierQuoteString, ResultSet columnMetaData ) {
        if( isPrimaryKey ) {
            targetTypename = "INT";
            precision = 0;
        }
        buffer.append( identifierQuoteString + columnName + identifierQuoteString + " " + targetTypename );
        if( columnSize > 0 ) {
            buffer.append( "(" + columnSize );
            if( precision > 0 ) {
                buffer.append( "," + precision );
            }
            buffer.append( ")" );
        }
        if( "id".equalsIgnoreCase( columnName ) ) {
            buffer.append( " UNIQUE" );
        }
        if( nullsProhibited ) {
            buffer.append( " NOT NULL" );
        }
        if( isPrimaryKey ) {
            buffer.append( " AUTO_INCREMENT" );
            buffer.append( " PRIMARY KEY" );
        }
    }

    public String targetTypename( String sqlTypename, int typeCode ) {
        return sqlTypename;
    }
}
Forereach answered 22/11, 2011 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.