예제 #1
0
        /// <summary>
        /// Finds the primary key field on a given entity type.
        /// </summary>
        /// <param name="entityType">Entity type to get the primary key field for.</param>
        /// <param name="pkField">Field attribute for the found primary key. Will be null if no primary key is found and <paramref name="required">required</paramref> is set to false.</param>
        /// <param name="pkProp">Property info for the property acting as the primary key. Will be null if no primary key is found and <paramref name="required">required</paramref> is set to false.</param>
        /// <param name="required">If set to true, an exception will be thrown if no primary key field is found on the entity type.</param>
        /// <exception cref="InvalidOperationException">
        /// If more than one property is found with a primary key field attribute, or,
        /// if <paramref name="required">required</paramref> is set and no primary key is found.
        /// </exception>
        internal static void FindPrimaryKey(Type entityType, out FieldAttribute pkField, out PropertyInfo pkProp, bool required)
        {
            Debug.Assert(entityType != null);

            pkField = null;
            pkProp  = null;

            //
            // Find field attribute and corresponding property for the field with PrimaryKey attribute value set to true.
            //
            foreach (PropertyInfo property in entityType.GetProperties())
            {
                FieldAttribute field = Helpers.GetFieldAttribute(property);
                if (field != null && field.PrimaryKey && field.FieldType == FieldType.Counter)
                {
                    if (pkField != null)
                    {
                        throw RuntimeErrors.MoreThanOnePrimaryKey();
                    }

                    pkField = field;
                    pkProp  = property;
                    break;
                }
            }

            //
            // Primary key field should be present in order to make the query.
            //
            if (required && (pkField == null || pkProp == null))
            {
                throw RuntimeErrors.MissingPrimaryKey();
            }
        }
예제 #2
0
        /// <summary>
        /// Executes the query and returns a single result of the specified type.
        /// </summary>
        /// <typeparam name="TResult">Type of the query result object.</typeparam>
        /// <param name="expression">Expression representing the query.</param>
        /// <returns>Singleton query result object.</returns>
        public TResult Execute <TResult>(Expression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            //
            // We expect a method call expression.
            //
            MethodCallExpression mc = expression as MethodCallExpression;

            if (mc != null && mc.Method.DeclaringType == typeof(Queryable))
            {
                //
                // First and FirstOrDefault query operators.
                //
                if (mc.Method.Name == "First" || mc.Method.Name == "FirstOrDefault")
                {
                    //
                    // Execute the query. The parser will take care of the retrieval of only one item.
                    //
                    IEnumerator <TResult> res = _context.ExecuteQuery <TResult>(expression);

                    //
                    // Return the first element of the sequence, if it exists.
                    //
                    if (res.MoveNext())
                    {
                        return(res.Current);
                    }
                    //
                    // Empty sequence is valid for FirstOrDefault call. Return the default value of TResult.
                    //
                    else if (mc.Method.Name.EndsWith("OrDefault", StringComparison.Ordinal))
                    {
                        return(default(TResult));
                    }
                    //
                    // Empty sequence is invalid for First call.
                    //
                    else
                    {
                        throw RuntimeErrors.EmptySequence();
                    }
                }
                else
                {
                    throw RuntimeErrors.UnsupportedQueryOperator(mc.Method.Name);
                }
            }

            throw RuntimeErrors.FatalError();
        }
예제 #3
0
파일: Url.cs 프로젝트: bartdesmet/CodePlex
        /// <summary>
        /// Creates a new Url instance based on a given URI and a description.
        /// </summary>
        /// <param name="address">URL to refer to.</param>
        /// <param name="description">Description for the URL.</param>
        public Url(string address, string description)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            if (!Uri.IsWellFormedUriString(address, UriKind.Absolute))
            {
                throw RuntimeErrors.InvalidUriSpecified();
            }

            _address     = address;
            _description = description ?? "";
        }
예제 #4
0
파일: Url.cs 프로젝트: bartdesmet/CodePlex
        public UrlValue(string url, string description)
        {
            _urlValue = new SPFieldUrlValue();

            if (url != null)
            {
                if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
                {
                    throw RuntimeErrors.InvalidUriSpecified();
                }

                _urlValue.Url         = url;
                _urlValue.Description = description;
            }
        }
예제 #5
0
        /// <summary>
        /// Registers a list source object for the entity as specified by <typeparamref name="T">T</typeparamref>.
        /// </summary>
        /// <param name="list">List source to register.</param>
        /// <typeparam name="T">Entity type to register the corresponding list source object for.</typeparam>
        internal void RegisterList <T>(SharePointList <T> list) where T : class
        {
            Debug.Assert(list != null);

            CheckDisposed();

            Type t = typeof(T);

            //
            // The context can only hold one SharePointList<T> object for each type T.
            //
            if (_lists.ContainsKey(t))
            {
                throw RuntimeErrors.DuplicateSharePointListObject();
            }

            _lists.Add(t, list);
        }
예제 #6
0
        /// <summary>
        /// Helper method to get the ListAttribute applied for the given entity type. An InvalidOperationException will be thrown if no ListAttribute is found.
        /// </summary>
        /// <param name="type">Type to get the ListAttribute for.</param>
        /// <param name="throwExceptionIfMissing">Indicates whether or not to throw an exception if the list attribute is missing.</param>
        /// <returns>ListAttribute applied on the entity object.</returns>
        internal static ListAttribute GetListAttribute(Type type, bool throwExceptionIfMissing)
        {
            Debug.Assert(type != null);

            ListAttribute[] la = type.GetCustomAttributes(typeof(ListAttribute), false) as ListAttribute[];
            if (la != null && la.Length != 0)
            {
                return(la[0]);
            }
            else
            {
                if (throwExceptionIfMissing)
                {
                    throw RuntimeErrors.MissingListAttribute();
                }
                else
                {
                    return(null);
                }
            }
        }
예제 #7
0
 public static bool DateRangesOverlap(DateTime value, params DateTime?[] fields)
 {
     throw RuntimeErrors.CamlMethodsInvalidUse();
 }