Ejemplo n.º 1
0
        public void RegisterDynamicEntity(DynamicEntityDefinition entityDefinition, bool ensureCompatibility)
        {
            if (entityDefinition.Fields.Count == 0)
            {
                throw new ArgumentException("EntityDefinition must contain at least one field");
            }

            // verify this is a unique entity name
            // check the type-defined entities
            lock (m_entities)
            {
                var existing = m_entities.FirstOrDefault(e => e.EntityName == entityDefinition.EntityName);

                if (existing != null)
                {
                    if (!ensureCompatibility)
                    {
                        throw new EntityAlreadyExistsException(entityDefinition.EntityName);
                    }
                    else
                    {
                        m_entities[entityDefinition.EntityName] = entityDefinition;
                    }
                }
                else
                {
                    m_entities.Add(entityDefinition);
                }
            }


            OnDynamicEntityRegistration(entityDefinition, ensureCompatibility);
        }
Ejemplo n.º 2
0
        public override DynamicEntityDefinition DiscoverDynamicEntity(string entityName)
        {
            var table = m_session.Data.GetTable(entityName);

            if (table == null)
            {
                return(null);
            }

            var fields = new List <FieldAttribute>();

            foreach (var field in table.Fields)
            {
                fields.Add(new FieldAttribute()
                {
                    FieldName    = field.Name,
                    IsPrimaryKey = field.IsPrimaryKey.HasValue ? field.IsPrimaryKey.Value : false,
                    DataType     = field.TypeName.ParseToDbType()
                });
            }

            var entityDefinition = new DynamicEntityDefinition(entityName, fields);

            RegisterEntityInfo(entityDefinition);
            return(entityDefinition);
        }
Ejemplo n.º 3
0
        protected override void OnDynamicEntityRegistration(DynamicEntityDefinition definition, bool ensureCompatibility)
        {
            string name = definition.EntityName;

            if (!TableExists(name))
            {
                var fields = new List <Field>();

                foreach (var field in definition.Fields)
                {
                    if (ReservedWords.Contains(field.FieldName, StringComparer.InvariantCultureIgnoreCase))
                    {
                        throw new ReservedWordException(field.FieldName);
                    }

                    fields.Add(FieldFactory.GetFieldForAttribute(field, definition.EntityAttribute.KeyScheme));
                }

                m_session.Data.CreateTable(name, name, fields);
            }
            else
            {
                ValidateTable(definition);
            }
        }
Ejemplo n.º 4
0
        public static DynamicEntity AsDynamicEntity(this AzureEntity entity, DynamicEntityDefinition definition)
        {
            var de = new DynamicEntity(definition.EntityName);

            de.Fields.Add("PartitionKey", entity.PartitionKey);
            de.Fields.Add("RowKey", entity.RowKey);

            foreach (var f in entity.Fields)
            {
                if (!definition.Fields.ContainsField(f.Name))
                {
                    continue;
                }

                if ((f.Value.ToString() == string.Empty) && (definition.Fields[f.Name].DataType != System.Data.DbType.String))
                {
                    // we store an empty string for non-string 'null' values in the Table Service
                    de.Fields.Add(f.Name, null);
                }
                else
                {
                    de.Fields.Add(f.Name, f.Value);
                }
            }

            return(de);
        }
Ejemplo n.º 5
0
        public virtual void RegisterDynamicEntity(DynamicEntityDefinition entityDefinition)
        {
            foreach (var f in entityDefinition.Fields)
            {
                if (entityDefinition.EntityAttribute.KeyScheme == KeyScheme.Identity)
                {
                    throw new NotSupportedException("The current provider does not currently support Identity dynamic entities");
                }
            }

            RegisterDynamicEntity(entityDefinition, false);
        }
