Java Code Examples for java.sql.ParameterMetaData#getParameterCount()

The following examples show how to use java.sql.ParameterMetaData#getParameterCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Quicksql   File: QuicksqlServerMeta.java    License: MIT License 6 votes vote down vote up
/**
 * Converts from JDBC metadata to Avatica parameters
 */
protected static List<AvaticaParameter> parameters(ParameterMetaData metaData)
    throws SQLException {
    if (metaData == null) {
        return Collections.emptyList();
    }
    final List<AvaticaParameter> params = new ArrayList<>();
    for (int i = 1; i <= metaData.getParameterCount(); i++) {
        params.add(
            new AvaticaParameter(metaData.isSigned(i), metaData.getPrecision(i),
                metaData.getScale(i), metaData.getParameterType(i),
                metaData.getParameterTypeName(i),
                metaData.getParameterClassName(i), "?" + i));
    }
    return params;
}
 
Example 2
Source Project: calcite-avatica   File: JdbcMeta.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Converts from JDBC metadata to Avatica parameters
 */
protected static List<AvaticaParameter> parameters(ParameterMetaData metaData)
    throws SQLException {
  if (metaData == null) {
    return Collections.emptyList();
  }
  final List<AvaticaParameter> params = new ArrayList<>();
  for (int i = 1; i <= metaData.getParameterCount(); i++) {
    params.add(
        new AvaticaParameter(metaData.isSigned(i), metaData.getPrecision(i),
            metaData.getScale(i), metaData.getParameterType(i),
            metaData.getParameterTypeName(i),
            metaData.getParameterClassName(i), "?" + i));
  }
  return params;
}
 
Example 3
/**
        * tests parameterMetaData and reports error if the ParameterMetaData results
 * does not match the expected results.
        *
        * @param paramMetaData ParameterMetadata object
 * @param paramMetaDataArray 2 dimensional array containing expected test results. 
 * @exception SQLException if any error occurs
        */
static void testParameterMetaData(ParameterMetaData paramMetaData, String [][] paramMetaDataArray) throws SQLException {
	int numParam = paramMetaData.getParameterCount();
	
	for (int i=0, j=0; i<numParam; i++) {	
                      	assertEquals("Unexpected parameter isNullable", paramMetaDataArray[i][j++], parameterIsNullableInStringForm(paramMetaData.isNullable(i+1)));
                      	assertEquals("Unexpected parameter isSigned", Boolean.valueOf(paramMetaDataArray[i][j++]).booleanValue(), paramMetaData.isSigned(i+1));
                      	assertEquals("Unexpected parameter getPrecision", Integer.parseInt(paramMetaDataArray[i][j++]) ,  paramMetaData.getPrecision(i+1));
                      	assertEquals("Unexpected parameter getScale", Integer.parseInt(paramMetaDataArray[i][j++]) , paramMetaData.getScale(i+1));
                      	assertEquals("Unexpected parameter getParameterType", Integer.parseInt(paramMetaDataArray[i][j++]) , paramMetaData.getParameterType(i+1));
                      	assertEquals("Unexpected parameter getParameterTypeName", paramMetaDataArray[i][j++] , paramMetaData.getParameterTypeName(i+1));
                      	assertEquals("Unexpected parameter getParameterClassName", paramMetaDataArray[i][j++] , paramMetaData.getParameterClassName(i+1));
                      	assertEquals("Unexpected parameter getParameterMode", paramMetaDataArray[i][j++] , parameterModeInStringForm(paramMetaData.getParameterMode(i+1)));
		
		j=0;
	}
}
 
