Ejemplo n.º 1
0
		[Test, NorthwindDataContext(true)] // SQLite returns Int64 for column instead of Int32
		public void Test1(string context)
		{
			var connectionString = DataConnection.GetConnectionString(context);
			var dataProvider     = DataConnection.GetDataProvider(context);


			using (var conn = new DataConnection(dataProvider, connectionString))
			{
				conn.InitCommand(CommandType.Text, "SELECT 1", null, null);

				var rd = conn.Command.ExecuteReader();

				if (rd.Read())
				{
					var dp   = conn.DataProvider;
					var p    = Expression.Parameter(typeof(IDataReader));
					var dr   = Expression.Convert(p, dp.DataReaderType);
					var ex   = (Expression<Func<IDataReader,int,int>>)dp.GetReaderExpression(conn.MappingSchema, rd, 0, dr, typeof(int));
					var func = ex.Compile();

					do
					{
						var value = func(rd, 0);
						Assert.AreEqual(1, value);
					} while (rd.Read());
				}
				else
				{
					Assert.Fail();
				}
			}
		}
Ejemplo n.º 2
0
        public void Test1(string context)
        {
            var connectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;

            using (var conn = new DataConnection(SqlServerTools.GetDataProvider(), connectionString))
            {
                conn.SetCommand("SELECT 1");

                var rd = conn.Command.ExecuteReader();

                if (rd.Read())
                {
                    var dp   = conn.DataProvider;
                    var p    = Expression.Parameter(typeof(IDataReader));
                    var dr   = Expression.Convert(p, dp.DataReaderType);
                    var ex   = (Expression<Func<IDataReader,int,int>>)dp.GetReaderExpression(conn.MappingSchema, rd, 0, dr, typeof(int));
                    var func = ex.Compile();

                    do
                    {
                        var value = func(rd, 0);
                        Assert.AreEqual(1, value);
                    } while (rd.Read());
                }
                else
                {
                    Assert.Fail();
                }
            }
        }
