/** 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)); }
private void makeServices( String dbName, String[] tableNames, DatabaseMetaData metaData, List<DataService> dataServiceList, String schema, String datasourceId, String serviceNamespace) throws SQLException, DataServiceFault { for (String tableName : tableNames) { String serviceName = tableName + DBConstants.DataServiceGenerator.SERVICE_NAME_SUFFIX; DataService dataService = new DataService( serviceName, DBConstants.DataServiceGenerator.MUTLIPLE_SERVICE_DESCRIPTION, null, null, DBConstants.DataServiceGenerator.ACTIVE, false, false, false, null); if (DBUtils.isEmptyString(serviceNamespace)) { dataService.setServiceNamespace(DBConstants.WSO2_DS_NAMESPACE); } else { dataService.setServiceNamespace(serviceNamespace); } this.setConfig(dataService, datasourceId); String tablePrimaryKey = this.getPrimaryKey(metaData, dbName, schema, tableName); this.addOperations(dataService, schema, metaData, dbName, tableName, tablePrimaryKey); dataServiceList.add(dataService); } }
/** 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 void setConfig(DataService dataServiceObject, String carbonSourceId) throws DataServiceFault { Map<String, String> properties = new HashMap<String, String>(); properties.put(DBConstants.CarbonDatasource.NAME, carbonSourceId); Config config = new SQLCarbonDataSourceConfig( dataServiceObject, DBConstants.DataServiceGenerator.CONFIG_ID, properties); dataServiceObject.addConfig(config); }
private DataService generateService( String datasourceId, String dbName, String[] schemas, String[] tableNames, DatabaseMetaData metaData, String serviceNamespace, String serviceName) throws DataServiceFault, SQLException { // String serviceName = dbName + DBConstants.DataServiceGenerator.SERVICE_NAME_SUFFIX; DataService dataService = new DataService( serviceName, DBConstants.DataServiceGenerator.SINGLE_SERVICE_DESCRIPTION, null, null, DBConstants.DataServiceGenerator.ACTIVE, false, false, false, null); this.setConfig(dataService, datasourceId); if (DBUtils.isEmptyString(serviceNamespace)) { dataService.setServiceNamespace(DBConstants.WSO2_DS_NAMESPACE); } else { dataService.setServiceNamespace(serviceNamespace); } if (schemas.length != 0) { for (String schema : schemas) { makeService(dbName, tableNames, metaData, dataService, schema); } } else { String schema = null; makeService(dbName, tableNames, metaData, dataService, schema); } return dataService; }