Esempio n. 1
0
    void FirebirdAction(DbConnection connection)
    {
        using (var conn = LinqToDB.DataProvider.Firebird.FirebirdTools.CreateDataConnection(connection))
        {
            conn.Execute(@"
				UPDATE ""Person""
				SET
					""FirstName"" = @FIRSTNAME,
					""LastName""  = @LASTNAME
				WHERE ""PersonID"" = 4"                ,
                         new
            {
                FIRSTNAME = "Jürgen",
                LASTNAME  = "König",
            });

            using var _ = new DisableBaseline("Non-deterministic database cleanup");
            var sp     = conn.DataProvider.GetSchemaProvider();
            var schema = sp.GetSchema(conn, new GetSchemaOptions {
                GetProcedures = false
            });

            foreach (var table in schema.Tables)
            {
                if (table.TableName !.StartsWith("Animals") ||
                    table.TableName !.StartsWith("Eyes") ||
                    table.TableName !.StartsWith("xxPatient"))
                {
                    conn.Execute($"DROP TABLE \"{table.TableName}\"");
                }
            }
        }
    }
Esempio n. 2
0
        public void MergeInsert([MergeTests.MergeDataContextSource(false, TestProvName.AllSybase, TestProvName.AllInformix)] string context)
        {
            using var d1 = new DisableBaseline("Multi-threading");
            using var d2 = new DisableLogging();

            var testData = MultiThreadedData.TestData();

            // transaction (or delay) required for Access and Firebird, otherwise it is possible for other threads
            // to read incomplete results, because inserted data is not made available yet to other threads by
            // database engine
            using (var db = (DataConnection)GetDataContext(context))
                using (var table = db.CreateLocalTable(testData, true))
                {
                    ConcurrentRunner(db, context, 1,
                                     (threadDb, p) =>
                    {
                        var result = threadDb.GetTable <MultiThreadedData>()
                                     .Merge()
                                     .Using(testData.Where(x => x.Id <= p))
                                     .OnTargetKey()
                                     .InsertWhenNotMatched()
                                     .Merge();
                        return(result);
                    }, (result, p) =>
                    {
                    }, Enumerable.Range(1, 100).ToArray());
                }
        }
Esempio n. 3
0
        public void ParamOptimization([DataSources(false, TestProvName.AllSybase)] string context)
        {
            using var d1 = new DisableBaseline("Multi-threading");
            using var d2 = new DisableLogging();

            var testData = MultiThreadedData.TestData();

            // transaction (or delay) required for Access and Firebird, otherwise it is possible for other threads
            // to read incomplete results, because inserted data is not made available yet to other threads by
            // database engine
            using (var db = (DataConnection)GetDataContext(context))
                using (var table = db.CreateLocalTable(testData, true))
                {
                    ConcurrentRunner(db, context, 2,
                                     (threadDb, p) =>
                    {
                        var query = threadDb.GetTable <MultiThreadedData>().Where(x => p % 2 == 0 && x.Id == p || x.Id == p % 3 + 1);
                        return(query.Select(q => q.Id).ToArray());
                    }, (result, p) =>
                    {
                        var query    = testData.Where(x => p % 2 == 0 && x.Id == p || x.Id == p % 3 + 1);
                        var expected = query.Select(q => q.Id).ToArray();
                        AreEqual(expected, result);
                    }, Enumerable.Range(1, 50).ToArray());
                }
        }
Esempio n. 4
0
        public void EndsWithTests([DataSources(false, TestProvName.AllSybase)] string context)
        {
            using var d1 = new DisableBaseline("Multi-threading");
            using var d2 = new DisableLogging();

            var testData = MultiThreadedData.TestData();

            // transaction (or delay) required for Access and Firebird, otherwise it is possible for other threads
            // to read incomplete results, because inserted data is not made available yet to other threads by
            // database engine
            using (var db = (DataConnection)GetDataContext(context))
                using (var table = db.CreateLocalTable(testData, true))
                {
                    ConcurrentRunner(db, context, 10,
                                     (threadDb, p) =>
                    {
                        var query = threadDb.GetTable <MultiThreadedData>().Where(x => x.StrValue.Trim().EndsWith(p));
                        return(query.Select(q => q.StrValue).ToArray());
                    }, (result, p) =>
                    {
                        var query    = testData.Where(x => x.StrValue.EndsWith(p));
                        var expected = query.Select(q => q.StrValue).ToArray();
                        AreEqual(expected, result);
                    }, "1", "x1", "x11", "x33", "x2");
                }
        }
