Esempio n. 1
0
        public async Task <string> QueryAsync(string sqlStatement, RqLiteFlags flags = 0)
        {
            Exception ex = null;

            for (int i = 0; i < endpoint_uri.Count(); i++)
            {
                try
                {
                    string uri      = raftLeaderEndpoint.ToString() + "db/query?" + getFlagsQueryString(flags) + "&q=" + Uri.EscapeDataString(sqlStatement);
                    var    response = await client.GetAsync(uri);

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        return(await response.Content.ReadAsStringAsync());
                    }
                    if (response.StatusCode == System.Net.HttpStatusCode.MovedPermanently)
                    {
                        continue;
                    }
                    throw new Exception(response.StatusCode.ToString());
                }
                catch (Exception ex1)
                {
                    raftLeaderEndpoint = endpoint_uri[i];
                    ex = ex1;
                    continue;
                }
            }
            throw new Exception("No RqLite Leader Node found.", ex);
        }
Esempio n. 2
0
        public async Task <string> ExecuteAsync(IEnumerable <string> sqlStatements, RqLiteFlags flags = 0)
        {
            Exception     ex      = null;
            StringContent content = new StringContent(JsonSerializer.Serialize(sqlStatements), Encoding.UTF8, "application/json");

            for (int i = 0; i < endpoint_uri.Count(); i++)
            {
                try
                {
                    var response = await client.PostAsync(raftLeaderEndpoint.ToString() + "db/execute?" + getFlagsQueryString(flags), content);

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        return(await response.Content.ReadAsStringAsync());
                    }
                    if (response.StatusCode == System.Net.HttpStatusCode.MovedPermanently)
                    {
                        continue;
                    }
                    throw new Exception(response.StatusCode.ToString());
                }
                catch (Exception ex1)
                {
                    raftLeaderEndpoint = endpoint_uri[i];
                    ex = ex1;
                    continue;
                }
            }
            throw new Exception("No RqLite Leader Node found.", ex);
        }
Esempio n. 3
0
        private StringContent createPayload(IEnumerable <string> sqlStatements, RqLiteFlags flags = 0, params object[] parameters)
        {
            StringContent content = null;

            if (parameters.Length == 0)
            {
                content = new StringContent(JsonSerializer.Serialize(sqlStatements), Encoding.UTF8, "application/json");
            }
            else
            {
                if (sqlStatements.Count() > 1)
                {
                    throw new Exception("This Rqlite client does not (yet) support multiple parameterized SQL statements in one transaction.");
                }
                else
                {
                    var parameterizedPayload = new object[parameters.Count() + 1];
                    parameterizedPayload[0] = sqlStatements.ElementAt(0);
                    for (int i = 0; i < parameters.Count(); i++)
                    {
                        parameterizedPayload[i + 1] = parameters[i];
                    }
                    content = new StringContent(JsonSerializer.Serialize(new[] { parameterizedPayload }), Encoding.UTF8, "application/json");
                }
            }
            return(content);
        }
Esempio n. 4
0
        public async Task <string> ExecuteAsync(IEnumerable <string> sqlStatements, RqLiteFlags flags = 0, params object[] parameters)
        {
            Exception           ex       = null;
            var                 content  = createPayload(sqlStatements, flags, parameters);
            HttpResponseMessage response = null;

            for (int i = 0; i < endpoint_uri.Count(); i++)
            {
                try
                {
                    response = await client.PostAsync(raftLeaderEndpoint.ToString() + "db/execute?" + getFlagsQueryString(flags), content);

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        return(await response.Content.ReadAsStringAsync());
                    }
                    if (response.StatusCode == System.Net.HttpStatusCode.MovedPermanently)
                    {
                        continue;
                    }
                    throw new Exception(response.StatusCode.ToString());
                }
                catch (Exception ex1)
                {
                    switch (ex1.HResult)
                    {
                    case -2147467259:     // Connection refused, select a new leader.
                        if (i == endpoint_uri.Length)
                        {
                            throw new Exception("No RqLite Leader Node found.", ex);
                        }
                        i++;
                        raftLeaderEndpoint = endpoint_uri[i];
                        response           = await client.GetAsync(raftLeaderEndpoint.ToString() + "status");

                        break;

                    case -2146233088:
                        throw new RqLiteQueryException(sqlStatements.ElementAt(0), response);
                        break;

                    default:
                        throw ex1;
                    }
                    ex = ex1;
                    continue;
                }
            }
            throw new Exception("No RqLite Leader Node found.", ex);
        }
Esempio n. 5
0
        private string getFlagsQueryString(RqLiteFlags mask)
        {
            var queryString = new StringBuilder();

            if ((mask & RqLiteFlags.Pretty) == RqLiteFlags.Pretty)
            {
                queryString.Append("&pretty");
            }
            if ((mask & RqLiteFlags.Timings) == RqLiteFlags.Timings)
            {
                queryString.Append("&timings");
            }
            if ((mask & RqLiteFlags.Transaction) == RqLiteFlags.Transaction)
            {
                queryString.Append("&transaction");
            }
            return(queryString.ToString().TrimStart('&'));
        }
        public async void ShouldPerformParameterizedWriteAsync()
        {
            RqLiteFlags maskDefault = (RqLiteFlags.Pretty | RqLiteFlags.Timings | RqLiteFlags.Transaction);
            var         client      = new RqLiteClient(connectionString);
            var         dropTable   = await client.ExecuteAsync("DROP TABLE foo");

            var createTable = await client.ExecuteAsync("CREATE TABLE foo (id integer not null primary key, name text, age int)");

            Assert.Equal(dropTable, createTable);
            var parameters  = new object[] { "fiona", 20 };
            var createFiona = await client.ExecuteAsync("INSERT INTO foo(name,age) VALUES(?,?)", parameters);

            Assert.Contains("\"last_insert_id\":1,", createFiona);
            var readFiona = await client.QueryAsync("SELECT * FROM FOO WHERE name=?", parameters[0]);

            Assert.Contains("fiona", readFiona);
            Assert.Contains("20", readFiona);
        }