Ejemplo n.º 6
0
        protected override void OnDynamicEntityRegistration(DynamicEntityDefinition definition, bool ensureCompatibility)
        {
            // create the table if necessary
            TableService.Tables.CreateTableIfNotExist(definition.EntityName);

            // store the definiton.  later we'll use it for detecting nulls, ignoring columns, etc
            if (!m_dynamicDefinitions.ContainsKey(definition.EntityName))
            {
                m_dynamicDefinitions.Add(definition.EntityName, definition);
            }
            else if (ensureCompatibility)
            {
                m_dynamicDefinitions[definition.EntityName] = definition;
            }
            else
            {
                throw new Exception("Dynamic type already registered");
            }
        }
Ejemplo n.º 7
0
        private DynamicEntityDefinition(DynamicEntityDefinition source)
        {
            var entityAttribute = new EntityAttribute(source.EntityAttribute.KeyScheme);

            entityAttribute.NameInStore = source.EntityAttribute.NameInStore;

            this.EntityType = typeof(DynamicEntityDefinition);
            this.Initialize(entityAttribute, this.EntityType);

            // TODO make a copy of these?
            this.Fields.AddRange(source.Fields);

            var k = this.Fields.FirstOrDefault(c => c.IsPrimaryKey);

            if (k != null)
            {
                this.PrimaryKeyColumnName = k.FieldName;
            }
        }
Ejemplo n.º 8
0
 protected abstract void OnDynamicEntityRegistration(DynamicEntityDefinition definition, bool ensureCompatibility);
Ejemplo n.º 9
0
        public override DynamicEntityDefinition DiscoverDynamicEntity(string entityName)
        {
            if (!TableExists(entityName))
            {
                return(null);
            }

            var connection = GetConnection(true);

            try
            {
                using (var cmd = GetNewCommandObject())
                {
                    cmd.Connection  = connection;
                    cmd.Transaction = CurrentTransaction;

                    cmd.CommandText = string.Format("SELECT COLUMN_NAME, ORDINAL_POSITION, IS_NULLABLE, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE, CHARACTER_MAXIMUM_LENGTH "
                                                    + "FROM information_schema.columns WHERE TABLE_NAME = '{0}' ORDER BY ORDINAL_POSITION", entityName);

                    var fields = new List <FieldAttribute>();

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var name     = reader.GetString(0);
                            var nullable = string.Compare(reader.GetString(2), "YES", true) == 0;
                            var type     = reader.GetString(3).ParseToDbType();

                            var field = new FieldAttribute()
                            {
                                DataType    = type,
                                FieldName   = name,
                                AllowsNulls = nullable,
                            };

                            if (!reader.IsDBNull(4))
                            {
                                field.Precision = Convert.ToInt32(reader.GetValue(4));
                            }
                            if (!reader.IsDBNull(5))
                            {
                                field.Scale = Convert.ToInt32(reader.GetValue(5));
                            }

                            if (!reader.IsDBNull(6))
                            {
                                field.Length = Convert.ToInt32(reader.GetValue(6));
                            }

                            fields.Add(field);
                        }
                    }

                    cmd.CommandText = string.Format(
                        "SELECT ac.name, ind.is_primary_key, ind.is_unique, ic.is_descending_key, col.collation_name, idc.name " +
                        "FROM sys.indexes ind " +
                        "INNER JOIN sys.index_columns ic " +
                        "  ON  ind.object_id = ic.object_id and ind.index_id = ic.index_id " +
                        "INNER JOIN sys.columns col  " +
                        "  ON ic.object_id = col.object_id and ic.column_id = col.column_id  " +
                        "INNER JOIN sys.tables t  " +
                        "  ON ind.object_id = t.object_id " +
                        "INNER JOIN sys.identity_columns idc " +
                        "  ON col.object_id = idc.object_id and col.column_id = idc.column_id " +
                        "INNER JOIN sys.columns ac " +
                        "  ON ac.object_id = col.object_id and ac.column_id = col.column_id " +
                        "WHERE t.name = '{0}'", entityName);

                    string identiyColumn = null;

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var column = reader.GetString(0);
                            var pk     = Convert.ToBoolean(reader.GetValue(1));
                            var unique = Convert.ToBoolean(reader.GetValue(2));

                            var field = fields.FirstOrDefault(f => f.FieldName == column);
                            if (pk)
                            {
                                field.IsPrimaryKey = true;
                            }
                            else
                            {
                                var isdescending = Convert.ToInt32(reader.GetValue(3));
                                field.SearchOrder = isdescending == 0 ? FieldSearchOrder.Ascending : FieldSearchOrder.Descending;
                            }
                            if (unique)
                            {
                                field.RequireUniqueValue = true;
                            }

                            identiyColumn = reader.GetString(5);
                        }
                    }


                    var entityDefinition = new DynamicEntityDefinition(
                        entityName,
                        fields,
                        string.IsNullOrEmpty(identiyColumn) ? KeyScheme.None : KeyScheme.Identity);

                    RegisterEntityInfo(entityDefinition);

                    return(entityDefinition);
                }
            }
            finally
            {
                DoneWithConnection(connection, true);
            }
        }
