DBUntypedValue.java

  1. /*
  2.  * Copyright 2018 gregorygraham.
  3.  *
  4.  * This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
  5.  * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/
  6.  * or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
  7.  *
  8.  * You are free to:
  9.  *     Share - copy and redistribute the material in any medium or format
  10.  *     Adapt - remix, transform, and build upon the material
  11.  *
  12.  *     The licensor cannot revoke these freedoms as long as you follow the license terms.              
  13.  *     Under the following terms:
  14.  *                
  15.  *         Attribution -
  16.  *             You must give appropriate credit, provide a link to the license, and indicate if changes were made.
  17.  *             You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  18.  *         NonCommercial -
  19.  *             You may not use the material for commercial purposes.
  20.  *         ShareAlike -
  21.  *             If you remix, transform, or build upon the material,
  22.  *             you must distribute your contributions under the same license as the original.
  23.  *         No additional restrictions -
  24.  *             You may not apply legal terms or technological measures that legally restrict others from doing anything the
  25.  *             license permits.
  26.  *
  27.  * Check the Creative Commons website for any details, legalese, and updates.
  28.  */
  29. package nz.co.gregs.dbvolution.datatypes;

  30. import nz.co.gregs.dbvolution.columns.UntypedColumn;
  31. import java.sql.ResultSet;
  32. import java.sql.SQLException;
  33. import java.util.Comparator;
  34. import nz.co.gregs.dbvolution.databases.definitions.DBDefinition;
  35. import nz.co.gregs.dbvolution.exceptions.IncorrectRowProviderInstanceSuppliedException;
  36. import nz.co.gregs.dbvolution.expressions.StringExpression;
  37. import nz.co.gregs.dbvolution.query.RowDefinition;
  38. import nz.co.gregs.dbvolution.results.StringResult;
  39. import nz.co.gregs.dbvolution.utility.comparators.HashCodeComparator;

  40. /**
  41.  * A case to represent values of indeterminate type
  42.  *
  43.  * @author gregorygraham
  44.  */
  45. public class DBUntypedValue extends QueryableDatatype<Object> implements StringResult {

  46.     private static final long serialVersionUID = 1l;

  47.     /**
  48.      * Sets the value of this DBString to the value provided.
  49.      *
  50.      * @param obj the value
  51.      */
  52.     @Override
  53.     public void setValue(Object obj) {
  54.         super.setLiteralValue(obj.toString());
  55.     }

  56.     @Override
  57.     protected void setValueFromStandardStringEncoding(String encodedValue) {
  58.         setValue(encodedValue);
  59.     }

  60.     @Override
  61.     public String getSQLDatatype() {
  62.         return "VARCHAR(101)";
  63.     }

  64.     @Override
  65.     public String formatValueForSQLStatement(DBDefinition defn) {
  66.         if (getLiteralValue().toString().isEmpty()) {
  67.             return defn.getEmptyString();
  68.         } else {
  69.             String unsafeValue = getLiteralValue().toString();
  70.             return defn.beginStringValue() + defn.safeString(unsafeValue) + defn.endStringValue();
  71.         }
  72.     }

  73.     @Override
  74.     protected String getFromResultSet(DBDefinition database, ResultSet resultSet, String fullColumnName) throws SQLException {
  75.         String gotString = resultSet.getString(fullColumnName);
  76.         if (!database.canProduceNullStrings()) {
  77.             if (gotString != null && gotString.isEmpty()) {
  78.                 return null;
  79.             }
  80.         }
  81.         return gotString;
  82.     }

  83.     @Override
  84.     public UntypedColumn getColumn(RowDefinition row) throws IncorrectRowProviderInstanceSuppliedException {
  85.         return new UntypedColumn(row, this);
  86.     }

  87.     @Override
  88.     public boolean isAggregator() {
  89.         return false;
  90.     }

  91.     @Override
  92.     public DBUntypedValue copy() {
  93.         return (DBUntypedValue) super.copy();
  94.     }

  95.     @Override
  96.     public boolean getIncludesNull() {
  97.         throw new UnsupportedOperationException("DBUntypedValue does not support getIncludesNull() yet."); //To change body of generated methods, choose Tools | Templates.
  98.     }

  99.     @Override
  100.     public StringExpression stringResult() {
  101.         throw new UnsupportedOperationException("DBUntypedValue does not support stringResult() yet."); //To change body of generated methods, choose Tools | Templates.
  102.     }

  103.     @Override
  104.     public Comparator<Object> getComparator() {
  105.         return untypedDatatypeComparator;
  106.     }
  107.     private static final HashCodeComparator<Object> untypedDatatypeComparator = new HashCodeComparator<Object>();

  108. }