use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.
the class ProjectTest method testQueryCompiledValidate.
@Test
public void testQueryCompiledValidate() throws ProjectElementException {
Query testQuery = projectProvider.getProject().getEntity("testtable").getQueries().get("All records");
String validatedQuery = testQuery.getQueryCompiled().validate().trim();
assertNotNull(validatedQuery);
assertEquals("SELECT\n" + " t.name AS \"Name\",\n" + " t.value AS \"Value\"\n" + "FROM\n" + " testtable t", validatedQuery);
}
use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.
the class ActionHelperTest method legacyUrlTest.
@Test
public void legacyUrlTest() throws Exception {
Query query = getQuery(QueryType.STATIC, "welfareGroups.redir?_qn_=Муниципальные+услуги");
assertEquals("table/welfareGroups/Муниципальные услуги", ActionUtils.toAction(query).arg);
}
use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.
the class MassChange method apply.
public List<BeModelElement> apply(LoadContext loadContext, Project project) {
List<BeModelElement> elements = SelectorUtils.select(project, getRule());
List<BeModelElement> changedElements = new ArrayList<>();
for (BeModelElement element : elements) {
if (element instanceof Query) {
Query oldQuery = (Query) element;
Query newQuery = YamlDeserializer.readQuery(loadContext, oldQuery.getName(), data, oldQuery.getEntity());
DataElementUtils.saveQuiet(newQuery);
newQuery.merge(oldQuery, false, true);
newQuery.setOriginModuleName(oldQuery.getOriginModuleName());
} else if (element instanceof Operation) {
Operation oldOperation = (Operation) element;
Map<String, Object> realData = data;
// Set type, because it cannot be inherited yet
if (!data.containsKey("type") && !Operation.OPERATION_TYPE_JAVA.equals(oldOperation.getType())) {
realData = new HashMap<>(data);
realData.put("type", oldOperation.getType());
}
Operation newOperation = YamlDeserializer.readOperation(loadContext, oldOperation.getName(), realData, oldOperation.getEntity());
DataElementUtils.saveQuiet(newOperation);
newOperation.merge(oldOperation, false, true);
newOperation.setOriginModuleName(oldOperation.getOriginModuleName());
} else if (element instanceof Entity) {
Entity oldEntity = (Entity) element;
Map<String, Object> realData = data;
// Set type, because it cannot be inherited yet
if (!data.containsKey("type")) {
realData = new HashMap<>(data);
realData.put("type", oldEntity.getType().getSqlName());
}
Entity newEntity = YamlDeserializer.readEntity(loadContext, oldEntity.getName(), realData, oldEntity.getModule());
for (EntityItem q : newEntity.getQueries()) q.setOriginModuleName(oldEntity.getModule().getName());
for (EntityItem o : newEntity.getOperations()) o.setOriginModuleName(oldEntity.getModule().getName());
DataElementUtils.saveQuiet(newEntity);
newEntity.merge(oldEntity, false, true);
} else {
loadContext.addWarning(new ReadException(element, null, "Mass change is not supported for type " + element.getClass().getSimpleName()));
continue;
}
changedElements.add(element);
}
return changedElements;
}
use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.
the class Entity method getErrors.
@Override
public List<ProjectElementException> getErrors() {
final List<ProjectElementException> errors = new ArrayList<>();
if (getName().length() > Constants.MAX_ID_LENGTH) {
errors.add(new ProjectElementException(getCompletePath(), "name", "Entity name is too long."));
}
for (final Module module : getProject().getModulesAndApplication()) {
final Module thisModule = getModule();
if (module == thisModule)
break;
final Entity duplicate = module.getEntity(getName());
if (duplicate != null) {
errors.add(new ProjectElementException(getCompletePath(), "name", "Entity with name '" + getName() + "' already exists."));
break;
}
}
final TableDef tableDef = findTableDefinition();
if (tableDef != null) {
errors.addAll(tableDef.getErrors());
}
for (final Query query : getQueries()) {
errors.addAll(query.getErrors());
}
for (final Operation operation : getOperations()) {
errors.addAll(operation.getErrors());
}
return errors;
}
use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.
the class OracleSchemaReader method readColumns.
@Override
public Map<String, List<SqlColumnInfo>> readColumns(SqlExecutor sql, String defSchema, ProcessController controller) throws SQLException {
DbmsConnector connector = sql.getConnector();
Map<String, List<SqlColumnInfo>> result = new HashMap<>();
ResultSet rs = connector.executeQuery("SELECT " + "c.table_name," + "c.column_name," + "c.data_type," + "c.char_length," + "c.data_precision," + "c.data_scale," + "c.nullable " + "from user_tab_cols c" + " JOIN entities e ON (UPPER(e.name)=c.table_name)" + " WHERE NOT(c.column_id IS NULL) ORDER BY c.table_name,c.column_id");
try {
while (rs.next()) {
String tableName = rs.getString(1).toLowerCase();
List<SqlColumnInfo> list = result.get(tableName);
if (list == null) {
list = new ArrayList<>();
result.put(tableName, list);
}
SqlColumnInfo info = new SqlColumnInfo();
list.add(info);
info.setName(rs.getString(2));
info.setType(rs.getString(3));
info.setCanBeNull("Y".equals(rs.getString(7)));
info.setSize(rs.getInt(5));
if (rs.wasNull()) {
info.setSize(rs.getInt(4));
}
info.setPrecision(rs.getInt(6));
}
} finally {
connector.close(rs);
}
// Read default values as separate query, because it's LONG column which is streaming and
// transmitted slowly
rs = connector.executeQuery("SELECT " + "c.data_default," + "c.table_name," + "c.column_name " + "from user_tab_cols c" + " JOIN entities e ON (UPPER(e.name)=c.table_name)" + " WHERE NOT(c.column_id IS NULL) AND NOT (data_default IS NULL) ORDER BY c.table_name");
try {
while (rs.next()) {
// Read streaming column at first
String defaultValue = rs.getString(1);
String tableName = rs.getString(2);
String columnName = rs.getString(3);
SqlColumnInfo column = findColumn(result, tableName, columnName);
if (column == null)
continue;
defaultValue = defaultValue.trim();
defaultValue = DEFAULT_DATE_PATTERN.matcher(defaultValue).replaceFirst("'$1'");
if ("'auto-identity'".equals(defaultValue)) {
column.setAutoIncrement(true);
} else {
column.setDefaultValue(defaultValue);
}
}
} finally {
connector.close(rs);
}
/*rs = connector.executeQuery( "SELECT uc.SEARCH_CONDITION,uc.TABLE_NAME FROM user_constraints uc "
+ " JOIN entities e ON (UPPER(e.name)=uc.table_name)"
+ " WHERE uc.CONSTRAINT_TYPE = 'C'" );*/
// The following query works faster (much faster!) as it doesn't return "NOT NULL" constraints
// though it's probably Oracle version specific (at least undocumented)
// tested on Oracle 11r2
rs = connector.executeQuery("SELECT c.condition,o.name " + "FROM sys.cdef$ c, sys.\"_CURRENT_EDITION_OBJ\" o,entities e " + "WHERE c.type#=1 AND c.obj# = o.obj# " + "AND o.owner# = userenv('SCHEMAID') " + "AND UPPER(e.name)=o.name");
try {
while (rs.next()) {
String constr = rs.getString(1);
String table = rs.getString(2);
// ENUM VALUES
// Copied from OperationSupport.loadEntityEnums
StringTokenizer st = new StringTokenizer(constr.trim());
int nTok = st.countTokens();
if (nTok < 3) {
continue;
}
String colName = st.nextToken().toUpperCase();
String in = st.nextToken();
if (!"IN".equalsIgnoreCase(in)) {
continue;
}
SqlColumnInfo column = findColumn(result, table, colName);
if (column == null) {
continue;
}
List<String> values = new ArrayList<>();
try {
do {
String val = st.nextToken("(,')");
if (!val.trim().isEmpty()) {
values.add(val);
}
} while (st.hasMoreTokens());
} catch (NoSuchElementException ignore) {
}
if (values.size() > 0) {
column.setEnumValues(values.toArray(new String[values.size()]));
}
}
} finally {
connector.close(rs);
}
rs = connector.executeQuery("SELECT trigger_name,table_name,trigger_body FROM user_triggers " + "WHERE triggering_event='INSERT OR UPDATE' " + "AND TRIGGER_TYPE='BEFORE EACH ROW'");
try {
while (rs.next()) {
// Read streaming column as first
String triggerBody = rs.getString(3);
String tableName = rs.getString(2);
Matcher matcher = GENERATED_TRIGGER_PATTERN.matcher(triggerBody);
if (matcher.find()) {
String columnName = matcher.group(1);
String targetName = matcher.group(3);
SqlColumnInfo column = findColumn(result, tableName, columnName);
if (column == null)
continue;
column.setDefaultValue(new ColumnFunction(targetName, ColumnFunction.TRANSFORM_GENERIC).toString());
}
}
} finally {
connector.close(rs);
}
return result;
}
Aggregations