protected List<OType> findAvailableOTypes(OType type) { if(type==null) return WHOLE_LIST; else { List<OType> ret = CACHE_ORDERED.get(type); if(ret==null) { List<OType> candidates = new ArrayList<>(Arrays.asList(OType.values())); candidates.removeIf(candidate->!candidate.getCastable().contains(type)); ret = orderTypes(candidates); CACHE_ORDERED.put(type, ret); } return ret; } }
public void checkPersistentPropertyType(final ODatabaseInternal<ORecord> database, final String propertyName, final OType type) { if (OType.ANY.equals(type)) { return; } final boolean strictSQL = database.getStorage().getConfiguration().isStrictSql(); final StringBuilder builder = new StringBuilder(256); builder.append("select count(*) as count from "); builder.append(getEscapedName(name, strictSQL)); builder.append(" where "); builder.append(getEscapedName(propertyName, strictSQL)); builder.append(".type() not in ["); final Iterator<OType> cur = type.getCastable().iterator(); while (cur.hasNext()) { builder.append('"').append(cur.next().name()).append('"'); if (cur.hasNext()) builder.append(","); } builder.append("] and ").append(getEscapedName(propertyName, strictSQL)).append(" is not null "); if (type.isMultiValue()) builder.append(" and ").append(getEscapedName(propertyName, strictSQL)).append(".size() <> 0 limit 1"); try (final OResultSet res = database.command(builder.toString())) { if (((Long) res.next().getProperty("count")) > 0) throw new OSchemaException("The database contains some schema-less data in the property '" + name + "." + propertyName + "' that is not compatible with the type " + type + ". Fix those records and change the schema again"); } }
/** * Change the type. It checks for compatibility between the change of type. * * @param iType */ protected void setTypeInternal(final OType iType) { getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE); acquireSchemaWriteLock(); try { if (iType == globalRef.getType()) // NO CHANGES return; if (!iType.getCastable().contains(globalRef.getType())) throw new IllegalArgumentException("Cannot change property type from " + globalRef.getType() + " to " + iType); this.globalRef = owner.owner.findOrCreateGlobalProperty(this.globalRef.getName(), iType); } finally { releaseSchemaWriteLock(); } }