Beispiel #1
0
        private static CallResult EstabilishConnection(JObject parameters)
        {
            var pms = AuthorizeConnect(parameters);

            if (pms == null)
            {
                return(CallResult.Error("Method EstabilishConnection got wrong parameters."));
            }
            (string dbName, string login, string password) = pms.Value;

            _dataAccess = new DataAccess(
                $"Host=localhost;Username={login};Password={password};Database={dbName}");
            try
            {
                var connection = _dataAccess.ProvideConnection();
                connection.Open();

                return(connection.IsOpen()
                    ? CallResult.Ok
                    : CallResult.Error("Coudn't open connection to database."));
            }
            catch (PostgresException e)
            {
                return(CallResult.Error(e.Message));
            }
            catch (SocketException)
            {
                Debug.Log("Please enable socket access to PostgreSQL server.");
                throw;
            }
        }
Beispiel #2
0
        private static CallResult Setup(JObject parameters)
        {
            if (AuthorizeConnect(parameters) == null)
            {
                return(CallResult.Error("Method Setup got wrong parameters."));
            }

            try
            {
                var connection = _dataAccess.ProvideConnection();
                var res        = _dataAccess.ExecuteSqlFromFile("db/open.sql", connection);
                if (res.Data != null && res.Data.Count == 0)
                {   //WORKAROUND, because it will be diff tested probably ;c
                    return(CallResult.Ok);
                }
                return(res);
            }
            catch (PostgresException e)
            {
                return(CallResult.Error(e.Message));
            }
            catch (SocketException)
            {
                Debug.Log("Please enable socket access to PostgreSQL server.");
                throw;
            }
        }
Beispiel #3
0
        private static Func <JObject, CallResult> SqlProcWrapper(
            string procName, params string[] parameters)
        {
            return(args =>
            {
                if (!ValidCallParameters(args, parameters))
                {
                    return CallResult.Error(
                        $"Invalid parameters for {procName} sql function.");
                }

                var actualParams = parameters.Select(
                    key => args[key].ToSqlString());

                return _dataAccess.ExecuteSqlFromString(
                    $"select * from {procName}("
                    + string.Join(", ", actualParams)
                    + ");");
            });
        }