Esempio n. 7
0
        public async void ShouldPerformBasicFunctionsAsync()
        {
            var         connectionString = "http://localhost:4001,http://localhost:4002,http://localhost:4003";
            RqLiteFlags maskDefault      = (RqLiteFlags.Pretty | RqLiteFlags.Timings | RqLiteFlags.Transaction);
            RqLiteFlags maskTransaction  = (RqLiteFlags.Transaction);
            var         client           = new RqLiteClient(connectionString);
            var         dropTable        = await client.ExecuteAsync("DROP TABLE foo");

            var createTable = await client.ExecuteAsync("CREATE TABLE foo (id integer not null primary key, name text)");

            Assert.Equal(dropTable, createTable);
            var createFiona = await client.ExecuteAsync("INSERT INTO foo(name) VALUES(\"fiona\")");

            Assert.Equal("{\"results\":[{\"last_insert_id\":1,\"rows_affected\":1}]}", createFiona);
            var readFiona = await client.QueryAsync("SELECT * FROM FOO WHERE name=\"fiona\"");

            Assert.Equal("{\"results\":[{\"columns\":[\"id\",\"name\"],\"types\":[\"integer\",\"text\"],\"values\":[[1,\"fiona\"]]}]}", readFiona);
            // Check timings
            Assert.DoesNotContain("time", readFiona);
            // Check json pretty print
            Assert.DoesNotContain("\n", readFiona);
            // Do multiple inserts in a single transaction.
            var transaction = await client.ExecuteAsync(new string[] {
                "INSERT INTO foo(name) VALUES(\"gary\")",
                "INSERT INTO foo(name) VALUES(\"fred\")"
            }, maskTransaction);

            Assert.Equal("{\"results\":[{\"last_insert_id\":2,\"rows_affected\":1},{\"last_insert_id\":3,\"rows_affected\":1}]}", transaction);
            var count = await client.QueryAsync("SELECT COUNT(*) FROM foo");

            Assert.Equal("{\"results\":[{\"columns\":[\"COUNT(*)\"],\"types\":[\"\"],\"values\":[[3]]}]}", count);
            // Check flags
            readFiona = await client.QueryAsync("SELECT * FROM FOO WHERE name=\"fiona\"", maskDefault);

            // Check timings
            Assert.Contains("time", readFiona);
            // Check json pretty print
            Assert.Contains("\n", readFiona);
        }
Esempio n. 8
0
        public async Task <string> QueryAsync(string sqlStatement, RqLiteFlags flags = 0, params object[] parameters)
        {
            Exception ex      = null;
            var       content = createPayload(new[] { sqlStatement }, flags, parameters);

            for (int i = 0; i < endpoint_uri.Count(); i++)
            {
                try
                {
                    string uri = null;
                    HttpResponseMessage response = null;
                    if (parameters.Length == 0)
                    {
                        uri      = raftLeaderEndpoint.ToString() + "db/query?" + getFlagsQueryString(flags) + "&q=" + Uri.EscapeDataString(sqlStatement);
                        response = await client.GetAsync(uri);
                    }
                    else
                    {
                        response = await client.PostAsync(raftLeaderEndpoint.ToString() + "db/query?" + getFlagsQueryString(flags), content);
                    }
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        return(await response.Content.ReadAsStringAsync());
                    }
                    if (response.StatusCode == System.Net.HttpStatusCode.MovedPermanently)
                    {
                        continue;
                    }
                    throw new Exception(response.StatusCode.ToString());
                }
                catch (Exception ex1)
                {
                    raftLeaderEndpoint = endpoint_uri[i];
                    ex = ex1;
                    continue;
                }
            }
            throw new Exception("No RqLite Leader Node found.", ex);
        }
Esempio n. 9
0
 public string Query(string sqlStatement, RqLiteFlags flags = 0)
 {
     return(QueryAsync(sqlStatement, flags).Result);
 }
Esempio n. 10
0
 public string Execute(IEnumerable <string> sqlStatements, RqLiteFlags flags = 0)
 {
     return(ExecuteAsync(sqlStatements, flags).Result);
 }
Esempio n. 11
0
 public string Execute(string sqlStatement, RqLiteFlags flags = 0)
 {
     return(ExecuteAsync(new[] { sqlStatement }, flags).Result);
 }
Esempio n. 12
0
 public async Task <string> ExecuteAsync(string sqlStatement, RqLiteFlags flags = 0)
 {
     return(await ExecuteAsync(new[] { sqlStatement }, flags));
 }
Esempio n. 13
0
 public async Task <string> ExecuteAsync(string sqlStatement, RqLiteFlags flags, params object[] parameters)
 {
     return(await ExecuteAsync(new[] { sqlStatement }, flags, parameters));
 }