Example #1
0
        private static Territories ORM_CreateProxy(OpenNETCF.ORM.FieldAttributeCollection fields, System.Data.IDataReader results)
        {
            var item = new Territories();

            foreach (var field in fields)
            {
                var value = results[field.Ordinal];
                switch (field.FieldName)
                {
                case "TerritoryID":
                    // If this is a TimeSpan, use the commented line below
                    // item.TerritoryID = (value == DBNull.Value) ? TimeSpan.MinValue; : new TimeSpan((long)value);
                    item.TerritoryID = (value == DBNull.Value) ? 0 : (long)value;
                    break;

                case "TerritoryDescription":
                    item.TerritoryDescription = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "RegionID":
                    // If this is a TimeSpan, use the commented line below
                    // item.RegionID = (value == DBNull.Value) ? TimeSpan.MinValue; : new TimeSpan((long)value);
                    item.RegionID = (value == DBNull.Value) ? 0 : (long)value;
                    break;
                }
            }
            return(item);
        }
Example #2
0
        protected object CreateEntityInstance(string entityName, Type objectType, FieldAttributeCollection fields, IDataReader results, out bool fieldsSet)
        {
            if (objectType.Equals(typeof(DynamicEntity)))
            {
                var entity = new DynamicEntity(entityName, fields);

                foreach (var field in entity.Fields)
                {
                    // we should probably cache these ordinals
                    field.Value = results[field.Name];
                }

                fieldsSet = true;
                return(entity);
            }

            var info = GetEntityInfo(entityName);

            if (info.CreateProxy == null)
            {
                // no create proxy exists, create the item and let the caller know it needs to fill the fields
                fieldsSet = false;
                return(Activator.CreateInstance(objectType));
            }

            var item = info.CreateProxy(fields, results);

            fieldsSet = true;
            return(item);
        }
Example #3
0
        private static InternationalOrders ORM_CreateProxy(OpenNETCF.ORM.FieldAttributeCollection fields, System.Data.IDataReader results)
        {
            var item = new InternationalOrders();

            foreach (var field in fields)
            {
                var value = results[field.Ordinal];
                switch (field.FieldName)
                {
                case "OrderID":
                    // If this is a TimeSpan, use the commented line below
                    // item.OrderID = (value == DBNull.Value) ? TimeSpan.MinValue; : new TimeSpan((long)value);
                    item.OrderID = (value == DBNull.Value) ? 0 : (long)value;
                    break;

                case "CustomsDescription":
                    item.CustomsDescription = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "ExciseTax":
                    item.ExciseTax = (value == DBNull.Value) ? 0 : (decimal)value;
                    break;
                }
            }
            return(item);
        }
Example #4
0
        private void PopulateFields(string entityName, FieldAttributeCollection fields, IDataReader results, object item, bool fillReferences)
        {
            object rowPK;

            foreach (var field in fields)
            {
                var value = results[field.Ordinal];
                if (value != DBNull.Value)
                {
                    if (field.DataType == DbType.Object)
                    {
                        // get serializer
                        var itemType     = item.GetType();
                        var deserializer = GetDeserializer(itemType);

                        if (deserializer == null)
                        {
                            throw new MissingMethodException(
                                      string.Format("The field '{0}' requires a custom serializer/deserializer method pair in the '{1}' Entity",
                                                    field.FieldName, entityName));
                        }

                        var @object = deserializer.Invoke(item, new object[] { field.FieldName, value });
                        field.PropertyInfo.SetValue(item, @object, null);
                    }
                    else if (field.IsRowVersion)
                    {
                        // sql stores this an 8-byte array
                        field.PropertyInfo.SetValue(item, BitConverter.ToInt64((byte[])value, 0), null);
                    }
                    else if (field.IsTimespan)
                    {
                        // SQL Compact doesn't support Time, so we're convert to ticks in both directions
                        var valueAsTimeSpan = new TimeSpan((long)value);
                        field.PropertyInfo.SetValue(item, valueAsTimeSpan, null);
                    }
                    else
                    {
                        field.PropertyInfo.SetValue(item, value, null);
                    }
                }

                // Check if it is reference key to set, not primary.
                ReferenceAttribute attr = null;

                if (m_references != null)
                {
                    attr = m_references.Where(
                        x => x.ForeignReferenceField == field.FieldName).FirstOrDefault();
                }

                if ((field.IsPrimaryKey) || (attr != null))
                {
                    rowPK = value;
                }
            }
        }
