Exemplo n.º 1
0
 internal EntityDeleteArgs(EntityInfo entity, IEnumerable<FilterCondition> filters)
 {
     Item = null;
     Filters = filters;
     EntityInfo = entity;
     Data = null;
     Timespan = new TimeSpan(0);
 }
        public DynamicEntityInfo(string entityName, IEnumerable<FieldAttribute> fields, IEnumerable<ReferenceAttribute> references, KeyScheme keyScheme, EntityInfo.SerializerDelegate serializer, EntityInfo.DeserializerDelegate deserializer)
        {
            this.Registered = false;
            this.AllowRuntimeChanges = DynamicEntityInfo.CanChangeDefinitionsAtRuntime;

            if (entityName == null || entityName.Length <= 0)
                throw new ArgumentException("EntityName", "A DynamicEntityInfo does not accept a null or empty EntityName");

            var entityAttribute = new EntityAttribute(keyScheme);
            entityAttribute.NameInStore = entityName;
            this.EntityType = typeof(DynamicEntityInfo);
            this.Initialize(entityAttribute, this.EntityType);
            this.Serializer = serializer;
            this.Deserializer = deserializer;
            if (fields != null) this.Fields.AddRange(fields);
            if (references != null) this.References.AddRange(references);
        }
Exemplo n.º 3
0
 protected abstract void CreateTable(EntityInfo entity);
Exemplo n.º 4
0
 public abstract bool FieldExists(EntityInfo entityInfo, FieldAttribute fieldInfo);
Exemplo n.º 5
0
 public abstract bool TableExists(EntityInfo entityInfo);
Exemplo n.º 6
0
 internal EntityTypeAddedArgs(EntityInfo info)
 {
     EntityInfo = info;
 }
Exemplo n.º 7
0
 internal EntityTypeChangedArgs(EntityInfo info, FieldAttribute field)
 {
     EntityInfo = info;
     ReferenceAttribute = null;
     FieldAttribute = field;
     TableCreated = false;
 }
Exemplo n.º 8
0
 internal EntityTypeAddedArgs(EntityInfo info)
 {
     EntityInfo = info;
 }
Exemplo n.º 9
0
 internal EntitySelectArgs(EntityInfo info, IEnumerable<FilterCondition> filters, bool fillReferences)
 {
     Info = info;
     FillReferences = fillReferences;
     Filters = filters;
     Data = null;
     Timespan = new TimeSpan(0);
 }
Exemplo n.º 10
0
        protected void ValidateTable(IDbConnection connection, EntityInfo entity)
        {
            using (var command = new SqlCommand())
            {
                command.Connection = connection as SqlConnection;

                // first make sure the table exists
                //var sql = string.Format("SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '{0}'", entity.EntityAttribute.NameInStore);
                var sql = string.Format("SELECT COUNT(*) FROM sysobjects WHERE xtype='u' AND name='{0}'", entity.EntityAttribute.NameInStore);
                command.CommandText = sql;

                var count = Convert.ToInt32(command.ExecuteScalar());

                if (count == 0)
                {
                    CreateTable(connection, entity);
                }
                else
                {
                    foreach (var field in entity.Fields)
                    {
                        if (ReservedWords.Contains(field.FieldName, StringComparer.InvariantCultureIgnoreCase))
                        {
                            throw new ReservedWordException(field.FieldName);
                        }

                        // yes, I realize hard-coded ordinals are not a good practice, but the SQL isn't changing, it's method specific
                        sql = string.Format("SELECT column_name, "         // 0
                                            + "data_type, "                // 1
                                            + "character_maximum_length, " // 2
                                            + "numeric_precision, "        // 3
                                            + "numeric_scale, "            // 4
                                            + "is_nullable "
                                            + "FROM information_schema.columns "
                                            + "WHERE (table_name = '{0}' AND column_name = '{1}')",
                                            entity.EntityAttribute.NameInStore, field.FieldName);

                        command.CommandText = sql;

                        using (var reader = command.ExecuteReader())
                        {
                            if (!reader.Read())
                            {
                                // field doesn't exist - we must create it
                                var alter = new StringBuilder(string.Format("ALTER TABLE {0} ", entity.EntityAttribute.NameInStore));
                                alter.Append(string.Format("ADD [{0}] {1} {2}",
                                                           field.FieldName,
                                                           GetFieldDataTypeString(entity.EntityName, field),
                                                           GetFieldCreationAttributes(entity.EntityAttribute, field)));

                                using (var altercmd = new SqlCommand(alter.ToString(), connection as SqlConnection))
                                {
                                    altercmd.ExecuteNonQuery();
                                }
                            }
                            else
                            {
                                // TODO: verify field length, etc.
                            }
                        }
                    }
                }
            }
        }