Skip to content

Commit

Permalink
feat: first definition of create class if not exists method
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Feb 24, 2025
1 parent da97d90 commit 588160f
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.metadata.security.ORule;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
Expand Down Expand Up @@ -219,6 +220,56 @@ public OClass createClass(
return result;
}

@Override
public boolean createClassIfNotExists(ODatabaseDocumentInternal database, String className) {
return createClassIfNotExists(database, className, new OClass[] {});
}

@Override
public boolean createClassIfNotExists(
ODatabaseDocumentInternal database, String className, OClass... superClasses) {
final Character wrongCharacter = OSchemaShared.checkClassNameIfValid(className);
if (wrongCharacter != null)
throw new OSchemaException(
"Invalid class name found. Character '"
+ wrongCharacter
+ "' cannot be used in class name '"
+ className
+ "'");

database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_CREATE);
acquireSchemaWriteLock(database);
try {
StringBuilder cmd = new StringBuilder("create class ");
cmd.append('`');
cmd.append(className);
cmd.append('`');

List<OClass> superClassesList = new ArrayList<OClass>();
if (superClasses != null && superClasses.length > 0) {
boolean first = true;
for (OClass superClass : superClasses) {
// Filtering for null
if (superClass != null) {
if (first) cmd.append(" extends ");
else cmd.append(", ");
cmd.append(superClass.getName());
first = false;
superClassesList.add(superClass);
}
}
}

OResultSet queryResult = database.command(cmd.toString());
boolean created = queryResult.hasNext();
queryResult.close();
reload(database);
return created;
} finally {
releaseSchemaWriteLock(database);
}
}

public OView createView(
ODatabaseDocumentInternal database, OViewConfig cfg, ViewCreationListener listener)
throws UnsupportedOperationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ public OClass createClass(String className, int[] clusterIds, OClass... superCla
throw new UnsupportedOperationException();
}

@Override
public boolean createClassIfNotExists(String className) {
throw new UnsupportedOperationException();
}

@Override
public boolean createClassIfNotExists(String iClassName, OClass... superClasses) {
throw new UnsupportedOperationException();
}

@Override
public OClass createAbstractClass(String iClassName) {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public interface OSchema {

OClass createClass(String className, int[] clusterIds, OClass... superClasses);

boolean createClassIfNotExists(String className);

boolean createClassIfNotExists(String iClassName, OClass... superClasses);

OClass createAbstractClass(String iClassName);

OClass createAbstractClass(String iClassName, OClass iSuperClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ public OClass createClass(
return doCreateClass(database, className, clusters, superClasses);
}

@Override
public boolean createClassIfNotExists(ODatabaseDocumentInternal session, String className) {
try {
acquireSchemaWriteLock(session);
if (existsClass(className)) {
return false;
} else {
createClass(session, className);
return true;
}
} finally {
releaseSchemaWriteLock(session);
}
}

public boolean createClassIfNotExists(
ODatabaseDocumentInternal session, String className, OClass... superclasses) {
try {
acquireSchemaWriteLock(session);
if (existsClass(className)) {
return false;
} else {
createClass(session, className, superclasses);
return true;
}
} finally {
releaseSchemaWriteLock(session);
}
}

private OClass doCreateClass(
ODatabaseDocumentInternal database,
final String className,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ public OClass createClass(String className, int clusters, OClass... superClasses
return delegate.createClass(database, className, clusters, superClasses);
}

@Override
public boolean createClassIfNotExists(String className) {
return delegate.createClassIfNotExists(database, className);
}

@Override
public boolean createClassIfNotExists(String className, OClass... superClasses) {
return delegate.createClassIfNotExists(database, className, superClasses);
}

@Override
public OClass getClassByClusterId(int clusterId) {
return delegate.getClassByClusterId(clusterId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ public abstract OView createView(
String statement,
Map<String, Object> metadata);

public abstract boolean createClassIfNotExists(
ODatabaseDocumentInternal session, String className);

public abstract boolean createClassIfNotExists(
ODatabaseDocumentInternal session, String className, OClass... superclasses);

public abstract OView createView(ODatabaseDocumentInternal database, OViewConfig cfg);

public abstract OView createView(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.orientechnologies.orient.core.metadata.schema;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.orientechnologies.BaseMemoryDatabase;
import org.junit.Test;

public class CreateClassTest extends BaseMemoryDatabase {

@Test
public void testCreateClassSQL() {
db.command("drop class V").close();
db.command("create class V clusters 16").close();
db.command("create class X extends V clusters 32").close();

final OClass clazzV = db.getMetadata().getSchema().getClass("V");
assertEquals(16, clazzV.getClusterIds().length);

final OClass clazzX = db.getMetadata().getSchema().getClass("X");
assertEquals(32, clazzX.getClusterIds().length);
}

@Test
public void testCreateClassSQLSpecifiedClusters() {
int s = db.addCluster("second");
int t = db.addCluster("third");
db.command("drop class V").close();
db.command("create class V cluster " + s + "," + t).close();

final OClass clazzV = db.getMetadata().getSchema().getClass("V");
assertEquals(2, clazzV.getClusterIds().length);

assertEquals(s, clazzV.getClusterIds()[0]);
assertEquals(t, clazzV.getClusterIds()[1]);
}

@Test
public void testCreateClassIfNotExists() {
OSchema schema = db.getMetadata().getSchema();
schema.createClass("TestCreateClass");
boolean result = schema.createClassIfNotExists("TestCreateClassNotExists");
assertTrue(result);
assertNotNull(schema.getClass("TestCreateClassNotExists"));

result = schema.createClassIfNotExists("TestCreateClass");
assertFalse(result);
}

@Test
public void testCreateClassIfNotExistsSuperclass() {
OSchema schema = db.getMetadata().getSchema();
schema.createClass("TestCreateClass");
boolean result =
schema.createClassIfNotExists("TestCreateClassNotExists", schema.getClass("V"));
assertTrue(result);
assertNotNull(schema.getClass("TestCreateClassNotExists"));
assertEquals(1, schema.getClass("TestCreateClassNotExists").getSuperClasses().size());

result = schema.createClassIfNotExists("TestCreateClass");
assertFalse(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ public OClass createClass(String className, int clusters, OClass... superClasses
return underlying.createClass(className, clusters, superClasses);
}

@Override
public boolean createClassIfNotExists(String className) {
return underlying.createClassIfNotExists(className);
}

@Override
public boolean createClassIfNotExists(String className, OClass... superClasses) {
return underlying.createClassIfNotExists(className, superClasses);
}

public OSchema getUnderlying() {
return underlying;
}
Expand Down

0 comments on commit 588160f

Please sign in to comment.