private Single<String> getIdColName(String tableName) {
        String alias = "idColumnName";
        return new CustomSelectAction() {
            @Override
            protected String patternSql() {
                return String.format("SELECT k.COLUMN_NAME as %s\n" +
                        "FROM information_schema.table_constraints t\n" +
                        "LEFT JOIN information_schema.key_column_usage k\n" +
                        "USING(constraint_name,table_schema,table_name)\n" +
                        "WHERE t.constraint_type='PRIMARY KEY'\n" +
                        "    AND t.table_schema=DATABASE() \n" +
                        "    AND t.table_name='%s';", alias, tableName);
            }

            @Override
            public int resultType() {
                return SELECT_SINGLE;
            }
        }.execute(null, null, connection, MsgIdHolder.get()).map(unitResponse -> {
            SingleRecordSelectionResult autoIncrement = unitResponse.getData();
            if (autoIncrement.getCount() == 0) {
                LOG.info("No primary key exists in table " + tableName);
                return "";
            }
            return autoIncrement.getRecord().get(alias).toString();
        });
    }
private Single < String > getIdColName ( String tableName ) { String alias = "idColumnName" ; return new CustomSelectAction ( ) { @ Override protected String patternSql ( ) { return String . format ( "SELECT k.COLUMN_NAME as %s\n" + "FROM information_schema.table_constraints t\n" + "LEFT JOIN information_schema.key_column_usage k\n" + "USING(constraint_name,table_schema,table_name)\n" + "WHERE t.constraint_type='PRIMARY KEY'\n" + "    AND t.table_schema=DATABASE() \n" + "    AND t.table_name='%s';" , alias , tableName ) ; } @ Override public int resultType ( ) { return SELECT_SINGLE ; } } . execute ( null , null , connection , MsgIdHolder . get ( ) ) . map ( unitResponse -> { SingleRecordSelectionResult autoIncrement = unitResponse . getData ( ) ; if ( autoIncrement . getCount ( ) == 0 ) { LOG . info ( "No primary key exists in table " + tableName ) ; return "" ; } return autoIncrement . getRecord ( ) . get ( alias ) . toString ( ) ; } ) ; }
查询主键名