예제 #1
0
        public IEnumerator <TResult> GetEnumerator()
        {
            while (Reader.Read())
            {
                TRowType item     = Activator.CreateInstance <TRowType>();
                var      typeData = KnownTypeData.GetTypeData(typeof(TRowType));

                var currentKeys = Reader.GetCurrentKeys();

                foreach (var k in currentKeys)
                {
                    var p = typeData.PropertiesByFieldName[k];
                    var realPropertyName = k;
                    var fqlFieldName     = p.FqlFieldName;
                    var propInfo         = p.PropertyInfo;
                    var value            = Reader[fqlFieldName, propInfo.PropertyType];
                    if (value != null)
                    {
                        propInfo.SetValue(item, value, null);
                    }
                }
                if (Selector != null)
                {
                    yield return(Selector(item));
                }
                else
                {
                    yield return((TResult)(object)item);                   //TODO:
                }
            }
        }
예제 #2
0
        private void _Build(MemberExpression exp)
        {
            var member     = exp.Member;
            var memberType = ReflectionHelper.GetMemberType(member);

            if (typeof(IFqlDataQuery).IsAssignableFrom(memberType))             //Query
            {
                if (exp.Expression is ConstantExpression)
                {
                    var obj   = ((ConstantExpression)(exp.Expression)).Value;
                    var value = ReflectionHelper.GetMemberValue(member, obj);
                    Build(value);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
            else if (typeof(IFqlTable).IsAssignableFrom(memberType))             //Table
            {
                var tableRowType = ((PropertyInfo)exp.Member).PropertyType.GetGenericArguments()[0];
                var td           = KnownTypeData.GetTypeData(tableRowType);
                var tblName      = GetTableName(td);
                Write(tblName);
            }
            else             //Column
            {
                var memberName = exp.Member.Name;
                var td         = KnownTypeData.GetTypeData(exp.Member.DeclaringType);
                if (exp.Member.Name == "Me")
                {
                    Write("me()");
                }
                else if (td.Properties.ContainsKey(memberName))
                {
                    var pd = td.Properties[exp.Member.Name];
                    Write(pd.FqlFieldName);
                }
                else if (exp.Expression != null &&
                         exp.Expression.Type == typeof(string) &&
                         exp.Member != null &&
                         exp.Member.Name == "Length"
                         )
                {
                    var p = exp.Expression == null ? null : Eval(exp.Expression);
                    Write("strlen(" + (p as PropNameString).Name + ")");
                }
                else                 //Variable
                {
                    var value = Eval(exp);
                    Build(value);
                }
            }
        }
예제 #3
0
        private T LoadObject <T>(object key) where T : class        //FacebookObject
        {
            var td     = KnownTypeData.GetTypeData(typeof(T));
            var column = td.IdentityProperty;

            if (column == null)
            {
                return(null);
            }
            var propsCSV = td.Properties.StringConcat(p => p.Value.FqlFieldName, "", ",", "");
            var fql      = String.Format("SELECT {0} from {1} where {2}={3} limit 1",
                                         propsCSV,
                                         td.FqlTableName,
                                         td.IdentityProperty.FqlFieldName,
                                         key);

            return((GetType().GetMethod("PerformGenericFql").MakeGenericMethod(typeof(T)).Invoke(this, new object[] { fql }) as IEnumerable <T>).FirstOrDefault());
        }
예제 #4
0
        internal static string FixQuery(string fql)
        {
            var words = fql.Split(' ');

            if (words.Length < 3)
            {
                return(fql);
            }
            if (words[0].ToLower() != "select")
            {
                return(fql);
            }
            if (words[2].ToLower() != "from")
            {
                return(fql);
            }
            var replacement = words[1];

            if (replacement != "*")
            {
                return(fql);
            }
            var tableName = words[3].ToLower();

            if (tableName.StartsWith("app."))
            {
                tableName = tableName.Substring(4);
            }
            var td = KnownTypeData.GetTypeDataByTableName(tableName);

            if (td == null)
            {
                throw new Exception();
            }
            replacement = td.PropertiesByFieldName.Keys.StringConcat(",");
            if (replacement.IsNotNullOrEmpty())
            {
                words[1] = replacement;
            }
            return(words.StringConcat(" "));
        }
예제 #5
0
        /// <summary>
        /// Performs a generic FQL query against facebook
        /// </summary>
        public IEnumerable <T> PerformGenericFql <T>(string fql) where T : class
        {
            var items = new List <T>();

            fql = FixQuery(fql);

            var result = Provider.ExecuteFqlQuery(fql);
            var reader = new FacebookDataReader(result);
            var td     = KnownTypeData.GetTypeData(typeof(T));

            while (reader.Read())
            {
                T   item    = null;
                var elem    = Activator.CreateInstance <T>();
                var fetched = false;
                if (td.IdentityProperty != null)
                {
                    var idVal = reader[td.IdentityProperty.FqlFieldName];
                    item    = GetObject <T>(idVal, false);
                    fetched = item != null;
                }
                if (!fetched)
                {
                    item = Activator.CreateInstance <T>();
                    //item.DataContext = this;
                    if (item != null && td.IdentityProperty != null)
                    {
                        StoreObject <T>(item, reader[td.IdentityProperty.FqlFieldName]);
                    }
                }

                foreach (var pData in td.Properties)
                {
                    pData.Value.PropertyInfo.SetValue(item, reader[pData.Value.FqlFieldName], null);
                }
                items.Add(item);
            }
            return(items);
        }
예제 #6
0
        private void _Build(ConstantExpression exp)
        {
            var value = exp.Value;

            if (value == null)
            {
                Write("NULL");
            }
            else
            {
                var type = value.GetType();
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(FqlTable <>))
                {
                    var elemType = EnumerableHelper.GetEnumerableItemType(type);
                    var tblName  = GetTableName(KnownTypeData.GetTypeData(elemType));
                    Write("{0}", tblName);
                }
                else
                {
                    Build(exp.Value);
                }
            }
        }
예제 #7
0
 /// <summary>
 /// Stores an object in the database cache.
 /// </summary>
 public virtual void StoreObject <T>(T o, object key) where T : class       // : FacebookObject
 {
     StoreObject(KnownTypeData.GetTypeData(typeof(T)), o, key);
 }