package org.gcube.dataanalysis.databases.structure; import java.util.List; import org.gcube.dataanalysis.databases.utils.ConnectionManager; import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; import org.hibernate.SessionFactory; /** Class that allows to manage the MySQL database. */ public class MySQLTableStructure extends AbstractTableStructure { public MySQLTableStructure(String Databasename, String TableName, SessionFactory dbSession) throws Exception { super(Databasename, TableName, dbSession, false); } // Method that executes the query "show create table" in order to retrieve // the "create table" statement public String showCreateTable(ConnectionManager connection, SessionFactory dbSession) throws Exception { // Retrieve the query String queryForIndexes = getQueryForIndexes(dbSession); try { // List indexSet = DatabaseFactory.executeSQLQuery( // String.format(queryForIndexes, tableName), dbSession); List indexSet = connection.executeQuery(String.format(queryForIndexes, tableName), dbSession); String createTableStatement = (String) (((Object[]) indexSet.get(0))[1]); AnalysisLogger.getLogger().debug( "MySQLTableStructure->'Create Table' statement: " + createTableStatement); return createTableStatement; } catch (Exception e) { throw e; } } // Method that returns the query to build the table's structure. This method // is not useful for mysql. @Override protected String getQueryForTableStructure(SessionFactory dbSession) throws Exception { // TODO Auto-generated method stub String queryForStructure = "SELECT table_schema,table_name,column_name,column_default,is_nullable,data_type,character_maximum_length,character_set_name,column_type,column_key FROM information_schema.COLUMNS WHERE table_name ='%1$s' and table_schema='%2$s';"; return queryForStructure; } // Method that returns the query to show the create statement @Override protected String getQueryForIndexes(SessionFactory dbSession) throws Exception { // TODO Auto-generated method stub String queryForIndexes = "SHOW CREATE TABLE `%1$s`;"; return queryForIndexes; } // This method is not useful for the database mysql because the slq query // "show create" makes available the create table statement. @Override protected void buildStructure(SessionFactory dbSession) throws Exception { // //retrieve the query // String queryForStructure=getQueryForTableStructure(dbSession); // // String queryStructure = // String.format(queryForStructure,tableName,databaseName); // List resultSet = // DatabaseFactory.executeSQLQuery(queryStructure, dbSession); // // // AnalysisLogger.getLogger().debug("MySQLTableStructure->Building Structure with query: "+queryStructure); // // int resultsNumber = resultSet.size(); // // for (int i=0;i0)) // // // System.err.println("MySQLTableStructure->KEY NOT CONTEMPLATED : "+columnKey); // // /* // if (columnKey.equals("PRI")) // ColumnKeys.add(columnname); // else if (columnKey.equals("UNI")) // UniqueKeys.add(columnname); // else if (columnKey.equals("MUL")) // UniqueKeys.add(columnname); // // else if ((columnKey != null) && (columnKey.length()>0)) // System.err.println("MySQLTableStructure->KEY NOT CONTEMPLATED : "+columnKey); // */ // } catch (Exception e) { // e.printStackTrace(); // } // } // // // parseIndexes2(dbSession); // //fill up the indexes array // // //***Commentata // // // for (String index:Indexes){ // //// // AnalysisLogger.getLogger().debug("unique index removal: "+index); // // //eliminate the unicity // // UniqueKeys.remove(index); // // } } // This method is not useful for the database mysql because the slq query // "show create" makes available the create table statement. // private void parseIndexes2 (SessionFactory dbSession) throws Exception{ // // // //Retrieve the query // String queryForIndexes=getQueryForIndexes(dbSession); // // List indexSet = // DatabaseFactory.executeSQLQuery(String.format(queryForIndexes,tableName), // dbSession); // // String createTableStatement = (String)(((Object[])indexSet.get(0))[1]); // String [] splitted = createTableStatement.split("\n"); // // for (int i=0;i