View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    * 
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.model.bean.database;
12  
13  import com.jsql.util.LogLevelUtil;
14  import com.jsql.util.StringUtil;
15  import org.apache.commons.lang3.StringUtils;
16  import org.apache.logging.log4j.LogManager;
17  import org.apache.logging.log4j.Logger;
18  
19  /**
20   * Define a Table, e.g. is sent to the view by the model after injection.
21   * Allow to traverse upward to its corresponding database.
22   */
23  public class Table extends AbstractElementDatabase {
24      
25      /**
26       * Log4j logger sent to view.
27       */
28      private static final Logger LOGGER = LogManager.getRootLogger();
29      
30      // The database that contains the current column.
31      private final Database parentDatabase;
32      
33      // The number of rows in the table.
34      // TODO to int and move to abstract class
35      private String rowCount;
36  
37      /**
38       * Define the table label, number of rows and parent database.
39       */
40      public Table(String tableName, String rowCount, Database parentDatabase) {
41          this.elementValue = tableName;
42          this.rowCount = rowCount;
43          this.parentDatabase = parentDatabase;
44      }
45      
46      // Return the parent database.
47      @Override
48      public AbstractElementDatabase getParent() {
49          return this.parentDatabase;
50      }
51      
52      // Return the number of rows in the table.
53      @Override
54      public int getChildCount() {
55          return Integer.parseInt(this.rowCount);
56      }
57  
58      /**
59       * A readable label for the table, with number of rows, displayed
60       * by the view. If parent database is the system information_schema, number
61       * of rows is unknown, e.g. my_table (7 rows).
62       */
63      @Override
64      public String getLabelWithCount() {
65          String nbRow = StringUtil.INFORMATION_SCHEMA.equals(this.parentDatabase.toString())
66              ? "?"
67              : this.rowCount;
68          
69          // Report #138: detect incorrect number of rows
70          String sPlural = StringUtils.EMPTY;
71          try {
72              if (Integer.parseInt(this.rowCount) > 1) {
73                  sPlural = "s";
74              }
75          } catch (NumberFormatException e) {
76              this.rowCount = "0";
77              nbRow = "0";
78              LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Incorrect number of rows.");
79          }
80          
81          return String.format(
82               "%s (%s row%s)",
83               this.elementValue,
84               nbRow,
85               sPlural
86          );
87      }
88  }