Ejemplo n.º 3
0
        public void TestChar([IncludeDataContexts(CurrentProvider)] string context)
        {
            using (var conn = new DataConnection(context))
            {
                Assert.That(conn.Execute<char> ("SELECT Cast('1' as char)"),         Is.EqualTo('1'));
                Assert.That(conn.Execute<char?>("SELECT Cast('1' as char)"),         Is.EqualTo('1'));
                Assert.That(conn.Execute<char> ("SELECT Cast('1' as char(1))"),      Is.EqualTo('1'));
                Assert.That(conn.Execute<char?>("SELECT Cast('1' as char(1))"),      Is.EqualTo('1'));

                Assert.That(conn.Execute<char> ("SELECT @p",                  DataParameter.Char("p",  '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char?>("SELECT @p",                  DataParameter.Char("p",  '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char> ("SELECT Cast(@p as char)",    DataParameter.Char("p",  '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char?>("SELECT Cast(@p as char)",    DataParameter.Char("p",  '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char> ("SELECT Cast(@p as char(1))", DataParameter.Char("@p", '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char?>("SELECT Cast(@p as char(1))", DataParameter.Char("@p", '1')), Is.EqualTo('1'));

                Assert.That(conn.Execute<char> ("SELECT @p", DataParameter.VarChar ("p", '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char?>("SELECT @p", DataParameter.VarChar ("p", '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char> ("SELECT @p", DataParameter.NChar   ("p", '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char?>("SELECT @p", DataParameter.NChar   ("p", '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char> ("SELECT @p", DataParameter.NVarChar("p", '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char?>("SELECT @p", DataParameter.NVarChar("p", '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char> ("SELECT @p", DataParameter.Create  ("p", '1')), Is.EqualTo('1'));
                Assert.That(conn.Execute<char?>("SELECT @p", DataParameter.Create  ("p", '1')), Is.EqualTo('1'));

                Assert.That(conn.Execute<char> ("SELECT @p", new DataParameter { Name = "p", Value = '1' }), Is.EqualTo('1'));
                Assert.That(conn.Execute<char?>("SELECT @p", new DataParameter { Name = "p", Value = '1' }), Is.EqualTo('1'));
            }
        }
Ejemplo n.º 4
0
        public void Test(string context)
        {
            SqlServerTools.ResolveSqlTypes("");

            using (var conn = new DataConnection(context))
            {
                var sp       = conn.DataProvider.GetSchemaProvider();
                var dbSchema = sp.GetSchema(conn);

                dbSchema.Tables.ToDictionary(
                    t => t.IsDefaultSchema ? t.TableName : t.SchemaName + "." + t.TableName,
                    t => t.Columns.ToDictionary(c => c.ColumnName));

                var table = dbSchema.Tables.SingleOrDefault(t => t.TableName.ToLower() == "parent");

                Assert.That(table,               Is.Not.Null);
                Assert.That(table.Columns.Count, Is.EqualTo(2));

            //				Assert.That(dbSchema.Tables.Single(t => t.TableName.ToLower() == "doctor").ForeignKeys.Count, Is.EqualTo(1));

                switch (context)
                {
                    case ProviderName.SqlServer2000 :
                    case ProviderName.SqlServer2005 :
                    case ProviderName.SqlServer2008 :
                    case ProviderName.SqlServer2012 :
                        var indexTable = dbSchema.Tables.Single(t => t.TableName == "IndexTable");
                        Assert.That(indexTable.ForeignKeys.Count,                Is.EqualTo(1));
                        Assert.That(indexTable.ForeignKeys[0].ThisColumns.Count, Is.EqualTo(2));
                        break;
                }
            }
        }
Ejemplo n.º 5
0
		public void TestDataTypes(string context)
		{
			using (var conn = new DataConnection(context))
			{
				Assert.That(TestType<long?>       (conn, "bigintDataType",   DataType.Int64),     Is.EqualTo(1000000L));
				Assert.That(TestType<long?>       (conn, "int8DataType",     DataType.Int64),     Is.EqualTo(1000001L));
				Assert.That(TestType<int?>        (conn, "intDataType",      DataType.Int32),     Is.EqualTo(7777777));
				Assert.That(TestType<short?>      (conn, "smallintDataType", DataType.Int16),     Is.EqualTo(100));
				Assert.That(TestType<decimal?>    (conn, "decimalDataType",  DataType.Decimal),   Is.EqualTo(9999999m));
				Assert.That(TestType<IfxDecimal?> (conn, "decimalDataType",  DataType.Decimal),   Is.EqualTo(new IfxDecimal(9999999m)));
				Assert.That(TestType<decimal?>    (conn, "moneyDataType",    DataType.Money),     Is.EqualTo(8888888m));
				Assert.That(TestType<float?>      (conn, "realDataType",     DataType.Single),    Is.EqualTo(20.31f));
				Assert.That(TestType<double?>     (conn, "floatDataType",    DataType.Double),    Is.EqualTo(16.2d));

				Assert.That(TestType<bool?>       (conn, "boolDataType",     DataType.Boolean),   Is.EqualTo(true));

				Assert.That(TestType<string>      (conn, "charDataType",     DataType.Char),      Is.EqualTo("1"));
				Assert.That(TestType<string>      (conn, "varcharDataType",  DataType.VarChar),   Is.EqualTo("234"));
				Assert.That(TestType<string>      (conn, "ncharDataType",    DataType.NChar),     Is.EqualTo("55645"));
				Assert.That(TestType<string>      (conn, "nvarcharDataType", DataType.NVarChar),  Is.EqualTo("6687"));
				Assert.That(TestType<string>      (conn, "lvarcharDataType", DataType.NVarChar),  Is.EqualTo("AAAAA"));

				Assert.That(TestType<DateTime?>   (conn, "dateDataType",     DataType.Date),      Is.EqualTo(new DateTime(2012, 12, 12)));
				Assert.That(TestType<DateTime?>   (conn, "datetimeDataType", DataType.DateTime2), Is.EqualTo(new DateTime(2012, 12, 12, 12, 12, 12)));
				Assert.That(TestType<IfxDateTime?>(conn, "datetimeDataType", DataType.DateTime),  Is.EqualTo(new IfxDateTime(new DateTime(2012, 12, 12, 12, 12, 12))));
				Assert.That(TestType<TimeSpan?>   (conn, "intervalDataType", DataType.Time),      Is.EqualTo(new TimeSpan(12, 12, 12)));
				Assert.That(TestType<IfxTimeSpan?>(conn, "intervalDataType", DataType.Time),      Is.EqualTo(new IfxTimeSpan(new TimeSpan(12, 12, 12))));

				Assert.That(TestType<string>      (conn, "textDataType",     DataType.Text,      skipPass:true), Is.EqualTo("BBBBB"));
				Assert.That(TestType<string>      (conn, "textDataType",     DataType.NText,     skipPass:true), Is.EqualTo("BBBBB"));
				Assert.That(TestType<byte[]>      (conn, "byteDataType",     DataType.Binary,    skipPass:true), Is.EqualTo(new byte[] { 1, 2 }));
				Assert.That(TestType<byte[]>      (conn, "byteDataType",     DataType.VarBinary, skipPass:true), Is.EqualTo(new byte[] { 1, 2 }));
			}
		}
Ejemplo n.º 6
0
        public void Test3([IncludeDataContexts(
            ProviderName.SqlServer,
            ProviderName.SqlServer2008,
            ProviderName.SqlServer2008 + ".1",
            ProviderName.SqlServer2005,
            ProviderName.SqlServer2005 + ".1",
            ProviderName.Access
            )] string context)
        {
            using (var conn = new DataConnection(context))
            {
                Assert.That(conn.Connection.State,    Is.EqualTo(ConnectionState.Open));
                Assert.That(conn.ConfigurationString, Is.EqualTo(context));

                if (context.EndsWith(".2005"))
                {
                    var sdp = conn.DataProvider;
                    Assert.That(sdp.Name, Is.EqualTo("SqlServer.2005"));
                }

                if (context.EndsWith(".2008"))
                {
                    var sdp = conn.DataProvider;
                    Assert.That(sdp.Name, Is.EqualTo("SqlServer.2008"));
                }
            }
        }
Ejemplo n.º 7
0
 protected override void TearDown()
 {
     using (var dbManager = new DataConnection("PerformanceTest"))
         dbManager
             .SetCommand("TRUNCATE TABLE Simplests")
             .Execute();
 }
Ejemplo n.º 8
0
        public void Test1([IncludeDataContexts("Northwind")] string context)
        {
            using (var conn = new DataConnection(SqlServerFactory.GetDataProvider(), "Server=.;Database=Northwind;Integrated Security=SSPI"))
            {
                conn.SetCommand("SELECT 1");

                var rd = conn.Command.ExecuteReader();

                if (rd.Read())
                {
                    var dp   = conn.DataProvider;
                    var p    = Expression.Parameter(typeof(IDataReader));
                    var dr   = Expression.Convert(p, dp.DataReaderType);
                    var ex   = (Expression<Func<IDataReader,int,int>>)dp.GetReaderExpression(conn.MappingSchema, rd, 0, dr, typeof(int));
                    var func = ex.Compile();

                    do
                    {
                        var value = func(rd, 0);
                        Assert.AreEqual(1, value);
                    } while (rd.Read());
                }
                else
                {
                    Assert.Fail();
                }
            }
        }
Ejemplo n.º 9
0
 public void NorthwindTest([IncludeDataContexts("Northwind")] string context)
 {
     using (var conn = new DataConnection(context))
     {
         var sp       = conn.DataProvider.GetSchemaProvider();
         var dbSchema = sp.GetSchema(conn);
     }
 }
Ejemplo n.º 10
0
		public void Test2()
		{
			using (var conn = new DataConnection())
			{
				Assert.That(conn.Connection.State,    Is.EqualTo(ConnectionState.Open));
				Assert.That(conn.ConfigurationString, Is.EqualTo(DataConnection.DefaultConfiguration));
			}
		}
Ejemplo n.º 11
0
 public void Test1([IncludeDataContexts("Northwind")] string context)
 {
     using (var conn = new DataConnection(SqlServerFactory.GetDataProvider(), "Server=.;Database=Northwind;Integrated Security=SSPI"))
     {
         Assert.That(conn.Connection.State,    Is.EqualTo(ConnectionState.Open));
         Assert.That(conn.ConfigurationString, Is.Null);
     }
 }
 public ReplaceDataObjectsInBulkActorFactory(
     IReadOnlyCollection<Type> dataObjectTypes,
     DataConnection sourceDataConnection,
     DataConnection targetDataConnection)
 {
     _dataObjectTypes = dataObjectTypes;
     _sourceDataConnection = sourceDataConnection;
     _targetDataConnection = targetDataConnection;
 }
Ejemplo n.º 13
0
		public void TestScalar3(string context)
		{
			using (var conn = new DataConnection(context))
			{
				var list = conn.Query<DateTimeOffset>("SELECT CURRENT_TIMESTAMP").ToList();

				Assert.That(list.Count, Is.EqualTo(1));
			}
		}
Ejemplo n.º 14
0
		public void TestScalar2([IncludeDataContexts(ProviderName.SqlServer)] string context)
		{
			using (var conn = new DataConnection(context))
			{
				var list = conn.Query<int>("SELECT 1").ToList();

				Assert.That(new[] { 1 }, Is.EquivalentTo(list));
			}
		}
Ejemplo n.º 15
0
		public void TestObject1(string context)
		{
			using (var conn = new DataConnection(context))
			{
				var list = conn.Query<QueryObject>("SELECT 1 as Column1, CURRENT_TIMESTAMP as Column2").ToList();

				Assert.That(list.Count, Is.EqualTo(1));
			}
		}
Ejemplo n.º 16
0
		static void Main(string[] args)
		{
			SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);

			using (var db = new DataConnection())
			{
				var list = db.GetTable<AllTypes2>().ToList();
			}
		}
Ejemplo n.º 17
0
		public void TestScalar2(string context)
		{
			using (var conn = new DataConnection(context))
			{
				var list = conn.Query<int>("SELECT 1").ToList();

				Assert.That(new[] { 1 }, Is.EquivalentTo(list));
			}
		}
        public IWritingStrategy Create(DataConnection dataConnection, int changesAmount)
        {
            if (dataConnection.DataProvider.Name == ProviderName.SqlServer2012 ||
                dataConnection.DataProvider.Name == ProviderName.SqlServer2014)
            {
                return changesAmount < ThresholdValue ? (IWritingStrategy)new IterativeWritingStrategy(dataConnection) : new BulkWritingStrategy(dataConnection);
            }

            return new IterativeWritingStrategy(dataConnection);
        }
Ejemplo n.º 19
0
        public DatabaseUpgrader(DataConnection conn)
        {
            connection = conn;

            upgrades = new Dictionary<string, Func<string>>
            {
                ["1.1"] = UpgradeFrom1_1,
                ["1.2"] = UpgradeFrom1_2
            };
        }
Ejemplo n.º 20
0
        public void Test1(string context)
        {
            var connectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;

            using (var conn = new DataConnection(SqlServerTools.GetDataProvider(), connectionString))
            {
                Assert.That(conn.Connection.State,    Is.EqualTo(ConnectionState.Open));
                Assert.That(conn.ConfigurationString, Is.Null);
            }
        }
Ejemplo n.º 21
0
        public void Test([IncludeDataContexts(ProviderName.SqlServer2008)] string context)
        {
            using (var db = new DataConnection(context))
            {
                var p1 = PersonSelectByKey(db, 1).First();
                var p2 = db.Query<Person>("SELECT * FROM Person WHERE PersonID = @id", new { id = 1 }).First();

                Assert.AreEqual(p1, p2);
            }
        }
Ejemplo n.º 22
0
 //        [Fact]
 public void Test()
 {
     if (TestBase.UserProviders.ContainsKey("SqlServer.2012"))
     {
         using (var db = new DataConnection("SqlServer.2012"))
         {
             var count = db.GetTable<Parent>().Count();
         }
     }
 }
Ejemplo n.º 23
0
		public void Test(string context)
		{
			using (var db = new DataConnection(context))
			{
				var p1 = PersonSelectByKey(db, 1).First();
				var p2 = db.Query<Person>("SELECT * FROM Person WHERE PersonID = @id", new { id = 1 }).First();

				Assert.AreEqual(p1, p2);
			}
		}
Ejemplo n.º 24
0
		public void Test()
		{
			//var conn = new System.Data.SQLite.SQLiteConnection();
			//var conn = new IBM.Data.Informix.IfxConnection();

			using (var db = new DataConnection("SqlServer.2012"))
			{
				var count = db.GetTable<Parent>().Count();
			}
		}
Ejemplo n.º 25
0
		public void TestDataTypes(string context)
		{
			using (var conn = new DataConnection(context))
			{
				Assert.That(TestType<long?>   (conn, "bigintDataType", DataType.Int64), Is.EqualTo(123456789123456789));
				Assert.That(TestType<short?>  (conn, "smallintDataType", DataType.Int16), Is.EqualTo(12345));
				Assert.That(TestType<decimal?>(conn, "decimalDataType", DataType.Decimal), Is.EqualTo(1234.567m));
				Assert.That(TestType<decimal?>(conn, "smalldecimalDataType", DataType.Decimal), Is.EqualTo(123.456m));
				Assert.That(TestType<int?>    (conn, "intDataType", DataType.Int32), Is.EqualTo(123456789));
				Assert.That(TestType<byte?>   (conn, "tinyintDataType", DataType.Byte), Is.EqualTo(123));
				Assert.That(TestType<double?> (conn, "floatDataType", DataType.Double), Is.EqualTo(1234.567));

				//Assert.That(TestType<float?>(conn, "realDataType", DataType.Single), Is.EqualTo(1234.567f));

				Assert.That(TestType<DateTime?>(conn, "dateDataType", DataType.Date), Is.EqualTo(new DateTime(2012, 12, 12)));
				Assert.That(TestType<TimeSpan?>(conn, "timeDataType", DataType.Time), Is.EqualTo(new TimeSpan(12, 12, 12)));
				Assert.That(TestType<DateTime?>(conn, "seconddateDataType", DataType.DateTime), Is.EqualTo(new DateTime(2012, 12, 12, 12, 12, 12)));
				Assert.That(TestType<DateTime?>(conn, "timestampDataType", DataType.Timestamp), Is.EqualTo(new DateTime(2012, 12, 12, 12, 12, 12, 123)));

				Assert.That(TestType<char?>(conn, "charDataType", DataType.Char), Is.EqualTo('a'));
				Assert.That(TestType<string>(conn, "charDataType", DataType.Char), Is.EqualTo("a"));
				Assert.That(TestType<string>(conn, "charDataType", DataType.NChar), Is.EqualTo("a"));
				Assert.That(TestType<string>(conn, "varcharDataType", DataType.VarChar), Is.EqualTo("bcd"));
				Assert.That(TestType<string>(conn, "varcharDataType", DataType.NVarChar), Is.EqualTo("bcd"));

				Assert.That(TestType<string>(conn, "shorttextDataType", DataType.Text), Is.EqualTo("def"));
				Assert.That(TestType<char?>(conn, "ncharDataType", DataType.NChar), Is.EqualTo('ą'));
				Assert.That(TestType<string>(conn, "nvarcharDataType", DataType.NVarChar), Is.EqualTo("ąčęėįš"));
				Assert.That(TestType<string>(conn, "alphanumDataType", DataType.Text), Is.EqualTo("qwert123QWE"));

				Assert.That(TestType<byte[]>(conn, "binaryDataType", DataType.Binary), Is.EqualTo(new byte[] { 97, 98, 99, 100, 101, 102, 103, 104 }));
				Assert.That(TestType<byte[]>(conn, "binaryDataType", DataType.VarBinary), Is.EqualTo(new byte[] { 97, 98, 99, 100, 101, 102, 103, 104 }));
				Assert.That(TestType<byte[]>(conn, "varbinaryDataType", DataType.Binary), Is.EqualTo(new byte[] { 97, 98, 99, 100, 101, 102, 103, 104 }));
				Assert.That(TestType<byte[]>(conn, "varbinaryDataType", DataType.VarBinary), Is.EqualTo(new byte[] { 97, 98, 99, 100, 101, 102, 103, 104 }));
				Assert.That(TestType<Binary>(conn, "varbinaryDataType", DataType.VarBinary).ToArray(), Is.EqualTo(new byte[] { 97, 98, 99, 100, 101, 102, 103, 104 }));

				//field types, than cannot be included in where clause

				//Assert.That(TestType<string>(conn, "textDataType", DataType.Text), Is.EqualTo("abcdefgh"));

				//Assert.That(TestType<byte[]>(conn, "blobDataType", DataType.Binary), Is.EqualTo(new byte[] { 97, 98, 99, 100, 101, 102, 103, 104 }));
				//Assert.That(TestType<byte[]>(conn, "blobDataType", DataType.VarBinary), Is.EqualTo(new byte[] { 97, 98, 99, 100, 101, 102, 103, 104 }));
				//Assert.That(TestType<byte[]>(conn, "blobDataType", DataType.Blob), Is.EqualTo(new byte[] { 97, 98, 99, 100, 101, 102, 103, 104 }));

				//Assert.That(TestType<string>(conn, "clobDataType", DataType.Text), Is.EqualTo("qwertyuiop"));
				//Assert.That(TestType<string>(conn, "clobDataType", DataType.NText), Is.EqualTo("qwertyuiop"));
				//Assert.That(TestType<string>(conn, "clobDataType", DataType.VarChar), Is.EqualTo("qwertyuiop"));
				//Assert.That(TestType<string>(conn, "clobDataType", DataType.NVarChar), Is.EqualTo("qwertyuiop"));

				//Assert.That(TestType<string>(conn, "nclobDataType", DataType.Text), Is.EqualTo("ąčęėįšqwerty123456"));
				//Assert.That(TestType<string>(conn, "nclobDataType", DataType.NText), Is.EqualTo("ąčęėįšqwerty123456"));
				//Assert.That(TestType<string>(conn, "nclobDataType", DataType.VarChar), Is.EqualTo("ąčęėįšqwerty123456"));
				//Assert.That(TestType<string>(conn, "nclobDataType", DataType.NVarChar), Is.EqualTo("ąčęėįšqwerty123456"));
			}
		}
Ejemplo n.º 26
0
		public void Test1(string context)
		{
			var connectionString = DataConnection.GetConnectionString(context);
			var dataProvider = DataConnection.GetDataProvider(context);

			using (var conn = new DataConnection(dataProvider, connectionString))
			{
				Assert.That(conn.Connection.State,    Is.EqualTo(ConnectionState.Open));
				Assert.That(conn.ConfigurationString, Is.Null);
			}
		}
Ejemplo n.º 27
0
		public void CloneTest(string context)
		{
			using (var con = new DataConnection(context))
			{
				var dbName = con.Connection.Database;

				for (var i = 0; i < 150; i++)
					using (var clone = (DataConnection)con.Clone())
						dbName = clone.Connection.Database;
			}
		}
Ejemplo n.º 28
0
		public void TestParameters(string context)
		{
			using (var conn = new DataConnection(context))
			{
				Assert.That(conn.Execute<string>("SELECT @p",        new { p =  1  }), Is.EqualTo("1"));
				Assert.That(conn.Execute<string>("SELECT @p",        new { p = "1" }), Is.EqualTo("1"));
				Assert.That(conn.Execute<int>   ("SELECT @p",        new { p =  new DataParameter { Value = 1   } }), Is.EqualTo(1));
				Assert.That(conn.Execute<string>("SELECT @p1",       new { p1 = new DataParameter { Value = "1" } }), Is.EqualTo("1"));
				Assert.That(conn.Execute<int>   ("SELECT @p1 + ?p2", new { p1 = 2, p2 = 3 }), Is.EqualTo(5));
				Assert.That(conn.Execute<int>   ("SELECT @p2 + ?p1", new { p2 = 2, p1 = 3 }), Is.EqualTo(5));
			}
		}
Ejemplo n.º 29
0
        public void MySqlPKTest(string context)
        {
            using (var conn = new DataConnection(context))
            {
                var sp       = conn.DataProvider.GetSchemaProvider();
                var dbSchema = sp.GetSchema(conn);
                var table    = dbSchema.Tables.Single(t => t.TableName == "person");
                var pk       = table.Columns.FirstOrDefault(t => t.IsPrimaryKey);

                Assert.That(pk, Is.Not.Null);
            }
        }
Ejemplo n.º 30
0
        static Func <IDataReader, T> CreateObjectReader <T>(
            DataConnection dataConnection,
            IDataReader dataReader,
            Func <Type, int, Expression, Expression> getMemberExpression)
        {
            var parameter      = Expression.Parameter(typeof(IDataReader));
            var dataReaderExpr = Expression.Convert(parameter, dataReader.GetType());

            Expression expr;

            if (dataConnection.MappingSchema.IsScalarType(typeof(T)))
            {
                expr = getMemberExpression(typeof(T), 0, dataReaderExpr);
            }
            else
            {
                var td    = dataConnection.MappingSchema.GetEntityDescriptor(typeof(T));
                var names = new List <string>(dataReader.FieldCount);

                for (var i = 0; i < dataReader.FieldCount; i++)
                {
                    names.Add(dataReader.GetName(i));
                }

                expr = null;

                var ctors = typeof(T).GetConstructors().Select(c => new { c, ps = c.GetParameters() }).ToList();

                if (ctors.Count > 0 && ctors.All(c => c.ps.Length > 0))
                {
                    var q =
                        from c in ctors
                        let count = c.ps.Count(p => names.Contains(p.Name))
                                    orderby count descending
                                    select c;

                    var ctor = q.FirstOrDefault();

                    if (ctor != null)
                    {
                        expr = Expression.New(
                            ctor.c,
                            ctor.ps.Select(p => names.Contains(p.Name) ?
                                           getMemberExpression(p.ParameterType, names.IndexOf(p.Name), dataReaderExpr) :
                                           Expression.Constant(dataConnection.MappingSchema.GetDefaultValue(p.ParameterType), p.ParameterType)));
                    }
                }

                if (expr == null)
                {
                    var members =
                        (
                            from n in names.Select((name, idx) => new { name, idx })
                            let member = td.Columns.FirstOrDefault(m =>
                                                                   string.Compare(m.ColumnName, n.name, dataConnection.MappingSchema.ColumnComparisonOption) == 0)
                                         where member != null
                                         select new
                    {
                        Member = member,
                        Expr = getMemberExpression(member.MemberType, n.idx, dataReaderExpr),
                    }
                        ).ToList();

                    expr = Expression.MemberInit(
                        Expression.New(typeof(T)),
                        members.Select(m => Expression.Bind(m.Member.MemberInfo, m.Expr)));
                }
            }

            if (expr.GetCount(e => e == dataReaderExpr) > 1)
            {
                var dataReaderVar = Expression.Variable(dataReaderExpr.Type, "dr");
                var assignment    = Expression.Assign(dataReaderVar, dataReaderExpr);

                expr = expr.Transform(e => e == dataReaderExpr ? dataReaderVar : e);
                expr = Expression.Block(new[] { dataReaderVar }, new[] { assignment, expr });
            }

            var lex = Expression.Lambda <Func <IDataReader, T> >(expr, parameter);

            return(lex.Compile());
        }
Ejemplo n.º 31
0
 public QueryRunner(Query query, int queryNumber, DataConnection dataConnection, Expression expression, object[] parameters)
     : base(query, queryNumber, dataConnection, expression, parameters)
 {
     _dataConnection = dataConnection;
 }
Ejemplo n.º 32
0
 public static IEnumerable <T> Query <T>(this DataConnection connection, Func <IDataReader, T> objectReader, string sql, object parameters)
 {
     return(new CommandInfo(connection, sql, parameters).Query(objectReader));
 }
Ejemplo n.º 33
0
 public static T Execute <T>(this DataConnection connection, string sql)
 {
     return(new CommandInfo(connection, sql).Execute <T>());
 }
Ejemplo n.º 34
0
 public static int Merge <T>(this DataConnection dataConnection, IEnumerable <T> source,
                             string tableName = null, string databaseName = null, string schemaName = null)
     where T : class
 {
     return(dataConnection.DataProvider.Merge(dataConnection, null, false, source, tableName, databaseName, schemaName));
 }
Ejemplo n.º 35
0
 /// <summary>
 /// Creates database command instance using provided database connection and command text.
 /// </summary>
 /// <param name="dataConnection">Database connection instance.</param>
 /// <param name="commandText">Command text.</param>
 public CommandInfo(DataConnection dataConnection, string commandText)
 {
     DataConnection = dataConnection;
     CommandText    = commandText;
 }
Ejemplo n.º 36
0
 public QueryRunner(Query query, int queryNumber, DataConnection dataConnection, Expression expression, object?[]?parameters, object?[]?preambles)
     : base(query, queryNumber, dataConnection, expression, parameters, preambles)
 {
     _dataConnection = dataConnection;
     _executionScope = _dataConnection.DataProvider.ExecuteScope(_dataConnection);
 }
Ejemplo n.º 37
0
 public static IEnumerable <T> Query <T>(this DataConnection connection, T template, string sql, object parameters)
 {
     return(new CommandInfo(connection, sql, parameters).Query(template));
 }
Ejemplo n.º 38
0
 /// <summary>
 /// Creates database command instance using provided database connection, command text and single parameter.
 /// </summary>
 /// <param name="dataConnection">Database connection instance.</param>
 /// <param name="commandText">Command text.</param>
 /// <param name="parameter">Command parameter.</param>
 public CommandInfo(DataConnection dataConnection, string commandText, DataParameter parameter)
 {
     DataConnection = dataConnection;
     CommandText    = commandText;
     Parameters     = new[] { parameter };
 }
Ejemplo n.º 39
0
 /// <summary>
 /// Creates database command instance using provided database connection, command text and parameters.
 /// </summary>
 /// <param name="dataConnection">Database connection instance.</param>
 /// <param name="commandText">Command text.</param>
 /// <param name="parameters">List of command parameters.</param>
 public CommandInfo(DataConnection dataConnection, string commandText, params DataParameter[] parameters)
 {
     DataConnection = dataConnection;
     CommandText    = commandText;
     Parameters     = parameters;
 }
Ejemplo n.º 40
0
        static DataParameter[] GetDataParameters(DataConnection dataConnection, object parameters)
        {
            if (parameters == null)
            {
                return(null);
            }

            if (parameters is DataParameter[])
            {
                return((DataParameter[])parameters);
            }

            if (parameters is DataParameter)
            {
                return new[] { (DataParameter)parameters }
            }
            ;

            Func <object, DataParameter[]> func;
            var type = parameters.GetType();
            var key  = new ParamKey(type, dataConnection.ID);

            if (!_parameterReaders.TryGetValue(key, out func))
            {
                var td = dataConnection.MappingSchema.GetEntityDescriptor(type);

                var p   = Expression.Parameter(typeof(object), "p");
                var obj = Expression.Parameter(parameters.GetType(), "obj");

                var expr = Expression.Lambda <Func <object, DataParameter[]> >(
                    Expression.Block(
                        new[] { obj },
                        new Expression[]
                {
                    Expression.Assign(obj, Expression.Convert(p, type)),
                    Expression.NewArrayInit(
                        typeof(DataParameter),
                        td.Columns.Select(m =>
                    {
                        if (m.MemberType == typeof(DataParameter))
                        {
                            var pobj = Expression.Parameter(typeof(DataParameter));

                            return(Expression.Block(
                                       new[] { pobj },
                                       new Expression[]
                            {
                                Expression.Assign(pobj, Expression.PropertyOrField(obj, m.MemberName)),
                                Expression.MemberInit(
                                    Expression.New(typeof(DataParameter)),
                                    Expression.Bind(
                                        _dataParameterName,
                                        Expression.Coalesce(
                                            Expression.MakeMemberAccess(pobj, _dataParameterName),
                                            Expression.Constant(m.ColumnName))),
                                    Expression.Bind(
                                        _dataParameterDataType,
                                        Expression.MakeMemberAccess(pobj, _dataParameterDataType)),
                                    Expression.Bind(
                                        _dataParameterValue,
                                        Expression.Convert(
                                            Expression.MakeMemberAccess(pobj, _dataParameterValue),
                                            typeof(object))))
                            }));
                        }

                        var memberType  = m.MemberType.ToNullableUnderlying();
                        var valueGetter = Expression.PropertyOrField(obj, m.MemberName) as Expression;
                        var mapper      = dataConnection.MappingSchema.GetConvertExpression(memberType, typeof(DataParameter), createDefault: false);

                        if (mapper != null)
                        {
                            return(Expression.Call(
                                       MemberHelper.MethodOf(() => PrepareDataParameter(null, null)),
                                       mapper.GetBody(valueGetter),
                                       Expression.Constant(m.ColumnName)));
                        }

                        if (memberType.IsEnumEx())
                        {
                            var mapType  = ConvertBuilder.GetDefaultMappingFromEnumType(dataConnection.MappingSchema, memberType);
                            var convExpr = dataConnection.MappingSchema.GetConvertExpression(m.MemberType, mapType);

                            memberType  = mapType;
                            valueGetter = convExpr.GetBody(valueGetter);
                        }

                        return((Expression)Expression.MemberInit(
                                   Expression.New(typeof(DataParameter)),
                                   Expression.Bind(
                                       _dataParameterName,
                                       Expression.Constant(m.ColumnName)),
                                   Expression.Bind(
                                       _dataParameterDataType,
                                       Expression.Constant(dataConnection.MappingSchema.GetDataType(memberType).DataType)),
                                   Expression.Bind(
                                       _dataParameterValue,
                                       Expression.Convert(valueGetter, typeof(object)))));
                    }))
                }
                        ),
                    p);

                _parameterReaders[key] = func = expr.Compile();
            }

            return(func(parameters));
        }
Ejemplo n.º 41
0
 public static CommandInfo SetCommand(DataConnection dataConnection, string commandText, object parameters)
 {
     return(new CommandInfo(dataConnection, commandText, parameters));
 }
Ejemplo n.º 42
0
 public static CommandInfo SetCommand(this DataConnection dataConnection, string commandText)
 {
     return(new CommandInfo(dataConnection, commandText));
 }
Ejemplo n.º 43
0
 public static DataReader ExecuteReader(this DataConnection connection, string sql, object parameters)
 {
     return(new CommandInfo(connection, sql, parameters).ExecuteReader());
 }
Ejemplo n.º 44
0
 public DataReaderAsync(DataConnection dataConnection, Func <int> skipAction, Func <int> takeAction)
 {
     _dataConnection = dataConnection;
     _skipAction     = skipAction;
     _takeAction     = takeAction;
 }
Ejemplo n.º 45
0
 public static DataReader ExecuteReader(this DataConnection connection, string sql)
 {
     return(new CommandInfo(connection, sql).ExecuteReader());
 }
Ejemplo n.º 46
0
 public static T Execute <T>(this DataConnection connection, string sql, DataParameter parameter)
 {
     return(new CommandInfo(connection, sql, parameter).Execute <T>());
 }
Ejemplo n.º 47
0
 public static T ExecuteProc <T>(this DataConnection connection, string sql, params DataParameter[] parameters)
 {
     return(new CommandInfo(connection, sql, parameters).ExecuteProc <T>());
 }
Ejemplo n.º 48
0
 public static IEnumerable <T> Query <T>(this DataConnection connection, string sql, DataParameter parameter)
 {
     return(new CommandInfo(connection, sql, parameter).Query <T>());
 }
Ejemplo n.º 49
0
 public static int Merge <T>(this DataConnection dataConnection, Expression <Func <T, bool> > predicate, IEnumerable <T> source,
                             string tableName = null, string databaseName = null, string schemaName = null)
     where T : class
 {
     return(dataConnection.DataProvider.Merge(dataConnection, predicate, true, source, tableName, databaseName, schemaName));
 }
Ejemplo n.º 50
0
 /// <summary>
 /// Creates database command instance using provided database connection, command text and parameters.
 /// </summary>
 /// <param name="dataConnection">Database connection instance.</param>
 /// <param name="commandText">Command text.</param>
 /// <param name="parameters">Command parameters. Supported values:
 /// <para> - <c>null</c> for command without parameters;</para>
 /// <para> - single <see cref="DataParameter"/> instance;</para>
 /// <para> - array of <see cref="DataParameter"/> parameters;</para>
 /// <para> - mapping class entity.</para>
 /// <para>Last case will convert all mapped columns to <see cref="DataParameter"/> instances using following logic:</para>
 /// <para> - if column is of <see cref="DataParameter"/> type, column value will be used. If parameter name (<see cref="DataParameter.Name"/>) is not set, column name will be used;</para>
 /// <para> - if converter from column type to <see cref="DataParameter"/> is defined in mapping schema, it will be used to create parameter with colum name passed to converter;</para>
 /// <para> - otherwise column value will be converted to <see cref="DataParameter"/> using column name as parameter name and column value will be converted to parameter value using conversion, defined by mapping schema.</para>
 /// </param>
 public CommandInfo(DataConnection dataConnection, string commandText, object parameters)
 {
     DataConnection = dataConnection;
     CommandText    = commandText;
     Parameters     = GetDataParameters(dataConnection, parameters);
 }
Ejemplo n.º 51
0
 /// <summary>
 /// Creates new transaction controller for data connection.
 /// </summary>
 /// <param name="dataConnection">Data connection instance.</param>
 public DataConnectionTransaction(DataConnection dataConnection)
 {
     DataConnection = dataConnection ?? throw new ArgumentNullException(nameof(dataConnection));
 }
Ejemplo n.º 52
-1
		public void EnumExecuteScalarTest()
		{
			using (var dbm = new DataConnection())
			{
				var gender = dbm.Execute<Gender>("select 'M'");

				Assert.That(gender, Is.EqualTo(Gender.Male));
			}
		}