Example #5
0
        private static Customers ORM_CreateProxy(OpenNETCF.ORM.FieldAttributeCollection fields, System.Data.IDataReader results)
        {
            var item = new Customers();

            foreach (var field in fields)
            {
                var value = results[field.Ordinal];
                switch (field.FieldName)
                {
                case "CustomerID":
                    item.CustomerID = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "CompanyName":
                    item.CompanyName = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "ContactName":
                    item.ContactName = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "ContactTitle":
                    item.ContactTitle = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Address":
                    item.Address = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "City":
                    item.City = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Region":
                    item.Region = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "PostalCode":
                    item.PostalCode = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Country":
                    item.Country = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Phone":
                    item.Phone = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Fax":
                    item.Fax = (value == DBNull.Value) ? null : (string)value;
                    break;
                }
            }
            return(item);
        }
Example #6
0
        public DynamicEntity(string entityName, FieldAttributeCollection fields)
        {
            EntityName = entityName;
            Fields     = new FieldCollection();

            if (fields != null)
            {
                foreach (var f in fields)
                {
                    this.Fields.Add(f.FieldName);

                    if (f.IsPrimaryKey)
                    {
                        this.KeyField = f.FieldName;
                    }
                }
            }
        }
Example #7
0
        private static OrderDetails ORM_CreateProxy(OpenNETCF.ORM.FieldAttributeCollection fields, System.Data.IDataReader results)
        {
            var item = new OrderDetails();

            foreach (var field in fields)
            {
                var value = results[field.Ordinal];
                switch (field.FieldName)
                {
                case "OrderID":
                    // If this is a TimeSpan, use the commented line below
                    // item.OrderID = (value == DBNull.Value) ? TimeSpan.MinValue; : new TimeSpan((long)value);
                    item.OrderID = (value == DBNull.Value) ? 0 : (long)value;
                    break;

                case "ProductID":
                    // If this is a TimeSpan, use the commented line below
                    // item.ProductID = (value == DBNull.Value) ? TimeSpan.MinValue; : new TimeSpan((long)value);
                    item.ProductID = (value == DBNull.Value) ? 0 : (long)value;
                    break;

                case "UnitPrice":
                    item.UnitPrice = (value == DBNull.Value) ? 0 : (decimal)value;
                    break;

                case "Quantity":
                    item.Quantity = (value == DBNull.Value) ? (short)0 : (short)value;
                    break;

                case "Discount":
                    item.Discount = (value == DBNull.Value) ? 0 : (float)value;
                    break;
                }
            }
            return(item);
        }
Example #8
0
        private static Suppliers ORM_CreateProxy(OpenNETCF.ORM.FieldAttributeCollection fields, System.Data.IDataReader results)
        {
            var item = new Suppliers();

            foreach (var field in fields)
            {
                var value = results[field.Ordinal];
                switch (field.FieldName)
                {
                case "SupplierID":
                    // If this is a TimeSpan, use the commented line below
                    // item.SupplierID = (value == DBNull.Value) ? TimeSpan.MinValue; : new TimeSpan((long)value);
                    item.SupplierID = (value == DBNull.Value) ? 0 : (long)value;
                    break;

                case "CompanyName":
                    item.CompanyName = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "ContactName":
                    item.ContactName = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "ContactTitle":
                    item.ContactTitle = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Address":
                    item.Address = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "City":
                    item.City = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Region":
                    item.Region = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "PostalCode":
                    item.PostalCode = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Country":
                    item.Country = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Phone":
                    item.Phone = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Fax":
                    item.Fax = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "HomePage":
                    item.HomePage = (value == DBNull.Value) ? null : (string)value;
                    break;
                }
            }
            return(item);
        }
