private static void CreateExtensionTable(DbConnection connection, ExtendedTable extendedTable)
        {
            using (var queryCommand = connection.CreateCommand())
            {
                string primaryKeyColumn = GetPrimaryKey(connection, extendedTable.BaseTableName);

                queryCommand.CommandText = string.Format(
                    "SELECT {0} FROM {1}",
                    primaryKeyColumn,
                    extendedTable.BaseTableName);

                string dataType   = null;
                string properties =
                    string.Join(
                        ", ",
                        new [] { string.Format("{0} uniqueidentifier PRIMARY KEY", primaryKeyColumn) }
                        .Union(
                            from property in extendedTable.ExtendedProperties
                            where TypeMapping.TryGetValue(property.PropertyType, out dataType)
                            select string.Format("{0} {1}", property.PropertyName, dataType)));

                var creationCommand = connection.CreateCommand();
                creationCommand.CommandText = string.Format(
                    "CREATE TABLE {0} ({1});",
                    extendedTable.TableName,
                    properties);
                creationCommand.ExecuteNonQuery();

                using (DbDataReader reader = queryCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        object keyValue = reader[primaryKeyColumn];

                        creationCommand             = connection.CreateCommand();
                        creationCommand.CommandText = string.Format(
                            "INSERT INTO {0} ({1}) VALUES (@value);",
                            extendedTable.TableName,
                            primaryKeyColumn);

                        var parameter = creationCommand.CreateParameter();
                        parameter.DbType        = DbType.Guid;
                        parameter.ParameterName = "@value";
                        parameter.Value         = keyValue;

                        creationCommand.Parameters.Add(parameter);

                        creationCommand.ExecuteNonQuery();
                    }
                }
            }
        }
        private static void CreateExtensionTable(DbConnection connection, ExtendedTable extendedTable)
        {
            using (var queryCommand = connection.CreateCommand())
            {
                string primaryKeyColumn = GetPrimaryKey(connection, extendedTable.BaseTableName);

                queryCommand.CommandText = string.Format(
                    "SELECT {0} FROM {1}",
                    primaryKeyColumn,
                    extendedTable.BaseTableName);

                string dataType = null;
                string properties =
                    string.Join(
                        ", ",
                        new [] { string.Format("{0} uniqueidentifier PRIMARY KEY", primaryKeyColumn) }
                        .Union(
                            from property in extendedTable.ExtendedProperties
                            where TypeMapping.TryGetValue(property.PropertyType, out dataType)
                            select string.Format("{0} {1}", property.PropertyName, dataType)));

                var creationCommand = connection.CreateCommand();
                creationCommand.CommandText = string.Format(
                    "CREATE TABLE {0} ({1});",
                    extendedTable.TableName,
                    properties);
                creationCommand.ExecuteNonQuery();

                using (DbDataReader reader = queryCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        object keyValue = reader[primaryKeyColumn];

                        creationCommand = connection.CreateCommand();
                        creationCommand.CommandText = string.Format(
                            "INSERT INTO {0} ({1}) VALUES (@value);",
                            extendedTable.TableName,
                            primaryKeyColumn);

                        var parameter = creationCommand.CreateParameter();
                        parameter.DbType = DbType.Guid;
                        parameter.ParameterName = "@value";
                        parameter.Value = keyValue;

                        creationCommand.Parameters.Add(parameter);

                        creationCommand.ExecuteNonQuery();
                    }
                }
            }
        }
Esempio n. 3
0
        private static Type CreateDerivedType(ModuleBuilder moduleBuilder, ExtendedTable extendedTable, ExtendedType extendedType)
        {
            // TODO: fix
            string randomString = Guid.NewGuid().ToString().Split(new char[] { '-' })[0];
            var    typeBuilder  = moduleBuilder.DefineType(extendedType.BaseType.Name + randomString, TypeAttributes.Class | TypeAttributes.Public, extendedType.BaseType);

            foreach (var extendedProperty in extendedTable.ExtendedProperties)
            {
                AddProperty(typeBuilder, extendedType.BaseType, extendedProperty.PropertyName, extendedProperty.PropertyType);
            }

            return(typeBuilder.CreateType());
        }
Esempio n. 4
0
        private static Type CreateDerivedType(ModuleBuilder moduleBuilder, ExtendedTable extendedTable, ExtendedType extendedType)
        {
            // TODO: fix
            string randomString = Guid.NewGuid().ToString().Split(new char[] { '-' })[0];
            var typeBuilder = moduleBuilder.DefineType(extendedType.BaseType.Name + randomString, TypeAttributes.Class | TypeAttributes.Public, extendedType.BaseType);

            foreach (var extendedProperty in extendedTable.ExtendedProperties)
            {
                AddProperty(typeBuilder, extendedType.BaseType, extendedProperty.PropertyName, extendedProperty.PropertyType);
            }

            return typeBuilder.CreateType();
        }