#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.MsSqlServer { public class SqlServerExplorer : DbExplorerBase { protected override string EscapeSqlString(string s) { return "[" + s + "]"; } private enum NumericDataType { Numeric, Decimal, Tinyint, Smallint, Int, bigint, Float, Real } public override bool IsNumericDataType(string type) { try { Enum.Parse(typeof(NumericDataType), type, true); return true; } catch (ArgumentException) { } return false; } private enum DateDataType { DateTime, SmallDateTime } public override bool IsDateDataType(string type) { try { Enum.Parse(typeof(DateDataType), type, true); return true; } catch (ArgumentException) { } return false; } private enum StringDataType { Char, NChar, Varchar, NVarchar } public override bool IsStringDataType(string type) { try { Enum.Parse(typeof(StringDataType), type, true); return true; } catch (ArgumentException) { } return false; } public override bool IsFilterableDataType(string type) { return IsNumericDataType(type) || IsDateDataType(type) || IsStringDataType(type); } protected override string ConvertDate(DateTime date) { string ret = "'" + date.ToString(CultureInfo.InvariantCulture) + "'"; return ret; } protected override string ConvertDouble(double value) { return value.ToString(CultureInfo.InvariantCulture); } public override string MenuItemDescription { get { return "Connect to Microsoft SqlServer"; } } private SqlServerConnectionWizard wizard; protected override IDbConnectionWizard DbConnectionWizard { get { if (wizard == null) wizard = new SqlServerConnectionWizard(); 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 table_schema,table_name from INFORMATION_SCHEMA.tables"; using (DbDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { allTables.Add(new DbTable(reader.GetString(0), reader.GetString(1))); } reader.Close(); }//end using reader cmd.Dispose(); }//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 INFORMATION_SCHEMA.columns where table_name = '" + table.TableName + "'"; 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; } } } }