Example 4
public static DataRecordMetadata dbMetadata2jetel(ParameterMetaData dbMetadata, String metadataName, JdbcSpecific jdbcSpecific) throws SQLException {
	DataFieldMetadata fieldMetadata;
	DataRecordMetadata jetelMetadata = new DataRecordMetadata(metadataName, DataRecordMetadata.DELIMITED_RECORD);
	jetelMetadata.setLabel(metadataName);
	jetelMetadata.setFieldDelimiter(DEFAULT_DELIMITER);
	jetelMetadata.setRecordDelimiter(END_RECORD_DELIMITER);
	String colName;

	for (int i = 1; i <= dbMetadata.getParameterCount(); i++) {
		colName = "field" + i;
		fieldMetadata = dbMetadata2jetel(colName, dbMetadata, i, jdbcSpecific);
		jetelMetadata.addField(fieldMetadata);
	}
	
	jetelMetadata.normalize();
	
	return jetelMetadata;
}
 
Example 5
/**
        * tests parameterMetaData and reports error if the ParameterMetaData results
 * does not match the expected results.
        *
        * @param paramMetaData ParameterMetadata object
 * @param paramMetaDataArray 2 dimensional array containing expected test results. 
 * @exception SQLException if any error occurs
        */
static void testParameterMetaData(ParameterMetaData paramMetaData, String [][] paramMetaDataArray) throws SQLException {
	int numParam = paramMetaData.getParameterCount();
	
	for (int i=0, j=0; i<numParam; i++) {	
                      	assertEquals("Unexpected parameter isNullable", paramMetaDataArray[i][j++], parameterIsNullableInStringForm(paramMetaData.isNullable(i+1)));
                      	assertEquals("Unexpected parameter isSigned", Boolean.valueOf(paramMetaDataArray[i][j++]).booleanValue(), paramMetaData.isSigned(i+1));
                      	assertEquals("Unexpected parameter getPrecision", Integer.parseInt(paramMetaDataArray[i][j++]) ,  paramMetaData.getPrecision(i+1));
                      	assertEquals("Unexpected parameter getScale", Integer.parseInt(paramMetaDataArray[i][j++]) , paramMetaData.getScale(i+1));
                      	assertEquals("Unexpected parameter getParameterType", Integer.parseInt(paramMetaDataArray[i][j++]) , paramMetaData.getParameterType(i+1));
                      	assertEquals("Unexpected parameter getParameterTypeName", paramMetaDataArray[i][j++] , paramMetaData.getParameterTypeName(i+1));
                      	assertEquals("Unexpected parameter getParameterClassName", paramMetaDataArray[i][j++] , paramMetaData.getParameterClassName(i+1));
                      	assertEquals("Unexpected parameter getParameterMode", paramMetaDataArray[i][j++] , parameterModeInStringForm(paramMetaData.getParameterMode(i+1)));
		
		j=0;
	}
}
 
Example 6
private void assertStatementState(int[] parameterExpectedValues,
                                  int[] expectedValues, Statement s)
        throws SQLException {
    assertEquals(expectedValues[0], s.getResultSetType());
    assertEquals(
            expectedValues[1], s.getResultSetConcurrency());
    assertEquals(
            expectedValues[2], s.getFetchDirection());
    assertEquals(expectedValues[3], s.getFetchSize());
    assertEquals(expectedValues[4], s.getMaxFieldSize());
    assertEquals(expectedValues[5], s.getMaxRows());
    assertEquals(expectedValues[6], s.getResultSetHoldability());

    if (s instanceof PreparedStatement) {
        PreparedStatement ps = (PreparedStatement) s;
        ParameterMetaData psmd = ps.getParameterMetaData();
        // Parameter count:
        assertEquals(parameterExpectedValues[0], psmd.getParameterCount());
        for (int i = 1; i <= psmd.getParameterCount(); i++) {
            assertEquals(parameterExpectedValues[i], psmd.getParameterType(i));
        }
    }
}
 
Example 7
private void assertStatementState(int[] parameterExpectedValues, 
    int[] expectedValues, Statement s) 
