Skip to content

Commit

Permalink
fix: supported SQL size() on records #1855 (#1856)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca authored Dec 7, 2024
1 parent a6eaaf7 commit e8314a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
7 changes: 7 additions & 0 deletions engine/src/main/java/com/arcadedb/database/BaseRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public Record getRecord(final boolean loadContent) {
return this;
}

@Override
public int size() {
if (buffer == null)
reload();
return buffer != null ? buffer.size() : -1;
}

@Override
public void reload() {
if (rid != null && buffer == null && database.isOpen()) {
Expand Down
7 changes: 6 additions & 1 deletion engine/src/main/java/com/arcadedb/database/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ public interface Record extends Identifiable {

void delete();

default JSONObject toJSON(){
default JSONObject toJSON() {
return toJSON(true);
}

JSONObject toJSON(boolean includeMetadata);

/**
* Returns the binary record size if known, otherwise -1
*/
int size();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.arcadedb.database.Identifiable;
import com.arcadedb.query.sql.executor.CommandContext;
import com.arcadedb.query.sql.executor.MultiValue;
import com.arcadedb.query.sql.executor.Result;
import com.arcadedb.query.sql.method.AbstractSQLMethod;

/**
Expand All @@ -36,17 +37,20 @@ public SQLMethodSize() {
}

@Override
public Object execute(final Object value, final Identifiable iCurrentRecord, final CommandContext iContext, final Object[] iParams) {
public Object execute(final Object value, final Identifiable iCurrentRecord, final CommandContext iContext,
final Object[] iParams) {

final Number size;
if (value != null) {
if (value instanceof Identifiable) {
size = 1;
} else if (value instanceof String) {
if (value instanceof Result result)
size = result.getRecord().isPresent() ? result.getRecord().get().size() : -1;
else if (value instanceof Identifiable record)
size = record.getRecord(true).size();
else if (value instanceof String)
size = value.toString().length();
} else {
else
size = MultiValue.getSize(value);
}

} else {
size = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ public void queryMax() {
}
}

@Test
public void testSelectSize() {
final ResultSet result = database.query("sql", "select @this.size() as size from Account");
assertThat(result.hasNext()).isTrue();
for (final ResultSet it = result; it.hasNext(); ) {
final Result d = it.next();
assertThat(d.<Integer>getProperty("size")).isNotNull();
assertThat(d.<Integer>getProperty("size")).isGreaterThan(0);
}
}

@Test
public void queryMaxInline() {
final ResultSet result = database.command("sql", "select max(1,2,7,0,-2,3) as max");
Expand Down

0 comments on commit e8314a3

Please sign in to comment.