#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab 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 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab 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 HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Globalization;
using HeuristicLab.DataImporter.DbExplorer.Interfaces;
namespace HeuristicLab.DataImporter.DbExplorer.Oracle {
public class OracleExplorer : DbExplorerBase {
private enum NumericDataType { Number, Binary_Float, Binary_Double, Smallint, Int, bigint, Float, Real }
public override bool IsNumericDataType(string type) {
NumericDataType numericDataType;
return Enum.TryParse(type, true, out numericDataType);
}
private enum DateDataType { Date, TimeStamp } //caution TimeStamp With Time Zone, With Local Time Zone also available
public override bool IsDateDataType(string type) {
DateDataType dateDataType;
return Enum.TryParse(type, true, out dateDataType);
}
private enum StringDataType { Char, NChar, Varchar, Varchar2, NVarchar2 }
public override bool IsStringDataType(string type) {
StringDataType stringDataType;
return Enum.TryParse(type, true, out stringDataType);
}
public override bool IsFilterableDataType(string type) {
return IsNumericDataType(type) || IsDateDataType(type) || IsStringDataType(type);
}
public override string MenuItemDescription {
get { return "Connect to Oracle"; }
}
private OracleConnectionWizard wizard;
protected override IDbConnectionWizard DbConnectionWizard {
get {
if (wizard == null)
wizard = new OracleConnectionWizard();
return wizard;
}
}
public override IEnumerable AllTables {
get {
if (Connection == null) {
throw new InvalidOperationException("Connection to database not correctly set!");
}
List allTables = new List();
using (DbConnection conn = Connection.CreateConnection()) {
conn.Open();
using (DbCommand cmd = conn.CreateCommand()) {
cmd.CommandText = @"select owner,table_name from all_tables where lower(owner) not like '%sys%' union " +
@"select owner,view_name from all_views where lower(owner) not like '%sys%' order by owner,table_name";
using (DbDataReader reader = cmd.ExecuteReader()) {
while (reader.Read()) {
allTables.Add(new DbTable(reader.GetString(0), reader.GetString(1)));
}
reader.Close();
}//end using reader
}//end using command
//fill columns for table
foreach (DbTable table in allTables) {
using (DbCommand cmd = conn.CreateCommand()) {
cmd.CommandText = @"select column_name,data_type from all_tab_columns where table_name = '" + table.TableName + "' and owner = '" + table.OwnerName + "'";
using (DbDataReader reader = cmd.ExecuteReader()) {
while (reader.Read()) {
table.AddColumn(new DbColumn(reader.GetString(0), reader.GetString(1)));
}
reader.Close();
}//end using reader
}//end using command
}//end foreach table
conn.Close();
}//end using connection
allTables.Sort(new DbTableComparator());
return allTables;
}
}
protected override string ConvertDate(DateTime date) {
string ret = date.ToString(CultureInfo.InvariantCulture);
ret = "to_date('" + ret + "','MM/DD/YYYY HH24:MI:SS')";
return ret;
}
protected override string ConvertDouble(double value) {
return value.ToString(CultureInfo.InvariantCulture);
}
}
}