/** * Create query param list according to table column names * * @param param -List of column names in the database table * @param tableName - * @return paramList-List of QueryParam * @throws IllegalArgumentException -"" * @throws SQLException -"" * @throws DataServiceFault - */ private List<QueryParam> getQueryParamList( DatabaseMetaData metaData, String dbName, String schema, String tableName, List<String> param) throws IllegalArgumentException, SQLException, DataServiceFault { List<QueryParam> paramList = new ArrayList<QueryParam>(); int ordinal = 1; for (String pName : param) { /* Get the SQL type of parameter */ String sqlType = ""; ResultSet rs = getColumnNames(metaData, schema, dbName, tableName, pName); while (rs.next()) { int type = rs.getInt(DBConstants.DataServiceGenerator.DATA_TYPE); if ((-1 == type) || (-16 == type) || (-15 == type) || (2009 == type)) { type = 1; } sqlType = DSSqlTypes.getDefinedTypes().get(type); } List<Validator> validator = new ArrayList<Validator>(); QueryParam queryParam = new QueryParam( pName, sqlType, DBConstants.DataServiceGenerator.IN, DBConstants.DataServiceGenerator.SCALAR, ordinal, null, null, validator); paramList.add(queryParam); ordinal++; // increase the ordinal value one by one } return paramList; }
/** Create the data-service for select data by given key operation. */ private void addSelectWithKeyOperation( DataService dataServiceObject, String schema, DatabaseMetaData metaData, String dbName, String tableName, String pKey) throws SQLException, DataServiceFault, NullPointerException { Map<String, WithParam> paramMap = new HashMap<String, WithParam>(); List<String> paramList = new ArrayList<String>(); ResultSet columnNames = getColumnNames(metaData, schema, dbName, tableName, null); String colomNames = ""; int i = 0; while (columnNames.next()) { String name = columnNames.getString(DBConstants.DataServiceGenerator.COLUMN_NAME); // get the colomn names for the query if (i == 0) { colomNames = " " + name; } else { colomNames = colomNames + ", " + name; } i++; if (pKey.equals(name)) { WithParam withParam = new WithParam(pKey, pKey, pKey, DBConstants.DataServiceGenerator.QUERY_PARAM); paramMap.put(pKey, withParam); paramList.add(pKey); } } Set<String> requiredRoles = new HashSet<String>(); String queryId = DBConstants.DataServiceGenerator.SELECT_WITH_KEY + tableName + DBConstants.DataServiceGenerator._QUERY; String OpName = DBConstants.DataServiceGenerator.SELECT_WITH_KEY + tableName + DBConstants.DataServiceGenerator._OPERATION; CallQuery callQuery = new CallQuery(dataServiceObject, queryId, paramMap, requiredRoles); List<CallQuery> callQueries = new ArrayList<CallQuery>(); callQueries.add(callQuery); CallQueryGroup callQueryGroup = new CallQueryGroup(callQueries); // batchRequest=false // parentOperation=null Operation operation = new Operation(dataServiceObject, OpName, null, callQueryGroup, false, null, false, false); dataServiceObject.addOperation(operation); dataServiceObject.addQuery( this.getSelectWithKeyQuery( paramList, pKey, queryId, tableName, dataServiceObject, metaData, dbName, schema, colomNames)); }
/** Select all operation. */ private void addSelectAllOperation( DataService dataServiceObject, String schema, DatabaseMetaData metaData, String dbName, String tableName, String pKey) throws SQLException, DataServiceFault, NullPointerException { Map<String, WithParam> paramMap = new HashMap<String, WithParam>(); List<String> paramList = new ArrayList<String>(); /* get the primary key */ // ResultSet resultSet = this.metaObject.getColumns(this.dbName, null, // this.tableName, null); ResultSet columnNames = getColumnNames(metaData, schema, dbName, tableName, null); String colomNames = ""; int i = 0; while (columnNames.next()) { String name = columnNames.getString(DBConstants.DataServiceGenerator.COLUMN_NAME); if (i == 0) { colomNames = " " + name; } else { colomNames = colomNames + ", " + name; } i++; } paramMap.clear(); Set<String> requiredRoles = new HashSet<String>(); // empty set String queryId = new StringBuilder() .append(DBConstants.DataServiceGenerator.SELECT_ALL) .append(tableName) .append(DBConstants.DataServiceGenerator._QUERY) .toString(); String OpName = new StringBuilder() .append(DBConstants.DataServiceGenerator.SELECT_ALL) .append(tableName) .append(DBConstants.DataServiceGenerator._OPERATION) .toString(); CallQuery callQuery = new CallQuery(dataServiceObject, queryId, paramMap, requiredRoles); List<CallQuery> callQueries = new ArrayList<CallQuery>(); callQueries.add(callQuery); CallQueryGroup callQueryGroup = new CallQueryGroup(callQueries); // batchRequest=false // parentOperation=null Operation operation = new Operation(dataServiceObject, OpName, null, callQueryGroup, false, null, false, false); dataServiceObject.addOperation(operation); dataServiceObject.addQuery( this.getSelectAllQuery( paramList, queryId, tableName, dataServiceObject, metaData, dbName, schema, colomNames)); }
public static List<String> getTableNamesList( DatabaseMetaData mObject, String dbName, String schema) throws SQLException { List<String> tableList = new ArrayList<String>(); ResultSet tableNamesList = mObject.getTables(dbName, schema, null, null); while (tableNamesList.next()) { tableList.add(tableNamesList.getString(DBConstants.DataServiceGenerator.TABLE_NAME)); } return tableList; }
public static String[] getSchemas(Connection connection) throws Exception { if (connection != null) { List<String> schemaList = new ArrayList<String>(); DatabaseMetaData mObject = connection.getMetaData(); ResultSet schemas = mObject.getSchemas(); while (schemas.next()) { schemaList.add(schemas.getString(DBConstants.DataServiceGenerator.TABLE_SCHEM)); } String str[] = schemaList.toArray(new String[schemaList.size()]); return str; } else { return null; } }
private ResultSet getColumnNames( DatabaseMetaData metaData, String schema, String dbName, String tableName, String columnNamePattern) throws SQLException { ResultSet columnNames = metaData.getColumns(dbName, schema, tableName, columnNamePattern); if (columnNames.next()) { columnNames = metaData.getColumns(dbName, schema, tableName, columnNamePattern); } else { columnNames = metaData.getColumns(null, schema, tableName, columnNamePattern); } return columnNames; }
private String getPrimaryKey( DatabaseMetaData meta, String dbName, String schema, String tableName) throws SQLException { String pKey = null; ResultSet resultSet = meta.getPrimaryKeys(dbName, schema, tableName); if (resultSet.next()) { resultSet = meta.getPrimaryKeys(dbName, schema, tableName); } else { resultSet = meta.getPrimaryKeys(null, schema, tableName); } while (resultSet.next()) { pKey = resultSet.getString(DBConstants.DataServiceGenerator.COLUMN_NAME); return pKey; } return pKey; }
/** Update Operation. */ private void addUpdateOperation( DataService dataServiceObject, String schema, DatabaseMetaData metaData, String dbName, String tableName, String pKey) throws SQLException, DataServiceFault { Map<String, WithParam> paramMap = new HashMap<String, WithParam>(); List<String> paramList = new ArrayList<String>(); ResultSet columnNames = getColumnNames(metaData, schema, dbName, tableName, null); while (columnNames.next()) { String name = columnNames.getString(DBConstants.DataServiceGenerator.COLUMN_NAME); if (!name.equals(pKey)) { WithParam withParam1 = new WithParam(name, name, name, DBConstants.DataServiceGenerator.QUERY_PARAM); paramMap.put(name, withParam1); paramList.add(name); // add to this @param into @param List } } WithParam withParam2 = new WithParam(pKey, pKey, pKey, DBConstants.DataServiceGenerator.QUERY_PARAM); paramMap.put(pKey, withParam2); paramList.add(pKey); Set<String> requiredRoles = new HashSet<String>(); // empty set String queryId = DBConstants.DataServiceGenerator.UPDATE_ + tableName + DBConstants.DataServiceGenerator._QUERY; String OpName = DBConstants.DataServiceGenerator.UPDATE_ + tableName + DBConstants.DataServiceGenerator._OPERATION; CallQuery callQuery = new CallQuery(dataServiceObject, queryId, paramMap, requiredRoles); List<CallQuery> callQueries = new ArrayList<CallQuery>(); callQueries.add(callQuery); CallQueryGroup callQueryGroup = new CallQueryGroup(callQueries); // batchRequest=false // parentOperation=null Operation operation = new Operation(dataServiceObject, OpName, null, callQueryGroup, false, null, false, false); dataServiceObject.addOperation(operation); dataServiceObject.addQuery( this.getUpdateQuery( paramList, pKey, queryId, tableName, dataServiceObject, metaData, dbName, schema)); }
/** Insert Operation. */ private void addInsertOperation( DataService dataServiceObject, String schema, DatabaseMetaData metaData, String dbName, String tableName) throws DataServiceFault, SQLException { /* do insertion operation */ Map<String, WithParam> paramMap = new HashMap<String, WithParam>(); List<String> paramList = new ArrayList<String>(); ResultSet columnNames = getColumnNames(metaData, schema, dbName, tableName, null); while (columnNames.next()) { if (this.isAutoIncrementField(columnNames)) { continue; } String name = columnNames.getString(DBConstants.DataServiceGenerator.COLUMN_NAME); WithParam withParam = new WithParam(name, name, name, DBConstants.DataServiceGenerator.QUERY_PARAM); paramMap.put(name, withParam); paramList.add(name); } Set<String> requiredRoles = new HashSet<String>(); String queryId = DBConstants.DataServiceGenerator.INSERT_ + tableName + DBConstants.DataServiceGenerator._QUERY; String OpName = DBConstants.DataServiceGenerator.INSERT_ + tableName + DBConstants.DataServiceGenerator._OPERATION; CallQuery callQuery = new CallQuery(dataServiceObject, queryId, paramMap, requiredRoles); List<CallQuery> callQueries = new ArrayList<CallQuery>(); callQueries.add(callQuery); CallQueryGroup callQueryGroup = new CallQueryGroup(callQueries); // batchRequest=false // parentOperation=null Operation op = new Operation(dataServiceObject, OpName, null, callQueryGroup, false, null, false, false); dataServiceObject.addOperation(op); dataServiceObject.addQuery( this.getInsertQuery( paramList, queryId, tableName, dataServiceObject, metaData, dbName, schema)); }
private boolean isAutoIncrementField(ResultSet columnNames) throws SQLException { try { Boolean autoIncr = columnNames.getBoolean(AUTOINCREMENT_COLUMN); if (autoIncr != null) { return autoIncr; } } catch (SQLException ignore) { // ignore } try { Boolean identity = columnNames.getBoolean(IDENTITY_COLUMN); if (identity != null) { return identity; } } catch (SQLException ignore) { // ignore } return false; }
/** This method construct the result object. */ private Result getResult( DataService dataServiceObject, DatabaseMetaData metaData, String dbName, String schema, String tableName) throws DataServiceFault, IllegalArgumentException, SQLException { // row name is equal to table Name String rowElementName = tableName + DBConstants.DataServiceGenerator.ROW_ELEMENT_NAME_SUFFIX; Result result = new Result(rowElementName, tableName, null, null, ResultTypes.XML); ResultSet columnNames = getColumnNames(metaData, schema, dbName, tableName, null); OutputElementGroup defGroup = new OutputElementGroup(null, null, null, null); while (columnNames.next()) { String columnName = columnNames.getString(DBConstants.DataServiceGenerator.COLUMN_NAME); int typeInt = columnNames.getInt("DATA_TYPE"); if ((-1 == typeInt) || (-16 == typeInt) || (-15 == typeInt) || (2009 == typeInt)) { typeInt = 1; } String type = DSSqlTypes.getQNameType(typeInt); QName qName = QueryFactory.getXsdTypeQName(type); Set<String> requiredRoles = new HashSet<String>(); // empty set StaticOutputElement outputElement = new StaticOutputElement( dataServiceObject, columnName, columnName, columnName, DBConstants.DataServiceGenerator.COLUMN, DBConstants.DataServiceGenerator.ELEMENT, null, qName, requiredRoles, 0, 0, null, ParamValue.PARAM_VALUE_SCALAR, null); defGroup.addAttributeEntry(outputElement); } result.setDefaultElementGroup(defGroup); return result; }