Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *      Copyright (C) 2012 DataStax Inc.
   *
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *   Unless required by applicable law or agreed to in writing, software
  *   distributed under the License is distributed on an "AS IS" BASIS,
  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *   See the License for the specific language governing permissions and
  *   limitations under the License.
  */
 package com.datastax.driver.core;
 
 import java.util.*;
 
 
Metadata describing the columns returned in a ResultSet or a PreparedStatement.

A columnDefinitions} instance is mainly a list of ColumnsDefinitions.Definition. The definitions or metadata for a column can be accessed either by:

  • index (indexed from 0)
  • name

When accessed by name, column selection is case insensitive. In case multiple columns only differ by the case of their name, then the column returned with be the first column that has been defined in CQL without forcing case sensitivity (that is, it has either been defined without quotes or is fully lowercase). If none of the columns have been defined in this manner, the first column matching (with case insensitivity) is returned. You can force the case of a selection by double quoting the name.

For example:

  • If cd contains column fOO, then cd.contains("foo"), cd.contains("fOO") and cd.contains("Foo") will return true.
  • If cd contains both foo and FOO then:
    • cd.getType("foo"), cd.getType("fOO") and cd.getType("FOO") will all match column foo.
    • cd.getType("\"FOO\"") will match column FOO
Note that the preceding rules mean that if a ColumnDefinitions object contains multiple occurrences of the exact same name (be it the same column multiple times or columns from different tables with the same name), you will have to use selection by index to disambiguate.
 
 public class ColumnDefinitions implements Iterable<ColumnDefinitions.Definition> {
 
     static final ColumnDefinitions EMPTY = new ColumnDefinitions(new Definition[0]);
 
     private final Definition[] byIdx;
     private final Map<Stringint[]> byName;
 
     ColumnDefinitions(Definition[] defs) {
 
         this. = defs;
         this. = new HashMap<Stringint[]>(defs.length);
 
         for (int i = 0; i < defs.lengthi++) {
             // Be optimistic, 99% of the time, previous will be null.
             int[] previous = this..put(defs[i]..toLowerCase(), new int[]{ i });
             if (previous != null) {
                 int[] indexes = new int[previous.length + 1];
                 System.arraycopy(previous, 0, indexes, 0, previous.length);
                 indexes[indexes.length - 1] = i;
                 this..put(defs[i]..toLowerCase(), indexes);
             }
         }
     }

    
Returns the number of columns described by this Columns instance.

Returns:
the number of columns described by this metadata.
 
     public int size() {
         return .;
     }

    
Returns whether this metadata contains a given column.

Parameters:
name the name of column.
Returns:
true if this metadata contains the column named name, false otherwise.
    public boolean contains(String name) {
        return findIdx(name) >= 0;
    }

    
The index in this metadata of the povided column name, if present.

Parameters:
name the name of the column.
Returns:
the index of name in this metadata if this metadata contains(name), -1 otherwise.
    public int getIndexOf(String name) {
        return findIdx(name);
    }

    
Returns an iterator over the ColumnDefinitions.Definition contained in this metadata. The order of the iterator will be the one of this metadata.

Returns:
an iterator over the ColumnDefinitions.Definition contained in this metadata.
    @Override
    public Iterator<Definitioniterator() {
        return Arrays.asList().iterator();
    }

    
Returns a list containing all the definitions of this metadata in order.

Returns:
a list of the ColumnDefinitions.Definition contained in this metadata.
    public List<DefinitionasList() {
        return Arrays.asList();
    }

    
Returns the name of the ith column in this metadata.

Parameters:
i the index in this metadata.
Returns:
the name of the ith column in this metadata.
Throws:
java.lang.IndexOutOfBoundsException if i < 0 or i >= size()
    public String getName(int i) {
        return [i].;
    }

    
Returns the type of the ith column in this metadata.

Parameters:
i the index in this metadata.
Returns:
the type of the ith column in this metadata.
Throws:
java.lang.IndexOutOfBoundsException if i < 0 or i >= size()
    public DataType getType(int i) {
        return [i].;
    }

    
Returns the type of column name in this metadata.

Parameters:
name the name of the column.
Returns:
the type of column name in this metadata.
Throws:
java.lang.IllegalArgumentException if name is not one of the columns in this metadata.
    public DataType getType(String name) {
        return getType(getIdx(name));
    }

    
Returns the keyspace of the ith column in this metadata.

Parameters:
i the index in this metadata.
Returns:
the keyspace of the ith column in this metadata.
Throws:
java.lang.IndexOutOfBoundsException if i < 0 or i >= size()
    public String getKeyspace(int i) {
        return [i].;
    }

    
Returns the keyspace of column name in this metadata.

Parameters:
name the name of the column.
Returns:
the keyspace of column name in this metadata.
Throws:
java.lang.IllegalArgumentException if name is not one of the columns in this metadata.
    public String getKeyspace(String name) {
        return getKeyspace(getIdx(name));
    }

    
Returns the table of the ith column in this metadata.

Parameters:
i the index in this metadata.
Returns:
the table of the ith column in this metadata.
Throws:
java.lang.IndexOutOfBoundsException if i < 0 or i >= size()
    public String getTable(int i) {
        return [i].;
    }

    
Returns the table of column name in this metadata.

Parameters:
name the name of the column.
Returns:
the table of column name in this metadata.
Throws:
java.lang.IllegalArgumentException if name is not one of the columns in this metadata.
    public String getTable(String name) {
        return getTable(getIdx(name));
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Columns[");
        for (int i = 0; i < size(); i++) {
            if (i != 0)
                sb.append(", ");
            Definition def = [i];
            sb.append(def.name).append("(").append(def.type).append(")");
        }
        sb.append("]");
        return sb.toString();
    }
    int findIdx(String name) {
        boolean caseSensitive = false;
        if (name.length() >= 2 && name.charAt(0) == '"' && name.charAt(name.length() - 1) == '"') {
            name = name.substring(1, name.length() - 1);
            caseSensitive = true;
        }
        int[] indexes = .get(name.toLowerCase());
        if (indexes == null) {
            return -1;
        } else if (indexes.length == 1) {
            return indexes[0];
        } else {
            for (int i = 0; i < indexes.lengthi++) {
                int idx = indexes[i];
                if (caseSensitive) {
                    if (name.equals([idx].))
                        return idx;
                } else {
                    if (name.toLowerCase().equals([idx].))
                        return idx;
                }
            }
            if (caseSensitive)
                return -1;
            else
                return indexes[0];
        }
    }
    int getIdx(String name) {
        int idx = findIdx(name);
        if (idx < 0)
            throw new IllegalArgumentException(name + " is not a column defined in this metadata");
        return idx;
    }
    void checkBounds(int i) {
        if (i < 0 || i >= size())
            throw new ArrayIndexOutOfBoundsException(i);
    }
    // Note: we avoid having a vararg method to avoid the array allocation that comes with it.
    void checkType(int iDataType.Name name) {
        DataType defined = getType(i);
        if (name != defined.getName())
            throw new InvalidTypeException(String.format("Column %s is of type %s"getName(i), defined));
    }
    DataType.Name checkType(int iDataType.Name name1DataType.Name name2) {
        DataType defined = getType(i);
        if (name1 != defined.getName() && name2 != defined.getName())
            throw new InvalidTypeException(String.format("Column %s is of type %s"getName(i), defined));
        return defined.getName();
    }
    DataType.Name checkType(int iDataType.Name name1DataType.Name name2DataType.Name name3) {
        DataType defined = getType(i);
        if (name1 != defined.getName() && name2 != defined.getName() && name3 != defined.getName())
            throw new InvalidTypeException(String.format("Column %s is of type %s"getName(i), defined));
        return defined.getName();
    }

    
A column definition.
    public static class Definition {
        private final String keyspace;
        private final String table;
        private final String name;
        private final DataType type;
        Definition(String keyspaceString tableString nameDataType type) {
            this. = keyspace;
            this. = table;
            this. = name;
            this. = type;
        }
            return new Definition(spec.ksNamespec.cfNamespec.name.toString(), Codec.rawTypeToDataType(spec.type));
        }

        
The name of the keyspace this column is part of.

Returns:
the name of the keyspace this column is part of.
        public String getKeyspace() {
            return ;
        }

        
Returns the name of the table this column is part of.

Returns:
the name of the table this column is part of.
        public String getTable() {
            return ;
        }

        
Returns the name of the column.

Returns:
the name of the column.
        public String getName() {
            return ;
        }

        
Returns the type of the column.

Returns:
the type of the column.
        public DataType getType() {
            return ;
        }
        @Override
        public final int hashCode() {
            return Arrays.hashCode(new Object[]{ });
        }
        @Override
        public final boolean equals(Object o) {
            if(!(o instanceof Definition))
                return false;
            Definition other = (Definition)o;
            return .equals(other.keyspace)
                && .equals(other.table)
                && .equals(other.name)
                && .equals(other.type);
        }
    }
New to GrepCode? Check out our FAQ X