Project

General

Profile

6129b-sequences.diff

Boris Schegolev, 02/13/2023 06:17 PM

Download (10.7 KB)

View differences:

new/src/com/goldencode/p2j/persist/SchemaCheck.java 2023-02-13 22:53:22 +0000
195 195
                                   : comparator.compareWithPrevious();
196 196
         if (!result.isSame())
197 197
         {
198
            SchemaValidator.validate(result.getState().getSequences());
199

  
198 200
            Set<Table> tablesToUnload = new HashSet<>();
199 201
            Set<Table> tablesToLoad = new HashSet<>();
200 202
            for (Difference difference : result.getDifferences())
201 203
            {
204
               // Sequences are not attached to tables directly. Differences in sequences (or any other change
205
               // not related to tables) should not reload any classes.
206
               if (difference.getUpdatedTable() == null)
207
               {
208
                  continue;
209
               }
210

  
202 211
               Element newElement = difference.getNewElement();
203 212
               if (newElement != null)
204 213
               {
new/src/com/goldencode/p2j/persist/dialect/Dialect.java 2023-02-13 22:13:34 +0000
1565 1565
   }
1566 1566

  
1567 1567
   /**
1568
    * Provides SQL to load sequences.
1569
    *
1570
    * @param   database
1571
    *          database object
1572
    * @return  sequence SQL
1573
    */
1574
   public String getSequenceSQL(Database database)
1575
   {
1576
      throw new UnsupportedOperationException("SQL for sequences is not yet implemented for this dialect");
1577
   }
1578

  
1579
   /**
1568 1580
    * Helper interface for weights' aggregation
1569 1581
    */
1570 1582
   public static interface WeightAggregator
new/src/com/goldencode/p2j/persist/dialect/MariaDbLenientDialect.java 2023-02-13 22:33:19 +0000
1557 1557
   }
1558 1558

  
1559 1559
   /**
1560
    * Provides SQL to load sequences.
1561
    *
1562
    * @param   database
1563
    *          database object
1564
    * @return  sequence SQL
1565
    */
1566
   @Override
1567
   public String getSequenceSQL(Database database)
1568
   {
1569
      return "SELECT `table_name` AS sequence_name FROM information_schema.tables WHERE `table_schema` = '" + database.getName() + "' AND `table_type` = 'SEQUENCE'";
1570
   }
1571

  
1572
   /**
1560 1573
    * Obtain a DDL statement which drops a SQL index.
1561 1574
    *
1562 1575
    * @param   ifExists
new/src/com/goldencode/p2j/persist/dialect/P2JPostgreSQLDialect.java 2023-02-13 22:16:07 +0000
2529 2529
   {
2530 2530
      return null;
2531 2531
   }
2532

  
2533
   /**
2534
    * Provides SQL to load sequences.
2535
    *
2536
    * @param   database
2537
    *          database object
2538
    * @return  sequence SQL
2539
    */
2540
   @Override
2541
   public String getSequenceSQL(Database database)
2542
   {
2543
      return "SELECT sequence_name FROM information_schema.sequences WHERE sequence_catalog = '" + database.getName() + "'";
2544
   }
2532 2545
   
2533 2546
   /**
2534 2547
    * UDF types 
new/src/com/goldencode/p2j/persist/orm/schema/DatabaseState.java 2023-02-13 21:54:22 +0000
83 83
    * Table list that further expands to columns and indexes.
84 84
    */
85 85
   private final Collection<Table> tables = new HashSet<>();
86

  
87
   /**
88
    * Sequence list.
89
    */
90
   private final Collection<Sequence> sequences = new HashSet<>();
86 91
   
87 92
   /**
88 93
    * Default constructor.
......
103 108
   {
104 109
      return tables;
105 110
   }
111

  
112
   /**
113
    * Getter for sequences.
114
    *
115
    * @return Sequence list
116
    */
117
   public Collection<Sequence> getSequences()
118
   {
119
      return sequences;
120
   }
106 121
   
107 122
   /**
108 123
    * Getter for timestamp.
new/src/com/goldencode/p2j/persist/orm/schema/SchemaComparator.java 2023-02-13 22:33:19 +0000
108 108
   private static final String XML_COLUMNS = "columns";
109 109
   private static final String XML_COLUMN = "column";
110 110
   private static final String XML_INDEX = "index";
111
   private static final String XML_SEQUENCES = "sequences";
112
   private static final String XML_SEQUENCE = "sequence";
111 113
   private static final String XML_NAME = "name";
112 114
   private static final String XML_TYPE = "type";
113 115
   private static final String XML_PRECISION = "precision";
......
217 219
         getAndCompareTables(conn, previousState, comparisonResult);
218 220
         getAndCompareColumns(conn, previousState, comparisonResult);
219 221
         getAndCompareIndexes(conn, previousState, comparisonResult);
220
         
222
         getAndCompareSequences(conn, previousState, comparisonResult);
223

  
221 224
         if (!comparisonResult.isSame())
222 225
         {
223 226
            if (WRITE_KNOWN_STATE_FILE)
......
295 298
                  }
296 299
               }
297 300
            }
301
            NodeList sequenceNodes = dom.getElementsByTagName(XML_SEQUENCES);
302
            for (int i = 0; i < sequenceNodes.getLength(); i++)
303
            {
304
               Node sequenceNode = sequenceNodes.item(i);
305
               Sequence sequence = new Sequence(
306
                  database,
307
                  XmlHelper.getAttribute(sequenceNode, XML_NAME),
308
                  XmlHelper.getAttribute(sequenceNode, XML_COMMENT)
309
               );
310
               lastKnownState.getSequences().add(sequence);
311
            }
298 312
         }
299 313
         catch (FileNotFoundException exc)
300 314
         {
......
323 337
      try (FileOutputStream fos = new FileOutputStream(LAST_KNOWN_STATE_FILE))
324 338
      {
325 339
         Document dom = XmlHelper.newDocument();
326
         Element root = dom.createElement(XML_TABLES);
340
         Element tableRoot = dom.createElement(XML_TABLES);
327 341
         for (Table table : lastKnownState.getTables())
328 342
         {
329 343
            Element tableNode = dom.createElement(XML_TABLE);
330 344
            XmlHelper.setAttributeOptional(tableNode, XML_NAME, table.getName());
331 345
            XmlHelper.setAttributeOptional(tableNode, XML_COMMENT, table.getComment());
332
            root.appendChild(tableNode);
346
            tableRoot.appendChild(tableNode);
333 347
            
334 348
            for (Attribute attribute : table.getAttributes())
335 349
            {
......
354 368
               tableNode.appendChild(indexNode);
355 369
            }
356 370
         }
357
         dom.appendChild(root);
371
         dom.appendChild(tableRoot);
372

  
373
         Element sequenceRoot = dom.createElement(XML_SEQUENCES);
374
         for (Sequence sequence : lastKnownState.getSequences())
375
         {
376
            Element sequenceNode = dom.createElement(XML_SEQUENCE);
377
            XmlHelper.setAttributeOptional(sequenceNode, XML_NAME, sequence.getName());
378
            XmlHelper.setAttributeOptional(sequenceNode, XML_COMMENT, sequence.getComment());
379
            sequenceRoot.appendChild(sequenceNode);
380
         }
381
         dom.appendChild(sequenceRoot);
358 382
         
359 383
         XmlHelper.write(dom, fos);
360 384
      }
......
664 688
         }
665 689
      }
666 690
   }
691

  
692
   /**
693
    * Load information on sequences from the provided connection, put the state into result. If previous state is provided
694
    * we can compare current state with the previous one and report any differences into result.
695
    *
696
    * @param   conn
697
    *          Already initiated connection.
698
    * @param   previousState
699
    *          Last known state.
700
    * @param   comparisonResult
701
    *          Instance of result to be filled in.
702
    *
703
    * @throws  SQLException
704
    *          on unsuccessful calls to DB.
705
    */
706
   private void getAndCompareSequences(Connection conn,
707
                                       DatabaseState previousState,
708
                                       ComparisonResult comparisonResult)
709
   throws SQLException
710
   {
711
      Dialect dialect = Dialect.getDialect(database);
712
      // Load sequences from DB
713
      ResultSet sequences = conn.prepareStatement(dialect.getSequenceSQL(database)).executeQuery();
714

  
715
      // Put all data into the new State
716
      DatabaseState currentState = comparisonResult.getState();
717
      while (sequences.next())
718
      {
719
         currentState.getSequences()
720
                 .add(new Sequence(database, sequences.getString("sequence_name"), ""));
721
      }
722

  
723
      // Compare if old State is available
724
      if (previousState != null)
725
      {
726
         // Detect added
727
         for (Sequence sequence : currentState.getSequences())
728
         {
729
            if (!previousState.getSequences().contains(sequence))
730
            {
731
               comparisonResult.getDifferences()
732
                       .add(new Difference(null, null, sequence));
733
            }
734
         }
735

  
736
         // Detect removed
737
         for (Sequence sequence : currentState.getSequences())
738
         {
739
            if (!currentState.getSequences().contains(sequence))
740
            {
741
               comparisonResult.getDifferences()
742
                       .add(new Difference(null, sequence, null));
743
            }
744
         }
745
      }
746
   }
667 747
   
668 748
   /**
669 749
    * If table validation or DMO generation failed, this method can remove it from result so that it can be loaded next time.
new/src/com/goldencode/p2j/persist/orm/schema/SchemaValidator.java 2023-02-13 23:04:15 +0000
63 63

  
64 64
package com.goldencode.p2j.persist.orm.schema;
65 65

  
66
import com.goldencode.p2j.persist.*;
66 67
import com.goldencode.p2j.persist.orm.*;
67 68
import com.goldencode.p2j.persist.orm.schema.element.*;
68 69
import com.goldencode.p2j.util.*;
......
135 136
         }
136 137
      }
137 138
   }
139

  
140
   /**
141
    * Apply validation rules on sequences. Currently just check that p2j_id_generator_sequence exists.
142
    *
143
    * @param   sequences
144
    *          Sequence list to validate
145
    * @throws  ErrorConditionException
146
    *          Exception is thrown in case of a failed validation
147
    */
148
   public static void validate(Collection<Sequence> sequences)
149
   throws ErrorConditionException
150
   {
151
      if (sequences.stream().noneMatch(sequence -> sequence.getName().equalsIgnoreCase(Persistence.ID_GEN_SEQUENCE)))
152
      {
153
         ErrorManager.recordOrThrowError(0, "Sequence " + Persistence.ID_GEN_SEQUENCE + " is not defined in database.");
154
      }
155
   }
138 156
}