Project

General

Profile

Revision 7b586792

ID7b58679270a5898b2b35b1547fea3db2987a397a
Parent b0f0b1f1
Child 15fcad28

Added by Andreas Müller almost 7 years ago

Schema update for referenceType persistence #2615

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/ColumnTypeChanger.java
45 45
	public static final ColumnTypeChanger NewInt2DoubleInstance(String stepName, String tableName, String columnName, boolean includeAudTable){
46 46
		return new ColumnTypeChanger(stepName, tableName, columnName, "double", includeAudTable, null, false, null);
47 47
	}
48

  
49
	public static final ColumnTypeChanger NewInt2StringInstance(String stepName, String tableName, String columnName, int size, boolean includeAudTable, Integer defaultValue, boolean notNull){
50
		return new ColumnTypeChanger(stepName, tableName, columnName, "nvarchar("+size+")", includeAudTable, defaultValue, notNull, null);
51
	}
52
	
48 53
	
49 54
	protected ColumnTypeChanger(String stepName, String tableName, String columnName, String newColumnType, boolean includeAudTable, Object defaultValue, boolean notNull, String referencedTable) {
50 55
		super(stepName);
......
57 62
		this.referencedTable = referencedTable;
58 63
	}
59 64

  
60

  
61
	/* (non-Javadoc)
62
	 * @see eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase#invoke(eu.etaxonomy.cdm.database.ICdmDataSource, eu.etaxonomy.cdm.common.IProgressMonitor)
63
	 */
64 65
	@Override
65 66
	public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {
66 67
		boolean result = true;
......
75 76
	private boolean changeColumn(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) {
76 77
		boolean result = true;
77 78
		try {
78
			String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
79
			
80
			String updateQuery;
79 81
			try {
82
				if (this.isNotNull){
83
					updateQuery = getNotNullUpdateQuery(tableName, datasource, monitor);
84
					datasource.executeUpdate(updateQuery);
85
				}
86
				
87
				updateQuery = getUpdateQueryString(tableName, datasource, monitor);
80 88
				datasource.executeUpdate(updateQuery);
89
				
90
				if (defaultValue instanceof Boolean){
91
					updateQuery = "UPDATE @tableName SET @columnName = " + (defaultValue == null ? "null" : getBoolean((Boolean) defaultValue, datasource));
92
					updateQuery = updateQuery.replace("@tableName", tableName);
93
					updateQuery = updateQuery.replace("@columnName", columnName);
94
					try {
95
						datasource.executeUpdate(updateQuery);
96
					} catch (SQLException e) {
97
						logger.error(e);
98
						result = false;
99
					}
100
				}
101
				if (referencedTable != null){
102
					result &= TableCreator.makeForeignKey(tableName, datasource, columnName, referencedTable);
103
				}
104
				
81 105
			} catch (SQLException e) {
82 106
				logger.error(e);
83 107
				result = false;
84 108
			}
85
			
86
			if (defaultValue instanceof Boolean){
87
				updateQuery = "UPDATE @tableName SET @columnName = " + (defaultValue == null ? "null" : getBoolean((Boolean) defaultValue, datasource));
88
				updateQuery = updateQuery.replace("@tableName", tableName);
89
				updateQuery = updateQuery.replace("@columnName", columnName);
90
				try {
91
					datasource.executeUpdate(updateQuery);
92
				} catch (SQLException e) {
93
					logger.error(e);
94
					result = false;
95
				}
96
			}
97
			if (referencedTable != null){
98
				result &= TableCreator.makeForeignKey(tableName, datasource, columnName, referencedTable);
99
			}
100
			
109
				
101 110
			return result;
102 111
		} catch ( DatabaseTypeNotSupportedException e) {
103 112
			return false;
104 113
		}
105 114
	}
106 115

  
116
	private String getNotNullUpdateQuery(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) {
117
		String query = " UPDATE %s SET %s = %S WHERE %s IS NULL ";
118
		String defaultValueStr = String.valueOf(this.defaultValue);
119
		if (this.defaultValue instanceof Integer){
120
			//OK
121
		}else{
122
			defaultValueStr = "'" + defaultValueStr + "'";
123
		}
124
		query = String.format(query, tableName, this.columnName, defaultValueStr, this.columnName);
125
		return query;
126
	}
127

  
107 128
	public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
108 129
		String updateQuery;
109 130
		DatabaseTypeEnum type = datasource.getDatabaseType();
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/v31_33/SchemaUpdater_31_33.java
62 62
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
63 63
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
64 64
import eu.etaxonomy.cdm.model.reference.Reference;
65
import eu.etaxonomy.cdm.model.reference.ReferenceType;
65 66
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
66 67
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
67 68

  
......
123 124
		step = TableDroper.NewInstance(stepName, tableName, INCLUDE_AUDIT);
124 125
		stepList.add(step);
125 126
		
127
		
126 128
		//create original source type column
127 129
		stepName = "Create original source type column";
128 130
		tableName = "OriginalSourceBase";
......
141 143
		stepName = "Create taxon node tree index";
142 144
		tableName = "TaxonNode";
143 145
		columnName = "treeIndex";
144
		//TODO NOT NULL unclear
146
		//TODO NOT NULL unclear  //see also columnTypeChanger
145 147
		step = ColumnAdder.NewStringInstance(stepName, tableName, columnName, 255, INCLUDE_AUDIT);
146 148
		stepList.add(step);
147 149
		
......
397 399
		tableName = "DescriptionBase_SpecimenOrObservationBase";
398 400
		step = TableDroper.NewInstance(stepName, tableName, INCLUDE_AUDIT);
399 401
		stepList.add(step);
402

  
403
		//change column type for reference type
404
		//TODO test with non-Mysql
405
		stepName = "Change column type for Reference.type";
406
		tableName = "Reference";
407
		columnName = "refType";
408
		Integer defaultValueStr = -1;
409
		notNull = true;
410
		int size = 3;
411
		step = ColumnTypeChanger.NewInt2StringInstance(stepName, tableName, columnName, size, true, defaultValueStr, notNull);
412
		stepList.add(step);
413
		
414
		//update reference type
415
		updateReferenceType(stepList);
400 416
		
401 417
		//create table CdmPreferences  #3555
402 418
		stepName = "Create table 'CdmPreferences'";
......
621 637
		
622 638
		return stepList;
623 639
	}
