001package com.nimbusds.infinispan.persistence.sql;
002
003
004import org.jooq.DSLContext;
005import org.jooq.Result;
006import org.jooq.Table;
007import org.jooq.impl.DSL;
008
009import java.util.LinkedList;
010import java.util.List;
011
012
013/**
014 * SQL table utilities.
015 */
016public class SQLTableUtils {
017        
018        
019        /**
020         * Gets the column names of the specified table.
021         *
022         * @param table      The table.
023         * @param dslContext The DSL context.
024         *
025         * @return The column names normalised to lowercase, empty list if
026         *         none.
027         */
028        public static List<String> getColumnNames(final Table<?> table, final DSLContext dslContext) {
029
030                // LIMIT 0 causes extremely slow execution in Oracle DB with many records, see iss #22
031                Result<?> result = dslContext.selectFrom(table).limit(DSL.inline(1)).fetch();
032                
033                List<String> columnNames = new LinkedList<>();
034                
035                for (int i=0; i < result.fields().length; i++)
036                        columnNames.add(result.fields()[i].getName().toLowerCase());
037                
038                return columnNames;
039        }
040        
041        
042        private SQLTableUtils(){}
043}