public void TestConnectDoubleOpen()
        {
            MonetDbConnection conn = new MonetDbConnection(TestConnectionString);

            conn.Open();
            conn.Open();
        }
Пример #2
0
 public void TestConnectDoubleOpen()
 {
     Assert.ThrowsException <InvalidOperationException>(() =>
     {
         var conn = new MonetDbConnection(TestConnectionString);
         conn.Open();
         conn.Open();
     });
 }
Пример #3
0
        public void TestConnect()
        {
            var connection = new MonetDbConnection();

            Assert.IsTrue(connection.State == ConnectionState.Closed);

            Assert.ThrowsException <InvalidOperationException>(() => connection.Open());

            connection = new MonetDbConnection(TestConnectionString);
            connection.Open();
            Assert.IsTrue(connection.State == ConnectionState.Open);
            Assert.AreEqual(connection.Database, "demo");
            connection.Close();
            Assert.IsTrue(connection.State == ConnectionState.Closed);
            Assert.AreEqual(connection.Database, "demo");

            try
            {
                connection = new MonetDbConnection(TestConnectionString.Replace("ssl=false", "ssl=true"));
                connection.Open();
            }
            catch (MonetDbException ex)
            {
                if (ex.Message.IndexOf("not supported", StringComparison.InvariantCultureIgnoreCase) < 0)
                {
                    throw;
                }
            }
            finally
            {
                connection.Close();
            }
        }
Пример #4
0
        public void TestParallel()
        {
            Parallel.For(0, 100, new ParallelOptions {
                MaxDegreeOfParallelism = 2
            }, i =>
            {
                using (var connection = new MonetDbConnection(TestConnectionString))
                {
                    connection.Open();

                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = $"SELECT {i} as n";

                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Assert.AreEqual(i, reader.GetInt32(0));
                            }
                        }
                    }
                }
            });
        }
Пример #5
0
        public void TestSchemaTable()
        {
            return;

            var query = "SELECT * FROM env();";

            using (var connection = new MonetDbConnection(TestConnectionString))
            {
                connection.Open();

                using (var command = connection.CreateCommand())
                {
                    // create table
                    command.CommandText = query;

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var schema = reader.GetSchemaTable();

                            Assert.IsNotNull(schema);
                            Assert.IsTrue(schema.Columns.Contains("name"));
                            Assert.IsTrue(schema.Columns.Contains("value"));
                            Assert.AreEqual(typeof(string), schema.Columns["name"].DataType);
                            Assert.AreEqual(typeof(string), schema.Columns["value"].DataType);
                        }
                    }
                }
            }
        }
Пример #6
0
        public void TestSchemaTable()
        {
            var query = "SELECT * FROM env();";

            using (var connection = new MonetDbConnection(TestConnectionString))
            {
                connection.Open();

                using (var command = connection.CreateCommand())
                {
                    // create table
                    command.CommandText = query;

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var schema = reader.GetSchemaTable();

                            Assert.IsNotNull(schema);
                            Assert.IsTrue((string)schema.Rows[0]["ColumnName"] == "name");
                            Assert.IsTrue((string)schema.Rows[1]["ColumnName"] == "value");
                        }
                    }
                }
            }
        }
Пример #7
0
        public void BulkLoadSTDINTest()
        {
            // random table name
            var tableName = Guid.NewGuid().ToString();

            var id   = 1;
            var data = new bool[10].ToDictionary(x => id++, x => Guid.NewGuid().ToString());

            // SQL scripts
            var createScript = string.Format("CREATE TABLE \"{0}\" (id int, t text);", tableName);
            var copyScript   = new StringBuilder($"COPY 10 records INTO \"{tableName}\" (id,t) FROM STDIN DELIMITERS ',','\\n';");
            var selectScript = string.Format("SELECT * FROM \"{0}\";", tableName);
            var dropScript   = string.Format("DROP TABLE \"{0}\";", tableName);

            using (var connection = new MonetDbConnection(TestConnectionString))
            {
                connection.Open();

                using (var command = connection.CreateCommand())
                {
                    // create table
                    command.CommandText = createScript;
                    command.ExecuteNonQuery();

                    try
                    {
                        command.CommandText = copyScript.ToString();
                        command.ExecuteNonQuery();

                        copyScript.Clear();
                        foreach (var row in data)
                        {
                            copyScript.AppendLine($"{row.Key},{row.Value}");
                        }

                        // copyScript.Append("^D");

                        command.CommandText = copyScript.ToString();
                        command.ExecuteNonQuery();

                        // select from
                        command.CommandText = selectScript;
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            id = reader.GetInt32(0);
                            var t = reader.GetString(1);
                        }
                    }
                    finally
                    {
                        // drop table
                        command.CommandText = dropScript;
                        command.ExecuteNonQuery();
                    }
                }
            }
        }
        public void TestChangeDatabase()
        {
            MonetDbConnection conn = new MonetDbConnection(TestConnectionString);

            conn.Open();
            Assert.IsTrue(conn.State == System.Data.ConnectionState.Open);

            conn.ChangeDatabase("somethingelse");
        }
        public object Execute([FromBody] Request request)
        {
            try
            {
                var csb = new MonetDbConnectionStringBuilder
                {
                    Host     = request.Host,
                    Port     = request.Port,
                    Username = request.Username,
                    Password = request.Password,
                    Database = request.Database
                };

                using (var connection = new MonetDbConnection(csb.ToString()))
                {
                    connection.Open();

                    using (var command = connection.CreateCommand())
                    {
                        lastCommand = command;

                        command.CommandText = request.Query;

                        using (var reader = command.ExecuteReader())
                        {
                            var result = new Result(reader.FieldCount);
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                result.Meta[i] = reader.GetName(i);
                            }

                            while (reader.Read())
                            {
                                var row = new object[reader.FieldCount];
                                for (int i = 0; i < reader.FieldCount; i++)
                                {
                                    row[i] = reader.GetValue(i);
                                }

                                result.Data.Add(row);
                            }

                            return(result);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(new
                {
                    Error = ex.Message + Environment.NewLine + ex.StackTrace
                });
            }
        }
Пример #10
0
        public void TestChangeDatabase()
        {
            Assert.ThrowsException <MonetDbException>(() =>
            {
                var conn = new MonetDbConnection(TestConnectionString);
                conn.Open();
                Assert.IsTrue(conn.State == ConnectionState.Open);

                conn.ChangeDatabase("demo2");
            },
                                                      "This should throw a message that the database doesn't exist, but it's successfully changing the database name and reconnecting if it's doing so");
        }
Пример #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static IDataReader ExecuteReader(string connectionString, string commandText)
        {
            var connection = new MonetDbConnection(connectionString);

            connection.Open();

            var command = new MonetDbCommand(connection)
            {
                CommandText = commandText
            };

            return(command.ExecuteReader());
        }
Пример #12
0
        public void TestQuotes()
        {
            using (var connection = new MonetDbConnection(TestConnectionString))
            {
                connection.Open();

                using (var command = connection.CreateCommand())
                {
                    command.CommandText = $"SELECT 'q \"q' as n";

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Assert.AreEqual("q \"q", reader.GetString(0));
                        }
                    }
                }
            }
        }
