/[AnywhereTS-MSSQL]/trunk/AnywhereTS.DBSupport/DBConnector.cs
ViewVC logotype

Diff of /trunk/AnywhereTS.DBSupport/DBConnector.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 87 by william, Fri Jul 13 22:37:40 2012 UTC revision 128 by william, Sat Jul 14 13:01:52 2012 UTC
# Line 5  using System.Data.SqlClient; Line 5  using System.Data.SqlClient;
5  using System.Data.Common;  using System.Data.Common;
6  using System.Data;  using System.Data;
7  using System.Text.RegularExpressions;  using System.Text.RegularExpressions;
8    using System.IO;
9    
10    
11  namespace AnywhereTS.DBSupport  namespace AnywhereTS.DBSupport
12  {  {
# Line 24  namespace AnywhereTS.DBSupport Line 26  namespace AnywhereTS.DBSupport
26          List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo);          List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo);
27    
28          DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo);          DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo);
29          public bool RunScript(string strFile, MsSqlConnector con, out Exception ErrorInfo);          bool RunScript(string strFile, out Exception ErrorInfo);
30      }      }
31      public abstract class DBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter> :      public abstract class DBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter> :
32          IDBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter>          IDBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter>
# Line 34  namespace AnywhereTS.DBSupport Line 36  namespace AnywhereTS.DBSupport
36          where DBDataAdapter : DbDataAdapter, new()          where DBDataAdapter : DbDataAdapter, new()
37      {      {
38    
39          public DBConnector(string DBServerAddress, string DBServerInstance, string DBDatabase)          public DBConnector(string Server, string Instance, string Database)
40          {          {
41              this.DBServerAddress = DBServerAddress;              DBServerAddress = Server;
42              this.DBServerInstance = DBServerInstance;              DBServerInstance = Instance;
43              this.DBDatabase = DBDatabase;              DBDatabase = Database;
44                using (log4net.NDC.Push(string.Format("[Server={0}] [Instance={1}] [Database={2}]", Server, Instance, Database)))
45                {
46                    Logging.DatabaseLog.Debug("Creating DBConnector instance");
47                }
48          }          }
49    
50            public static string GetConnectionString()
51            {
52                return string.Format(@"Data Source={0}\{1};Database={2};Integrated Security=SSPI", DBServerAddress, DBServerInstance, DBDatabase);
53            }
54          protected DBConnection connection;          protected DBConnection connection;
55          protected string DBServerAddress = "";          #region DBServerAddress, DBServerInstance, DBDatabase
56          protected string DBServerInstance = "";          internal static string DBServerAddress = "";
57          protected string DBDatabase = "";          internal static string DBServerInstance = "";
58            internal static string DBDatabase = "";
59            #endregion
60            #region  private string SafeSqlLiteral(string inputSQL)
61          private string SafeSqlLiteral(string inputSQL) { return inputSQL.Replace("'", "''"); }          private string SafeSqlLiteral(string inputSQL) { return inputSQL.Replace("'", "''"); }
62            #endregion
63          #region IDBConnector members          #region IDBConnector members
64          public DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo)          #region  public virtual bool ConnectionIsOpen
65            public virtual bool ConnectionIsOpen { get; protected set; }
66            #endregion
67            #region public virtual DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo)
68            public virtual DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo)
69          {          {
70              ErrorInfo = null;              ErrorInfo = null;
71              try              try
# Line 61  namespace AnywhereTS.DBSupport Line 79  namespace AnywhereTS.DBSupport
79              }              }
80              catch (SqlException ex)              catch (SqlException ex)
81              {              {
82                  using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString())))                  SqlException e = (ex.GetBaseException() as SqlException);
83                    using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", e.Number.ToString(), e.Message, System.Environment.NewLine, e.ToString())))
84                  {                  {
85                      Logging.DatabaseLog.Error(string.Format("Failed to create command instance using command: {0}", command));                      Logging.DatabaseLog.Error(string.Format("Failed to create command instance using command: {0}", command));
86                  }                  }
# Line 69  namespace AnywhereTS.DBSupport Line 88  namespace AnywhereTS.DBSupport
88              }              }
89              catch (Exception ex)              catch (Exception ex)
90              {              {
91                  using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))                  Exception e = ex.GetBaseException();
92                    using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", e.GetType().Name, e.Message, System.Environment.NewLine, e.ToString())))
93                  {                  {
94                      Logging.DatabaseLog.Error(string.Format("Failed to create command instance using command: {0}", command));                      Logging.DatabaseLog.Error(string.Format("Failed to create command instance using command: {0}", command));
95                  }                  }
96                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
97              }              }
98          }          }
99          private bool _ConnectionIsOpen;          #endregion
100          public virtual bool ConnectionIsOpen { get { return _ConnectionIsOpen; } protected set { _ConnectionIsOpen = value; } }          #region  public virtual void CreateConnection(out Exception ErrorInfo)
101          public virtual void CreateConnection(out Exception ErrorInfo)          public virtual void CreateConnection(out Exception ErrorInfo)
102          {          {
103              ErrorInfo = null;              ErrorInfo = null;
104                string connetionString = string.Empty;
105              try              try
106              {              {                
107                  string connetionString = null;                  connetionString = GetConnectionString();
108                  connetionString = string.Format(@"Data Source={0}\{1};Initial Catalog={2};Integrated Security=SSPI", DBServerAddress, DBServerInstance, DBDatabase);                  using (log4net.NDC.Push(string.Format("connetionString={0}", connetionString)))
109                  connection = new DBConnection();                  {
110                  connection.ConnectionString = connetionString;                      Logging.DatabaseLog.Debug("Creating Connection");
111                        connection = new DBConnection();
112                        connection.ConnectionString = connetionString;
113                        Logging.DatabaseLog.Debug("Created Connection");
114                    }
115              }              }
116              catch (SqlException ex)              catch (SqlException ex)
117              {              {
# Line 103  namespace AnywhereTS.DBSupport Line 128  namespace AnywhereTS.DBSupport
128                      Logging.DatabaseLog.Error(string.Format("Failed to create connection to {0} Database", DBDatabase));                      Logging.DatabaseLog.Error(string.Format("Failed to create connection to {0} Database", DBDatabase));
129                  }                  }
130                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
131              }              }
132          }          }
133            #endregion
134            #region public virtual void OpenConnection(out Exception ErrorInfo)
135          public virtual void OpenConnection(out Exception ErrorInfo)          public virtual void OpenConnection(out Exception ErrorInfo)
136          {          {
137              ErrorInfo = null;              ErrorInfo = null;
138                string connetionString = string.Empty;
139              try              try
140              {              {
141                  //this.CloseConnection(out ErrorInfo);                  //this.CloseConnection(out ErrorInfo);
142                  connection.Open();                  connetionString = GetConnectionString();
143                  this.ConnectionIsOpen = true;                  using (log4net.NDC.Push(string.Format("connectionString={0}", connetionString)))
144                    {
145                        Logging.DatabaseLog.Debug("Opening Connection");
146                        connection.Open();
147                        this.ConnectionIsOpen = true;
148                        Logging.DatabaseLog.Debug("Opened Connection");
149                    }
150              }              }
151              //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex;  throw ErrorInfo;}              //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex;  throw ErrorInfo;}
152              catch (SqlException ex)              catch (SqlException ex)
# Line 130  namespace AnywhereTS.DBSupport Line 164  namespace AnywhereTS.DBSupport
164                      Logging.DatabaseLog.Error(string.Format("Failed to open connection to {0} Database", DBDatabase));                      Logging.DatabaseLog.Error(string.Format("Failed to open connection to {0} Database", DBDatabase));
165                  }                  }
166                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
167              }              }
168          }          }
169            #endregion
170            #region public virtual void CloseConnection(out Exception ErrorInfo)
171          public virtual void CloseConnection(out Exception ErrorInfo)          public virtual void CloseConnection(out Exception ErrorInfo)
172          {          {
173              ErrorInfo = null;              ErrorInfo = null;
# Line 155  namespace AnywhereTS.DBSupport Line 191  namespace AnywhereTS.DBSupport
191                      Logging.DatabaseLog.Error(string.Format("Failed to close connection to {0} Database", DBDatabase));                      Logging.DatabaseLog.Error(string.Format("Failed to close connection to {0} Database", DBDatabase));
192                  }                  }
193                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
194              }              }
195          }          }
196            #endregion
197            #region public virtual DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
198          public virtual DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)          public virtual DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
199          {          {
200              ErrorInfo = null;              ErrorInfo = null;
# Line 187  namespace AnywhereTS.DBSupport Line 224  namespace AnywhereTS.DBSupport
224                      Logging.DatabaseLog.Error(string.Format("Failed to execute querey: {0}", command));                      Logging.DatabaseLog.Error(string.Format("Failed to execute querey: {0}", command));
225                  }                  }
226                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
227              }              }
228          }          }
229            #endregion
230            #region public virtual void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
231          public virtual void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)          public virtual void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
232          {          {
233              ErrorInfo = null;              ErrorInfo = null;
# Line 217  namespace AnywhereTS.DBSupport Line 256  namespace AnywhereTS.DBSupport
256                      Logging.DatabaseLog.Error(string.Format("Failed to execute non querey: {0}", command));                      Logging.DatabaseLog.Error(string.Format("Failed to execute non querey: {0}", command));
257                  }                  }
258                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
259              }                  }
260          }          }
261            #endregion
262            #region public virtual List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo)
263          public virtual List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo)          public virtual List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo)
264          {          {
265              ErrorInfo = null;              ErrorInfo = null;
# Line 258  namespace AnywhereTS.DBSupport Line 299  namespace AnywhereTS.DBSupport
299                      Logging.DatabaseLog.Error(string.Format("Failed to get colum names from reader: {0}", command));                      Logging.DatabaseLog.Error(string.Format("Failed to get colum names from reader: {0}", command));
300                  }                  }
301                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
302              }                          }
         }  
         public string[] ParseScriptToCommands(string strScript)  
         {  
             string[] commands;  
             commands = Regex.Split(strScript, "GO\r\n", RegexOptions.IgnoreCase);  
             return commands;  
303          }          }
304          public virtual bool RunScript(string strFile, out Exception ErrorInfo)          #endregion
305            #region public string[] ParseScriptToCommands(string strScript)
306        
307            protected abstract bool ClientRunScript(string strFile, out Exception ErrorInfo);
308            #endregion
309            #region public virtual bool RunScript(string strFile, out Exception ErrorInfo)
310            public bool RunScript(string strFile, out Exception ErrorInfo)
311          {          {
312              ErrorInfo = null;              ErrorInfo = null;
313              try              try
314              {              {
                 string[] strCommands;  
                 strCommands = ParseScriptToCommands(strFile);  
315                  if (this.ConnectionIsOpen)                  if (this.ConnectionIsOpen)
316                  {                  {
317                      foreach (string strCmd in strCommands)                      if (!ClientRunScript(strFile, out ErrorInfo))
318                      {                      {
319                          if (strCmd.Length > 0)                          if(ErrorInfo != null)
320                          {                              throw ErrorInfo;
321                              // Substitute database directory with the decided one.                          return false;
                             DBCommand command = this.CreateCommandInstance(strCmd, new List<DBParameter>(), out ErrorInfo);  
                             command.ExecuteNonQuery();  
                         }  
322                      }                      }
                     return true;  
323                  }                  }
324                  else                  else
325                  {                  {
326                      Logging.ATSAdminLog.Fatal(string.Format("Failed to run script: [database connection is not open] {0}{1}", System.Environment.NewLine, strFile));                      Logging.DatabaseLog.Fatal(string.Format("Failed to run script: [database connection is not open] {0}{1}", System.Environment.NewLine, strFile));
327                      return false;                      return false;
328                  }                  }
329              }              }
330              catch (SqlException ex)              catch (SqlException ex)
331              {              {
332                  using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString())))                  SqlException e = (ex.GetBaseException() as SqlException);
333                    using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", e.Number.ToString(), e.Message, System.Environment.NewLine, e.ToString())))
334                  {                  {
335                      Logging.DatabaseLog.Error(string.Format("Failed to run script: {0}{1}", System.Environment.NewLine, strFile));                      Logging.DatabaseLog.Error(string.Format("Failed to run script: {0}{1}", System.Environment.NewLine, strFile));
336                  }                  }
337                  ErrorInfo = ex;                  ErrorInfo = ex; throw ErrorInfo;
338              }              }
339              catch (Exception ex)              catch (Exception ex)
340              {              {
341                  using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))                  Exception e = ex.GetBaseException();
342                    using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", e.GetType().Name, e.Message, System.Environment.NewLine, e.ToString())))
343                  {                  {
344                      Logging.DatabaseLog.Error(string.Format("Failed to run script: {0}{1}", System.Environment.NewLine, strFile));                      Logging.DatabaseLog.Error(string.Format("Failed to run script: {0}{1}", System.Environment.NewLine, strFile));
345                  }                  }
346                  ErrorInfo = ex;                  ErrorInfo = ex; throw ErrorInfo;
347              }              }
348              return false;              return false;
349          }          }
350          #endregion          #endregion
351            #endregion
352    
353          #region IDisposable Members          #region IDisposable Members
354          public virtual void Dispose()          public virtual void Dispose()

Legend:
Removed from v.87  
changed lines
  Added in v.128

  ViewVC Help
Powered by ViewVC 1.1.22