Ejemplo n.º 1
0
        /// establish a database connection to the alternative sqlite database for the sessions
        private static TDataBase EstablishDBConnectionSqliteSessionDB(String AConnectionName = "")
        {
            TDBType DBType = CommonTypes.ParseDBType(TAppSettingsManager.GetValue("Server.RDBMSType", "postgresql"));

            if (DBType != TDBType.SQLite)
            {
                throw new Exception("EstablishDBConnectionSqliteSessionDB: we should not get here.");
            }

            string DatabaseHostOrFile = TAppSettingsManager.GetValue("Server.DBSqliteSession", "localhost");
            string DatabasePort       = String.Empty;
            string DatabaseName       = TAppSettingsManager.GetValue("Server.DBName", "openpetra");
            string DBUsername         = TAppSettingsManager.GetValue("Server.DBUserName", "petraserver");
            string DBPassword         = TAppSettingsManager.GetValue("Server.DBPassword", string.Empty, false);

            if (!File.Exists(DatabaseHostOrFile))
            {
                // create the sessions database file
                TLogging.Log("create the sessions database file: " + DatabaseHostOrFile);

                // sqlite on Windows does not support encryption with a password
                // System.EntryPointNotFoundException: sqlite3_key
                DBPassword = string.Empty;

                SqliteConnection conn = new SqliteConnection("Data Source=" + DatabaseHostOrFile + (DBPassword.Length > 0 ? ";Password="******""));
                conn.Open();

                string createStmt =
                    @"CREATE TABLE s_session (
                      s_session_id_c varchar(128) NOT NULL,
                      s_valid_until_d datetime NOT NULL,
                      s_session_values_c text,
                      s_date_created_d date,
                      s_created_by_c varchar(20),
                      s_date_modified_d date,
                      s_modified_by_c varchar(20),
                      s_modification_id_t timestamp,
                      CONSTRAINT s_session_pk
                        PRIMARY KEY (s_session_id_c)
                    )";

                SqliteCommand cmd = new SqliteCommand(createStmt, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
            }

            TDataBase DBAccessObj = new TDataBase();

            DBAccessObj.EstablishDBConnection(DBType,
                                              DatabaseHostOrFile,
                                              DatabasePort,
                                              DatabaseName,
                                              DBUsername,
                                              DBPassword,
                                              "",
                                              true,
                                              AConnectionName);

            return(DBAccessObj);
        }