Example #9
0
        private IEnumerable <object> Select(string entityName, Type objectType, IEnumerable <FilterCondition> filters, int fetchCount, int firstRowOffset, bool fillReferences)
        {
            UpdateIndexCacheForType(entityName);

            var  items = new List <object>();
            bool tableDirect;

            var          connection = GetConnection(false);
            SqlCeCommand command    = null;

            if (UseCommandCache)
            {
                Monitor.Enter(CommandCache);
            }

            try
            {
                CheckOrdinals(entityName);
                CheckPrimaryKeyIndex(entityName);

                command             = GetSelectCommand <SqlCeCommand, SqlCeParameter>(entityName, filters, out tableDirect);
                command.Connection  = connection as SqlCeConnection;
                command.Transaction = CurrentTransaction as SqlCeTransaction;

                int searchOrdinal = -1;

                object matchValue = null;
                string matchField = null;

                if (tableDirect) // use index
                {
                    if ((filters != null) && (filters.Count() > 0))
                    {
                        var filter = filters.First();

                        matchValue = filter.Value ?? DBNull.Value;
                        matchField = filter.FieldName;

                        var sqlfilter = filter as SqlFilterCondition;
                        if ((sqlfilter != null) && (sqlfilter.PrimaryKey))
                        {
                            searchOrdinal = ((SqlEntityInfo)Entities[entityName]).PrimaryKeyOrdinal;
                        }
                    }

                    // we need to ensure that the search value does not exceed the length of the indexed
                    // field, else we'll get an exception on the Seek call below
                    var indexInfo = GetIndexInfo(command.IndexName);
                    if (indexInfo != null)
                    {
                        if (indexInfo.MaxCharLength > 0)
                        {
                            var value = (string)matchValue;
                            if (value.Length > indexInfo.MaxCharLength)
                            {
                                matchValue = value.Substring(0, indexInfo.MaxCharLength);
                            }
                        }
                    }
                }

                using (var results = command.ExecuteReader())
                {
                    ReferenceAttribute[] referenceFields = null;

                    int currentOffset = 0;

                    if (matchValue != null)
                    {
                        // convert enums to an int, else the .Equals later check will fail
                        // this feels a bit kludgey, but for now it's all I can think of
                        if (matchValue.GetType().IsEnum)
                        {
                            matchValue = (int)matchValue;
                        }

                        if (searchOrdinal < 0)
                        {
                            searchOrdinal = results.GetOrdinal(matchField);
                        }

                        if (tableDirect)
                        {
                            results.Seek(DbSeekOptions.FirstEqual, new object[] { matchValue });
                        }
                    }

                    while (results.Read())
                    {
                        if (currentOffset < firstRowOffset)
                        {
                            currentOffset++;
                            continue;
                        }

                        if (tableDirect && (matchValue != null))
                        {
                            // if we have a match value, we'll have seeked to the first match above
                            // then at this point the first non-match means we have no more matches, so
                            // we can exit out once we hit the first non-match.

                            // For string we want a case-insensitive search, so it's special-cased here
                            if (matchValue is string)
                            {
                                if (string.Compare((string)results[searchOrdinal], (string)matchValue, true) != 0)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                if (!results[searchOrdinal].Equals(matchValue))
                                {
                                    break;
                                }
                            }
                        }

                        // autofill references if desired
                        if (referenceFields == null)
                        {
                            referenceFields = Entities[entityName].References.ToArray();
                        }

                        // if the entity type changed since the last Select call, re-cache some items (perf improvements)
                        if (m_lastEntity != entityName)
                        {
                            m_fields     = Entities[entityName].Fields;
                            m_references = Entities[entityName].References.ToArray();
                            m_lastEntity = entityName;
                        }

                        bool   fieldsSet;
                        object item = CreateEntityInstance(entityName, objectType, m_fields, results, out fieldsSet);

                        if (!fieldsSet)
                        {
                            // fill in the entity field values
                            PopulateFields(entityName, m_fields, results, item, fillReferences);
                        }
                        // autofill references if desired
                        if ((fillReferences) && (referenceFields.Length > 0))
                        {
                            FillReferences(item, null, referenceFields, false);
                        }

                        yield return(item);

                        if ((fetchCount > 0) && (items.Count >= fetchCount))
                        {
                            break;
                        }
                    }
                }
            }
            finally
            {
                if ((!UseCommandCache) && (command != null))
                {
                    command.Dispose();
                }

                if (UseCommandCache)
                {
                    Monitor.Exit(CommandCache);
                }

                FlushReferenceTableCache();
                DoneWithConnection(connection, false);
            }
        }
Example #10
0
 protected EntityInfo()
 {
     Fields        = new FieldAttributeCollection();
     References    = new ReferenceAttributeCollection();
     SortingFields = new FieldAttributeCollection();
 }
Example #11
0
 public EntityInfo()
 {
     Fields     = new FieldAttributeCollection();
     References = new ReferenceAttributeCollection();
 }
