system/src/sc/lang/sql/SQLFileModel.java
private void addTableProperties(TypeDeclaration type, CreateTable table, TypeDeclaration prevType, boolean primary) {
      if (table.tableDefs == null) {
         return; // TODO: should we display an error here?
      }
      for (TableDef tableDef:table.tableDefs) {
         if (tableDef instanceof ColumnDef) {
            ColumnDef colDef = (ColumnDef) tableDef;
            SQLIdentifier colIdent = colDef.columnName;
            String colName;
            if (colIdent != null) {
               colName = colIdent.toString();
               String propName = DBUtil.getJavaName(colName);
               propName = CTypeUtil.decapitalizePropertyName(propName);
               ReferencesConstraint refConstr = colDef.getReferencesConstraint();
               String javaTypeName;
               if (refConstr != null) {
                  javaTypeName = DBUtil.getJavaName(refConstr.refTable.toString());
                  // TODO: if this is a multi table, we should make this List<typeName>
               }
               else {
                  SQLDataType colType = colDef.columnType;
                  javaTypeName = colType.getJavaTypeName();
               }
               if (javaTypeName == null)
                  continue;
               JavaType javaType = JavaType.createJavaTypeFromName(javaTypeName);
               Object member = prevType == null ? null : prevType.definesMember(propName, MemberType.PropertyAnySet, type, null);
               boolean required = colDef.hasNotNullConstraint();
               boolean unique = colDef.hasUniqueConstraint();
               String dbDefault = colDef.getDefaultExpression();
               if (member == null) {
                  FieldDefinition field = FieldDefinition.createFromJavaType(javaType, propName);
                  if (colDef.isPrimaryKey()) {
                     field.addModifier(Annotation.create("sc.obj.Constant"));
                     Annotation annot = Annotation.create("sc.db.IdSettings");
                     if (DBUtil.isDefinedInDBColumnType(colDef.columnType.toString())) {
                        annot.addAnnotationValues(AnnotationValue.create("definedByDB", true));
                     }
                     field.addModifier(annot);
                  }
                  else if (required || unique || dbDefault != null)
                     addDBPropertySettings(field, required, unique, dbDefault);
                  field.addModifier("public");
                  type.addBodyStatementIndent(field);
               }
               else {
                  if (required || unique || dbDefault != null) {
                     if (member instanceof Definition)
                        addDBPropertySettings((Definition) member, required, unique, dbDefault);
                     else
                        displayError("Unhandled case - no way to set unique/required on property: " + member);
                  }
               }
            }
            else
               displayError("No name for column definition");
         }
      }
   }
// Fills in any missing properties in the type from the information we have in the SQL definition.
Fills in any missing properties in the type from the information we have in the SQL definition.
Fills in any missing properties in the type from the information we have in the SQL definition .
private void addTableProperties(TypeDeclaration type, CreateTable table, TypeDeclaration prevType, boolean primary) {
      if (table.tableDefs == null) {
         return; 
      }
      for (TableDef tableDef:table.tableDefs) {
         if (tableDef instanceof ColumnDef) {
            ColumnDef colDef = (ColumnDef) tableDef;
            SQLIdentifier colIdent = colDef.columnName;
            String colName;
            if (colIdent != null) {
               colName = colIdent.toString();
               String propName = DBUtil.getJavaName(colName);
               propName = CTypeUtil.decapitalizePropertyName(propName);
               ReferencesConstraint refConstr = colDef.getReferencesConstraint();
               String javaTypeName;
               if (refConstr != null) {
                  javaTypeName = DBUtil.getJavaName(refConstr.refTable.toString());
               }
               else {
                  SQLDataType colType = colDef.columnType;
                  javaTypeName = colType.getJavaTypeName();
               }
               if (javaTypeName == null)
                  continue;
               JavaType javaType = JavaType.createJavaTypeFromName(javaTypeName);
               Object member = prevType == null ? null : prevType.definesMember(propName, MemberType.PropertyAnySet, type, null);
               boolean required = colDef.hasNotNullConstraint();
               boolean unique = colDef.hasUniqueConstraint();
               String dbDefault = colDef.getDefaultExpression();
               if (member == null) {
                  FieldDefinition field = FieldDefinition.createFromJavaType(javaType, propName);
                  if (colDef.isPrimaryKey()) {
                     field.addModifier(Annotation.create("sc.obj.Constant"));
                     Annotation annot = Annotation.create("sc.db.IdSettings");
                     if (DBUtil.isDefinedInDBColumnType(colDef.columnType.toString())) {
                        annot.addAnnotationValues(AnnotationValue.create("definedByDB", true));
                     }
                     field.addModifier(annot);
                  }
                  else if (required || unique || dbDefault != null)
                     addDBPropertySettings(field, required, unique, dbDefault);
                  field.addModifier("public");
                  type.addBodyStatementIndent(field);
               }
               else {
                  if (required || unique || dbDefault != null) {
                     if (member instanceof Definition)
                        addDBPropertySettings((Definition) member, required, unique, dbDefault);
                     else
                        displayError("Unhandled case - no way to set unique/required on property: " + member);
                  }
               }
            }
            else
               displayError("No name for column definition");
         }
      }
   }
