Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.DbExplorer.Interfaces/DbExplorerBase.cs @ 17832

Last change on this file since 17832 was 16994, checked in by gkronber, 5 years ago

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

File size: 10.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Data.Common;
26using System.Windows.Forms;
27using System.ComponentModel;
28using HeuristicLab.DataImporter.Data.Model;
29
30namespace HeuristicLab.DataImporter.DbExplorer.Interfaces {
31  public abstract class DbExplorerBase : IDbExplorer {
32
33    private DbConnectionBase connection;
34    protected DbConnectionBase Connection {
35      get { return this.connection; }
36      set { this.connection = value; }
37    }
38    public bool IsConnected {
39      get {
40        if (connection != null)
41          return connection.TestConnection();
42        return false;
43      }
44    }
45
46    protected virtual string EscapeSqlString(string s) {
47      return s;
48    }
49
50    public abstract string MenuItemDescription { get; }
51    public abstract bool IsDateDataType(string type);
52    public abstract bool IsNumericDataType(string type);
53    public abstract bool IsStringDataType(string type);
54    public abstract bool IsFilterableDataType(string type);
55    protected abstract string ConvertDate(DateTime date);
56    protected abstract string ConvertDouble(double value);
57    protected abstract IDbConnectionWizard DbConnectionWizard { get; }
58
59    private ProgressChangedEventHandler newRowLoaded;
60    public ProgressChangedEventHandler NewRowLoaded {
61      get { return newRowLoaded; }
62      set { newRowLoaded = value; }
63    }
64
65    private DbTable actTable;
66    private void FireNewRowLoaded(int actRow) {
67      if (newRowLoaded != null && actTable != null && actTable.AffectedRows != 0)
68        newRowLoaded(this, new ProgressChangedEventArgs(actRow * 100 / actTable.AffectedRows
69          , "Loading " + actRow + " / " + actTable.AffectedRows + " rows of table " + actTable.TableName));
70    }
71
72    public void ShowDbConnectionWizard() {
73      if (DbConnectionWizard.ShowDialog() == DialogResult.OK) {
74        if (!DbConnectionWizard.Connection.TestConnection()) {
75          this.connection = null;
76          HandleConnectionError();
77        } else
78          connection = DbConnectionWizard.Connection;
79      }
80    }
81
82    private void HandleConnectionError() {
83      DialogResult result = MessageBox.Show(DbConnectionWizard.Connection.ErrorMessage,
84        "Connect to database failed", MessageBoxButtons.RetryCancel);
85      if (result == DialogResult.Retry)
86        ShowDbConnectionWizard();
87    }
88    public abstract IEnumerable<DbTable> AllTables { get; }
89
90    public virtual void FillMinMaxValuesForTable(DbTable table) {
91      using (DbConnection conn = connection.CreateConnection()) {
92        conn.Open();
93        List<DbColumn> filterColumns = new List<DbColumn>();
94        using (DbCommand cmd = conn.CreateCommand()) {
95          string cmdText = @"select ";
96          foreach (DbColumn col in table.Columns)
97            if (this.IsFilterableDataType(col.SqlDataType) && !this.IsStringDataType(col.SqlDataType)) {
98              cmdText += @"min(" + EscapeSqlString(col.ColumnName) + "), max(" + EscapeSqlString(col.ColumnName) + "), ";
99              filterColumns.Add(col);
100            }
101          cmdText += "count(*) from " + EscapeSqlString(table.OwnerName) + "." + EscapeSqlString(table.TableName);
102          cmd.CommandText = cmdText;
103
104          using (DbDataReader reader = cmd.ExecuteReader()) {
105            reader.Read();
106            for (int i = 0; i < reader.FieldCount - 1; i++) {
107              if (i % 2 == 0)
108                filterColumns[i / 2].MinValue = reader.GetValue(i).ToString();
109              else
110                filterColumns[i / 2].MaxValue = reader.GetValue(i).ToString();
111            }
112            table.TotalRows = reader.GetInt32(reader.FieldCount - 1);
113            reader.Close();
114          }//end using reader
115          // cmd.Dispose();
116        }//end using command
117        conn.Close();
118      }//end using connection
119      table.UpdateInformationLoaded();
120    }
121
122    public void FillAffectedRowsForTable(DbTable table) {
123      string cmdText = @"select count(*) from " + EscapeSqlString(table.OwnerName) + "." + EscapeSqlString(table.TableName) + " where ";
124      bool executeSql = false;
125      foreach (DbColumn col in table.SelectedColumns) {
126        if (col.FilterColumn) {
127          executeSql = true;
128          if (IsNumericDataType(col.SqlDataType)) {
129            string min = this.ConvertDouble(double.Parse(col.MinValue));
130            string max = this.ConvertDouble(double.Parse(col.MaxValue));
131            cmdText += EscapeSqlString(col.ColumnName) + ">=" + min + " and " + EscapeSqlString(col.ColumnName) + "<=" + max + " and ";
132          } else if (IsDateDataType(col.SqlDataType)) {
133            string x = this.ConvertDate(DateTime.Parse(col.MinValue));
134            string y = this.ConvertDate(DateTime.Parse(col.MaxValue));
135            cmdText += EscapeSqlString(col.ColumnName) + ">=" + x + " and " + EscapeSqlString(col.ColumnName) + "<=" + y + " and ";
136          } else if (IsStringDataType(col.SqlDataType))
137            cmdText += EscapeSqlString(col.ColumnName) + " like '%" + EscapeSqlString(col.LikeValue) + "%' and ";
138        }
139      }
140      if (!executeSql) {
141        if (table.IsSelected)
142          table.AffectedRows = table.TotalRows;
143        else
144          table.AffectedRows = 0;
145      } else {
146        cmdText = cmdText.Remove(cmdText.Length - 5);
147        using (DbConnection conn = Connection.CreateConnection()) {
148          conn.Open();
149          using (DbCommand cmd = conn.CreateCommand()) {
150            cmd.CommandText = cmdText;
151            table.AffectedRows = Convert.ToInt32(cmd.ExecuteScalar());
152          }//end using cmd
153          conn.Close();
154        }//end using connection
155      }
156    }
157
158    public ColumnGroup CreateColumnGroup(DbTable table) {
159      //select clause
160      string sqlString = "select ";
161      foreach (DbColumn col in table.SelectedColumns) {
162        sqlString += EscapeSqlString(table.TableName) + "." + EscapeSqlString(col.ColumnName) + ", ";
163      }
164      sqlString = sqlString.Remove(sqlString.Length - 2);
165      sqlString += " from " + EscapeSqlString(table.OwnerName) + "." + EscapeSqlString(table.TableName);
166
167      //where clause
168      bool addedWhereclause = false;
169      foreach (DbColumn col in table.SelectedColumns) {
170        if (col.FilterColumn) {
171          if (!addedWhereclause) {
172            sqlString += " where ";
173            addedWhereclause = true;
174          }
175          if (IsNumericDataType(col.SqlDataType))
176            sqlString += EscapeSqlString(col.ColumnName) + ">=" + col.MinValue + " and " + EscapeSqlString(col.ColumnName) + "<=" + col.MaxValue + " and ";
177          else if (IsDateDataType(col.SqlDataType)) {
178            string x = this.ConvertDate(DateTime.Parse(col.MinValue));
179            string y = this.ConvertDate(DateTime.Parse(col.MaxValue));
180            sqlString += EscapeSqlString(col.ColumnName) + ">=" + x + " and " + EscapeSqlString(col.ColumnName) + "<=" + y + " and ";
181          } else if (IsStringDataType(col.SqlDataType))
182            sqlString += EscapeSqlString(col.ColumnName) + " like '%" + EscapeSqlString(col.LikeValue) + "%' and ";
183        }
184      }
185      if (addedWhereclause)
186        sqlString = sqlString.Remove(sqlString.Length - 5);
187
188      this.actTable = table;
189      ColumnGroup columnGroup = CreateColumnGroup(sqlString);
190      columnGroup.Name = table.TableName;
191      this.actTable = null;
192      return columnGroup;
193    }
194
195    public ColumnGroup CreateColumnGroup(string sqlString) {
196      ColumnGroup columnGroup = new ColumnGroup();
197      bool initColumns = true;
198      string dbDataType;
199      int actRow = 0;
200
201      using (DbConnection conn = connection.CreateConnection()) {
202        conn.Open();
203        using (DbCommand cmd = conn.CreateCommand()) {
204          cmd.CommandText = sqlString;
205          using (DbDataReader reader = cmd.ExecuteReader()) {
206            while (reader.Read()) {
207              //create & initialize columns
208              if (initColumns) {
209                for (int i = 0; i < reader.FieldCount; i++) {
210                  dbDataType = reader.GetDataTypeName(i);
211                  if (IsDateDataType(dbDataType))
212                    columnGroup.AddColumn(new DateTimeColumn(reader.GetName(i)));
213                  else if (IsNumericDataType(dbDataType))
214                    columnGroup.AddColumn(new DoubleColumn(reader.GetName(i)));
215                  else
216                    columnGroup.AddColumn(new StringColumn(reader.GetName(i)));
217                }
218                initColumns = false;
219              }
220              //fill columns with values
221              for (int i = 0; i < reader.FieldCount; i++) {
222                if (reader.IsDBNull(i)) {
223                  columnGroup.Columns.ElementAt(i).AddValue(null);
224                } else if (columnGroup.Columns.ElementAt(i).DataType == typeof(double?)) {
225                  try { columnGroup.Columns.ElementAt(i).AddValue(Convert.ToDouble(reader.GetValue(i))); }
226                  catch (DbException) { columnGroup.Columns.ElementAt(i).AddValue(null); }
227                } else if (columnGroup.Columns.ElementAt(i).DataType == typeof(DateTime?)) {
228                  try { columnGroup.Columns.ElementAt(i).AddValue(reader.GetDateTime(i)); }
229                  catch (DbException) { columnGroup.Columns.ElementAt(i).AddValue(null); }
230                } else {
231                  try { columnGroup.Columns.ElementAt(i).AddValue(reader.GetValue(i).ToString()); }
232                  catch (DbException) { columnGroup.Columns.ElementAt(i).AddValue(null); }
233                }
234              }
235              actRow++;
236              if (actRow % 1000 == 0)
237                FireNewRowLoaded(actRow);
238            }
239            if (initColumns) {
240              for (int i = 0; i < reader.FieldCount; i++) {
241                if (reader.GetName(i).ToUpper().Contains("ZEIT"))
242                  columnGroup.AddColumn(new DateTimeColumn(reader.GetName(i)));
243                else
244                  columnGroup.AddColumn(new DoubleColumn(reader.GetName(i)));
245              }
246            }
247            reader.Close();
248          }  //end using reader
249        } //end using command
250      } // using connection
251
252      return columnGroup;
253    }
254  }
255}
Note: See TracBrowser for help on using the repository browser.