Ejemplo n.º 10
0
 public override void RegisterDynamicEntity(DynamicEntityDefinition entityDefinition)
 {
     base.RegisterDynamicEntity(entityDefinition, true);
 }
Ejemplo n.º 11
0
        public override DynamicEntityDefinition DiscoverDynamicEntity(string entityName)
        {
            if (!TableExists(entityName))
            {
                throw new EntityNotFoundException(entityName);
            }

            var connection = GetConnection(true);

            try
            {
                using (var cmd = GetNewCommandObject())
                {
                    cmd.Connection  = connection;
                    cmd.Transaction = CurrentTransaction;

                    cmd.CommandText = string.Format("SELECT COLUMN_NAME, ORDINAL_POSITION, IS_NULLABLE, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE FROM information_schema.columns WHERE TABLE_NAME = '{0}' ORDER BY ORDINAL_POSITION", entityName);

                    var fields = new List <FieldAttribute>();

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var name     = reader.GetString(0);
                            var nullable = string.Compare(reader.GetString(2), "YES", true) == 0;
                            var type     = reader.GetString(3).ParseToDbType();

                            var field = new FieldAttribute()
                            {
                                DataType    = type,
                                FieldName   = name,
                                AllowsNulls = nullable,
                            };

                            if (!reader.IsDBNull(4))
                            {
                                field.Precision = Convert.ToInt32(reader.GetValue(4));
                            }
                            if (!reader.IsDBNull(5))
                            {
                                field.Scale = Convert.ToInt32(reader.GetValue(5));
                            }

                            fields.Add(field);
                        }
                    }

                    // get PK info
                    cmd.CommandText = string.Format(
                        "SELECT COLUMN_NAME, CONSTRAINT_TYPE " +
                        "FROM information_schema.table_constraints t " +
                        "LEFT JOIN information_schema.key_column_usage k " +
                        "USING(constraint_name,table_schema,table_name) " +
                        "WHERE TABLE_NAME = '{0}'", entityName);

                    // result will be PRIMARY_KEY or UNIQUE

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var column = reader.GetString(0);
                            var t      = reader.GetString(1);

                            var pk     = t == "PRIMARY_KEY";
                            var unique = t == "UNIQUE";

                            var field = fields.FirstOrDefault(f => f.FieldName == column);
                            if (pk)
                            {
                                field.IsPrimaryKey = true;
                            }

                            if (unique)
                            {
                                field.RequireUniqueValue = true;
                            }
                        }
                    }


                    var entityDefinition = new DynamicEntityDefinition(entityName, fields);
                    RegisterEntityInfo(entityDefinition);
                    return(entityDefinition);
                }
            }
            finally
            {
                DoneWithConnection(connection, true);
            }
        }
