<commit_before>/**
 *  Copyright (c) 2012, 2014 Sme.UP and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 * 
 * Contributors: 
 *   <NAME> - Initial API and implementation 
 */
package org.asup.db.core.base;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.asup.db.core.QCatalogContainer;
import org.asup.db.core.QConnectionConfig;
import org.asup.db.core.QDatabaseContainer;
import org.asup.db.syntax.QDefinitionWriter;
import org.asup.db.syntax.QDefinitionWriterRegistry;
import org.asup.db.syntax.QQueryWriter;
import org.asup.db.syntax.QQueryWriterRegistry;
import org.asup.fw.core.QContext;
import org.eclipse.datatools.modelbase.sql.schema.Schema;
import org.eclipse.datatools.modelbase.sql.tables.Table;

public class BaseDatabaseLoader {

	@Inject
	private QContext context;
	@Inject
	private QQueryWriterRegistry queryWriterRegistry;
	@Inject
	private QDefinitionWriterRegistry definitionWriterRegistry;

	
	public void loadDatabase(QDatabaseContainer databaseContainer) {	

			// prepare catalog containers
			List<QCatalogContainer> catalogContainers = new ArrayList<QCatalogContainer>();
			catalogContainers.addAll(databaseContainer.getCatalogContainers());
			
			for (QCatalogContainer catalogContainer : catalogContainers) {

				QConnectionConfig connectionConfig = catalogContainer.getConnectionConfig();
				
				// build catalog context
				QContext catalogContext = new BaseCatalogContextImpl(context.createChild(), catalogContainer.getName());
				QQueryWriter queryWriter = queryWriterRegistry.lookup(connectionConfig);
				catalogContext.set(QQueryWriter.class, queryWriter);
				QDefinitionWriter definitionWriter = definitionWriterRegistry.lookup(connectionConfig);
				catalogContext.set(QDefinitionWriter.class, definitionWriter);

				try {
					// initialize new container
					BaseCatalogContainerImpl newCatalogContainer = new BaseCatalogContainerImpl(catalogContext);
					newCatalogContainer.setName(catalogContainer.getName());
					newCatalogContainer.setConnectionConfig(catalogContainer.getConnectionConfig());
					newCatalogContainer.setSupportsGuestAccess(catalogContainer.isSupportsGuestAccess());
					newCatalogContainer.setSupportsRelativeRecordNumber(catalogContainer.isSupportsRelativeRecordNumber());					
					newCatalogContainer.initialize();
					
					// update references
					databaseContainer.getCatalogContainers().remove(catalogContainer);
					databaseContainer.getCatalogContainers().add(newCatalogContainer);
					if(databaseContainer.getDefaultCatalogContainer() == catalogContainer)
						databaseContainer.setDefaultCatalogContainer(newCatalogContainer);					

				} catch (SQLException e) {
					System.err.println(e.getMessage());
					continue;
				}

			}
			
//			printDatabase(databaseContainer);
	}
	
	@SuppressWarnings("unchecked")
	protected void printDatabase(QDatabaseContainer databaseContainer) {
		
		for(QCatalogContainer catalogContainer: databaseContainer.getCatalogContainers()) {
			System.out.println(catalogContainer);
			for(Schema schema: (List<Schema>)catalogContainer.getMetaData().getSchemas()) {
				System.out.println("\t"+schema);
				for(Table table: (List<Table>)schema.getTables()) {
					System.out.println("\t\t"+table);
				}
			}
		}		
	}
}<commit_msg>Correzione errore se database inesistente	<commit_after>/**
 *  Copyright (c) 2012, 2014 Sme.UP and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 * 
 * Contributors: 
 *   <NAME> - Initial API and implementation 
 */
package org.asup.db.core.base;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.asup.db.core.QCatalogContainer;
import org.asup.db.core.QConnectionConfig;
import org.asup.db.core.QDatabaseContainer;
import org.asup.db.syntax.QDefinitionWriter;
import org.asup.db.syntax.QDefinitionWriterRegistry;
import org.asup.db.syntax.QQueryWriter;
import org.asup.db.syntax.QQueryWriterRegistry;
import org.asup.fw.core.QContext;
import org.eclipse.datatools.modelbase.sql.schema.Schema;
import org.eclipse.datatools.modelbase.sql.tables.Table;

public class BaseDatabaseLoader {

	@Inject
	private QContext context;
	@Inject
	private QQueryWriterRegistry queryWriterRegistry;
	@Inject
	private QDefinitionWriterRegistry definitionWriterRegistry;

	
	public void loadDatabase(QDatabaseContainer databaseContainer) {	

			// prepare catalog containers
			List<QCatalogContainer> catalogContainers = new ArrayList<QCatalogContainer>();
			catalogContainers.addAll(databaseContainer.getCatalogContainers());
			
			for (QCatalogContainer catalogContainer : catalogContainers) {

				QConnectionConfig connectionConfig = catalogContainer.getConnectionConfig();
				
				// build catalog context
				QContext catalogContext = new BaseCatalogContextImpl(context.createChild(), catalogContainer.getName());
				QQueryWriter queryWriter = queryWriterRegistry.lookup(connectionConfig);
				catalogContext.set(QQueryWriter.class, queryWriter);
				QDefinitionWriter definitionWriter = definitionWriterRegistry.lookup(connectionConfig);
				catalogContext.set(QDefinitionWriter.class, definitionWriter);

				try {
					// initialize new container
					BaseCatalogContainerImpl newCatalogContainer = new BaseCatalogContainerImpl(catalogContext);
					newCatalogContainer.setName(catalogContainer.getName());
					newCatalogContainer.setConnectionConfig(catalogContainer.getConnectionConfig());
					newCatalogContainer.setSupportsGuestAccess(catalogContainer.isSupportsGuestAccess());
					newCatalogContainer.setSupportsRelativeRecordNumber(catalogContainer.isSupportsRelativeRecordNumber());					
					newCatalogContainer.initialize();
					
					// update references
					databaseContainer.getCatalogContainers().remove(catalogContainer);
					databaseContainer.getCatalogContainers().add(newCatalogContainer);
					if(databaseContainer.getDefaultCatalogContainer() == catalogContainer)
						databaseContainer.setDefaultCatalogContainer(newCatalogContainer);					

				} catch (Exception e) {
					System.err.println(e.getMessage());
					continue;
				}

			}
			
//			printDatabase(databaseContainer);
	}
	
	@SuppressWarnings("unchecked")
	protected void printDatabase(QDatabaseContainer databaseContainer) {
		
		for(QCatalogContainer catalogContainer: databaseContainer.getCatalogContainers()) {
			System.out.println(catalogContainer);
			for(Schema schema: (List<Schema>)catalogContainer.getMetaData().getSchemas()) {
				System.out.println("\t"+schema);
				for(Table table: (List<Table>)schema.getTables()) {
					System.out.println("\t\t"+table);
				}
			}
		}		
	}
}
{'fraction_non_alphanumeric': 0.09456797072896145, 'fraction_numerical': 0.0033774275260343373, 'mean_word_length': 5.18, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2371750', 'n_tokens_mistral': 2192, 'n_tokens_neox': 1900, 'n_words': 343}