This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
/
Controller.cs
140 lines (118 loc) · 4.12 KB
/
Controller.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ANDREICSLIB;
using ANDREICSLIB.ClassExtras;
namespace SQLRegex
{
public class Controller
{
public string ConnectionString;
private DbContext c;
//public string Table;
//public List<string> Columns;
//public string InRegex;
//public string OutRegex;
//public bool TestMode = true;
public Controller(string connectionString)
{
ConnectionString = connectionString;
c = new DbContext(ConnectionString);
}
public List<string> GetTables()
{
string q = @"SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' ";
var qr = c.Database.SqlQuery<string>(q);
var res = qr.OrderBy(s => s.ToString()).ToList();
return res;
}
public List<string> GetColumns(string table)
{
string q = @"SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'" + table + "' ";
var qr = c.Database.SqlQuery<string>(q);
var res = qr.OrderBy(s => s.ToString()).Distinct().ToList();
return res;
}
public List<string> GetValues(string table, string column, string where)
{
string q = string.Format("select top 100 {0} from {1} {2}", column, table, where);
var resd = c.Database.DynamicSqlQuery(q);
var ret = new List<string>();
foreach (var r in resd)
{
var v = r.GetType().GetProperty(column).GetValue(r, null);
var rv = v == null ? "(null)" : v.ToString();
ret.Add(rv);
}
return ret;
}
private static string WhereGenerator(LviItem item)
{
string where = string.IsNullOrEmpty(item.where)
? string.Format("where {0} like '{1}'", item.column, item.rowValue.Replace("'", "''"))
: string.Format("{0} and {1} like '{2}'", item.where, item.column, item.rowValue.Replace("'", "''"));
return where;
}
public bool OnlyOnce(LviItem item)
{
var where2 = WhereGenerator(item);
string q = string.Format("select top 100 {0} from {1} {2}", item.column, item.table, where2);
var resd = c.Database.DynamicSqlQuery(q);
var ret = new List<string>();
foreach (var r in resd)
{
ret.Add("ok");
if (ret.Count > 1)
return false;
}
return true;
}
private string UpdateRowQuery(LviItem item)
{
var where = WhereGenerator(item);
string q = string.Format("update {0} \r\nset {1} = '{2}' \r\n{3}", item.table, item.column, item.postRegex.Replace("'", "''").Trim(), where);
return q;
}
public void SaveChanges(List<LviItem> item, bool testMode)
{
var fn = "queries.txt";
FileExtras.SaveToFile(fn, "");
foreach (var i in item)
{
//make sure only one matches
if (OnlyOnce(i) == false)
{
var msg = string.Format("col={0}, table={1}, where={2}, value={3}", i.column, i.table, i.where,
i.rowValue);
throw new Exception("error, there exists multiple values for:" + msg);
}
var query = UpdateRowQuery(i);
if (testMode)
{
FileExtras.SaveToFile(fn, query, true);
}
}
if (testMode)
{
Process.Start("notepad.exe", fn);
}
}
}
}