Skip to content

Commit

Permalink
chore: reduced use of toElement especially in jdbc module
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Jan 3, 2025
1 parent d6e5c04 commit 40458ce
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private OResult mapResult(OResult result, OCommandContext ctx) {
prevValue.setProperty(
"@class", ODocumentInternal.getImmutableSchemaClass(((ODocument) rec)).getName());
}
if (!result.toElement().getIdentity().isNew()) {
if (!result.getIdentity().map((i) -> i.isNew()).orElse(false)) {
for (String propName : result.getPropertyNames()) {
prevValue.setProperty(
propName, OLiveQueryHookV2.unboxRidbags(result.getProperty(propName)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.orientechnologies.orient.core.db.record.ORecordLazyList;
import com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.OBlob;
import com.orientechnologies.orient.core.record.impl.ODocument;
Expand Down Expand Up @@ -311,9 +312,8 @@ public Array getArray(String columnLabel) throws SQLException {

OType columnType =
result
.toElement()
.getSchemaType()
.map(t -> t.getProperty(columnLabel).getType())
.getElement()
.flatMap((e) -> e.getSchemaType().map(t -> t.getProperty(columnLabel).getType()))
.orElse(OType.EMBEDDEDLIST);

assert columnType.isEmbedded() && columnType.isMultiValue();
Expand Down Expand Up @@ -446,7 +446,7 @@ public boolean getBoolean(String columnLabel) throws SQLException {
"An error occurred during the retrieval of the boolean value at column '"
+ columnLabel
+ "' ---> "
+ result.toElement().toJSON(),
+ result.toJSON(),
e);
}
}
Expand Down Expand Up @@ -634,7 +634,7 @@ public int getInt(int columnIndex) throws SQLException {
}

public int getInt(String columnLabel) throws SQLException {
if ("@version".equals(columnLabel)) return result.toElement().getVersion();
if ("@version".equals(columnLabel)) return result.getProperty("@version");

try {
final Integer r = result.getProperty(columnLabel);
Expand Down Expand Up @@ -719,9 +719,14 @@ public Object getObject(String columnLabel) throws SQLException {
}

if ("@class".equals(columnLabel) || "class".equals(columnLabel)) {
String r = result.toElement().getSchemaType().map(t -> t.getName()).orElse(null);
lastReadWasNull = r == null;
return r;
Object cl = result.getProperty("@class");
if (cl != null) {
lastReadWasNull = false;
return cl.toString();
} else {
lastReadWasNull = true;
return null;
}
}

try {
Expand Down Expand Up @@ -777,7 +782,8 @@ public int getRow() throws SQLException {
public RowId getRowId(final int columnIndex) throws SQLException {
try {
lastReadWasNull = false;
return new OrientRowId(result.toElement().getIdentity());
return new OrientRowId(
result.getElement().map((e) -> e.getIdentity()).orElse(new ORecordId()));
} catch (Exception e) {
throw new SQLException(
"An error occurred during the retrieval of the rowid for record '" + result + "'", e);
Expand Down Expand Up @@ -828,12 +834,22 @@ public String getString(String columnLabel) throws SQLException {

if ("@rid".equals(columnLabel) || "rid".equals(columnLabel)) {
lastReadWasNull = false;
return result.toElement().getIdentity().toString();
Object id = result.getProperty("@rid");
if (id != null) {
return id.toString();
} else {
return "";
}
}

if ("@class".equals(columnLabel) || "class".equals(columnLabel)) {
lastReadWasNull = false;
return result.toElement().getSchemaType().map(c -> c.getName()).orElse("NOCLASS");
Object cl = result.getProperty("@class");
if (cl != null) {
return cl.toString();
} else {
return "NOCLASS";
}
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.orientechnologies.orient.jdbc;

import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordLazyList;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
Expand Down Expand Up @@ -122,10 +123,10 @@ public int getColumnType(final int column) throws SQLException {

OType otype =
currentRecord
.toElement()
.getSchemaType()
.map(st -> st.getProperty(fieldName))
.map(op -> op.getType())
.getElement()
.flatMap(
(e) ->
e.getSchemaType().map(st -> st.getProperty(fieldName)).map(op -> op.getType()))
.orElse(null);

if (otype == null) {
Expand Down Expand Up @@ -218,11 +219,13 @@ public String getColumnTypeName(final int column) throws SQLException {
String columnLabel = fieldNames[column - 1];

return currentRecord
.toElement()
.getSchemaType()
.map(st -> st.getProperty(columnLabel))
.map(p -> p.getType())
.map(t -> t.toString())
.getElement()
.flatMap(
(e) ->
e.getSchemaType()
.map(st -> st.getProperty(columnLabel))
.map(p -> p.getType())
.map(t -> t.toString()))
.orElse(null);
}

Expand All @@ -235,9 +238,7 @@ public int getScale(final int column) throws SQLException {
}

public String getSchemaName(final int column) throws SQLException {
final OResult currentRecord = getCurrentRecord();
if (currentRecord == null) return "";
else return currentRecord.toElement().getDatabase().getName();
return ODatabaseRecordThreadLocal.instance().get().getName();
}

public String getTableName(final int column) throws SQLException {
Expand Down Expand Up @@ -281,9 +282,10 @@ public boolean isSigned(final int column) throws SQLException {
final OResult currentRecord = getCurrentRecord();
OType otype =
currentRecord
.toElement()
.getSchemaType()
.map(st -> st.getProperty(fieldNames[column - 1]).getType())
.getElement()
.flatMap(
(e) ->
e.getSchemaType().map(st -> st.getProperty(fieldNames[column - 1]).getType()))
.orElse(null);

return this.isANumericColumn(otype);
Expand Down Expand Up @@ -315,9 +317,8 @@ protected OProperty getProperty(final int column) throws SQLException {
String fieldName = getColumnName(column);

return getCurrentRecord()
.toElement()
.getSchemaType()
.map(st -> st.getProperty(fieldName))
.getElement()
.flatMap((e) -> e.getSchemaType().map(st -> st.getProperty(fieldName)))
.orElse(null);
}
}

0 comments on commit 40458ce

Please sign in to comment.