/[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 121 by william, Sat Jul 14 09:40:33 2012 UTC
# Line 24  namespace AnywhereTS.DBSupport Line 24  namespace AnywhereTS.DBSupport
24          List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo);          List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo);
25    
26          DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo);          DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo);
27          public bool RunScript(string strFile, MsSqlConnector con, out Exception ErrorInfo);          bool RunScript(string strFile, out Exception ErrorInfo);
28      }      }
29      public abstract class DBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter> :      public abstract class DBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter> :
30          IDBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter>          IDBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter>
# Line 34  namespace AnywhereTS.DBSupport Line 34  namespace AnywhereTS.DBSupport
34          where DBDataAdapter : DbDataAdapter, new()          where DBDataAdapter : DbDataAdapter, new()
35      {      {
36    
37          public DBConnector(string DBServerAddress, string DBServerInstance, string DBDatabase)          public DBConnector(string Server, string Instance, string Database)
38          {          {
39              this.DBServerAddress = DBServerAddress;              DBServerAddress = Server;
40              this.DBServerInstance = DBServerInstance;              DBServerInstance = Instance;
41              this.DBDatabase = DBDatabase;              DBDatabase = Database;
42                using (log4net.NDC.Push(string.Format("[Server={0}] [Instance={1}] [Database={2}]", Server, Instance, Database)))
43                {
44                    Logging.DatabaseLog.Debug("Creating DBConnector instance");
45                }
46          }          }
47    
48            public static string GetConnectionString()
49            {
50                return string.Format(@"Data Source={0}\{1};Database={2};Integrated Security=SSPI", DBServerAddress, DBServerInstance, DBDatabase);
51            }
52          protected DBConnection connection;          protected DBConnection connection;
53          protected string DBServerAddress = "";          #region DBServerAddress, DBServerInstance, DBDatabase
54          protected string DBServerInstance = "";          internal static string DBServerAddress = "";
55          protected string DBDatabase = "";          internal static string DBServerInstance = "";
56            internal static string DBDatabase = "";
57            #endregion
58            #region  private string SafeSqlLiteral(string inputSQL)
59          private string SafeSqlLiteral(string inputSQL) { return inputSQL.Replace("'", "''"); }          private string SafeSqlLiteral(string inputSQL) { return inputSQL.Replace("'", "''"); }
60            #endregion
61          #region IDBConnector members          #region IDBConnector members
62          public DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo)          #region  public virtual bool ConnectionIsOpen
63            public virtual bool ConnectionIsOpen { get; protected set; }
64            #endregion
65            #region public virtual DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo)
66            public virtual DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo)
67          {          {
68              ErrorInfo = null;              ErrorInfo = null;
69              try              try
# Line 74  namespace AnywhereTS.DBSupport Line 90  namespace AnywhereTS.DBSupport
90                      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));
91                  }                  }
92                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
93              }              }
94          }          }
95          private bool _ConnectionIsOpen;          #endregion
96          public virtual bool ConnectionIsOpen { get { return _ConnectionIsOpen; } protected set { _ConnectionIsOpen = value; } }          #region  public virtual void CreateConnection(out Exception ErrorInfo)
97          public virtual void CreateConnection(out Exception ErrorInfo)          public virtual void CreateConnection(out Exception ErrorInfo)
98          {          {
99              ErrorInfo = null;              ErrorInfo = null;
100                string connetionString = string.Empty;
101              try              try
102              {              {                
103                  string connetionString = null;                  connetionString = GetConnectionString();
104                  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)))
105                  connection = new DBConnection();                  {
106                  connection.ConnectionString = connetionString;                      Logging.DatabaseLog.Debug("Creating Connection");
107                        connection = new DBConnection();
108                        connection.ConnectionString = connetionString;
109                        Logging.DatabaseLog.Debug("Created Connection");
110                    }
111              }              }
112              catch (SqlException ex)              catch (SqlException ex)
113              {              {
# Line 103  namespace AnywhereTS.DBSupport Line 124  namespace AnywhereTS.DBSupport
124                      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));
125                  }                  }
126                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
127              }              }
128          }          }
129            #endregion
130            #region public virtual void OpenConnection(out Exception ErrorInfo)
131          public virtual void OpenConnection(out Exception ErrorInfo)          public virtual void OpenConnection(out Exception ErrorInfo)
132          {          {
133              ErrorInfo = null;              ErrorInfo = null;
134                string connetionString = string.Empty;
135              try              try
136              {              {
137                  //this.CloseConnection(out ErrorInfo);                  //this.CloseConnection(out ErrorInfo);
138                  connection.Open();                  connetionString = GetConnectionString();
139                  this.ConnectionIsOpen = true;                  using (log4net.NDC.Push(string.Format("connectionString={0}", connetionString)))
140                    {
141                        Logging.DatabaseLog.Debug("Opening Connection");
142                        connection.Open();
143                        this.ConnectionIsOpen = true;
144                        Logging.DatabaseLog.Debug("Opened Connection");
145                    }
146              }              }
147              //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex;  throw ErrorInfo;}              //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex;  throw ErrorInfo;}
148              catch (SqlException ex)              catch (SqlException ex)
# Line 130  namespace AnywhereTS.DBSupport Line 160  namespace AnywhereTS.DBSupport
160                      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));
161                  }                  }
162                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
163              }              }
164          }          }
165            #endregion
166            #region public virtual void CloseConnection(out Exception ErrorInfo)
167          public virtual void CloseConnection(out Exception ErrorInfo)          public virtual void CloseConnection(out Exception ErrorInfo)
168          {          {
169              ErrorInfo = null;              ErrorInfo = null;
# Line 155  namespace AnywhereTS.DBSupport Line 187  namespace AnywhereTS.DBSupport
187                      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));
188                  }                  }
189                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
190              }              }
191          }          }
192            #endregion
193            #region public virtual DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
194          public virtual DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)          public virtual DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
195          {          {
196              ErrorInfo = null;              ErrorInfo = null;
# Line 187  namespace AnywhereTS.DBSupport Line 220  namespace AnywhereTS.DBSupport
220                      Logging.DatabaseLog.Error(string.Format("Failed to execute querey: {0}", command));                      Logging.DatabaseLog.Error(string.Format("Failed to execute querey: {0}", command));
221                  }                  }
222                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
223              }              }
224          }          }
225            #endregion
226            #region public virtual void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
227          public virtual void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)          public virtual void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo)
228          {          {
229              ErrorInfo = null;              ErrorInfo = null;
# Line 217  namespace AnywhereTS.DBSupport Line 252  namespace AnywhereTS.DBSupport
252                      Logging.DatabaseLog.Error(string.Format("Failed to execute non querey: {0}", command));                      Logging.DatabaseLog.Error(string.Format("Failed to execute non querey: {0}", command));
253                  }                  }
254                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
255              }                  }
256          }          }
257            #endregion
258            #region public virtual List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo)
259          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)
260          {          {
261              ErrorInfo = null;              ErrorInfo = null;
# Line 258  namespace AnywhereTS.DBSupport Line 295  namespace AnywhereTS.DBSupport
295                      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));
296                  }                  }
297                  ErrorInfo = ex; throw ErrorInfo;                  ErrorInfo = ex; throw ErrorInfo;
298              }                          }
299          }          }
300            #endregion
301            #region public string[] ParseScriptToCommands(string strScript)
302          public string[] ParseScriptToCommands(string strScript)          public string[] ParseScriptToCommands(string strScript)
303          {          {
304              string[] commands;              string[] commands;
305              commands = Regex.Split(strScript, "GO\r\n", RegexOptions.IgnoreCase);              commands = strScript.Split(new string[] { string.Format("GO{0}", System.Environment.NewLine) }, StringSplitOptions.RemoveEmptyEntries);
306              return commands;              return commands;
307          }          }
308          public virtual bool RunScript(string strFile, out Exception ErrorInfo)          #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
# Line 275  namespace AnywhereTS.DBSupport Line 316  namespace AnywhereTS.DBSupport
316                  strCommands = ParseScriptToCommands(strFile);                  strCommands = ParseScriptToCommands(strFile);
317                  if (this.ConnectionIsOpen)                  if (this.ConnectionIsOpen)
318                  {                  {
319                      foreach (string strCmd in strCommands)                      if (!ClientRunScript(strCommands, out ErrorInfo))
320                      {                      {
321                          if (strCmd.Length > 0)                          return false;
                         {  
                             // Substitute database directory with the decided one.  
                             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              }              }
# Line 310  namespace AnywhereTS.DBSupport Line 345  namespace AnywhereTS.DBSupport
345              }              }
346              return false;              return false;
347          }          }
348            public abstract bool ClientRunScript(string[] strCommands, out Exception ErrorInfo);
349            #endregion
350          #endregion          #endregion
351    
352          #region IDisposable Members          #region IDisposable Members

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

  ViewVC Help
Powered by ViewVC 1.1.22