Exemplo n.º 1
0
        //public List<DMSource> LoadAll()
        //{

        //    LoadQuery query = QueryBuilder.Load(TABLE);

        //    foreach (var entry in DATA_MAP.Columns)
        //    {
        //        query.AddProperty(entry.Key.ColumnName);
        //    }

        //    List<DMSource> items = new List<DMSource>();
        //    var cmd = db.GetCommand(query.Build());
        //        using (var reader = cmd.ExecuteReader())
        //        {
        //            while (reader.Read())
        //            {
        //                DMSource item = CreateNewItem();
        //                for (var i = 0; i < DATA_MAP.Columns.Count(); i++)
        //                {
        //                    SetItem(reader.GetName(i), reader.GetString(i), DATA_MAP, item);
        //                }
        //                items.Add(item);
        //            }
        //        }
        //    return items;
        //}

        public List <DMSource> LoadAll(Expression <Func <DMSource, bool> > exp = null)
        {
            LoadQuery query = QueryBuilder.Load(TABLE);

            foreach (var entry in DATA_MAP.Columns)
            {
                query.AddProperty(entry.Key.ColumnName);
            }

            if (exp != null)
            {
                BinaryExpression operation = (BinaryExpression)exp.Body;
                query.AddWhereFragment(operation);
            }

            List <DMSource> items = new List <DMSource>();
            var             cmd   = db.GetCommand(query.Build());

            using (var reader = cmd.ExecuteReader()) {
                while (reader.Read())
                {
                    DMSource item = CreateNewItem();
                    for (var i = 0; i < DATA_MAP.Columns.Count(); i++)
                    {
                        if (!reader.IsDBNull(i))
                        {
                            SetItem(reader.GetName(i), reader.GetString(i), DATA_MAP, item);
                        }
                    }
                    items.Add(item);
                }
            }
            return(items);
        }
Exemplo n.º 2
0
        public List <ConnectionDM> LoadConnections(Expression <Func <ConnectionDM, bool> > exp, DMSource model, ConnectionDataMap connection)
        {
            LoadQuery query = new LoadQuery(connection.SchemaName, connection.TableName);

            BinaryExpression operation = (BinaryExpression)exp.Body;

            query.AddWhereFragment(operation, DataObject: connection);

            //query.AddProperty("ID");
            //query.AddProperty(connection.ParentID);
            //query.AddProperty(connection.ChildID);
            var targetMap = new TableDataMap(typeof(ConnectionDM));
            Dictionary <string, string> currentPropertyMap = new Dictionary <string, string>();

            foreach (var entry in targetMap.IDColumns)
            {
                var prop = entry.Value;
                if (Attribute.IsDefined(prop, typeof(LookupPropertyAttribute)))
                {
                    var idData       = entry.Value.GetCustomAttribute(typeof(IDColumnAttribute)) as IDColumnAttribute;
                    var propertyName = idData.ColumnName;
                    var value        = (string)connection.GetType().GetProperty(propertyName).GetValue(connection);
                    query.AddProperty(value);
                    currentPropertyMap[value] = propertyName;
                }
                else
                {
                    query.AddProperty(entry.Key.ColumnName);
                }
            }



            List <ConnectionDM> items = new List <ConnectionDM>();
            var cmd = db.GetCommand(query.Build());

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    ConnectionDM item = CreateNewItem <ConnectionDM>();
                    for (var i = 0; i < DATA_MAP.Columns.Count(); i++)
                    {
                        var itemName = reader.GetName(i);
                        if (currentPropertyMap.ContainsKey(itemName))
                        {
                            itemName = currentPropertyMap[itemName];
                        }
                        SetItem(itemName, reader.GetString(i), targetMap, item);
                    }
                    items.Add(item);
                }
            }
            return(items);
        }
Exemplo n.º 3
0
        public void TestGenerateLoadWithID()
        {
            var       schemeName    = TestingTools.RandomString;
            var       tableName     = TestingTools.RandomString;
            var       propertyName  = TestingTools.RandomString;
            var       propertyValue = TestingTools.RandomString;
            var       id            = TestingTools.RandomInt;
            LoadQuery query         = new LoadQuery(schemeName, tableName);

            query.AddProperty(propertyName);

            query.AddWhereFragment(id);

            var result = query.Build();

            var expected = String.Format("SELECT \"{0}\" FROM \"{1}\".\"{2}\" WHERE \"ID\"={3};",
                                         propertyName, schemeName, tableName, id);

            Assert.IsNotNull(result);
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 4
0
        public void TestGenerateLoadWithWhere()
        {
            var       schemeName    = TestingTools.RandomString;
            var       tableName     = TestingTools.RandomString;
            var       propertyName  = TestingTools.RandomString;
            var       propertyValue = TestingTools.RandomString;
            var       id            = TestingTools.RandomInt;
            LoadQuery query         = new LoadQuery(schemeName, tableName);

            query.AddProperty(propertyName);

            Expression <Func <ConnectionDM, bool> > exp = (item) => item.ChildID == id;

            query.AddWhereFragment((BinaryExpression)exp.Body);

            var result = query.Build();

            var expected = String.Format("SELECT \"{0}\" FROM \"{1}\".\"{2}\" WHERE \"ChildID\" = '{3}';",
                                         propertyName, schemeName, tableName, id);

            Assert.IsNotNull(result);
            Assert.AreEqual(expected, result);
        }