using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;  // MessageBox
using System.Data;
using FirebirdSql.Data.FirebirdClient;

using System.IO;

namespace FbConsole {
    public class Dbs {
        FbConnection _cnn;

        public void setDbs(string sDatabaseFile) {
            // Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string sPath;
            sPath = Environment.CurrentDirectory;
            //sPath = "C:\\Daten";
            if (!sPath.EndsWith("\\")) sPath += "\\";
            string sArg =
                "User=SYSDBA;" +
                "Password=masterkey;" +
                "Database=" + sDatabaseFile + ";" +
                "DataSource=localhost;" +
                "Port=3050;" +
                "Dialect=3;" +
                "Charset=NONE;" +
                "Role=;" +
                "Connection lifetime=15;" +
                "Pooling=true;" +
                "MinPoolSize=0;" +
                "MaxPoolSize=50;" +
                "Packet Size=8192;" +
                "ServerType=1";

            if (_cnn != null) {
                //if (_cnn.Close();  // pro Forma
            }
            _cnn = new FbConnection(sArg);
            if (_cnn == null)
                MessageBox.Show("Fehler beim Öffnen der Datenbank\r\n" +
                      sPath + sDatabaseFile, "Fehler");
            else                
                _cnn.Open();
        }  // dbs

        

        //_dbs.createDatabase("C:\\Daten\\test1.fdb");
        public void createDatabase(string filename) {
            if (File.Exists(filename)) {
                File.Delete(filename);
            }
            string sArg1 =
                "User=SYSDBA;" +
                "Password=masterkey;" +
                "Database=" + filename + ";" +
                "DataSource=localhost;" +
                "Port=3050;" +
                "Dialect=3;" +
                "Charset=NONE;" +
                "Role=;" +
                "Connection lifetime=15;" +
                "Pooling=true;" +
                "MinPoolSize=0;" +
                "MaxPoolSize=50;" +
                "Packet Size=8192;" +
                "ServerType=1";

            string sArg =
                "User=SYSDBA;" +
                "Password=masterkey;" +
                "Database=" + filename + ";" +
                "DataSource=localhost;" +
                "Dialect=3;" +
                "Packet Size=8192;" +
                "ServerType=1";
            FbConnection.CreateDatabase(sArg);
            // Alternative Parameter
            //FbConnection.CreateDatabase(connectionstring, pagesize, forcedwrite, overwrite
        }

        public void Close() {
            _cnn.Close();
        }

        // Ruft die internen Strukturen der Datenbanken ab
        public DataTable GetSchema(string sItem, string[] filter) {
            return _cnn.GetSchema(sItem, filter);
        }

        // hier gibt es nur eine Tabelle
        public DataTable getDBSData(string sSQL) {
            FbDataAdapter da = new FbDataAdapter(sSQL, _cnn);
            DataTable dt = new DataTable("RELATIONS");
            da.Fill(dt);
            //da.Dispose();
            return dt;
        }

        // hier kann man mehrere Tabellen erhalten
        public DataSet getDBSDataSet(string sSQL, string sTable) {
            FbDataAdapter da = new FbDataAdapter(sSQL, _cnn);
            //FbCommand command = new FbCommand(sSQL, _cnn);
            DataSet ds = new DataSet();
            //DataTable dt = new DataTable("RELATIONS");

            int n = ds.Tables.Count;
            da.Fill(ds, sTable);  // z. B. "Employee", wichtig da es mehrere gibt
            n = ds.Tables.Count;  // Anzahl der internen Tabellen
            //da.Dispose();
            return ds;
        }

    }
}