throws SQLException {
    assertEquals(expectedValues[0], s.getResultSetType());
    assertEquals(
        expectedValues[1], s.getResultSetConcurrency());
    assertEquals(
        expectedValues[2], s.getFetchDirection());
    assertEquals(expectedValues[3], s.getFetchSize());
    assertEquals(expectedValues[4], s.getMaxFieldSize());
    assertEquals(expectedValues[5], s.getMaxRows());
    assertEquals(expectedValues[6], s.getResultSetHoldability());

    if (s instanceof PreparedStatement) {
        PreparedStatement ps = (PreparedStatement) s;
        ParameterMetaData psmd = ps.getParameterMetaData();
        // Parameter count:
        assertEquals(parameterExpectedValues[0], psmd.getParameterCount());
        for (int i = 1; i <= psmd.getParameterCount(); i++) {
            assertEquals(parameterExpectedValues[i], psmd.getParameterType(i));
        }
    }
}
 
Example 8
/**
        * tests parameterMetaData and reports error if the ParameterMetaData results
 * does not match the expected results.
        *
        * @param paramMetaData ParameterMetadata object
 * @param paramMetaDataArray 2 dimensional array containing expected test results. 
 * @exception SQLException if any error occurs
        */
static void testParameterMetaData(ParameterMetaData paramMetaData, String [][] paramMetaDataArray) throws SQLException {
	int numParam = paramMetaData.getParameterCount();
	
	for (int i=0, j=0; i<numParam; i++) {	
                      	assertEquals("Unexpected parameter isNullable", paramMetaDataArray[i][j++], parameterIsNullableInStringForm(paramMetaData.isNullable(i+1)));
                      	assertEquals("Unexpected parameter isSigned", Boolean.valueOf(paramMetaDataArray[i][j++]).booleanValue(), paramMetaData.isSigned(i+1));
                      	assertEquals("Unexpected parameter getPrecision", Integer.parseInt(paramMetaDataArray[i][j++]) ,  paramMetaData.getPrecision(i+1));
                      	assertEquals("Unexpected parameter getScale", Integer.parseInt(paramMetaDataArray[i][j++]) , paramMetaData.getScale(i+1));
                      	assertEquals("Unexpected parameter getParameterType", Integer.parseInt(paramMetaDataArray[i][j++]) , paramMetaData.getParameterType(i+1));
                      	assertEquals("Unexpected parameter getParameterTypeName", paramMetaDataArray[i][j++] , paramMetaData.getParameterTypeName(i+1));
                      	assertEquals("Unexpected parameter getParameterClassName", paramMetaDataArray[i][j++] , paramMetaData.getParameterClassName(i+1));
                      	assertEquals("Unexpected parameter getParameterMode", paramMetaDataArray[i][j++] , parameterModeInStringForm(paramMetaData.getParameterMode(i+1)));
		
		j=0;
	}
}
 
Example 9
Source Project: ignite   File: H2Utils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Converts h2 parameters metadata to Ignite one.
 *
 * @param h2ParamsMeta parameters metadata returned by h2.
 * @return Descriptions of the parameters.
 */
public static List<JdbcParameterMeta> parametersMeta(ParameterMetaData h2ParamsMeta) throws IgniteCheckedException {
    try {
        int paramsSize = h2ParamsMeta.getParameterCount();

        if (paramsSize == 0)
            return Collections.emptyList();

        ArrayList<JdbcParameterMeta> params = new ArrayList<>(paramsSize);

        for (int i = 1; i <= paramsSize; i++)
            params.add(new JdbcParameterMeta(h2ParamsMeta, i));

        return params;
    }
    catch (SQLException e) {
        throw new IgniteCheckedException("Failed to get parameters metadata", e);
    }
}
 
Example 10
private void sendParameterDescription(Prepared p) throws IOException {
    try {
        PreparedStatement prep = p.prep;
        ParameterMetaData meta = prep.getParameterMetaData();
        int count = meta.getParameterCount();
        startMessage('t');
        writeShort(count);
        for (int i = 0; i < count; i++) {
            int type;
            if (p.paramType != null && p.paramType[i] != 0) {
                type = p.paramType[i];
            } else {
                type = PgServer.PG_TYPE_VARCHAR;
            }
            server.checkType(type);
            writeInt(type);
        }
        sendMessage();
    } catch (Exception e) {
        sendErrorResponse(e);
    }
}
 
