public SaveProcedureBuilder WriteInputTableCreateTypeStatement(string innerNamespace, string className, List <SchemaColumnInfo> columns)
        {
            _inputTableVarTypeName = string.Format("{0}[{1}Input]",
                                                   string.IsNullOrWhiteSpace(innerNamespace) ? "" : string.Format("[{0}].", innerNamespace),
                                                   className);
            _inputTableVarName = "tbl";
            WriteLine("CREATE TYPE [{0}].[{1}Input] AS table ", innerNamespace, className)
            .Write("(").WriteLine();
            SchemaColumnInfo item = columns[0];

            WriteLine(" {0} {1}{2}{3}{4}",
                      item.ColumnName,
                      item.DataTypeName.ToLower(),
                      getItemSuffix(item),
                      item.AllowDBNull ? "" : " NOT NULL",
                      item.IsKey ? " UNIQUE" : "");
            for (int i = 1; i < columns.Count; i++)
            {
                item = columns[i];
                WriteLine(",{0} {1}{2}{3}{4}",
                          item.ColumnName,
                          item.DataTypeName.ToLower(),
                          getItemSuffix(item),
                          item.AllowDBNull ? "" : " NOT NULL",
                          item.IsKey ? " UNIQUE" : "");
            }
            WriteLine(")");
            return(this);
        }
        public SaveProcedureBuilder WriteUpsertStatementColumn(SchemaColumnInfo column, string prefix = "", string suffix = "")
        {
            string columnVariable = string.Format(
                "{0}{1}{2}",
                prefix,
                column.ColumnName,
                suffix);

            WriteLine(columnVariable);
            return(this);
        }
        protected string getItemSuffix(SchemaColumnInfo col)
        {
            string ret = "";

            switch (col.DataTypeName.ToLower())
            {
            case "varchar":
            case "nvarchar":
                ret = "(" + col.ColumnSize + ")";
                break;

            // todo: decimal etc.
            default:
                ret = "";
                break;
            }
            return(ret);
        }
Exemplo n.º 4
0
 protected void AddDataAnnotationAttributes(SchemaColumnInfo col)
 {
     if (_options.AddDataAnnotationAttributes)
     {
         _builder.WriteLine($"[Column(\"{col.ColumnName}\")]");
         if (col.ProviderSpecificDataType == typeof(System.Data.SqlTypes.SqlString))
         {
             if (col.AllowDBNull == false)
             {
                 _builder.WriteLine("[Required(AllowEmptyStrings = false)]");
             }
             _builder.WriteLine($"[MaxLength(\"{col.ColumnSize}\")]");
         }
         else if (col.AllowDBNull == false)
         {
             _builder.WriteLine("[Required]");
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Interrogates a table, builds the class file STRING from its output, and returns the name of the class.
        /// Note: the class's .cs file contents have still not been written after this method call.
        /// </summary>
        /// <param name="fullyQualifiedTableName"></param>
        /// <returns></returns>
        protected ClassConfiguration BuildClassForTable(string fullyQualifiedTableName, ICollection <SchemaColumnInfo> schemaColumns, string overrideEntityNamespace = null)
        {
            _builder.Clear();
            string tableName = fullyQualifiedTableName.RemoveAll(new char[] { '[', ']' });

            var    entityNamespace      = GetEntityNamespace();
            string innerNamespace       = GetInnerNamespace(tableName: tableName);
            var    fullNamespace        = innerNamespace.HasValue() ? $"{entityNamespace}.{innerNamespace}" : entityNamespace;
            string className            = GetEntityClassNameFromTableName(fullyQualifiedTableName);
            string unqualifiedTableName = StringHelpers.RightOfLast(tableName, ".");

            bool             foundPk          = false;
            SchemaColumnInfo primaryKeyColumn = null;

            if ((primaryKeyColumn = (schemaColumns.FirstOrDefault(col => col.IsKey))) != null)
            {
                foundPk = true;
            }

            var config = new ClassConfiguration(schemaColumns)
            {
                ClassName            = className,
                EntityNamespace      = entityNamespace,
                InnerNamespace       = innerNamespace,
                PrimaryKeyColumn     = primaryKeyColumn,
                UnqualifiedTableName = unqualifiedTableName
            };

            config.EntityOutputDirectory = GetEntityOutputDirectory(config);

            _builder
            .WriteUsingNamespaces()
            .WriteLine()
            .WriteNamespaceDeclaration(fullNamespace)
            .WriteClassDeclaration(
                className: className,
                // TODO: Move class-specific _Options properties onto ClassConfiguration
                inherits: _options.EntitiesInherit,
                implementsInterfaces: _options.EntitiesImplementInterfaces,
                classAttributes: _options.EntityAttributes,
                additionalFind: _options.AdditionalTokenFind,
                additionalReplace: _options.AdditionalTokenReplace,
                tableName: tableName);


            //List<SchemaColumnInfo> columnInfos = getTableColumns(fullyQualifiedTableName);
            foreach (var col in schemaColumns)
            {
                string propertyName    = col.PropertyName; //col.ColumnName;
                string propertyType    = col.DataType.Name;
                string columnName      = col.ColumnName ?? col.BaseColumnName;
                bool   useNullableType = (col.AllowDBNull && col.DataType.IsValueType);
                string newPropertyName = propertyName != col.ColumnName
                    ? propertyName
                    : col.ColumnName.Collapse(StringHelpers.ChangeCaseTypes.PascalCase, false, this._options.AdditionalCollapseTokens.Split(';'));
                if (col.IsKey || (!foundPk && (columnName.Equals(unqualifiedTableName + "ID", StringComparison.OrdinalIgnoreCase) || columnName.Equals("ID", StringComparison.OrdinalIgnoreCase))))
                {
                    foundPk          = true;
                    primaryKeyColumn = col;
                    primaryKeyColumn.PropertyName = newPropertyName = _options.PrimaryKeyGetsNamedId ? "Id" : primaryKeyColumn.PropertyName;
                    if (_options.GenerateCrmSpecificProperties && !className.EndsWith("ExtensionBase"))
                    {
                        _builder.WriteLine("public virtual {0} {1}", propertyType, newPropertyName)
                        .WriteLine("{");
                        _builder.WriteLine("get { return Id; }");
                        _builder.WriteLine("set { Id = value; }");
                        _builder.WriteLine("}");
                    }
                }

                _builder.ColumnNamePropertyNameMappings[columnName] = newPropertyName;
                AddDataAnnotationAttributes(col: col);
                _builder.WriteProperty(newPropertyName, propertyType, useNullableType, _options.UseAutomaticProperties);
            }
            if (_options.GenerateCrmSpecificProperties && !className.EndsWith("ExtensionBase"))
            {
                string extensionClassName = className.StripRight("Base") + "ExtensionBase";
                _builder.WriteProperty("Extension", extensionClassName, false, _options.UseAutomaticProperties);
            }
            _builder.WriteLine("}");
            _builder.WriteLine("}");
            return(config);
        }