Esempio n. 5
0
        private void PrepareIdentityData(ITestDataContext db, string context)
        {
            using var _1 = new DisableBaseline("Test Setup");
            using var _2 = new DisableLogging();

            db.Patient.Delete();
            db.Doctor.Delete();
            db.Person.Delete();

            var id = 1;

            foreach (var person in IdentityPersons)
            {
                person.ID = id++;

                person.ID = Convert.ToInt32(db.InsertWithIdentity(person));
            }

            IdentityDoctors[0].PersonID = IdentityPersons[4].ID;
            IdentityDoctors[1].PersonID = IdentityPersons[5].ID;

            foreach (var doctor in IdentityDoctors)
            {
                db.Insert(doctor);
            }

            IdentityPatients[0].PersonID = IdentityPersons[2].ID;
            IdentityPatients[1].PersonID = IdentityPersons[3].ID;

            foreach (var patient in IdentityPatients)
            {
                db.Insert(patient);
            }
        }
Esempio n. 6
0
        public async Task Issue3137([IncludeDataSources(true, TestProvName.AllSQLite)] string context)
        {
            using var _ = new DisableBaseline("multi-threading");
            var rnd = new Random();

            const int runs = 10;

            var tasks = new Task[runs];

            for (int i = 0; i < tasks.Length; i++)
            {
                tasks[i] = Task.Run(execute);
            }

            await Task.WhenAll(tasks);

            async Task execute()
            {
                // add uniqueness to hints to ensure no hints spilling between contexts through query cache
                using (var db = GetDataContext(context))
                {
                    var corr        = rnd.Next();
                    var sharedHint  = $"-- many {corr}!";
                    var oneTimeHint = $"-- once {corr}!";
                    db.QueryHints.Add(sharedHint);
                    db.NextQueryHints.Add(oneTimeHint);

                    var query = db.Parent.Where(r => r.ParentID == 11);
                    var sql   = db is DataConnection ? null : query.ToString();
                    await query.ToListAsync();

                    if (db is DataConnection dc)
                    {
                        sql = dc.LastQuery !;
                    }

                    Assert.True(sql !.Contains(sharedHint), $"(1) expected {sharedHint}. Has alien hint: {sql.Contains("many")}");
                    Assert.True(sql.Contains(oneTimeHint), $"(1) expected {oneTimeHint}. Has alien hint: {sql.Contains("once")}");

                    query = db.Parent.Where(r => r.ParentID == 11);
                    sql   = db is DataConnection ? null : query.ToString();
                    await query.ToListAsync();

                    if (db is DataConnection dc2)
                    {
                        sql = dc2.LastQuery !;
                    }

                    Assert.True(sql !.Contains(sharedHint), $"(2) expected {sharedHint}. Has alien hint: {sql.Contains("many")}");
                    Assert.False(sql.Contains(oneTimeHint), $"(2) expected no {oneTimeHint}");
                    Assert.False(sql.Contains("once"), $"(2) alien one-time hint found");
                }
            }
        }
Esempio n. 7
0
        public void Issue3017([IncludeDataSources(TestProvName.AllSqlServer)] string context)
        {
            using var scope = new DisableBaseline("Multithreading");

            var tasks = new List <Task>();

            for (var i = 0; i < 30; i++)
            {
                var local = i;
                tasks.Add(Task.Run(() => Issue3017Action(context)));
            }

            Task.WaitAll(tasks.ToArray());

            foreach (var task in tasks)
            {
                Assert.False(task.IsFaulted);
            }
        }