Example 11
Source Project: quark   File: QuarkMetaImpl.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Converts from JDBC metadata to Avatica parameters
 */
protected static List<AvaticaParameter> parameters(ParameterMetaData metaData)
    throws SQLException {
  if (metaData == null) {
    return Collections.emptyList();
  }
  final List<AvaticaParameter> params = new ArrayList<>();
  for (int i = 1; i <= metaData.getParameterCount(); i++) {
    params.add(
        new AvaticaParameter(metaData.isSigned(i), metaData.getPrecision(i),
            metaData.getScale(i), metaData.getParameterType(i),
            metaData.getParameterTypeName(i),
            metaData.getParameterClassName(i), "?" + i));
  }
  return params;
}
 
Example 12
public void testDataAwareProcedureWithoutResultSets()
throws Exception {
  
  setup();
  CallableStatement cs = prepareCall("CALL PROCEDURE_WITHOUT_RESULTSET(?, ?) ON TABLE EMP.PARTITIONTESTTABLE WHERE SECONDID=4 AND THIRDID='3'");
  cs.registerOutParameter(2, java.sql.Types.VARCHAR, 20);
  int number=2;
  String name="INOUTPARAMETER";
  cs.setInt(1, number);
  cs.setString(2, name);
 
  
  cs.execute();
  
  ResultSet rs=cs.getResultSet();
  if(rs!=null || cs.getMoreResults()) {
    fail("no dynamic result set for the procedure!");
  }
  
  ParameterMetaData pmd=cs.getParameterMetaData();  
  int numParameters=pmd.getParameterCount();
  assertTrue(" the number of parameter is 2", numParameters==2);
  try {
    cs.getInt(1);
    fail("the in parameteter cannot be read!");
  } catch (Exception e) {
    
  }
  Object parameter2=cs.getObject(2);    
  assertTrue("the second inout parameter is "+name+number, parameter2.equals(name+number));
    
  

}
 
Example 13
public void testDataAwareProcedureWithoutResultSetsUsingGlobalIndex()
throws Exception {
  
  setup();
  CallableStatement cs = prepareCall("CALL PROCEDURE_WITHOUT_RESULTSET(?, ?) ON TABLE EMP.PARTITIONTESTTABLE1 WHERE SECONDID=4 AND THIRDID='3'");
  cs.registerOutParameter(2, java.sql.Types.VARCHAR, 20);
  int number=2;
  String name="INOUTPARAMETER";
  cs.setInt(1, number);
  cs.setString(2, name);
 
  
  cs.execute();
  
  ResultSet rs=cs.getResultSet();
  if(rs!=null || cs.getMoreResults()) {
    fail("no dynamic result set for the procedure!");
  }
  
  ParameterMetaData pmd=cs.getParameterMetaData();  
  int numParameters=pmd.getParameterCount();
  assertTrue(" the number of parameter is 2", numParameters==2);
  try {
    cs.getInt(1);
    fail("the in parameteter cannot be read!");
  } catch (Exception e) {
    
  }
  Object parameter2=cs.getObject(2);    
  assertTrue("the second inout parameter is "+name+number, parameter2.equals(name+number));
    
  

}
 