640
	
641
	private void updateReferenceType(List<ISchemaUpdaterStep> stepList) {
642
		//TODO include AUD tables
643
		
644
		String stepName = "Update reference refType for Reference";
645
		String baseQuery = " UPDATE Reference " + 
646
			" SET refType = '%s' " +
647
			" WHERE refType = '%s' ";
648
		Integer index = 0;
649
		
650
		//0-Article
651
		String query = String.format(baseQuery, ReferenceType.Article.getKey(), String.valueOf(index++));
652
		ISchemaUpdaterStep step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
653
		stepList.add(step);
654

  
655
		//1-Book
656
		query = String.format(baseQuery, ReferenceType.Book.getKey(), String.valueOf(index++));
657
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
658
		stepList.add(step);
659

  
660
		//2-Book Section
661
		query = String.format(baseQuery, ReferenceType.BookSection.getKey(), String.valueOf(index++));
662
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
663
		stepList.add(step);
664

  
665
		//3-CD / DVD
666
		query = String.format(baseQuery, ReferenceType.CdDvd.getKey(), String.valueOf(index++));
667
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
668
		stepList.add(step);
669

  
670
		//4-Database
671
		query = String.format(baseQuery, ReferenceType.Database.getKey(), String.valueOf(index++));
672
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
673
		stepList.add(step);
674

  
675
		//5-Generic
676
		query = String.format(baseQuery, ReferenceType.Generic.getKey(), String.valueOf(index++));
677
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
678
		stepList.add(step);
679

  
680
		//6-InProceedings
681
		query = String.format(baseQuery, ReferenceType.InProceedings.getKey(), String.valueOf(index++));
682
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
683
		stepList.add(step);
684

  
685
		//7-Journal
686
		query = String.format(baseQuery, ReferenceType.Journal.getKey(), String.valueOf(index++));
687
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
688
		stepList.add(step);
689
		
690
		//8-Map
691
		query = String.format(baseQuery, ReferenceType.Map.getKey(), String.valueOf(index++));
692
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
693
		stepList.add(step);
694
		
695
		//9-Patent
696
		query = String.format(baseQuery, ReferenceType.Patent.getKey(), String.valueOf(index++));
697
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
698
		stepList.add(step);
699
		
700
		//10-Personal Communication
701
		query = String.format(baseQuery, ReferenceType.PersonalCommunication.getKey(), String.valueOf(index++));
702
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
703
		stepList.add(step);
704
		
705
		//11-PrintSeries
706
		query = String.format(baseQuery, ReferenceType.PrintSeries.getKey(), String.valueOf(index++));
707
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
708
		stepList.add(step);
709
		
710
		//12-Proceedings
711
		query = String.format(baseQuery, ReferenceType.Proceedings.getKey(), String.valueOf(index++));
712
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
713
		stepList.add(step);
714
		
715
		//13-Report
716
		query = String.format(baseQuery, ReferenceType.Report.getKey(), String.valueOf(index++));
717
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
718
		stepList.add(step);
719
		
720
		//14-Thesis
721
		query = String.format(baseQuery, ReferenceType.Thesis.getKey(), String.valueOf(index++));
722
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
723
		stepList.add(step);
724

  
725
		//15-WebPage
726
		query = String.format(baseQuery, ReferenceType.WebPage.getKey(), String.valueOf(index++));
727
		step = SimpleSchemaUpdaterStep.NewInstance(stepName, query);
728
		stepList.add(step);
729
	}
624 730

  
625 731
	private void updateRecordBasis(List<ISchemaUpdaterStep> stepList) {
626 732
		String stepName = "Update recordBasis for SpecimenOrObservationBase";

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)