src/java.sql/share/classes/java/sql2/SqlClob.java
branchJDK-8188051-branch
changeset 56380 f06946e00a26
parent 56373 1f76a5f8e999
child 56381 653b066f4a88
equal deleted inserted replaced
56373:1f76a5f8e999 56380:f06946e00a26
     1 /*
       
     2  * Copyright (c)  2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package java.sql2;
       
    26 
       
    27 import java.io.Reader;
       
    28 import java.io.Writer;
       
    29 import java.util.concurrent.CompletionStage;
       
    30 
       
    31 /**
       
    32  * A reference to a CHARACTER LARGE OBJECT in the attached database.
       
    33  *
       
    34  */
       
    35 public interface SqlClob extends AutoCloseable {
       
    36 
       
    37   /**
       
    38    * Return an {@link Operation} that will release the temporary resources
       
    39    * associated with this {@link SqlClob}.
       
    40    *
       
    41    * @return an {@link Operation} that will release the temporary resources
       
    42    * associated with this {@link SqlClob}.
       
    43    */
       
    44   public Operation<Void> closeOperation();
       
    45 
       
    46   @Override
       
    47   public default void close() {
       
    48     this.closeOperation().submit();
       
    49   }
       
    50 
       
    51   /**
       
    52    * Return a {@link Operation} that fetches the position of this {@link SqlClob}.
       
    53    * Position 0 is immediately before the first char in the {@link SqlClob}.
       
    54    * Position 1 is the first char in the {@link SqlClob}, etc. Position
       
    55    * {@link length()} is the last char in the {@link SqlClob}.
       
    56    *
       
    57    * Position is between 0 and length + 1.
       
    58    *
       
    59    * @return an {@link Operation} that returns the position of this {@link SqlClob}
       
    60    * @throws IllegalStateException if the {@link Connection} that created this
       
    61    * {@link SqlClob} is closed.;
       
    62    */
       
    63   public Operation<Long> getPositionOperation();
       
    64 
       
    65   /**
       
    66    * Get the position of this {@link SqlClob}. Position 0 is immediately before the
       
    67    * first char in the {@link SqlClob}. Position 1 is the first char in the
       
    68    * {@link SqlClob}, etc. Position {@link length()} is the last char in the SqlClob.
       
    69 
       
    70  Position is between 0 and length + 1.
       
    71 
       
    72  ISSUE: Should position be 1-based as SQL seems to do or 0-based as Java
       
    73  does?
       
    74    *
       
    75    * @return a future which value is the position of this {@link SqlClob}
       
    76    * @throws IllegalStateException if the {@link Connection} that created this
       
    77    * {@link SqlClob} is closed.
       
    78    */
       
    79   public default CompletionStage<Long> getPosition() {
       
    80     return getPositionOperation().submit().getCompletionStage();
       
    81   }
       
    82 
       
    83   /**
       
    84    * Return a {@link Operation} that fetches the length of this {@link SqlClob}.
       
    85    *
       
    86    * @return a {@link Operation} that returns the length of this {@link SqlClob}
       
    87    * @throws IllegalStateException if the {@link Connection} that created this
       
    88    * {@link SqlClob} is closed.
       
    89    */
       
    90   public Operation<Long> lengthOperation();
       
    91 
       
    92   /**
       
    93    * Get the length of this {@link SqlClob}.
       
    94    *
       
    95    * @return a {@link java.util.concurrent.Future} which value is the number of
       
    96    * chars in this {@link SqlClob}
       
    97    * @throws IllegalStateException if the {@link Connection} that created this
       
    98    * {@link SqlClob} is closed.
       
    99    */
       
   100   public default CompletionStage<Long> length() {
       
   101     return lengthOperation().submit().getCompletionStage();
       
   102   }
       
   103 
       
   104   /**
       
   105    * Return an {@link Operation} that sets the position of this {@link SqlClob}. If
       
   106    * {@code offset} exceeds the length of this {@link SqlClob} set position to the
       
   107    * length + 1 of this {@link SqlClob}, ie one past the last char.
       
   108    *
       
   109    * @param offset a non-negative number
       
   110    * @return a {@link Operation} that sets the position of this {@link SqlClob}
       
   111    * @throws IllegalArgumentException if {@code offset} is less than 0
       
   112    * @throws IllegalStateException if the {@link Connection} that created this
       
   113    * {@link SqlClob} is closed.
       
   114    */
       
   115   public Operation<Long> setPositionOperation(long offset);
       
   116 
       
   117   /**
       
   118    * Set the position of this {@link SqlClob}. If {@code offset} exceeds the length
       
   119    * of this {@link SqlClob} set position to the length + 1 of this {@link SqlClob},
       
   120    * ie one past the last char.
       
   121    *
       
   122    * @param offset the 1-based position to set
       
   123    * @return this {@link SqlClob}
       
   124    * @throws IllegalArgumentException if {@code offset} is less than 0
       
   125    * @throws IllegalStateException if the {@link Connection} that created this
       
   126    * {@link SqlClob} is closed.
       
   127    */
       
   128   public default SqlClob setPosition(long offset) {
       
   129     setPositionOperation(offset).submit();
       
   130     return this;
       
   131   }
       
   132 
       
   133   /**
       
   134    * Return an {@link Operation} to set the position to the beginning of the
       
   135    * next occurrence of the target after the position. If there is no such
       
   136    * occurrence set the position to 0.
       
   137    *
       
   138    * @param target a {@link SqlClob} created by the same {@link Connection}
       
   139    * containing the char sequence to search for
       
   140    * @return an {@link Operation} that locates {@code target} in this
       
   141    * {@link SqlClob}
       
   142    * @throws IllegalArgumentException if {@code target} was created by some
       
   143    * other {@link Connection}
       
   144    * @throws IllegalStateException if the {@link Connection} that created this
       
   145    * {@link SqlClob} is closed.
       
   146    */
       
   147   public Operation<Long> locateOperation(SqlClob target);
       
   148 
       
   149   /**
       
   150    * Set the position to the beginning of the next occurrence of the target
       
   151    * after the position. If there is no such occurrence set the position to 0.
       
   152    *
       
   153    * @param target the char sequence to search for
       
   154    * @return this {@link SqlClob}
       
   155    * @throws IllegalArgumentException if {@code target} was created by some
       
   156    * other {@link Connection}
       
   157    * @throws IllegalStateException if the {@link Connection} that created this
       
   158    * {@link SqlClob} is closed
       
   159    */
       
   160   public default SqlClob locate(SqlClob target) {
       
   161     locateOperation(target).submit();
       
   162     return this;
       
   163   }
       
   164 
       
   165   /**
       
   166    * Return an {@link Operation} to set the position to the beginning of the
       
   167    * next occurrence of the target after the position. If there is no such
       
   168    * occurrence set the position to 0.
       
   169    *
       
   170    * @param target the char sequence to search for. Not {@code null}. Captured.
       
   171    * @return an {@link Operation} that locates {@code target} in this
       
   172    * {@link SqlClob}
       
   173    * @throws IllegalStateException if the {@link Connection} that created this
       
   174    * {@link SqlClob} is closed.
       
   175    */
       
   176   public Operation<Long> locateOperation(CharSequence target);
       
   177 
       
   178   /**
       
   179    * Set the position to the beginning of the next occurrence of the target
       
   180    * after the position. If there is no such occurrence set the position to 0.
       
   181    *
       
   182    * @param target the char sequence to search for
       
   183    * @return this {@link SqlClob}
       
   184    * @throws IllegalStateException if the {@link Connection} that created this
       
   185    * {@link SqlClob} is closed.
       
   186    */
       
   187   public default SqlClob locate(CharSequence target) {
       
   188     locateOperation(target).submit();
       
   189     return this;
       
   190   }
       
   191 
       
   192   /**
       
   193    * Return an {@link Operation} that truncates this {@link SqlClob} so that the
       
   194    * current position is the end of the {@link SqlClob}. If the position is N, then
       
   195    * after trim() the length is N - 1. The position is still N. This will fail
       
   196    * if position is 0.
       
   197    *
       
   198    * @return an {@link Operation} that trims the length of this {@link SqlClob}
       
   199    * @throws IllegalStateException if the {@link Connection} that created this
       
   200    * {@link SqlClob} is closed or position is 0.
       
   201    */
       
   202   public Operation<Long> trimOperation();
       
   203 
       
   204   /**
       
   205    * Truncate this {@link SqlClob} so that the current position is the end of the
       
   206    * {@link SqlClob}. If the position is N, then after {@link trim()} the length is
       
   207    * N - 1. The position is still N. This will fail if position is 0.
       
   208    *
       
   209    * @return this {@link SqlClob}
       
   210    * @throws IllegalStateException if the {@link Connection} that created this
       
   211    * {@link SqlClob} is closed or position is 0.
       
   212    */
       
   213   public default SqlClob trim() {
       
   214     trimOperation().submit();
       
   215     return this;
       
   216   }
       
   217 
       
   218   /**
       
   219    * Returns a {@link Reader} for the characters in this {@link SqlClob}.
       
   220    * Characters are read starting at the current position. Each character read
       
   221    * advances the position by one.
       
   222    *
       
   223    * ISSUE: There is no character analog to
       
   224    * {@link java.nio.channels.AsynchronousByteChannel}. It is trivial to
       
   225    * construct a {@link java.io.Reader} from an
       
   226    * {@link java.nio.channels.AsynchronousByteChannel} however.
       
   227    *
       
   228    * @return a Reader for the characters in this SqlClob
       
   229    */
       
   230   public Reader getReader();
       
   231 
       
   232   /**
       
   233    * Returns a Writer for this {@link SqlClob}. Characters are written starting at
       
   234    * the current position. Each character written advances the position by one.
       
   235    *
       
   236    * ISSUE: There is no character analog to
       
   237    * {@link java.nio.channels.AsynchronousByteChannel}. It is trivial to
       
   238    * construct a {@link java.io.Writer} from an
       
   239    * {@link java.nio.channels.AsynchronousByteChannel} however.
       
   240    *
       
   241    * @return a Writer for the characters of this SqlClob
       
   242    */
       
   243   public Writer getWriter();
       
   244 
       
   245 }