Esempio n. 8
0
        public void EagerLoadMultiLevel([IncludeDataSources(TestProvName.AllSqlServer)] string context)
        {
            using var d1 = new DisableBaseline("Multi-threading");
            using var d2 = new DisableLogging();

            var testData = MultiThreadedData.TestData();

            using (var db = (DataConnection)GetDataContext(context))
                using (var table = db.CreateLocalTable(testData, true))
                {
                    ConcurrentRunner(db, context, 1,
                                     (threadDb, p) =>
                    {
                        var param1 = p;
                        var param2 = p;
                        var result = threadDb.GetTable <MultiThreadedData>()
                                     .Where(t => t.Id > param1)
                                     .Select(t => new
                        {
                            t.Id,
                            t.StrValue,
                            t.Value,
                            Others = threadDb.GetTable <MultiThreadedData>().Where(x => x.Id > t.Id && x.Id > param1 && x.Id > param2).OrderBy(x => x.Id)
                                     .Select(o => new
                            {
                                o.Id,
                                o.StrValue,
                                Param     = param1,
                                SubOthers = threadDb.GetTable <MultiThreadedData>()
                                            .Where(x => x.Id > o.Id)
                                            .OrderBy(x => x.Id)
                                            .ToArray()
                            }).ToArray()
                        })
                                     .ToList();
                        return(result);
                    }, (result, p) =>
                    {
                        var param1   = p;
                        var param2   = p;
                        var expected = testData
                                       .Where(t => t.Id > param1)
                                       .Select(t => new
                        {
                            t.Id,
                            t.StrValue,
                            t.Value,
                            Others = testData.Where(x => x.Id > t.Id && x.Id > param1 && x.Id > param2).OrderBy(x => x.Id)
                                     .Select(o => new
                            {
                                o.Id,
                                o.StrValue,
                                Param     = param1,
                                SubOthers = testData
                                            .Where(x => x.Id > o.Id)
                                            .OrderBy(x => x.Id)
                                            .ToArray()
                            }).ToArray()
                        })
                                       .ToList();

                        result.Count.Should().Be(expected.Count);

                        if (expected.Count > 0)
                        {
                            AreEqualWithComparer(result, expected);
                        }
                    }, Enumerable.Range(1, 100).ToArray());
                }
        }
Esempio n. 9
0
        public void EagerLoadMultiLevel([IncludeDataSources(TestProvName.AllSQLite)] string context)
        {
#if NETFRAMEWORK
            if (context.Contains(ProviderName.SQLiteMS))
            {
                Assert.Inconclusive("Disabled due to sporadical errors due to old SQLite version (SQLite Error 5: 'database is locked')");
            }
#endif

            using var d1 = new DisableBaseline("Multi-threading");
            using var d2 = new DisableLogging();

            var testData = MultiThreadedData.TestData();

            using (var db = (DataConnection)GetDataContext(context))
                using (var table = db.CreateLocalTable(testData, true))
                {
                    ConcurrentRunner(db, context, 1,
                                     (threadDb, p) =>
                    {
                        var param1 = p;
                        var param2 = p;
                        var result = threadDb.GetTable <MultiThreadedData>()
                                     .Where(t => t.Id > param1)
                                     .Select(t => new
                        {
                            t.Id,
                            t.StrValue,
                            t.Value,
                            Others = threadDb.GetTable <MultiThreadedData>().Where(x => x.Id > t.Id && x.Id > param1 && x.Id > param2).OrderBy(x => x.Id)
                                     .Select(o => new
                            {
                                o.Id,
                                o.StrValue,
                                Param     = param1,
                                SubOthers = threadDb.GetTable <MultiThreadedData>()
                                            .Where(x => x.Id > o.Id)
                                            .OrderBy(x => x.Id)
                                            .ToArray()
                            }).ToArray()
                        })
                                     .ToList();
                        return(result);
                    }, (result, p) =>
                    {
                        var param1   = p;
                        var param2   = p;
                        var expected = testData
                                       .Where(t => t.Id > param1)
                                       .Select(t => new
                        {
                            t.Id,
                            t.StrValue,
                            t.Value,
                            Others = testData.Where(x => x.Id > t.Id && x.Id > param1 && x.Id > param2).OrderBy(x => x.Id)
                                     .Select(o => new
                            {
                                o.Id,
                                o.StrValue,
                                Param     = param1,
                                SubOthers = testData
                                            .Where(x => x.Id > o.Id)
                                            .OrderBy(x => x.Id)
                                            .ToArray()
                            }).ToArray()
                        })
                                       .ToList();

                        result.Count.Should().Be(expected.Count);

                        if (expected.Count > 0)
                        {
                            AreEqualWithComparer(result, expected);
                        }
                    }, Enumerable.Range(1, 100).ToArray());
                }
        }