Example #1
0
 protected SyncReader(Stream stream, EntityType[] knownTypes)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     this._inputStream = stream;
     this._knownTypes = knownTypes;
 }
Example #2
0
        public static IEntity CreateInstance(EntityType type)
        {
            var entity = new Entity(type);
            entity.SetIsNew();
            foreach (EntityField field in type.Fields)
            {
                object value = field.Name == "Id" 
                    ? DbRef.CreateInstance(type.TableName, Guid.NewGuid()) 
                    : GetDefaultValue(field);

                entity.SetValue(field.Name, value);
            }
            return entity;
        }
Example #3
0
 /// <summary>
 /// Constructor with KnownTypes specified
 /// </summary>
 /// <param name="stream">Input reader stream</param>
 /// <param name="knownTypes">List of types to reflect from</param>
 public ODataAtomReader(Stream stream, EntityType[] knownTypes)
     : base(stream, knownTypes)
 {
     _reader = XmlReader.Create(stream);
 }
Example #4
0
 private string[] GetColumns(EntityType type)
 {
     string[] result;
     if (!_columnsByType.TryGetValue(type, out result))
     {
         result = type.GetColumns();
         _columnsByType.Add(type, result);
     }
     return result;
 }
Example #5
0
 public System.Collections.IEnumerable SelectDirty(EntityType type)
 {
     return Select(type, false, "WHERE [IsDirty] = 1 OR [IsTombstone] = 1");
 }
Example #6
0
 public IEntity SelectById(EntityType type, String guid)
 {
     return Select(type, true, "WHERE [Id] = @p1", guid).FirstOrDefault();
 }
Example #7
0
        public IEnumerable<IEntity> Select(EntityType type, bool useView, String where, params object[] arguments)
        {
            string[] columns = GetColumns(type);
            int n;
            String cmdKey = type.Name + where + useView;
            SqliteCommand cmd;
            if (!_commands.TryGetValue(cmdKey, out cmd))
            {
                String tableName = type.TableName;
                String columnNames = "";
                foreach (string column in columns)
                    columnNames = columnNames + String.Format("{0}{1}", String.IsNullOrEmpty(columnNames) ? "" : ",", column);
                if (!useView)
                    columnNames = columnNames + ",IsTombstone";
                columnNames = columnNames + ",IsDirty";

                cmd = new SqliteCommand
                {
                    Connection = ActiveConnection,
                    CommandText =
                        String.Format("SELECT {0} FROM [{1}]{2}", columnNames, useView ? tableName : "_" + tableName,
                            String.IsNullOrEmpty(@where) ? "" : " " + @where)
                };
                n = 1;
                foreach (object p in arguments)
                {
                    cmd.Parameters.AddWithValue(String.Format("@p{0}", n), p);
                    n++;
                }
                cmd.Prepare();
                _commands.Add(cmdKey, cmd);
            }
            else
            {
                for (int i = 0; i < arguments.Length; i++)
                {
                    cmd.Parameters[i].Value = arguments[i];
                }
            }

            var list = new List<IEntity>();
            using (SqliteDataReader r = cmd.ExecuteReader())
            {
                if (r.HasRows)
                {
                    while (r.Read())
                    {
                        IEntity entity = EntityFactory.CreateInstance(type);
                        n = 0;
                        foreach (string column in columns)
                        {
                            Type t = type.GetPropertyType(column);
                            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
                                t = Nullable.GetUnderlyingType(t);

                            object value = r[n];
                            if (value.GetType().Equals(typeof(DBNull)))
                                value = null;
                            else
                            {
                                if (t.Equals(typeof(IDbRef)))
                                    value = DbRef.FromString(value.ToString());
                                else
                                {
                                    if (t == typeof(Guid))
                                    {
                                        Guid g;
                                        if (!Guid.TryParse(value.ToString(), out g))
                                        {
                                            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
                                                value = null;
                                            else
                                                throw new ArgumentException(String.Format("Can't convert value '{0}' to System.Guid", value));
                                        }
                                        else
                                            value = g;
                                    }
                                    else
                                        value = Convert.ChangeType(value, t);
                                }
                            }
                            entity.SetValue(column, value);
                            n++;
                        }

                        bool isTombstone = !useView && (long)r["IsTombstone"] != 0;
                        (entity as ISqliteEntity).IsTombstone = isTombstone;
                        (entity as ISqliteEntity).Load((long)r["IsDirty"] != 0);

                        list.Add(entity);
                    }
                }
            }
            return list;
        }
 public IsolatedStorageSchema(EntityType[] entities)
 {
     _entities = entities;
 }
 /// <summary>
 /// Adds an Type to the collection of KnownTypes.
 /// </summary>
 /// <typeparam name="T">Type to include in search</typeparam>
 public void AddType(EntityType t)
 {
     lock (this._lockObject)
     {
         CheckLockState();
         this._knownTypes.Add(t);
     }
 }
