<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Threading;
using DotNetSimaticDatabaseProtokollerLibrary.Common;
using DotNetSimaticDatabaseProtokollerLibrary.Databases.Interfaces;
using DotNetSimaticDatabaseProtokollerLibrary.SettingsClasses.Datasets;
using DotNetSimaticDatabaseProtokollerLibrary.SettingsClasses.Storage;
using Npgsql;

namespace DotNetSimaticDatabaseProtokollerLibrary.Databases.PostgreSQL
{
    public class PostgreSQLStorage : IDBInterface, IDBViewable, IDBViewableSQL
    {
        private Action<string> _newDataCallback;
        public PostgreSQLStorage(Action<string> NewDataCallback)
        {
            _newDataCallback = NewDataCallback;
        }


        private PostgreSQLConfig myConfig;
        private IEnumerable<DatasetConfigRow> fieldList;
        private string dataTable;
        private string insertCommand = "";

        private DbConnection myDBConn;
        private DbCommand myCmd = new NpgsqlCommand();
        private DbDataReader myReader;
        
        
        public void Close()
        {
            if (myThread != null)
                myThread.Abort();
            if (myDBConn != null)
                myDBConn.Close();
        }

        public event ThreadExceptionEventHandler ThreadExceptionOccured;

        private string ConnectionString
        {
            get { return string.Format("server=" + myConfig.Server + ";user id=" + myConfig.Username + "; password=" + myConfig.Password + "; port=" + myConfig.Port + ";"); }
        }

        public void Connect_To_Database(StorageConfig config)
        {
            myConfig = config as PostgreSQLConfig;
            if (myConfig == null)
                throw new Exception("Database Config is NULL");
            try
            {
                myDBConn = new Npgsql.NpgsqlConnection(ConnectionString);
                myDBConn.Open();
                if (myDBConn.State != System.Data.ConnectionState.Open)
                    throw new Exception("Unable to Open Database. Storage:" + config.Name);                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void CreateOrModify_TablesAndFields(string dataTable, DatasetConfig datasetConfig)
        {
            this.dataTable = dataTable;
            this.fieldList = datasetConfig.DatasetConfigRows;

            string sql = "";
            try
            {
                sql = "CREATE DATABASE " + myConfig.Database + ";";
                myCmd.Connection = myDBConn;
                myCmd.CommandText = sql;<commit_msg>Fix in Protokoller<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Threading;
using DotNetSimaticDatabaseProtokollerLibrary.Common;
using DotNetSimaticDatabaseProtokollerLibrary.Databases.Interfaces;
using DotNetSimaticDatabaseProtokollerLibrary.SettingsClasses.Datasets;
using DotNetSimaticDatabaseProtokollerLibrary.SettingsClasses.Storage;
using Npgsql;

namespace DotNetSimaticDatabaseProtokollerLibrary.Databases.PostgreSQL
{
    public class PostgreSQLStorage : IDBInterface, IDBViewable, IDBViewableSQL
    {
        private Action<string> _newDataCallback;
        public PostgreSQLStorage(Action<string> NewDataCallback)
        {
            _newDataCallback = NewDataCallback;
        }


        private PostgreSQLConfig myConfig;
        private IEnumerable<DatasetConfigRow> fieldList;
        private string dataTable;
        private string insertCommand = "";

        private DbConnection myDBConn;
        private DbCommand myCmd = new NpgsqlCommand();
        private DbDataReader myReader;
        
        
        public void Close()
        {
            if (myThread != null)
                myThread.Abort();
            if (myDBConn != null)
                myDBConn.Close();
        }

        public event ThreadExceptionEventHandler ThreadExceptionOccured;

        private string ConnectionString
        {
            get { return string.Format("server=" + myConfig.Server + ";user id=" + myConfig.Username + "; password=" + myConfig.Password + "; port=" + myConfig.Port + ";"); }
        }

        public void Connect_To_Database(StorageConfig config)
        {
            myConfig = config as PostgreSQLConfig;
            if (myConfig == null)
                throw new Exception("Database Config is NULL");
            try
            {
                myDBConn = new Npgsql.NpgsqlConnection(ConnectionString);
                myDBConn.Open();
                if (myDBConn.State != System.Data.ConnectionState.Open)
                    throw new Exception("Unable to Open Database. Storage:" + config.Name);                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private DatasetConfig datasetConfig;

        public void CreateOrModify_TablesAndFields(string dataTable, DatasetConfig datasetConfig)
        {
            this.dataTable = dataTable;
            this.datasetConfig = datasetConfig;
            this.fieldList = datasetConfig.DatasetConfigRows;

            string sql = "";
            try
            {
                sql = "CREATE DATABASE " + myConfig.Database + ";";
                myCmd.Connection = myDBConn;
                myCmd.CommandText = sql;
{'fraction_non_alphanumeric': 0.0744661434568352, 'fraction_numerical': 0.0, 'mean_word_length': 2.140401146131805, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7325350', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1355, 'n_words': 304}