コード例 #1
0
ファイル: DbHelper.cs プロジェクト: lisandropodesta/lit.net
        /// <summary>
        /// Creates an instance of a db object and optionally assigns the db reference.
        /// </summary>
        public static object CreateInstance(IDbDataAccess db, Type type, params object[] args)
        {
            var result = Activator.CreateInstance(type, args);

            SetDbRef(result, db);
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Load results returned from stored procedure.
        /// </summary>
        public static void LoadResults(this IDbCommandBinding binding, IDbDataAccess db, DbDataReader reader, object instance)
        {
            var recordsetCount = binding.RecordsetCount;

            if (recordsetCount == 0)
            {
                reader.Read();
                binding.GetOutputFields(db, reader, instance);
            }
            else
            {
                for (var index = 0; index < recordsetCount; index++)
                {
                    if (index > 0 && !reader.NextResult())
                    {
                        throw new ArgumentException($"Unable to load recordset index {index}");
                    }

                    var rsBinding = binding.Recordsets?.FirstOrDefault(b => b.Attributes.Index == index);
                    if (rsBinding != null)
                    {
                        rsBinding.LoadResults(db, reader, instance);
                    }
                    else
                    {
                        var rBinding = binding.Records?.FirstOrDefault(b => b.Attributes.Index == index);
                        if (rBinding != null)
                        {
                            rBinding.LoadResults(db, reader, instance);
                        }
                    }
                }
            }
        }
コード例 #3
0
ファイル: DbHelper.cs プロジェクト: lisandropodesta/lit.net
        /// <summary>
        /// Creates an instance of a foreign key to a table.
        /// </summary>
        public static IDbForeignKeyRef CreateKey(IDbDataAccess db, Type tableType, object key, bool?isNullableForced = null)
        {
            var type     = GetPrimaryKeyType(db.Setup, tableType, isNullableForced, out Type _);
            var instance = Activator.CreateInstance(type) as IDbForeignKeyRef;

            instance.Db          = db;
            instance.KeyAsObject = key;
            return(instance);
        }
コード例 #4
0
ファイル: DbHelper.cs プロジェクト: lisandropodesta/lit.net
        /// <summary>
        /// Assigns data access reference.
        /// </summary>
        public static T SetDbRef <T>(T obj, IDbDataAccess db)
        {
            if (obj is IDbDataAccessRef dbRef && db != null)
            {
                dbRef.Db = db;
            }

            return(obj);
        }
コード例 #5
0
        /// <summary>
        /// Get output fields.
        /// </summary>
        public static void GetOutputFields(this IDbCommandBinding binding, IDbDataAccess db, DbDataReader reader, object instance)
        {
            DbHelper.SetDbRef(instance, db);

            if (reader.HasRows)
            {
                binding.Fields?.ForEach(b => b.GetOutputField(reader, instance));
            }
        }
コード例 #6
0
        /// <summary>
        /// Load results returned from stored procedure.
        /// </summary>
        public static bool LoadResults(this IDbTableBinding binding, IDbDataAccess db, DbDataReader reader, object instance)
        {
            DbHelper.SetDbRef(instance, db);

            reader.Read();

            if (reader.HasRows)
            {
                binding.Columns?.ForEach(c => c.GetOutputField(reader, instance));
                return(true);
            }

            return(false);
        }
コード例 #7
0
        /// <summary>
        /// Builds a generic list and loads a recordset in it.
        /// </summary>
        private static object LoadSqlRecordset(IDbDataAccess db, DbDataReader reader, Type type, int maxCount)
        {
            var binding = db.Setup.GetCommandBinding(type);
            var result  = TypeHelper.CreateInstance(typeof(List <>), new[] { type });
            var list    = result as IList;

            for (var ri = 0; ri < maxCount && reader.Read(); ri++)
            {
                var record = Activator.CreateInstance(type);
                binding.GetOutputFields(db, reader, record);
                list.Add(record);
            }

            return(result);
        }
コード例 #8
0
        /// <summary>
        /// Load the current recordset.
        /// </summary>
        public static void LoadResults(this IDbRecordBinding binding, IDbDataAccess db, DbDataReader reader, object instance)
        {
            var attr = binding.Attributes;
            var list = LoadSqlRecordset(db, reader, binding.BindingType, attr.AllowMultipleRecords ? 1 : 2) as IList;

            switch (list.Count)
            {
            case 0:
                binding.SetValue(instance, null);
                break;

            case 1:
                binding.SetValue(instance, list[0]);
                break;

            default:
                throw new ArgumentException($"Stored procedure returned more than one record and a single one was expected, at index {attr.Index}");
            }
        }
コード例 #9
0
 /// <summary>
 /// Integration tests are executed against the test database  <param name="_connStringSettings"></param> which by default contains few sample orders
 /// </summary>
 public OrderProcessorTests()
 {
     _connStringSettings = new ConnectionStringSettings("SQLite", @"Data Source=.\OrdersHandlerTestDB.db;Version=3;", "OrdersHandler.Tests.Integration");
     _sqliteDataAccess   = new SqliteDataAccess(_connStringSettings);
     _orderProcessor     = new OrderProcessor(_sqliteDataAccess);
 }
コード例 #10
0
ファイル: DbHelper.cs プロジェクト: lisandropodesta/lit.net
 /// <summary>
 /// Creates an instance of a foreign key to a table.
 /// </summary>
 public static IDbForeignKeyRef <T> CreateKey <T>(IDbDataAccess db, object key, bool?isNullableForced = null)
 {
     return(CreateKey(db, typeof(T), key, isNullableForced) as IDbForeignKeyRef <T>);
 }
コード例 #11
0
        /// <summary>
        /// Load the current recordset.
        /// </summary>
        public static void LoadResults(this IDbRecordsetBinding binding, IDbDataAccess db, DbDataReader reader, object instance)
        {
            var list = LoadSqlRecordset(db, reader, binding.BindingType, int.MaxValue);

            binding.SetValue(instance, list);
        }
コード例 #12
0
 public OrderProcessor(IDbDataAccess database)
 {
     _database = database;
 }
コード例 #13
0
 public MyDbDataRepository(IDbDataAccess dbDataAccess)
 {
     _dbDataAccess = dbDataAccess;
 }
コード例 #14
0
ファイル: DataAccess.cs プロジェクト: lisandropodesta/lit.net
        public static void Execute(IDbDataAccess db)
        {
            Audit.Message("\n  ** User table test **");

            var user = new User
            {
                FullName = "John Nash",
                NickName = "John",
                Status   = Status.Active
            };

            db.Insert(user);
            Assert(user.IdUser > 0);

            user.NickName = "j0hn";
            user.Status   = Status.Hold;
            db.Update(user);
            var idFirstUser = user.IdUser;

            user = new User
            {
                FullName    = "Mick Doe",
                NickName    = "mck",
                RefereeUser = DbHelper.CreateKey <User>(db, idFirstUser, true),
                Status      = Status.Active
            };

            Assert(user.RefereeUser.Record.IdUser == idFirstUser);

            db.Store(user);
            Assert(user.IdUser > 0);

            user = new User(1);
            db.Get(user);
            Assert(user.FullName == "John Nash");

            user = db.Find <User>("not exists");
            Assert(user == null);

            user = db.Find <User>("mck");
            Assert(user.FullName == "Mick Doe");

            var userList = db.List <User>();

            foreach (var u in userList)
            {
                Audit.Message($"  IdUser={u.IdUser}, Status={u.Status}, NickName={u.NickName}, FullName={u.FullName}, IdRefereeUser={u.RefereeUser}");
            }


            Audit.Message("\n  ** UserSession table test **");

            var userSession = new UserSession
            {
                IdUser     = userList[0].IdUser,
                Started    = DateTimeOffset.Now,
                DateTime   = DateTime.Now,
                TimeSpan   = TimeSpan.FromTicks(1234567890),
                Attributes = new Dictionary <string, object>
                {
                    { "Number", 123 },
                    { "Text", "hello" },
                    { "Bool", true },
                    { "Null", null },
                    { "Array", new[] { 1, 2, 3 } }
                },
                LongText = "long text!"
            };

            db.Store(userSession);
            Assert(userSession.IdUserSession > 0);

            var userSession2 = new UserSession(userSession.IdUserSession);

            db.Get(userSession2);
            Assert(userSession.IdUser == userSession2.IdUser);

            var sessionList = db.List <UserSession>();

            foreach (var s in sessionList)
            {
                Audit.Message($"  IdUserSession={s.IdUserSession}, IdUser={s.IdUser}, Started={s.Started}, DateTime={s.DateTime}, TimeSpan={s.TimeSpan}");
            }
        }
コード例 #15
0
 public CarsController(IDbDataAccess dbSource, IApiDataAccess apiSource)
 {
     _dbSource  = dbSource;
     _apiSource = apiSource;
 }