Ejemplo n.º 12
0
        public override DynamicEntityDefinition DiscoverDynamicEntity(string entityName)
        {
            if (!TableExists(entityName))
            {
                return(null);
            }

            var connection = GetConnection(true);

            try
            {
                using (var cmd = GetNewCommandObject())
                {
                    cmd.Connection  = connection;
                    cmd.Transaction = CurrentTransaction;

                    cmd.CommandText = string.Format("PRAGMA table_info({0})", entityName);

                    // cid, name, type, notnull, dflt_value, pk
                    var fields = new List <FieldAttribute>();

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var cid      = reader.GetInt64(0);
                            var name     = reader.GetString(1);
                            var type     = reader.GetString(2).ParseToDbType();
                            var nullable = reader.GetInt64(3) == 0;
                            // 4 == default value - TODO
                            var isPK = reader.GetInt64(5) == 1;

                            var field = new FieldAttribute()
                            {
                                DataType     = type,
                                FieldName    = name,
                                AllowsNulls  = nullable,
                                IsPrimaryKey = isPK
                            };

                            fields.Add(field);
                        }
                    }

                    cmd.CommandText = string.Format("SELECT 1 FROM sqlite_master WHERE tbl_name='{0}' AND sql LIKE '%AUTOINCREMENT%'", entityName);

                    var autoIncrement = cmd.ExecuteScalar();

                    // TODO: handle index metadata (ascending/descending, unique, etc)
                    // PRAGMA index_list(TABLENAME)
                    // seq | name | unique
                    // PRAGMA index_info(INDEXNAME)
                    // seqno | cid | name |


                    var entityDefinition = new DynamicEntityDefinition(entityName, fields);

                    try
                    {
                        if (Convert.ToInt64(autoIncrement) == 1L)
                        {
                            entityDefinition.EntityAttribute.KeyScheme = KeyScheme.Identity;
                        }
                    }
                    catch
                    {
                        // not auto-increment
                    }

                    RegisterEntityInfo(entityDefinition);

                    return(entityDefinition);
                }
            }
            finally
            {
                DoneWithConnection(connection, true);
            }
        }
Ejemplo n.º 13
0
        public override DynamicEntityDefinition DiscoverDynamicEntity(string entityName)
        {
            if (!TableExists(entityName))
            {
                return(null);
            }

            var connection = GetConnection(true);

            try
            {
                using (var cmd = GetNewCommandObject())
                {
                    cmd.Connection  = connection;
                    cmd.Transaction = CurrentTransaction;

                    cmd.CommandText = string.Format("SELECT COLUMN_NAME, ORDINAL_POSITION, IS_NULLABLE, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE FROM information_schema.columns WHERE TABLE_NAME = '{0}' ORDER BY ORDINAL_POSITION", entityName);

                    var fields = new List <FieldAttribute>();

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var name     = reader.GetString(0);
                            var nullable = string.Compare(reader.GetString(2), "YES", true) == 0;
                            var type     = reader.GetString(3).ParseToDbType();

                            var field = new FieldAttribute()
                            {
                                DataType    = type,
                                FieldName   = name,
                                AllowsNulls = nullable,
                            };

                            if (!reader.IsDBNull(4))
                            {
                                field.Precision = Convert.ToInt32(reader.GetValue(4));
                            }
                            if (!reader.IsDBNull(5))
                            {
                                field.Scale = Convert.ToInt32(reader.GetValue(5));
                            }

                            fields.Add(field);
                        }
                    }

                    cmd.CommandText = string.Format("SELECT COLUMN_NAME, PRIMARY_KEY, [UNIQUE], COLLATION FROM information_schema.indexes WHERE TABLE_NAME = '{0}'", entityName);

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var column = reader.GetString(0);
                            var pk     = Convert.ToBoolean(reader.GetValue(1));
                            var unique = Convert.ToBoolean(reader.GetValue(2));

                            var field = fields.FirstOrDefault(f => f.FieldName == column);
                            if (pk)
                            {
                                field.IsPrimaryKey = true;
                            }
                            else
                            {
                                var collation = Convert.ToInt32(reader.GetValue(3));
                                field.SearchOrder = collation == 1 ? FieldSearchOrder.Ascending : FieldSearchOrder.Descending;
                            }
                            if (unique)
                            {
                                field.RequireUniqueValue = true;
                            }
                        }
                    }


                    var entityDefinition = new DynamicEntityDefinition(entityName, fields);
                    RegisterEntityInfo(entityDefinition);
                    return(entityDefinition);
                }
            }
            finally
            {
                DoneWithConnection(connection, true);
            }
        }