Example #10
0
        public void CreateTable(EntityType type)
        {
            //String[] arr = type.Name.Split('.');
            String tableName = type.TableName;//String.Format("{0}_{1}", arr[arr.Length - 2], arr[arr.Length - 1]);
            String columns = "";
            String columnsOnly = "";

            var indexedFields = new List<string>();

            foreach (string column in type.GetColumns())
            {
                Type columnType = GetType(type.GetPropertyType(column));
                if (!_supportedTypes.ContainsKey(columnType))
                    throw new Exception(String.Format("Unsupported column type '{0}'", columnType));

                String columnName = String.Format("{0}", column);
                String typeDeclaration = String.Format(_supportedTypes[columnType].TypeName);

                String primaryKey = type.IsPrimaryKey(column) ? "PRIMARY KEY" : "";

                const string notNull = "";
                
                if (type.IsIndexed(column))
                    indexedFields.Add(columnName);

                String s = String.Format("[{0}] {1} {2} {3}", columnName, typeDeclaration, notNull, primaryKey);
                String s1 = String.Format("[{0}]", columnName);

                if (!String.IsNullOrEmpty(columns))
                {
                    s = "," + s;
                    s1 = "," + s1;
                }

                columns = columns + s;
                columnsOnly = columnsOnly + s1;
            }

            columns += ",[IsTombstone] INTEGER,[IsDirty] INTEGER";
            columnsOnly += ",[IsDirty]";

            //create table
            using (var cmd = new SqliteCommand(String.Format("CREATE TABLE [_{0}] ({1})", tableName, columns), ActiveConnection))
                cmd.ExecuteNonQuery();

            //create tran table
            using (var cmd = new SqliteCommand(String.Format("CREATE TABLE [__{0}] ({1})", tableName, columns), ActiveConnection))
                cmd.ExecuteNonQuery();

            //create indexes
            indexedFields.Add("IsTombstone");
            foreach (String idx in indexedFields)
            {
                String idxScript = String.Format("CREATE INDEX [{0}_{1}] ON _{0}([{1}])", tableName, idx);
                using (var idxCmd = new SqliteCommand(idxScript, ActiveConnection))
                    idxCmd.ExecuteNonQuery();
            }

            //create view
            using (var cmd = new SqliteCommand(String.Format(
                "CREATE VIEW [{0}] AS SELECT {1} FROM [_{0}] WHERE IsTombstone = 0", tableName, columnsOnly), ActiveConnection))
                cmd.ExecuteNonQuery();
        }
Example #11
0
 public void CreateEntityTables(EntityType[] types)
 {
     foreach (EntityType t in types)
     {
         if (t.IsTable)
             CreateTable(t);
     }
 }
Example #12
0
        protected static Entity CreateEntity(EntryInfoWrapper wrapper, EntityType[] knownTypes)
        {
            string[] split = wrapper.TypeName.Split('.');
            string schema = split[1];
            string name = split[2];

            EntityType entityType = knownTypes.First(val => val.Name == name && val.Schema == schema);

            var entity = new Entity(entityType);
            foreach (EntityField entityField in entityType.Fields)
            {
                string value;
                if (entityField.Type == typeof(IDbRef))
                    entity.SetDbRefValue(entityField.Name, entityField.DbRefTable, wrapper.PropertyBag["__" + entityField.Name]);

                else if (wrapper.PropertyBag.TryGetValue(entityField.Name, out value))
                    entity.SetValue(entityField.Name, value);
                else
                    throw new Exception(string.Format("Property {0} not exists in response", entityField.Name));
            }
            entity.ServiceMetadata = new OfflineEntityMetadata(wrapper.IsTombstone, wrapper.Id, wrapper.ETag, wrapper.EditUri);

            return entity;
        }
Example #13
0
        private static IEnumerable<EntityType> GetResources()
        {
            var entities = new[]
            {
                "Configuration",
                "BusinessProcess",
                "Image",
                "Screen",
                "Script",
                "Style",
                "Translation"
            };

            const string schema = "resource";
            var result = new List<EntityType>();
            foreach (String entity in entities)
            {
                var fields = new List<EntityField>
                {
                    new EntityField("Id", typeof(IDbRef), true, true, 0, ToTableName(schema, entity)),
                    new EntityField("Name", typeof(string), false, false, 1 ),
                    new EntityField("Data", typeof(string), false, false, 2 ),
                    new EntityField("Parent", typeof(string), false, false, 3 )
                };

                var e = new EntityType(schema, entity, fields);

                result.Add(e);
            }
            return result;
        }
Example #14
0
 public Entity(EntityType type)
 {
     EntityType = type;
     _values = new object[EntityType.Fields.Count];
 }