Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.zalando.typemapper.core.db;
  
  import java.sql.ResultSet;
  
  import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 public class DbFunctionRegister {
 
     private static final String FUNCTION_DETAILS = "SELECT ss.n_nspname AS specific_schema, "                                 //
             + "       ss.proname::text AS specific_name, "                                                                    //
             + "       (ss.x).n AS ordinal_position, "                                                                         //
             + "       NULLIF(ss.proargnames[(ss.x).n], ''::text) AS parameter_name, "                                         //
             + "       CASE "                                                                                                  //
             + "           WHEN t.typelem <> (0)::oid AND t.typlen = (-1) "                                                    //
             + "           THEN 'ARRAY'::text "                                                                                //
             + "           WHEN nt.nspname = 'pg_catalog'::name "                                                              //
             + "           THEN format_type(t.oid, NULL::INTEGER) "                                                            //
             + "           ELSE 'USER-DEFINED'::text "                                                                         //
             + "       END AS formatted_type_name, "                                                                           //
             + "       ss.p_oid AS procedure_oid, "                                                                            //
             + "       t.typname AS unformatted_type_name, "                                                                   //
             + "       t.oid AS type_oid "                                                                                     //
             + "  FROM pg_type t, "                                                                                            //
             + "       pg_namespace nt, "                                                                                      //
             + "       ( "                                                                                                     //
             + "         SELECT n.nspname AS n_nspname, "                                                                      //
             + "                p.proname, "                                                                                   //
             + "                p.oid AS p_oid, "                                                                              //
             + "                p.proargnames, "                                                                               //
             + "                p.proargmodes, "                                                                               //
             + "                information_schema._pg_expandarray(COALESCE(p.proallargtypes, (p.proargtypes)::oid[])) AS x "  //
             + "           FROM pg_namespace n, "                                                                              //
             + "                pg_proc p "                                                                                    //
             + "          WHERE ((n.oid = p.pronamespace) "                                                                    //
             + "            AND (pg_has_role(p.proowner, 'USAGE'::text) OR  has_function_privilege(p.oid, 'EXECUTE'::text))) " //
             + "        ) ss "                                                                                                 //
             + "WHERE t.oid = (ss.x).x "                                                                                       //
             + "  AND t.typnamespace = nt.oid "                                                                                //
             + "  AND ss.proargmodes[(ss.x).n] = ANY ('{o,b,t}'::char[]);";
 
     private Map<StringDbFunctionfunctions = null;
     private Map<StringList<String>> functionNameToFQName = null;
     private static Map<StringDbFunctionRegisterregisters;
     private List<StringsearchPath = null;
 
     public DbFunctionRegister(final Connection connectionthrows SQLException {
         PreparedStatement statement = null;
         ResultSet resultSet = null;
         try {
              = DbTypeRegister.getSearchPath(connection);
              = new HashMap<StringList<String>>();
              = new HashMap<StringDbFunction>();
             statement = connection.prepareStatement();
             resultSet = statement.executeQuery();
 
             while (resultSet.next()) {
                 int i = 1;
                 final String functionSchema = resultSet.getString(i++);
                 final String functionName = resultSet.getString(i++);
                 final int paramPosition = resultSet.getInt(i++);
                 final String paramName = resultSet.getString(i++);
                 final String paramType = resultSet.getString(i++);
                 final int procedureId = resultSet.getInt(i++);
                 final String paramTypeName = resultSet.getString(i++);
                 final int paramTypeId = resultSet.getInt(i++);
 
                 addFunctionParam(functionSchemafunctionNameparamNameparamPositionparamTypeprocedureId,
                     paramTypeNameparamTypeId);
             }
         } finally {
             if (resultSet != null) {
                 resultSet.close();
             }
 
             if (statement != null) {
                 statement.close();
             }
         }
     }
 
     private void addFunctionParam(final String functionSchemafinal String functionNamefinal String paramName,
             final int paramPositionfinal String paramTypefinal int procedureIdfinal String paramTypeName,
             final int paramTypeId) {
 
         final String functionId = getFunctionIdentifier(functionSchemafunctionName);
         DbFunction function = .get(functionId);
         if (function == null) {
             function = new DbFunction(functionSchemafunctionName);
             addFunction(function);
         }
 
         if (paramName != null) {
             function.addOutParam(new DbTypeField(paramNameparamPositionparamTypeparamTypeNameparamTypeId));
        }
    }
    private void addFunction(final DbFunction function) {
        String functionIdentifier = getFunctionIdentifier(function.getSchema(), function.getName());
        .put(functionIdentifierfunction);
        List<Stringlist = .get(function.getName());
        if (list == null) {
            list = new LinkedList<String>();
            .put(function.getName(), list);
        }
        list.add(functionIdentifier);
    }
    private static String getFunctionIdentifier(final String schemafinal String functionName) {
        return schema + "." + functionName;
    }
    public static final DbFunction getFunction(final String namefinal Connection connectionthrows SQLException {
        if ( == null) {
            initRegistry(connection"default");
        }
        for (DbFunctionRegister register : .values()) {
            List<Stringlist = register.functionNameToFQName.get(name);
            if (list.size() == 1) {
                return register.functions.get(list.get(0));
            } else {
                String fqName = SearchPathSchemaFilter.filter(listregister.searchPath);
                if (fqName != null) {
                    DbFunction function = register.functions.get(fqName);
                    if (function != null) {
                        return function;
                    }
                }
            }
        }
        return null;
    }
    public static synchronized void reInitRegistry(final Connection connectionthrows SQLException {
        if ( == null) {
             = new HashMap<StringDbFunctionRegister>();
        }
        .put("default"new DbFunctionRegister(connection));
    }
    public static synchronized void initRegistry(final Connection connectionfinal String namethrows SQLException {
        if ( == null) {
             = new HashMap<StringDbFunctionRegister>();
        }
        if (!.containsKey(name)) {
            .put(namenew DbFunctionRegister(connection));
        }
    }
New to GrepCode? Check out our FAQ X