Ejemplo n.º 14
0
        public override DynamicEntityDefinition DiscoverDynamicEntity(string entityName)
        {
            if (!TableExists(entityName))
            {
                throw new EntityNotFoundException(entityName);
            }

            var connection = GetConnection(true);

            try
            {
                using (var cmd = GetNewCommandObject())
                {
                    cmd.Connection  = connection;
                    cmd.Transaction = CurrentTransaction;

                    cmd.CommandText = string.Format(
                        "SELECT COLUMN_NAME, NULLABLE, DATA_TYPE, DATA_PRECISION, DATA_SCALE FROM all_tab_cols "
                        + " WHERE UPPER(TABLE_NAME) = UPPER('{0}') ORDER BY COLUMN_ID", entityName);

                    var fields = new List <FieldAttribute>();

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var name     = reader.GetString(0);
                            var nullable = string.Compare(reader.GetString(1), "Y", true) == 0;
                            var type     = ParseDbType(reader.GetString(2));

                            var field = new FieldAttribute()
                            {
                                DataType    = type,
                                FieldName   = name,
                                AllowsNulls = nullable,
                            };

                            if (!reader.IsDBNull(3))
                            {
                                field.Precision = Convert.ToInt32(reader.GetValue(3));
                            }
                            if (!reader.IsDBNull(4))
                            {
                                field.Scale = Convert.ToInt32(reader.GetValue(4));
                            }

                            fields.Add(field);
                        }
                    }

                    //CONSTRAINT_TYPE (from 11gR2 docs)
                    //C - Check constraint on a table
                    //P - Primary key
                    //U - Unique key
                    //R - Referential integrity
                    //V - With check option, on a view
                    //O - With read only, on a view
                    //H - Hash expression
                    //F - Constraint that involves a REF column
                    //S - Supplemental logging

                    // INDEX_TYPE
                    // ASC == NORMAL
                    // DESC == FUNCTION-BASED NORMAL

                    cmd.CommandText = string.Format(
                        "SELECT cons.constraint_type, cons.constraint_name, cols.column_name, i.index_type " +
                        "FROM all_constraints cons " +
                        "LEFT OUTER JOIN all_cons_columns cols " +
                        "ON cons.table_name = cols.table_name " +
                        "LEFT OUTER JOIN all_indexes i " +
                        "ON cons.index_name = i.index_name " +
                        "WHERE UPPER(cols.table_name) = UPPER('{0}')"
                        , entityName);

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var column = reader.GetString(2);
                            var ct     = reader.GetString(0).TrimEnd();
                            var pk     = false;
                            var unique = false;

                            switch (ct)
                            {
                            case "P":
                                pk     = true;
                                unique = true;
                                break;

                            case "U":
                                unique = true;
                                break;
                            }

                            var field = fields.FirstOrDefault(f => f.FieldName == column);
                            if (pk)
                            {
                                field.IsPrimaryKey = true;
                            }
                            else
                            {
                                var isdescending = reader.GetString(3).IndexOf("FUNTION-BASED", StringComparison.InvariantCultureIgnoreCase) >= 0;
                                field.SearchOrder = isdescending ? FieldSearchOrder.Descending : FieldSearchOrder.Ascending;
                            }
                            if (unique)
                            {
                                field.RequireUniqueValue = true;
                            }
                        }
                    }

                    var entityDefinition = new DynamicEntityDefinition(entityName, fields);
                    RegisterEntityInfo(entityDefinition);
                    return(entityDefinition);
                }
            }
            finally
            {
                DoneWithConnection(connection, true);
            }
        }