/[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 124 by william, Sat Jul 14 11:14:54 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 74  namespace AnywhereTS.DBSupport Line 92  namespace AnywhereTS.DBSupport
92                      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));
93                  }                  }
94                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
95              }              }
96          }          }
97          private bool _ConnectionIsOpen;          #endregion
98          public virtual bool ConnectionIsOpen { get { return _ConnectionIsOpen; } protected set { _ConnectionIsOpen = value; } }          #region  public virtual void CreateConnection(out Exception ErrorInfo)
99          public virtual void CreateConnection(out Exception ErrorInfo)          public virtual void CreateConnection(out Exception ErrorInfo)
100          {          {
101              ErrorInfo = null;              ErrorInfo = null;
102                string connetionString = string.Empty;
103              try              try
104              {              {                
105                  string connetionString = null;                  connetionString = GetConnectionString();
106                  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)))
107                  connection = new DBConnection();                  {
108                  connection.ConnectionString = connetionString;                      Logging.DatabaseLog.Debug("Creating Connection");
109                        connection = new DBConnection();
110                        connection.ConnectionString = connetionString;
111                        Logging.DatabaseLog.Debug("Created Connection");
112                    }
113              }              }
114              catch (SqlException ex)              catch (SqlException ex)
115              {              {
# Line 103  namespace AnywhereTS.DBSupport Line 126  namespace AnywhereTS.DBSupport
126                      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));
127                  }                  }
128                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
129              }              }
130          }          }
131            #endregion
132            #region public virtual void OpenConnection(out Exception ErrorInfo)
133          public virtual void OpenConnection(out Exception ErrorInfo)          public virtual void OpenConnection(out Exception ErrorInfo)
134          {          {
135              ErrorInfo = null;              ErrorInfo = null;
136                string connetionString = string.Empty;
137              try              try
138              {              {
139                  //this.CloseConnection(out ErrorInfo);                  //this.CloseConnection(out ErrorInfo);
140                  connection.Open();                  connetionString = GetConnectionString();
141                  this.ConnectionIsOpen = true;                  using (log4net.NDC.Push(string.Format("connectionString={0}", connetionString)))
142                    {
143                        Logging.DatabaseLog.Debug("Opening Connection");
144                        connection.Open();
145                        this.ConnectionIsOpen = true;
146                        Logging.DatabaseLog.Debug("Opened Connection");
147                    }
148              }              }
149              //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex;  throw ErrorInfo;}              //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex;  throw ErrorInfo;}
150              catch (SqlException ex)              catch (SqlException ex)
# Line 130  namespace AnywhereTS.DBSupport Line 162  namespace AnywhereTS.DBSupport
162                      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));
163                  }                  }
164                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
165              }              }
166          }          }
167            #endregion
168            #region public virtual void CloseConnection(out Exception ErrorInfo)
169          public virtual void CloseConnection(out Exception ErrorInfo)          public virtual void CloseConnection(out Exception ErrorInfo)
170          {          {
171              ErrorInfo = null;              ErrorInfo = null;
# Line 155  namespace AnywhereTS.DBSupport Line 189  namespace AnywhereTS.DBSupport
189                      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));
190                  }                  }
191                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
192              }              }
193          }          }
194            #endregion
195            #region public virtual DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
196          public virtual DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)          public virtual DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
197          {          {
198              ErrorInfo = null;              ErrorInfo = null;
# Line 187  namespace AnywhereTS.DBSupport Line 222  namespace AnywhereTS.DBSupport
222                      Logging.DatabaseLog.Error(string.Format("Failed to execute querey: {0}", command));                      Logging.DatabaseLog.Error(string.Format("Failed to execute querey: {0}", command));
223                  }                  }
224                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
225              }              }
226          }          }
227            #endregion
228            #region public virtual void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
229          public virtual void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)          public virtual void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
230          {          {
231              ErrorInfo = null;              ErrorInfo = null;
# Line 217  namespace AnywhereTS.DBSupport Line 254  namespace AnywhereTS.DBSupport
254                      Logging.DatabaseLog.Error(string.Format("Failed to execute non querey: {0}", command));                      Logging.DatabaseLog.Error(string.Format("Failed to execute non querey: {0}", command));
255                  }                  }
256                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
257              }                  }
258          }          }
259            #endregion
260            #region public virtual List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo)
261          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)
262          {          {
263              ErrorInfo = null;              ErrorInfo = null;
# Line 258  namespace AnywhereTS.DBSupport Line 297  namespace AnywhereTS.DBSupport
297                      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));
298                  }                  }
299                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
300              }                          }
         }  
         public string[] ParseScriptToCommands(string strScript)  
         {  
             string[] commands;  
             commands = Regex.Split(strScript, "GO\r\n", RegexOptions.IgnoreCase);  
             return commands;  
301          }          }
302          public virtual bool RunScript(string strFile, out Exception ErrorInfo)          #endregion
303            #region public string[] ParseScriptToCommands(string strScript)
304        
305            protected abstract bool ClientRunScript(string strFile, out Exception ErrorInfo);
306            #endregion
307            #region public virtual bool RunScript(string strFile, out Exception ErrorInfo)
308            public bool RunScript(string strFile, out Exception ErrorInfo)
309          {          {
310              ErrorInfo = null;              ErrorInfo = null;
311              try              try
312              {              {
                 string[] strCommands;  
                 strCommands = ParseScriptToCommands(strFile);  
313                  if (this.ConnectionIsOpen)                  if (this.ConnectionIsOpen)
314                  {                  {
315                      foreach (string strCmd in strCommands)                      if (!ClientRunScript(strFile, out ErrorInfo))
316                      {                      {
317                          if (strCmd.Length > 0)                          if (ErrorInfo != null)
318                          {                              throw ErrorInfo;
319                              // Substitute database directory with the decided one.                          return false;
                             DBCommand command = this.CreateCommandInstance(strCmd, new List<DBParameter>(), out ErrorInfo);  
                             command.ExecuteNonQuery();  
                         }  
320                      }                      }
                     return true;  
321                  }                  }
322                  else                  else
323                  {                  {
324                      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));
325                      return false;                      return false;
326                  }                  }
327              }              }
# Line 311  namespace AnywhereTS.DBSupport Line 344  namespace AnywhereTS.DBSupport
344              return false;              return false;
345          }          }
346          #endregion          #endregion
347            #endregion
348    
349          #region IDisposable Members          #region IDisposable Members
350          public virtual void Dispose()          public virtual void Dispose()

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

  ViewVC Help
Powered by ViewVC 1.1.22