public QueryStore(string sql, ParameterStoreCollection parameters) { _sql = sql; Parameters = new ParameterStoreCollection(); foreach (ParameterStore p in parameters) { Parameters.Add(p.Clone()); } }
private void AddToHistory(SQLPart sql, ParameterStoreCollection parameters) { QueryStore q = new QueryStore(sql.SQL, parameters); string dir = System.IO.Path.GetDirectoryName(_historyPath); Directory.CreateDirectory(dir); using (StreamWriter wr = new StreamWriter(_historyPath, true, Encoding.UTF8)) { q.WriteToStream(wr); } }
private LogListBoxItem NewLogListBoxItem(string text, string sql, ParameterStoreCollection parameters, LogStatus status, bool notice, Tuple <int, int> errorPos) { LogListBoxItem item = new LogListBoxItem(); item.Time = DateTime.Now; item.Status = status; item.Message = text; item.Sql = sql; item.Parameters = parameters; item.ErrorPosition = errorPos; return(item); }
private void AddLog(string text, string sql, ParameterStoreCollection parameters, LogStatus status, bool notice, Tuple <int, int> errorPos = null) { LogListBoxItem item = NewLogListBoxItem(text, sql, parameters, status, notice, errorPos); item.RedoSql += Item_RedoSql; listBoxLog.Items.Add(item); listBoxLog.SelectedItem = item; listBoxLog.ScrollIntoView(item); if (notice) { tabControlResult.SelectedItem = tabItemLog; } if (status == LogStatus.Error && errorPos != null) { ErrorListBoxItem err = NewErrorListBoxItem(text, errorPos); err.MouseDoubleClick += ListBoxErrors_MouseDoubleClick; listBoxErrors.Items.Add(err); listBoxErrors.SelectedItem = err; listBoxErrors.ScrollIntoView(err); listBoxErrors.Visibility = Visibility.Visible; } }
public static ParameterStoreCollection GetParameterStores(string[] paramNames, ParameterStoreCollection stores, out bool modified) { modified = false; ParameterStoreCollection l = new ParameterStoreCollection(); foreach (string param in paramNames) { ParameterStore ps = stores[param]; ParameterStore psAll = AllParameters[param]; if (ps != null) { ps = ps.Clone() as ParameterStore; ps.Target = null; } else if (psAll != null) { ps = psAll.Clone() as ParameterStore; ps.Target = null; modified = true; } else { ps = new ParameterStore(param); modified = true; } l.Add(ps); if (psAll != null) { ps.CopyTo(psAll); } else { AllParameters.Add(ps.Clone() as ParameterStore); } } return(l); }
public static ParameterStoreCollection GetParameterStores(IDbCommand command, ParameterStoreCollection stores, out bool modified) { modified = false; ParameterStoreCollection l = new ParameterStoreCollection(); foreach (DbParameter p in command.Parameters) { ParameterStore ps = stores[p.ParameterName]; ParameterStore psAll = AllParameters[p.ParameterName]; if (ps != null) { ps = ps.Clone() as ParameterStore; ps.Target = p; } else if (psAll != null) { ps = psAll.Clone() as ParameterStore; ps.Target = p; modified = true; } else { ps = new ParameterStore(p); modified = true; } l.Add(ps); if (psAll != null) { ps.CopyTo(psAll); } else { AllParameters.Add(ps.Clone() as ParameterStore); } } return(l); }
private void UpdateDataGridResult(SQLParts sqls) { Db2SourceContext ctx = CurrentDataSet; using (IDbConnection conn = ctx.NewConnection(true)) { bool modified; Parameters = ParameterStore.GetParameterStores(sqls.ParameterNames, Parameters, out modified); if (modified) { return; } foreach (ParameterStore p in Parameters) { if (p.IsError) { dataGridParameters.Focus(); dataGridParameters.SelectedItem = p; MessageBox.Show(string.Format("パラメータ{0}の値が不正です", p.ParameterName), "エラー", MessageBoxButton.OK, MessageBoxImage.Error); return; } } foreach (SQLPart sql in sqls.Items) { IDbCommand cmd = ctx.GetSqlCommand(sql.SQL, Command_Log, conn); ParameterStoreCollection stores = ParameterStore.GetParameterStores(cmd, Parameters, out modified); DateTime start = DateTime.Now; try { using (IDataReader reader = cmd.ExecuteReader()) { DataGridControllerResult.Load(reader); if (0 <= reader.RecordsAffected) { AddLog(string.Format("{0}行反映しました。", reader.RecordsAffected), null, null, LogStatus.Normal, true); } else if (0 < reader.FieldCount) { tabControlResult.SelectedItem = tabItemDataGrid; } } AddToHistory(sql, stores); } catch (Exception t) { Tuple <int, int> errPos = CurrentDataSet.GetErrorPosition(t, sql.SQL, 0); AddLog(CurrentDataSet.GetExceptionMessage(t), sql.SQL, stores, LogStatus.Error, true, errPos); Db2SrcDataSetController.ShowErrorPosition(t, textBoxSql, CurrentDataSet, sql.Offset); return; } finally { ParameterStore.GetParameterStores(cmd, stores, out modified); UpdateDataGridParameters(); DateTime end = DateTime.Now; TimeSpan time = end - start; string s = string.Format("{0}:{1:00}:{2:00}.{3:000}", (int)time.TotalHours, time.Minutes, time.Seconds, time.Milliseconds); AddLog(string.Format("実行しました (所要時間 {0})", s), cmd.CommandText, stores, LogStatus.Aux, false); textBlockGridResult.Text = string.Format("{0}件見つかりました。 所要時間 {1}", DataGridControllerResult.Rows.Count, s); } } } }
public QueryStore(TextReader reader) { StringBuilder buf = new StringBuilder(); for (string s = reader.ReadLine(); s != null && s.StartsWith(ParamSeparator); s = reader.ReadLine()) { buf.AppendLine(s); } _sql = buf.ToString(); Parameters = new ParameterStoreCollection(); while (reader.Peek() != -1) { string pName = GetWord(reader, false); if (pName == null) { break; } if (pName.StartsWith(ParamSeparator)) { return; } if (!pName.StartsWith("--")) { throw new ApplicationException("パラメータ宣言が -- で開始されていません"); } if (pName == "--") { pName = GetWord(reader, false); } else { pName = pName.Substring(2); } string s = GetWord(reader, false); if (s == null || s != ":") { throw new ApplicationException(":が見つかりません"); } string typeStr = GetWord(reader, false); s = GetWord(reader, false); if (s == null || s != "=") { throw new ApplicationException("=が見つかりません"); } string val = GetWord(reader, true); ParameterStore param = new ParameterStore(pName); DbTypeInfo info = DbTypeInfo.GetDbTypeInfo(typeStr); if (info == null) { throw new ApplicationException(string.Format("不明な型です: {0}", typeStr)); } param.DbType = info.DbType; if (string.IsNullOrEmpty(val) || string.Compare(val, "null", true) == 0) { param.Value = DBNull.Value; } else { val = DequoteStr(val); param.Value = info.Parse(val); } Parameters.Add(param); } }