Changeset 7


Ignore:
Timestamp:
May 13, 2019, 1:20:39 PM (5 years ago)
Author:
chronos
Message:
  • Added: Support logging in case of database exception.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/Database.cs

    r2 r7  
    2626        public delegate void DatabaseInitializedHandler();       
    2727        public static event DatabaseInitializedHandler DatabaseInitialized;
     28        public delegate void LogMessageHandler(string text);
     29        public static event LogMessageHandler LogMessage;
    2830        public static string connectionString = "";
    2931        public static string databaseName = "";
     
    7173                    {
    7274                        result = false;
     75                        Log("When connecting to DB: " + ex.Message);
    7376                        throw new Exception("When connecting to DB: " + ex.Message);
    7477                    }
     
    8487
    8588            return result;
     89        }
     90
     91        private static void Log(string message)
     92        {
     93            if (LogMessage != null) LogMessage(message);
    8694        }
    8795
     
    289297                if ((error.Class > 10) && (error.Number != 3013))
    290298                {
     299                    Log(error.ToString());
    291300                    throw new Exception(error.ToString());
    292301                }
     
    319328            catch (DbException ex)
    320329            {
     330                Log("When executing query sql command:\n" + query + "\nException text: " + ex.Message);
    321331                throw new Exception("When executing query sql command:\n" + query + "\nException text: " + ex.Message);
    322332            }
     
    347357            }
    348358            catch (DbException ex)
    349             {               
     359            {
     360                Log("When executing sql transaction:\nException text: " + ex.Message);
    350361                throw new Exception("When executing sql transaction:\nException text: " + ex.Message);
    351362            }
     
    386397            catch (DbException ex)
    387398            {
     399                Log("When executing query sql command:\n" + query + "\nException text: " + ex.Message);
    388400                throw new Exception("When executing query sql command:\n" + query + "\nException text: " + ex.Message);
    389401            }
     
    406418            catch (DbException ex)
    407419            {
     420                Log("When executing query sql command:\n" + query + "\nException text: " + ex.Message);
    408421                throw new Exception("When executing query sql command:\n" + query + "\nException text: " + ex.Message);
    409422            }
     
    433446            catch (DbException ex)
    434447            {
     448                Log("When executing non query sql command:\n" + query + "\nException text: " + ex.Message);
    435449                throw new Exception("When executing non query sql command:\n" + query + "\nException text: " + ex.Message);
    436450            }
     
    491505            return result;
    492506        }
     507
     508        public static void CreateIndexOnColumn(string table, string column, string indexName = null)
     509        {
     510            if (indexName == null) indexName = table + "_" + column;
     511            ExecuteNonQuery("CREATE INDEX " + indexName + " ON " + table + " (" + column + ")");
     512        }
    493513    }
    494514
Note: See TracChangeset for help on using the changeset viewer.