private string GetHasManys(Table tbl)
 {
     string source = "";
     foreach (var hasMany in tbl.HasManys)
     {
         source += string.Format("\t\t\tHasMany(x => x.{0});\r\n",
                             Utility.Utility.Pluralize(hasMany.Reference));
     }
     return source;
 }
 private string GetFNHTypeGeneratorName(Table tbl)
 {
     Column col = tbl.Columns.First(c => c.TableName == tbl.Name && c.IsPrimaryKey == true);
         // c.PrimaryKeyName == tbl.PrimaryKeys[0].KeyName);
     string csTypeName = Utility.Utility.GetCSDataType(col.DBDataType);
     if (csTypeName == "int" || csTypeName == "long"
         || csTypeName == "uint" || csTypeName == "ulong")
         return "Identity";
     return "Assigned";
 }
        private void GenerateMappingFile(Table tbl)
        {
            string baseClass = tbl.Name;
            string mapClass = tbl.Name + "Map";
             	        string fileName = mapClass + ".cs";

            string source = WriteUsings();
            source += WriteNamespaceHeader();
            source += WriteClassHeader(mapClass, baseClass);
            source += WriteConstructor(mapClass, tbl, DBInfo.DataBase.Connection.Database);
            source += WriteClassFooter();
            source += WriteNameSpaceFooter();
            SaveSource(source, fileName);
        }
 private static string WriteTableProperty(Table tbl)
 {
     var source = string.Empty;
     source += string.Format("\t\tpublic Table<{0}> {1}\r\n", tbl.Name, Utility.Utility.Pluralize(tbl.Name));
     source += string.Format("\t\t{0}\r\n\t\t\tget\r\n", "{");
     source += string.Format("\t\t\t{0}\r\n", "{");
     source += string.Format("\t\t\t\treturn GetTable<{0}>();\r\n\t\t\t{1}\r\n\t\t{2}\r\n\r\n", tbl.Name, "}", "}");
     return source;
 }
        private static string WriteTableClassHeader(Table tbl)
        {
            var retString = string.Empty;

            retString += WriteTableAttribute(tbl);
            retString += string.Format("\tclass {0}\r\n\t{1}\r\n", tbl.Name, "{");
            return retString;
        }
 private static string WriteTableAttribute(Table tbl)
 {
     var retString = string.Format("\t[Table(Name=\"{0}.{1}\")]\r\n", tbl.SchemaName, tbl.Name);
     return retString;
 }
 private static string WriteColumnAttribute(Column col, Table tbl)
 {
     var dbType = FormatDBDataType(col);
     var canBeNull = string.Empty;
     if (col.IsNullable == false)
     {
         canBeNull = ", CanBeNull=false";
     }
     var isPrimaryKey = string.Empty;
     if (col.IsPrimaryKey || (tbl.PrimaryKeys != null && tbl.PrimaryKeys.SingleOrDefault(p => p.ColumnName == col.Name) != default(PrimaryKey)))
     {
         isPrimaryKey = ", IsPrimaryKey=true";
     }
     var retString = string.Format("\t\t[Column(Name=\"{0}\", DBType=\"{1}\" {2} {3})]\r\n", col.Name, dbType, canBeNull, isPrimaryKey);
     return retString;
 }
 private static HBMIdentity GetIdentity(Table tbl)
 {
     var ident = new HBMIdentity {Name = tbl.PrimaryKeys[0].ColumnName};
     ident.Type = Utility.Utility.GetCSDataType(tbl.Columns.First(c => c.Name == ident.Name).DBDataType);
     ident.IdGenerator = new IdGenerator();
     if (ident.Type == "int" || ident.Type == "long"
         || ident.Type == "uint" || ident.Type == "ulong")
     {
         ident.IdGenerator.Class = "identity";
     }
     ident.IdGenerator.Class = "assigned";
     return ident;
 }
 private static CompositeId GetCompositeId(Table tbl)
 {
     throw new System.NotImplementedException();
 }
 private string GetHasOnes(Table tbl)
 {
     string source = string.Empty;
     return source;
 }
        private string WriteConstructor(string mapClass, Table tbl, string dbName)
        {
            string source = string.Format("\t\tpublic {0}()\r\n{1}\r\n", mapClass, "\t\t{");
            source += string.Format("\t\t\tTable(\"[{0}].[{1}].[{2}]\");\r\n", dbName, tbl.SchemaName, tbl.Name);
            if (tbl.PrimaryKeys != null)
            {
                source += string.Format("\t\t\tId(x => x.Id, \"{0}\").GeneratedBy.{1}();\r\n", tbl.PrimaryKeys[0], GetFNHTypeGeneratorName(tbl));
            }

            if (tbl.ForeignKeys != null)
            {
                source += GetReferences(tbl);
            }
            // Now do the rest of the columns
            foreach (var column in tbl.Columns)
            {
                if (tbl.ForeignKeys == null || tbl.ForeignKeys.FirstOrDefault(c => c.ForeignColumnName == column.Name) == default(ForeignKey))
                { // If not Foreiqn Keyed
                    source += GetMappingLineForColumn(column);
                }
            }
            // Put in refs for each one table that has this as a foreign key
            source += GetHasManys(tbl);
            source += GetHasOnes(tbl);
            source += "\n\t\t}\n";
            return source;
        }
 private string GetReferences(Table tbl)
 {
     string source = "";
     foreach (var fk in tbl.ForeignKeys)
     {
         source += string.Format("\t\t\tReferences(x => x.{0}).Column(\"{1}\");\r\n", fk.PrimaryTableName, fk.ForeignColumnName);
     }
     return source;
 }