Example 14
Source Project: gemfirexd-oss   File: ProcedureTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testDataAwareProcedureWithoutResultSetsUsingGlobalIndex()
throws SQLException {
  
  setup();
  CallableStatement cs = prepareCall("CALL PROCEDURE_WITHOUT_RESULTSET(?, ?) ON TABLE EMP.PARTITIONTESTTABLE1 WHERE SECONDID=4 AND THIRDID='3'");
  cs.registerOutParameter(2, java.sql.Types.VARCHAR, 20);
  int number=2;
  String name="INOUTPARAMETER";
  cs.setInt(1, number);
  cs.setString(2, name);
 
  
  cs.execute();
  
  ResultSet rs=cs.getResultSet();
  if(rs!=null || cs.getMoreResults()) {
    fail("no dynamic result set for the procedure!");
  }
  
  ParameterMetaData pmd=cs.getParameterMetaData();  
  int numParameters=pmd.getParameterCount();
  assertTrue(" the number of parameter is 2", numParameters==2);
  try {
    cs.getInt(1);
    fail("the in parameteter cannot be read!");
  } catch (Exception e) {
    
  }
  Object parameter2=cs.getObject(2);    
  assertTrue("the second inout parameter is "+name+number, parameter2.equals(name+number));
    
  

}
 
Example 15
/**
 *  Gets the fieldTypes attribute of the SQLUtil class
 *
 * @param  metadata          Description of the Parameter
 * @return                   The fieldTypes value
 * @exception  SQLException  Description of the Exception
 */
public static List<Integer> getFieldTypes(ParameterMetaData metadata) throws SQLException {
	List<Integer> fieldTypes = new LinkedList<Integer>();
	for (int i = 1; i <= metadata.getParameterCount(); i++) {
		fieldTypes.add(new Integer(metadata.getParameterType(i)));
	}
	return fieldTypes;
}
 
Example 16
/**
 * Creates a mapping between column positions and their data types
 * @return A map containing the position of the columns along with their data type as value
 */
private Map<Integer, Integer> getColumnPosSqlTypes() {
  try {
    final Map<Integer, Integer> columnPosSqlTypes = Maps.newHashMap();
    ParameterMetaData pMetaData = this.insertPstmtForFixedBatch.getParameterMetaData();
    for (int i = 1; i <= pMetaData.getParameterCount(); i++) {
      columnPosSqlTypes.put(i, pMetaData.getParameterType(i));
    }
    return columnPosSqlTypes;
  } catch (SQLException e) {
    throw new RuntimeException("Cannot retrieve columns types for batch insert", e);
  }
}
 
Example 17
public void testDataAwareProcedureWithoutResultSetsUsingGlobalIndex()
throws Exception {
  
  setup();
  CallableStatement cs = prepareCall("CALL PROCEDURE_WITHOUT_RESULTSET(?, ?) ON TABLE EMP.PARTITIONTESTTABLE1 WHERE SECONDID=4 AND THIRDID='3'");
  cs.registerOutParameter(2, java.sql.Types.VARCHAR, 20);
  int number=2;
  String name="INOUTPARAMETER";
  cs.setInt(1, number);
  cs.setString(2, name);
 
  
  cs.execute();
  
  ResultSet rs=cs.getResultSet();
  if(rs!=null || cs.getMoreResults()) {
    fail("no dynamic result set for the procedure!");
  }
  
  ParameterMetaData pmd=cs.getParameterMetaData();  
  int numParameters=pmd.getParameterCount();
  assertTrue(" the number of parameter is 2", numParameters==2);
  try {
    cs.getInt(1);
    fail("the in parameteter cannot be read!");
  } catch (Exception e) {
    
  }
  Object parameter2=cs.getObject(2);    
  assertTrue("the second inout parameter is "+name+number, parameter2.equals(name+number));
    
  

}
 
Example 18
Source Project: gemfirexd-oss   File: ProcedureTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testDataAwareProcedureWithoutResultSets()
throws SQLException {
  
  setup();
  CallableStatement cs = prepareCall("CALL PROCEDURE_WITHOUT_RESULTSET(?, ?) ON TABLE EMP.PARTITIONTESTTABLE WHERE SECONDID=4 AND THIRDID='3'");
  cs.registerOutParameter(2, java.sql.Types.VARCHAR, 20);
  int number=2;
  String name="INOUTPARAMETER";
  cs.setInt(1, number);
  cs.setString(2, name);
 
  
  cs.execute();
  
  ResultSet rs=cs.getResultSet();
  if(rs!=null || cs.getMoreResults()) {
    fail("no dynamic result set for the procedure!");
  }
  
  ParameterMetaData pmd=cs.getParameterMetaData();  
  int numParameters=pmd.getParameterCount();
  assertTrue(" the number of parameter is 2", numParameters==2);
  try {
    cs.getInt(1);
    fail("the in parameteter cannot be read!");
  } catch (Exception e) {
    
  }
  Object parameter2=cs.getObject(2);    
  assertTrue("the second inout parameter is "+name+number, parameter2.equals(name+number));
    
  

}
 
