internal TableColumns( DBConnection cn, string table, bool forRevisionHistoryLogic )
        {
            try {
                // NOTE: Cache this result.
                AllColumns = Column.GetColumnsInQueryResults( cn, "SELECT * FROM " + table, true );

                foreach( var col in AllColumns ) {
                    // This hack allows code to be generated against a database that is configured for ASP.NET Application Services.
                    var isAspNetApplicationServicesTable = table.StartsWith( "aspnet_" );

                    if( !( cn.DatabaseInfo is OracleInfo ) && col.DataTypeName == typeof( string ).ToString() && col.AllowsNull && !isAspNetApplicationServicesTable )
                        throw new UserCorrectableException( "String column " + col.Name + " allows null, which is not allowed." );
                }

                // Identify key, identity, and non identity columns.
                var nonIdentityColumns = new List<Column>();
                foreach( var col in AllColumns ) {
                    if( col.IsKey )
                        keyColumns.Add( col );
                    if( col.IsIdentity ) {
                        if( identityColumn != null )
                            throw new UserCorrectableException( "Only one identity column per table is supported." );
                        identityColumn = col;
                    }
                    else
                        nonIdentityColumns.Add( col );
                }
                if( !keyColumns.Any() )
                    throw new UserCorrectableException( "The table must contain a primary key or other means of uniquely identifying a row." );

                // If the table has a composite key, try to use the identity as the key instead since this will enable InsertRow to return a value.
                if( identityColumn != null && keyColumns.Count > 1 ) {
                    keyColumns.Clear();
                    keyColumns.Add( identityColumn );
                }

                RowVersionColumn = AllColumns.SingleOrDefault( i => i.IsRowVersion );
                AllColumnsExceptRowVersion = AllColumns.Where( i => !i.IsRowVersion ).ToArray();
                AllNonIdentityColumnsExceptRowVersion = nonIdentityColumns.Where( i => !i.IsRowVersion ).ToArray();

                if( forRevisionHistoryLogic ) {
                    if( keyColumns.Count != 1 ) {
                        throw new UserCorrectableException(
                            "A revision history modification class can only be created for tables with exactly one primary key column, which is assumed to also be a foreign key to the revisions table." );
                    }
                    primaryKeyAndRevisionIdColumn = keyColumns.Single();
                    if( primaryKeyAndRevisionIdColumn.IsIdentity )
                        throw new UserCorrectableException( "The revision ID column of a revision history table must not be an identity." );
                }

                dataColumns = AllColumns.Where( col => !col.IsIdentity && !col.IsRowVersion && col != primaryKeyAndRevisionIdColumn ).ToArray();
            }
            catch( Exception e ) {
                throw UserCorrectableException.CreateSecondaryException( "An exception occurred while getting columns for table " + table + ".", e );
            }
        }
 internal static string GetEqualityConditionClassName( DBConnection cn, Database database, string tableName, Column column )
 {
     return database.SecondaryDatabaseName + "CommandConditions." + CommandConditionStatics.GetTableEqualityConditionsClassName( cn, tableName ) + "." +
            CommandConditionStatics.GetConditionClassName( column );
 }
 private static void writeColumnProperty( TextWriter writer, Column column )
 {
     CodeGenerationStatics.AddSummaryDocComment(
         writer,
         "This object will " + ( column.AllowsNull && !column.NullValueExpression.Any() ? "sometimes" : "never" ) + " be null." );
     writer.WriteLine(
         "public " + column.DataTypeName + " " + EwlStatics.GetCSharpIdentifierSimple( column.PascalCasedNameExceptForOracle ) + " { get { return __basicRow." +
         EwlStatics.GetCSharpIdentifierSimple( column.PascalCasedName ) + "; } }" );
 }
 private static string getMemberVariableName( Column column )
 {
     // A single underscore is a pretty common thing for other code generators and even some developers to use, so two is more unique and avoids problems.
     return EwlStatics.GetCSharpIdentifierSimple( "__" + column.CamelCasedName );
 }
Пример #5
0
        internal TableColumns(DBConnection cn, string table, bool forRevisionHistoryLogic)
        {
            try {
                // This hack allows code to be generated against a database that is configured for ASP.NET Application Services.
                var isAspNetApplicationServicesTable = table.StartsWith("aspnet_");

                // NOTE: Cache this result.
                AllColumns = Column.GetColumnsInQueryResults(cn, "SELECT * FROM " + table, true, !isAspNetApplicationServicesTable);

                // Identify key, identity, and non identity columns.
                var nonIdentityColumns = new List <Column>();
                foreach (var col in AllColumns)
                {
                    if (col.IsKey)
                    {
                        keyColumns.Add(col);
                    }
                    if (col.IsIdentity)
                    {
                        if (identityColumn != null)
                        {
                            throw new UserCorrectableException("Only one identity column per table is supported.");
                        }
                        identityColumn = col;
                    }
                    else
                    {
                        nonIdentityColumns.Add(col);
                    }
                }
                if (!keyColumns.Any())
                {
                    throw new UserCorrectableException("The table must contain a primary key or other means of uniquely identifying a row.");
                }

                // If the table has a composite key, try to use the identity as the key instead since this will enable InsertRow to return a value.
                if (identityColumn != null && keyColumns.Count > 1)
                {
                    keyColumns.Clear();
                    keyColumns.Add(identityColumn);
                }

                RowVersionColumn                      = AllColumns.SingleOrDefault(i => i.IsRowVersion);
                AllColumnsExceptRowVersion            = AllColumns.Where(i => !i.IsRowVersion).ToArray();
                AllNonIdentityColumnsExceptRowVersion = nonIdentityColumns.Where(i => !i.IsRowVersion).ToArray();

                if (forRevisionHistoryLogic)
                {
                    if (keyColumns.Count != 1)
                    {
                        throw new UserCorrectableException(
                                  "A revision history modification class can only be created for tables with exactly one primary key column, which is assumed to also be a foreign key to the revisions table.");
                    }
                    primaryKeyAndRevisionIdColumn = keyColumns.Single();
                    if (primaryKeyAndRevisionIdColumn.IsIdentity)
                    {
                        throw new UserCorrectableException("The revision ID column of a revision history table must not be an identity.");
                    }
                }

                dataColumns = AllColumns.Where(col => !col.IsIdentity && !col.IsRowVersion && col != primaryKeyAndRevisionIdColumn).ToArray();
            }
            catch (Exception e) {
                throw UserCorrectableException.CreateSecondaryException("An exception occurred while getting columns for table " + table + ".", e);
            }
        }