private void addTableProperties ( TypeDeclaration type , CreateTable table , TypeDeclaration prevType , boolean primary ) { if ( table . tableDefs == null ) { return ; } for ( TableDef tableDef : table . tableDefs ) { if ( tableDef instanceof ColumnDef ) { ColumnDef colDef = ( ColumnDef ) tableDef ; SQLIdentifier colIdent = colDef . columnName ; String colName ; if ( colIdent != null ) { colName = colIdent . toString ( ) ; String propName = DBUtil . getJavaName ( colName ) ; propName = CTypeUtil . decapitalizePropertyName ( propName ) ; ReferencesConstraint refConstr = colDef . getReferencesConstraint ( ) ; String javaTypeName ; if ( refConstr != null ) { javaTypeName = DBUtil . getJavaName ( refConstr . refTable . toString ( ) ) ; } else { SQLDataType colType = colDef . columnType ; javaTypeName = colType . getJavaTypeName ( ) ; } if ( javaTypeName == null ) continue ; JavaType javaType = JavaType . createJavaTypeFromName ( javaTypeName ) ; Object member = prevType == null ? null : prevType . definesMember ( propName , MemberType . PropertyAnySet , type , null ) ; boolean required = colDef . hasNotNullConstraint ( ) ; boolean unique = colDef . hasUniqueConstraint ( ) ; String dbDefault = colDef . getDefaultExpression ( ) ; if ( member == null ) { FieldDefinition field = FieldDefinition . createFromJavaType ( javaType , propName ) ; if ( colDef . isPrimaryKey ( ) ) { field . addModifier ( Annotation . create ( "sc.obj.Constant" ) ) ; Annotation annot = Annotation . create ( "sc.db.IdSettings" ) ; if ( DBUtil . isDefinedInDBColumnType ( colDef . columnType . toString ( ) ) ) { annot . addAnnotationValues ( AnnotationValue . create ( "definedByDB" , true ) ) ; } field . addModifier ( annot ) ; } else if ( required || unique || dbDefault != null ) addDBPropertySettings ( field , required , unique , dbDefault ) ; field . addModifier ( "public" ) ; type . addBodyStatementIndent ( field ) ; } else { if ( required || unique || dbDefault != null ) { if ( member instanceof Definition ) addDBPropertySettings ( ( Definition ) member , required , unique , dbDefault ) ; else displayError ( "Unhandled case - no way to set unique/required on property: " + member ) ; } } } else displayError ( "No name for column definition" ) ; } } }
Fills in any missing properties in the type from the information we have in the SQL definition.
Fills in any missing properties in the type from the information we have in the SQL definition .
// TODO: should we display an error here? // TODO: if this is a multi table, we should make this List<typeName>