Example 19
Source Project: gemfirexd-oss   File: ProcedureTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testDataAwareProcedureWithoutResultSetsUsingGlobalIndex()
throws SQLException {
  
  setup();
  CallableStatement cs = prepareCall("CALL PROCEDURE_WITHOUT_RESULTSET(?, ?) ON TABLE EMP.PARTITIONTESTTABLE1 WHERE SECONDID=4 AND THIRDID='3'");
  cs.registerOutParameter(2, java.sql.Types.VARCHAR, 20);
  int number=2;
  String name="INOUTPARAMETER";
  cs.setInt(1, number);
  cs.setString(2, name);
 
  
  cs.execute();
  
  ResultSet rs=cs.getResultSet();
  if(rs!=null || cs.getMoreResults()) {
    fail("no dynamic result set for the procedure!");
  }
  
  ParameterMetaData pmd=cs.getParameterMetaData();  
  int numParameters=pmd.getParameterCount();
  assertTrue(" the number of parameter is 2", numParameters==2);
  try {
    cs.getInt(1);
    fail("the in parameteter cannot be read!");
  } catch (Exception e) {
    
  }
  Object parameter2=cs.getObject(2);    
  assertTrue("the second inout parameter is "+name+number, parameter2.equals(name+number));
    
  

}
 
Example 20
Source Project: gemfirexd-oss   File: DRDAStatement.java    License: Apache License 2.0 4 votes vote down vote up
private void setupCallableStatementParams(CallableStatement cs) throws SQLException
{
	ParameterMetaData pmeta = getParameterMetaData();
	int numElems = pmeta.getParameterCount();

	for ( int i = 0; i < numElems; i ++)
	{
		boolean outputFlag = false;
		
		int parameterMode = pmeta.getParameterMode(i + 1);
		int parameterType = pmeta.getParameterType(i + 1);
                       int parameterPrecision = pmeta.getPrecision(i + 1);
                       int parameterScale = pmeta.getScale(i + 1);

		switch (parameterMode) {
			case JDBC30Translation.PARAMETER_MODE_IN:
				break;
			case JDBC30Translation.PARAMETER_MODE_OUT:
			case JDBC30Translation.PARAMETER_MODE_IN_OUT:
				outputFlag = true;
				break;
			case JDBC30Translation.PARAMETER_MODE_UNKNOWN:
				// It's only unknown if array
				String objectType = pmeta.getParameterClassName(i+1);
				parameterType =
					getOutputParameterTypeFromClassName(objectType);
				if (parameterType  != NOT_OUTPUT_PARAM)
					outputFlag = true;
		}

		if (outputFlag)
		{
			if (outputTypes == null) //not initialized yet, since previously none output
			{
				outputTypes = new int[numElems];
				outputPrecision = new int [numElems];
				outputScale = new int [numElems];
				for (int j = 0; j < numElems; j++) {
					outputTypes[j] = NOT_OUTPUT_PARAM;  //default init value
					outputPrecision[j] = NOT_OUTPUT_PARAM;
					outputScale[j] = NOT_OUTPUT_PARAM;
				}
			}
			// save the output type so we can register when we parse
			// the SQLDTA
			outputTypes[i] = parameterType;
			outputPrecision[i] = parameterPrecision;
			outputScale[i] = parameterScale;                
		}
		
	}
}