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      private static final Logger LOGGER = LogManager.getRootLogger();
26      
27      // The database that contains the current column.
28      private final Database parentDatabase;
29      
30      // The number of rows in the table.
31      // TODO to int and move to abstract class
32      private String rowCount;
33  
34      /**
35       * Define the table label, number of rows and parent database.
36       */
37      public Table(String tableName, String rowCount, Database parentDatabase) {
38          this.elementValue = tableName;
39          this.rowCount = rowCount;
40          this.parentDatabase = parentDatabase;
41      }
42      
43      // Return the parent database.
44      @Override
45      public AbstractElementDatabase getParent() {
46          return this.parentDatabase;
47      }
48      
49      // Return the number of rows in the table.
50      @Override
51      public int getChildCount() {
52          return Integer.parseInt(this.rowCount);
53      }
54  
55      /**
56       * A readable label for the table, with number of rows, displayed
57       * by the view. If parent database is the system information_schema, number
58       * of rows is unknown, e.g. my_table (7 rows).
59       */
60      @Override
61      public String getLabelWithCount() {
62          String nbRow = StringUtil.INFORMATION_SCHEMA.equals(this.parentDatabase.toString())
63              ? "?"
64              : this.rowCount;
65          
66          // Report #138: detect incorrect number of rows
67          String sPlural = StringUtils.EMPTY;
68          try {
69              if (Integer.parseInt(this.rowCount) > 1) {
70                  sPlural = "s";
71              }
72          } catch (NumberFormatException e) {
73              this.rowCount = "0";
74              nbRow = "0";
75              LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Incorrect number of rows.");
76          }
77          
78          return String.format(
79               "%s (%s row%s)",
80               this.elementValue,
81               nbRow,
82               sPlural
83          );
84      }
85  }