Пример #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string connectionString, string commandText)
        {
            int result;

            using (var connection = new MonetDbConnection(connectionString))
            {
                connection.Open();

                using (var command = new MonetDbCommand(connection)
                {
                    CommandText = commandText
                })
                {
                    result = command.ExecuteNonQuery();
                }

                connection.Close();
            }

            return(result);
        }
Пример #14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string connectionString, string commandText)
        {
            object result;

            using (var connection = new MonetDbConnection(connectionString))
            {
                connection.Open();

                using (var command = new MonetDbCommand(connection)
                {
                    CommandText = commandText
                })
                {
                    result = command.ExecuteScalar();
                }

                connection.Close();
            }

            return(result);
        }
Пример #15
0
        public void TestCreateInsertSelectDropTable()
        {
            // random table name
            var tableName = Guid.NewGuid().ToString();

            // random integer value
            var value = new Random().Next();

            // SQL scripts
            var createScript = string.Format("CREATE TABLE \"{0}\" (id int);", tableName);
            var insertScript = string.Format("INSERT INTO \"{0}\" VALUES({1});", tableName, value);
            var selectScript = string.Format("SELECT * FROM \"{0}\";", tableName);
            var dropScript   = string.Format("DROP TABLE \"{0}\";", tableName);

            using (var connection = new MonetDbConnection(TestConnectionString))
            {
                connection.Open();

                using (var command = connection.CreateCommand())
                {
                    // create table
                    command.CommandText = createScript;
                    command.ExecuteNonQuery();

                    // insert into
                    command.CommandText = insertScript;
                    // rows affected 0 or 1
                    Assert.IsTrue(new[] { 0, 1 }.Contains(command.ExecuteNonQuery()));

                    // select from
                    command.CommandText = selectScript;
                    var value2 = (int)command.ExecuteScalar();
                    Assert.AreEqual(value, value2);

                    // drop table
                    command.CommandText = dropScript;
                    command.ExecuteNonQuery();
                }
            }
        }
Пример #16
0
        public void TestMultiLine()
        {
            using (var connection = new MonetDbConnection(TestConnectionString))
            {
                connection.Open();

                Assert.ThrowsException <MonetDbException>(() =>
                {
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = @"
SELECT 1 as n
fail";

                        using (var reader = command.ExecuteReader())
                        {
                            reader.Read();
                        }
                    }
                });

                using (var command = connection.CreateCommand())
                {
                    command.CommandText = @"
SELECT 1 as n
UNION ALL
SELECT 2 as n";

                    using (var reader = command.ExecuteReader())
                    {
                        reader.Read();
                        Assert.AreEqual(1, reader.GetInt32(0));

                        reader.Read();
                        Assert.AreEqual(2, reader.GetInt32(0));
                    }
                }
            }
        }
Пример #17
0
        public void LargeQueryTest()
        {
            var n    = 50;
            var cols = new string[n];

            for (int i = 0; i < n; i++)
            {
                cols[i] = "col";
                for (int k = 0; k <= i; k++)
                {
                    cols[i] += k;
                }
            }

            var sql = "SELECT '" + string.Join("', '", cols) + "';";

            var length = sql.Length;

            using (var connection = new MonetDbConnection(TestConnectionString))
            {
                connection.Open();

                using (var command = connection.CreateCommand())
                {
                    command.CommandText = sql;

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            for (int i = 0; i < n; i++)
                            {
                                Assert.AreEqual(cols[i], reader.GetString(i));
                            }
                        }
                    }
                }
            }
        }
Пример #18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(string connectionString, string commandText)
        {
            DataSet ds;

            using (var connection = new MonetDbConnection(connectionString))
            {
                connection.Open();

                using (var command = new MonetDbCommand(connection)
                {
                    CommandText = commandText
                })
                {
                    using (var reader = command.ExecuteReader())
                    {
                        ds = reader.ToDateSet();
                    }
                }

                connection.Close();
            }

            return(ds);
        }