Пример #1
0
        private List <object> PhaseTwo(DataStoreMapping mapping, Type objectType, SelectStatementResult result, List <PreResult> toLoad)
        {
            List <object> objects        = new List <object>();
            int           keyColumnIndex = mapping.Table.Columns.IndexOf(GetKeyColumn(mapping));
            List <DataStoreMapping.Column> refColumns = mapping.RefColumns.ToList();

            foreach (var row in result.Rows)
            {
                var key = row.Values[keyColumnIndex];
                if (key == null)
                {
                    throw new DataException("Key cannot be null.");
                }
                var obj = objectMap.Get(objectType, key);
                if (obj == null)
                {
                    obj = mapping.Create();
                    objectMap.Add(objectType, key, obj);
                }
                objects.Add(obj);
                foreach (var member in refColumns)
                {
                    var alias         = "T";
                    var dbCriteria    = BuildByKeyCriteria(member.Type, row.Values[member.Index], alias);
                    var memberMapping = mappings[member.Type];
                    toLoad.Add(new PreResult()
                    {
                        Mapping    = memberMapping,
                        ObjectType = member.Type,
                        Statement  = PhaseOne(memberMapping, member.Type, dbCriteria, alias)
                    });
                }
            }
            return(objects);
        }
Пример #2
0
 private void PhaseThree(DataStoreMapping mapping, List <object> objects, SelectStatementResult result)
 {
     for (int i = 0; i < objects.Count; i++)
     {
         mapping.Load(objects[i], result.Rows[i].Values, objectMap);
         objectMap.Accept(objects[i]);
     }
 }
Пример #3
0
        private SelectStatement PhaseOne(DataStoreMapping mapping, Type objectType, CriteriaOperator dbCriteria, string alias)
        {
            var statement = new SelectStatement(mapping.Table, alias);

            statement.Condition = dbCriteria;
            foreach (var column in mapping.Table.Columns)
            {
                statement.Operands.Add(new QueryOperand(column, alias));
            }
            return(statement);
        }
Пример #4
0
 private DBColumn GetKeyColumn(DataStoreMapping mapping)
 {
     return(mapping.Table.Columns.First(c => c.IsKey));
 }
Пример #5
0
        private void SetupInsertUpdateStatement(ModificationStatement statement, object obj, DataStoreMapping mapping)
        {
            var values = new object[mapping.Table.Columns.Count];

            mapping.Save(obj, values);
            for (int i = 0; i < values.Length; i++)
            {
                var column = mapping.Table.Columns[i];
                if (!column.IsIdentity)
                {
                    statement.Operands.Add(new QueryOperand(column, null));
                    statement.Parameters.Add(new OperandValue(values[i]));
                }
            }
        }
Пример #6
0
 private void SetupUpdateDeleteStatement(ModificationStatement statement, object obj, DataStoreMapping mapping, string alias)
 {
     statement.Condition = new BinaryOperator(
         new QueryOperand(GetKeyColumn(mapping), alias),
         new OperandValue(mapping.GetKey(obj)),
         BinaryOperatorType.Equal);
 }
Пример #7
0
        public PostOfficeClient()
        {
            this.DataStore = new InMemoryDataStore(AutoCreateOption.DatabaseAndSchema, false);
            this.Mappings  = new Dictionary <Type, DataStoreMapping>();
            var mAccount = new DataStoreMapping();

            mAccount.Table = new DBTable("Accounts");
            mAccount.Table.AddColumn(new DBColumn("UserName", true, null, 255, DBColumnType.String));
            mAccount.Table.AddColumn(new DBColumn("PublicName", false, null, 1024, DBColumnType.String));
            mAccount.Create = () => new NPTub();
            mAccount.Load   = (obj, values, omap) =>
            {
                var o = ((NPObj)obj);
                o.Id   = (int)values[0];
                o.Name = (string)values[1];
                //((NPTub)obj).SetKey((string)values[0]);
                //((Account)obj).PublicName = (string)values[1];
            };
            mAccount.Save = (obj, values) =>
            {
                values[0] = ((NPObj)obj).Id;
                values[1] = ((NPObj)obj).Name;
            };
            mAccount.GetKey     = (obj) => ((Account)obj).UserName;
            mAccount.RefColumns = Enumerable.Empty <DataStoreMapping.Column>();
            Mappings.Add(typeof(Account), mAccount);
            var mMessage = new DataStoreMapping();

            mMessage.Table = new DBTable("Messages");
            var mMessageKey = new DBColumn("ID", true, null, 0, DBColumnType.Int32);

            mMessageKey.IsIdentity = true;
            mMessage.Table.AddColumn(mMessageKey);
            mMessage.Table.AddColumn(new DBColumn("Subject", false, null, 1024, DBColumnType.String));
            mMessage.Table.AddColumn(new DBColumn("Body", false, null, -1, DBColumnType.String));
            mMessage.Table.AddColumn(new DBColumn("Sender", false, null, 255, DBColumnType.String));
            mMessage.Table.AddColumn(new DBColumn("Recepient", false, null, 255, DBColumnType.String));
            mMessage.Table.PrimaryKey = new DBPrimaryKey(new object[] { mMessageKey });
            mMessage.Create           = () => new Message();
            mMessage.SetKey           = (obj, key) =>
            {
                ((Message)obj).SetKey((int)key);
            };
            mMessage.GetKey = (obj) => ((Message)obj).ID;
            mMessage.Load   = (obj, values, omap) =>
            {
                var o = (Message)obj;
                o.SetKey((int)values[0]);
                o.Subject   = (string)values[1];
                o.Body      = (string)values[2];
                o.Sender    = GetReference <Account>(omap, values[3]);
                o.Recepient = GetReference <Account>(omap, values[4]);
            };
            mMessage.Save = (obj, values) =>
            {
                var o = (Message)obj;
                values[0] = o.ID;
                values[1] = o.Subject;
                values[2] = o.Body;
                values[3] = o.Sender?.UserName;
                values[4] = o.Recepient?.UserName;
            };
            mMessage.RefColumns = new DataStoreMapping.Column[] {
                new DataStoreMapping.Column()
                {
                    Index = 3, Type = typeof(Account)
                },
                new DataStoreMapping.Column()
                {
                    Index = 4, Type = typeof(Account)
                }
            };
            Mappings.Add(typeof(Message), mMessage);
            DataStore.UpdateSchema(false, mAccount.Table, mMessage.Table);
            CreateDemoData((InMemoryDataStore)DataStore);
        }