Ejemplo n.º 2
0
        static private TDataBase GetDatabaseFromSession(bool AOpenConnection = true)
        {
            // if another thread gets called, then the session object is null
            if (HttpContext.Current == null)
            {
                if (Thread.CurrentThread.Name == null)
                {
                    throw new Exception(
                              "TOpenPetraOrgSessionManager.GetDatabaseFromSession: we do need a name for the thread for managing the database connection");
                }

                if (!FDatabaseObjects.ContainsKey(Thread.CurrentThread.Name))
                {
                    TDataBase db = new TDataBase();

                    if (AOpenConnection)
                    {
                        db.EstablishDBConnection(TSrvSetting.RDMBSType,
                                                 TSrvSetting.PostgreSQLServer,
                                                 TSrvSetting.PostgreSQLServerPort,
                                                 TSrvSetting.PostgreSQLDatabaseName,
                                                 TSrvSetting.DBUsername,
                                                 TSrvSetting.DBPassword,
                                                 "");
                    }

                    FDatabaseObjects.Add(Thread.CurrentThread.Name, db);
                }

                return(FDatabaseObjects[Thread.CurrentThread.Name]);
            }

            if (TSession.GetVariable("DBAccessObj") == null)
            {
                if (TServerManager.TheServerManager == null)
                {
                    TLogging.Log("GetDatabaseFromSession : TheServerManager is null");
                }
                else
                {
                    // disconnect web user after 2 minutes of inactivity. should disconnect itself already earlier
                    TServerManager.TheCastedServerManager.DisconnectTimedoutDatabaseConnections(2 * 60, "ANONYMOUS");

                    // disconnect normal users after 3 hours of inactivity
                    TServerManager.TheCastedServerManager.DisconnectTimedoutDatabaseConnections(3 * 60 * 60, "");

                    if (AOpenConnection)
                    {
                        TServerManager.TheCastedServerManager.EstablishDBConnection();
                    }
                }
            }

            return((TDataBase)TSession.GetVariable("DBAccessObj"));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a new <see cref="TDataBase"/> instance and opens a DB Connection on it.
        /// </summary>
        /// <param name="AConnectionName">Name of the DB Connection (optional). It gets logged and hence can aid debugging
        /// (also useful for Unit Testing).</param>
        /// <param name="ADataBase">if this is not null, no new Connection is opened, but this connection is reused</param>
        /// <returns><see cref="TDataBase"/> instance with an open DB Connection.</returns>
        public static TDataBase Connect(string AConnectionName, TDataBase ADataBase = null)
        {
            if (ADataBase != null)
            {
                return(ADataBase);
            }

            TDataBase DBAccessObj = new TDataBase();

            DBAccessObj.EstablishDBConnection(AConnectionName);

            return(DBAccessObj);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// modified version taken from Ict.Petra.Server.App.Main::TServerManager
        /// </summary>
        private TDataBase EstablishDBConnectionAndReturnIt(string AConnectionName = null, bool ALogNumberOfConnections = false)
        {
            TDataBase ReturnValue;

            if (ALogNumberOfConnections)
            {
                if (FDBType == TDBType.PostgreSQL)
                {
                    TLogging.Log("  EstablishDBConnectionAndReturnIt: Number of open DB Connections on PostgreSQL BEFORE Connecting to Database: " +
                                 TDataBase.GetNumberOfDBConnections(FDBType) + TDataBase.GetDBConnectionName(AConnectionName));
                }
            }

            TLogging.Log("  EstablishDBConnectionAndReturnIt: Establishing connection to Database..." + TDataBase.GetDBConnectionName(AConnectionName));

            ReturnValue = new TDataBase();

            TLogging.DebugLevel = TAppSettingsManager.GetInt16("Server.DebugLevel", 10);

            try
            {
                ReturnValue.EstablishDBConnection(FDBType,
                                                  TAppSettingsManager.GetValue("Server.DBHostOrFile"),
                                                  TAppSettingsManager.GetValue("Server.DBPort"),
                                                  TAppSettingsManager.GetValue("Server.DBName"),
                                                  TAppSettingsManager.GetValue("Server.DBUserName"),
                                                  TAppSettingsManager.GetValue("Server.DBPassword"),
                                                  "", AConnectionName);
            }
            catch (Exception Exc)
            {
                TLogging.Log("  EstablishDBConnectionAndReturnIt: Encountered Exception while trying to establish connection to Database " +
                             TDataBase.GetDBConnectionName(AConnectionName) + Environment.NewLine +
                             Exc.ToString());

                throw;
            }

            TLogging.Log("  EstablishDBConnectionAndReturnIt: Connected to Database." + ReturnValue.GetDBConnectionIdentifier());

            if (ALogNumberOfConnections)
            {
                if (FDBType == TDBType.PostgreSQL)
                {
                    TLogging.Log("  EstablishDBConnectionAndReturnIt: Number of open DB Connections on PostgreSQL AFTER  Connecting to Database: " +
                                 TDataBase.GetNumberOfDBConnections(FDBType) + ReturnValue.GetDBConnectionIdentifier());
                }
            }

            return(ReturnValue);
        }
Ejemplo n.º 5
0
        // establish connection to database
        public static bool InitDBConnection(
            string ADBFile, string ADBPassword)
        {
            db = new TDataBase();

            new TLogging("debug.log");
            TLogging.DebugLevel = TAppSettingsManager.GetInt16("DebugLevel", 0);

            db.EstablishDBConnection(TDBType.SQLite,
                                     ADBFile,
                                     "",
                                     "",
                                     "",
                                     ADBPassword,
                                     "");
            DBAccess.GDBAccessObj = db;

            return(true);
        }
Ejemplo n.º 6
0
        // establish connection to database
        public static bool InitDBConnection(
            string ADBFile, string ADBPassword)
        {
            db = new TDataBase();

            new TLogging("debug.log");
            TLogging.DebugLevel = TAppSettingsManager.GetInt16("DebugLevel", 0);

            db.EstablishDBConnection(TDBType.SQLite,
                ADBFile,
                "",
                "",
                "",
                ADBPassword,
                "");
            DBAccess.GDBAccessObj = db;

            return true;
        }
Ejemplo n.º 7
0
        private void ExecuteFullQuery(string AContext = null, TDataBase ADataBase = null)
        {
            TDataBase      DBConnectionObj = null;
            TDBTransaction ReadTransaction = new TDBTransaction();
            bool           SeparateDBConnectionEstablished = false;

            if (FFindParameters.FParametersGivenSeparately)
            {
                string SQLOrderBy       = "";
                string SQLWhereCriteria = "";

                if (FFindParameters.FPagedTableWhereCriteria != "")
                {
                    SQLWhereCriteria = "WHERE " + FFindParameters.FPagedTableWhereCriteria;
                }

                if (FFindParameters.FPagedTableOrderBy != "")
                {
                    SQLOrderBy = " ORDER BY " + FFindParameters.FPagedTableOrderBy;
                }

                FSelectSQL = "SELECT " + FFindParameters.FPagedTableColumns + " FROM " + FFindParameters.FPagedTable +
                             ' ' +
                             SQLWhereCriteria + SQLOrderBy;
            }
            else
            {
                FSelectSQL = FFindParameters.FSqlQuery;
            }

            TLogging.LogAtLevel(9, (this.GetType().FullName + ".ExecuteFullQuery SQL:" + FSelectSQL));

            // clear temp table. do not recreate because it may be typed
            FTmpDataTable.Clear();

            try
            {
                if (ADataBase == null)
                {
                    ADataBase = new TDataBase();
                    ADataBase.EstablishDBConnection(AContext + " Connection");
                    SeparateDBConnectionEstablished = true;
                }

                ReadTransaction = ADataBase.BeginTransaction(IsolationLevel.ReadCommitted,
                                                             -1, AContext + " Transaction");

                // Fill temporary table with query results (all records)
                FTotalRecords = ADataBase.SelectUsingDataAdapter(FSelectSQL, ReadTransaction,
                                                                 ref FTmpDataTable, out FDataAdapterCanceller,
                                                                 delegate(ref IDictionaryEnumerator AEnumerator)
                {
                    if (FFindParameters.FColumNameMapping != null)
                    {
                        AEnumerator = FFindParameters.FColumNameMapping.GetEnumerator();

                        return(FFindParameters.FPagedTable + "_for_paging");
                    }
                    else
                    {
                        return(String.Empty);
                    }
                }, 60, FFindParameters.FParametersArray);
            }
            catch (PostgresException Exp)
            {
                if (Exp.SqlState == "57014")  // Exception with Code 57014 is what Npgsql raises as a response to a Cancel request of a Command
                {
                    TLogging.LogAtLevel(7, this.GetType().FullName + ".ExecuteFullQuery: Query got cancelled; proper reply from Npgsql!");
                }
                else
                {
                    TLogging.Log(this.GetType().FullName + ".ExecuteFullQuery: Query got cancelled; general PostgresException occured: " + Exp.ToString());
                }

                TProgressTracker.SetCurrentState(FProgressID, "Query cancelled!", 0.0m);
                TProgressTracker.CancelJob(FProgressID);
                return;
            }
            catch (Exception Exp)
            {
                TLogging.Log(this.GetType().FullName + ".ExecuteFullQuery: Query got cancelled; general Exception occured: " + Exp.ToString());

                TProgressTracker.SetCurrentState(FProgressID, "Query cancelled!", 0.0m);
                TProgressTracker.CancelJob(FProgressID);

                return;
            }
            finally
            {
                ReadTransaction.Rollback();

                // Close separate DB Connection if we opened one earlier
                if (SeparateDBConnectionEstablished)
                {
                    DBConnectionObj.CloseDBConnection();
                }
            }

            TLogging.LogAtLevel(7,
                                (this.GetType().FullName + ".ExecuteFullQuery: FDataAdapter.Fill finished. FTotalRecords: " + FTotalRecords.ToString()));

            FPageDataTable           = FTmpDataTable.Clone();
            FPageDataTable.TableName = FFindParameters.FSearchName;
            TProgressTracker.SetCurrentState(FProgressID, "Query executed.", 100.0m);
            TProgressTracker.FinishJob(FProgressID);
        }
Ejemplo n.º 8
0
        /// load data from csv files and sql statements
        public static bool ExecuteLoadScript(TDataDefinitionStore ADataDefinition, string AHostname, string ADatabaseName, string AUsername, string APassword, string ALoadSQLFileName)
        {
            StreamReader   sr = null;
            TDBTransaction WriteTransaction = new TDBTransaction();
            bool           SubmissionResult = false;

            TDataBase DBAccessObj = new TDataBase(TDBType.MySQL);

            try
            {
                DBAccessObj.EstablishDBConnection(TDBType.MySQL, AHostname, "", ADatabaseName, AUsername, APassword, "",
                                                  true,
                                                  "GenerateSQL.TLoadMysql.LoadData DB Connection");
                sr = new StreamReader(ALoadSQLFileName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                throw e;
            }

            // one command per line.
            // file is in postgresql syntax
            // either COPY FROM or INSERT

            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();

                while (!line.Trim().StartsWith("--") && !line.Trim().EndsWith(";") && !sr.EndOfStream)
                {
                    string templine = sr.ReadLine();

                    if (!templine.StartsWith("--"))
                    {
                        line += " " + templine;
                    }
                }

                if (line.Trim().ToUpper().StartsWith("INSERT") || line.Trim().ToUpper().StartsWith("UPDATE"))
                {
                    DBAccessObj.WriteTransaction(ref WriteTransaction,
                                                 ref SubmissionResult,
                                                 delegate
                    {
                        DBAccessObj.ExecuteNonQuery(line, WriteTransaction);
                        SubmissionResult = true;
                    });
                }
                else if (line.Trim().ToUpper().StartsWith("COPY"))
                {
                    // pgsql: COPY p_language FROM 'c:/p_language.csv' WITH DELIMITER AS ',' NULL AS '?' CSV QUOTE AS '"' ESCAPE AS '"';
                    // mysql: LOAD DATA LOCAL INFILE 'c:/p_language.csv' INTO TABLE p_language FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '"';
                    // But MySQL 8 makes it quite difficult for security reasons, to use LOAD DATA LOCAL INFILE.
                    // So we parse the file and load the data directly. It is not a huge performance loss.
                    DBAccessObj.WriteTransaction(ref WriteTransaction,
                                                 ref SubmissionResult,
                                                 delegate
                    {
                        string tablename = StringHelper.GetCSVValue(line.Trim().Replace(" ", ","), 1);
                        LoadData(DBAccessObj, ADataDefinition, Path.GetDirectoryName(ALoadSQLFileName), tablename);
                        SubmissionResult = true;
                    });
                }
            }

            sr.Close();

            DBAccessObj.CloseDBConnection();

            return(true);
        }
Ejemplo n.º 9
0
        public static void Main(string[] args)
        {
            try
            {
                // establish connection to database
                TCmdOpts settings = new TCmdOpts();

                if (!settings.IsFlagSet("Server.ODBC_DSN"))
                {
                    Console.WriteLine(
                        "sample call: " +
                        "ExportDataProgress.exe -Server.ODBC_DSN:Petra2_2sa -username:demo_sql -password:demo -sql:\"SELECT * from pub.a_account\" -output:test.xml");
                    Environment.Exit(-1);
                }

                new TLogging("debug.log");

                TDataBase db = new TDataBase();

                TDBType dbtype = TDBType.ProgressODBC;

                if (settings.IsFlagSet("Server.RDBMSType"))
                {
                    dbtype = CommonTypes.ParseDBType(settings.GetOptValue("Server.RDBMSType"));
                }

                if (dbtype != TDBType.ProgressODBC)
                {
                    throw new Exception("at the moment only Progress ODBC db is supported");
                }

                db.EstablishDBConnection(dbtype,
                                         settings.GetOptValue("Server.ODBC_DSN"),
                                         "",
                                         "",
                                         settings.GetOptValue("username"),
                                         settings.GetOptValue("password"),
                                         "");
                DBAccess.GDBAccessObj = db;

                TLogging.DebugLevel = 10;

                string sqlText = "";

                if (!settings.IsFlagSet("sql"))
                {
                    Console.WriteLine("Please enter sql and finish with semicolon: ");

                    while (!sqlText.Trim().EndsWith(";"))
                    {
                        sqlText += " " + Console.ReadLine();
                    }

                    sqlText = sqlText.Substring(0, sqlText.Length - 1);
                }
                else
                {
                    sqlText = settings.GetOptValue("sql");
                    Console.WriteLine(sqlText);
                }

                TDBTransaction transaction = DBAccess.GDBAccessObj.BeginTransaction(IsolationLevel.ReadUncommitted);

                DataTable table = db.SelectDT(sqlText, "temp", transaction);

                XmlDocument doc = TDataBase.DataTableToXml(table);

                if (settings.IsFlagSet("output"))
                {
                    if (settings.GetOptValue("output").EndsWith("yml"))
                    {
                        TYml2Xml.Xml2Yml(doc, settings.GetOptValue("output"));
                    }
                    else if (settings.GetOptValue("output").EndsWith("csv"))
                    {
                        TCsv2Xml.Xml2Csv(doc, settings.GetOptValue("output"));
                    }
                    else if (settings.GetOptValue("output").EndsWith("xml"))
                    {
                        StreamWriter sw = new StreamWriter(settings.GetOptValue("output"));
                        sw.Write(TXMLParser.XmlToString2(doc));
                        sw.Close();
                    }
                }
                else
                {
                    TYml2Xml.Xml2Yml(doc, "temp.yml");
                    StreamReader sr = new StreamReader("temp.yml");
                    Console.WriteLine(sr.ReadToEnd());
                    sr.Close();
                }

                db.RollbackTransaction();
                db.CloseDBConnection();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
Ejemplo n.º 10
0
        /// load data from csv files and sql statements
        public static bool LoadData(string AHostname, string ADatabaseName, string AUsername, string APassword, string ALoadSQLFileName)
        {
            StreamReader   sr = null;
            TDBTransaction WriteTransaction = new TDBTransaction();
            bool           SubmissionResult = false;

            TDataBase DBAccessObj = new TDataBase(TDBType.MySQL);

            try
            {
                DBAccessObj.EstablishDBConnection(TDBType.MySQL, AHostname, "", ADatabaseName, AUsername, APassword, "",
                                                  true,
                                                  "GenerateSQL.TLoadMysql.LoadData DB Connection");
                sr = new StreamReader(ALoadSQLFileName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                throw e;
            }

            DBAccessObj.WriteTransaction(ref WriteTransaction,
                                         ref SubmissionResult,
                                         delegate
            {
                // one command per line.
                // file is in postgresql syntax
                // either COPY FROM or INSERT

                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();

                    while (!line.Trim().StartsWith("--") && !line.Trim().EndsWith(";") && !sr.EndOfStream)
                    {
                        string templine = sr.ReadLine();

                        if (!templine.StartsWith("--"))
                        {
                            line += " " + templine;
                        }
                    }

                    if (line.Trim().ToUpper().StartsWith("INSERT"))
                    {
                        DBAccessObj.ExecuteNonQuery(line, WriteTransaction);
                    }
                    else if (line.Trim().ToUpper().StartsWith("UPDATE"))
                    {
                        DBAccessObj.ExecuteNonQuery(line, WriteTransaction);
                    }
                    else if (line.Trim().ToUpper().StartsWith("COPY"))
                    {
                        // pgsql: COPY p_language FROM 'c:/p_language.csv' WITH DELIMITER AS ',' NULL AS '?' CSV QUOTE AS '"' ESCAPE AS '"';
                        // mysql: LOAD DATA LOCAL INFILE 'c:/p_language.csv' INTO TABLE p_language FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '"';

                        // need to fix the NULL value from ? to NULL
                        string DataFilename = line.Substring(line.IndexOf("'") + 1);
                        DataFilename        = DataFilename.Substring(0, DataFilename.IndexOf("'"));
                        string TableName    = line.Substring(line.IndexOf("COPY ") + 5);
                        TableName           = TableName.Substring(0, TableName.IndexOf(" "));

                        StreamReader sData       = new StreamReader(DataFilename);
                        StreamWriter sDataWriter = new StreamWriter(DataFilename + ".local");
                        bool firstRow            = true;

                        while (!sData.EndOfStream)
                        {
                            string CSVDataQuestionMark = sData.ReadLine().Trim();
                            string CSVDataNULL         = string.Empty;

                            while (CSVDataQuestionMark.Length > 0)
                            {
                                bool quotedValue = CSVDataQuestionMark.StartsWith("\"");
                                string value     = StringHelper.GetNextCSV(ref CSVDataQuestionMark, ",");

                                if (value == "?")
                                {
                                    value = "NULL";
                                }

                                // if true or false is written in quotes, do not convert to integer. needed for a_account_property
                                if ((!quotedValue && (value == "false")) || (value == "no"))
                                {
                                    value = "0";
                                }

                                if ((!quotedValue && (value == "true")) || (value == "yes"))
                                {
                                    value = "1";
                                }

                                CSVDataNULL = StringHelper.AddCSV(CSVDataNULL, value);
                            }

                            if (CSVDataNULL.Length > 0)
                            {
                                if (firstRow)
                                {
                                    firstRow = false;
                                }
                                else
                                {
                                    sDataWriter.WriteLine();
                                }

                                sDataWriter.Write(CSVDataNULL);
                            }
                        }

                        sData.Close();
                        sDataWriter.Close();

                        // see also http://dev.mysql.com/doc/refman/5.1/en/insert-speed.html
                        string stmt = String.Format(
                            "LOAD DATA LOCAL INFILE '{0}' INTO TABLE {1} FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '"
                            +
                            Environment.NewLine + "';",
                            DataFilename + ".local",
                            TableName);

                        DBAccessObj.ExecuteNonQuery(stmt, WriteTransaction);
                    }
                }

                SubmissionResult = true;

                sr.Close();
            });

            DBAccessObj.CloseDBConnection();

            return(true);
        }
Ejemplo n.º 11
0
        public static void Main(string[] args)
        {
            try
            {
                // establish connection to database
                TCmdOpts settings = new TCmdOpts();

                if (!settings.IsFlagSet("Server.ODBC_DSN"))
                {
                    Console.WriteLine(
                        "sample call: " +
                        "ExportDataProgress.exe -Server.ODBC_DSN:Petra2_2sa -username:demo_sql -password:demo -sql:\"SELECT * from pub.a_account\" -output:test.xml");
                    Environment.Exit(-1);
                }

                new TLogging("debug.log");

                TDataBase db = new TDataBase();

                TDBType dbtype = TDBType.ProgressODBC;

                if (settings.IsFlagSet("Server.RDBMSType"))
                {
                    dbtype = CommonTypes.ParseDBType(settings.GetOptValue("Server.RDBMSType"));
                }

                if (dbtype != TDBType.ProgressODBC)
                {
                    throw new Exception("at the moment only Progress ODBC db is supported");
                }

                db.EstablishDBConnection(dbtype,
                    settings.GetOptValue("Server.ODBC_DSN"),
                    "",
                    "",
                    settings.GetOptValue("username"),
                    settings.GetOptValue("password"),
                    "");
                DBAccess.GDBAccessObj = db;

                TLogging.DebugLevel = 10;

                string sqlText = "";

                if (!settings.IsFlagSet("sql"))
                {
                    Console.WriteLine("Please enter sql and finish with semicolon: ");

                    while (!sqlText.Trim().EndsWith(";"))
                    {
                        sqlText += " " + Console.ReadLine();
                    }

                    sqlText = sqlText.Substring(0, sqlText.Length - 1);
                }
                else
                {
                    sqlText = settings.GetOptValue("sql");
                    Console.WriteLine(sqlText);
                }

                TDBTransaction transaction = DBAccess.GDBAccessObj.BeginTransaction(IsolationLevel.ReadUncommitted);

                DataTable table = db.SelectDT(sqlText, "temp", transaction);

                XmlDocument doc = TDataBase.DataTableToXml(table);

                if (settings.IsFlagSet("output"))
                {
                    if (settings.GetOptValue("output").EndsWith("yml"))
                    {
                        TYml2Xml.Xml2Yml(doc, settings.GetOptValue("output"));
                    }
                    else if (settings.GetOptValue("output").EndsWith("csv"))
                    {
                        TCsv2Xml.Xml2Csv(doc, settings.GetOptValue("output"));
                    }
                    else if (settings.GetOptValue("output").EndsWith("xml"))
                    {
                        StreamWriter sw = new StreamWriter(settings.GetOptValue("output"));
                        sw.Write(TXMLParser.XmlToString2(doc));
                        sw.Close();
                    }
                }
                else
                {
                    TYml2Xml.Xml2Yml(doc, "temp.yml");
                    StreamReader sr = new StreamReader("temp.yml");
                    Console.WriteLine(sr.ReadToEnd());
                    sr.Close();
                }

                db.RollbackTransaction();
                db.CloseDBConnection();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
Ejemplo n.º 12
0
        static private TDataBase GetDatabaseFromSession(bool AOpenConnection = true)
        {
            // if another thread gets called, then the session object is null
            if (HttpContext.Current == null)
            {
                if (Thread.CurrentThread.Name == null)
                {
                    throw new Exception(
                        "TOpenPetraOrgSessionManager.GetDatabaseFromSession: we do need a name for the thread for managing the database connection");
                }

                if (!FDatabaseObjects.ContainsKey(Thread.CurrentThread.Name))
                {
                    TDataBase db = new TDataBase();

                    if (AOpenConnection)
                    {
                        db.EstablishDBConnection(TSrvSetting.RDMBSType,
                            TSrvSetting.PostgreSQLServer,
                            TSrvSetting.PostgreSQLServerPort,
                            TSrvSetting.PostgreSQLDatabaseName,
                            TSrvSetting.DBUsername,
                            TSrvSetting.DBPassword,
                            "");
                    }

                    FDatabaseObjects.Add(Thread.CurrentThread.Name, db);
                }

                return FDatabaseObjects[Thread.CurrentThread.Name];
            }

            if (TSession.GetVariable("DBAccessObj") == null)
            {
                if (TServerManager.TheServerManager == null)
                {
                    TLogging.Log("GetDatabaseFromSession : TheServerManager is null");
                }
                else
                {
                    // disconnect web user after 2 minutes of inactivity. should disconnect itself already earlier
                    TServerManager.TheCastedServerManager.DisconnectTimedoutDatabaseConnections(2 * 60, "ANONYMOUS");

                    // disconnect normal users after 3 hours of inactivity
                    TServerManager.TheCastedServerManager.DisconnectTimedoutDatabaseConnections(3 * 60 * 60, "");

                    if (AOpenConnection)
                    {
                        TServerManager.TheCastedServerManager.EstablishDBConnection();
                    }
                }
            }

            return (TDataBase)TSession.GetVariable("DBAccessObj");
        }