Example #12
0
        private static Products ORM_CreateProxy(OpenNETCF.ORM.FieldAttributeCollection fields, System.Data.IDataReader results)
        {
            var item = new Products();

            foreach (var field in fields)
            {
                var value = results[field.Ordinal];
                switch (field.FieldName)
                {
                case "ProductID":
                    // If this is a TimeSpan, use the commented line below
                    // item.ProductID = (value == DBNull.Value) ? TimeSpan.MinValue; : new TimeSpan((long)value);
                    item.ProductID = (value == DBNull.Value) ? 0 : (long)value;
                    break;

                case "ProductName":
                    item.ProductName = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "SupplierID":
                    // If this is a TimeSpan, use the commented line below
                    // item.SupplierID = (value == DBNull.Value) ? null : new TimeSpan((long)value);
                    item.SupplierID = (value == DBNull.Value) ? 0 : (long?)value;
                    break;

                case "CategoryID":
                    // If this is a TimeSpan, use the commented line below
                    // item.CategoryID = (value == DBNull.Value) ? null : new TimeSpan((long)value);
                    item.CategoryID = (value == DBNull.Value) ? 0 : (long?)value;
                    break;

                case "QuantityPerUnit":
                    item.QuantityPerUnit = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "UnitPrice":
                    item.UnitPrice = (value == DBNull.Value) ? 0 : (decimal?)value;
                    break;

                case "UnitsInStock":
                    item.UnitsInStock = (value == DBNull.Value) ? (short?)0 : (short?)value;
                    break;

                case "UnitsOnOrder":
                    item.UnitsOnOrder = (value == DBNull.Value) ? (short?)0 : (short?)value;
                    break;

                case "ReorderLevel":
                    item.ReorderLevel = (value == DBNull.Value) ? (short?)0 : (short?)value;
                    break;

                case "Discontinued":
                    // Field 'Discontinued' not generated: Unsupported type 'Boolean'
                    break;

                case "DiscontinuedDate":
                    item.DiscontinuedDate = (value == DBNull.Value) ? null : (DateTime?)value;
                    break;
                }
            }
            return(item);
        }
Example #13
0
        private static Employees ORM_CreateProxy(OpenNETCF.ORM.FieldAttributeCollection fields, System.Data.IDataReader results)
        {
            var item = new Employees();

            foreach (var field in fields)
            {
                var value = results[field.Ordinal];
                switch (field.FieldName)
                {
                case "EmployeeID":
                    // If this is a TimeSpan, use the commented line below
                    // item.EmployeeID = (value == DBNull.Value) ? TimeSpan.MinValue; : new TimeSpan((long)value);
                    item.EmployeeID = (value == DBNull.Value) ? 0 : (long)value;
                    break;

                case "LastName":
                    item.LastName = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "FirstName":
                    item.FirstName = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Title":
                    item.Title = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "TitleOfCourtesy":
                    item.TitleOfCourtesy = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "BirthDate":
                    item.BirthDate = (value == DBNull.Value) ? null : (DateTime?)value;
                    break;

                case "HireDate":
                    item.HireDate = (value == DBNull.Value) ? null : (DateTime?)value;
                    break;

                case "Address":
                    item.Address = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "City":
                    item.City = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Region":
                    item.Region = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "PostalCode":
                    item.PostalCode = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Country":
                    item.Country = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "HomePhone":
                    item.HomePhone = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Extension":
                    item.Extension = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "Photo":
                    item.Photo = (value == DBNull.Value) ? null : (byte[])value;
                    break;

                case "Notes":
                    item.Notes = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "PhotoPath":
                    item.PhotoPath = (value == DBNull.Value) ? null : (string)value;
                    break;
                }
            }
            return(item);
        }
Example #14
0
        private static Orders ORM_CreateProxy(OpenNETCF.ORM.FieldAttributeCollection fields, System.Data.IDataReader results)
        {
            var item = new Orders();

            foreach (var field in fields)
            {
                var value = results[field.Ordinal];
                switch (field.FieldName)
                {
                case "OrderID":
                    // If this is a TimeSpan, use the commented line below
                    // item.OrderID = (value == DBNull.Value) ? TimeSpan.MinValue; : new TimeSpan((long)value);
                    item.OrderID = (value == DBNull.Value) ? 0 : (long)value;
                    break;

                case "CustomerID":
                    item.CustomerID = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "EmployeeID":
                    // If this is a TimeSpan, use the commented line below
                    // item.EmployeeID = (value == DBNull.Value) ? null : new TimeSpan((long)value);
                    item.EmployeeID = (value == DBNull.Value) ? 0 : (long?)value;
                    break;

                case "OrderDate":
                    item.OrderDate = (value == DBNull.Value) ? null : (DateTime?)value;
                    break;

                case "RequiredDate":
                    item.RequiredDate = (value == DBNull.Value) ? null : (DateTime?)value;
                    break;

                case "ShippedDate":
                    item.ShippedDate = (value == DBNull.Value) ? null : (DateTime?)value;
                    break;

                case "Freight":
                    item.Freight = (value == DBNull.Value) ? 0 : (decimal?)value;
                    break;

                case "ShipName":
                    item.ShipName = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "ShipAddress":
                    item.ShipAddress = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "ShipCity":
                    item.ShipCity = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "ShipRegion":
                    item.ShipRegion = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "ShipPostalCode":
                    item.ShipPostalCode = (value == DBNull.Value) ? null : (string)value;
                    break;

                case "ShipCountry":
                    item.ShipCountry = (value == DBNull.Value) ? null : (string)value;
                    break;
                }
            }
            return(item);
        }