Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.dirigible.components.base.helpers.JsonHelper;
import org.eclipse.dirigible.components.data.store.DataStore;
import org.eclipse.dirigible.components.data.store.model.EntityFieldMetadata;
import org.eclipse.dirigible.components.data.store.model.EntityMetadata;
import org.eclipse.dirigible.components.data.store.parser.EntityParser;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.gson.JsonElement;

/**
Expand Down Expand Up @@ -123,6 +122,7 @@ private static String listAll(String name) {
List list = DataStoreFacade.get()
.getDataStore()
.list(name);
convertBlob(name, list);
return JsonHelper.toJson(list);
}

Expand All @@ -138,6 +138,7 @@ public static String list(String name, String options) {
List list = DataStoreFacade.get()
.getDataStore()
.list(name, options);
convertBlob(name, list);
return JsonHelper.toJson(list);
} else {
return listAll(name);
Expand Down Expand Up @@ -177,6 +178,7 @@ public static String find(String name, String example, int limit, int offset) {
List list = DataStoreFacade.get()
.getDataStore()
.findByExample(name, example, limit, offset);
convertBlob(name, list);
return JsonHelper.toJson(list);
}

Expand Down Expand Up @@ -263,6 +265,7 @@ public static String get(String name, Serializable id) {
Map object = DataStoreFacade.get()
.getDataStore()
.get(name, id);
convertBlob(name, object);
return JsonHelper.toJson(object);
}

Expand Down Expand Up @@ -325,4 +328,61 @@ public static String getIdColumn(String name) {
.getColumnName();
}

private static void convertBlob(String entityName, List<Map> data) {
EntityMetadata metadata = EntityParser.ENTITIES.get(entityName);

if (metadata == null) {
return;
}

for (EntityFieldMetadata field : metadata.getFields()) {
if (field.getColumnDetails()
.getDatabaseType()
.toLowerCase()
.contains("blob")) {
for (Map next : data) {
String propertyName = field.getPropertyName();
Object value = next.get(propertyName);
if (value instanceof java.sql.Blob) {
try {
java.sql.Blob blob = (java.sql.Blob) value;
byte[] bytes = blob.getBytes(1, (int) blob.length());
next.put(propertyName, bytes);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}
}

private static void convertBlob(String entityName, Map data) {
EntityMetadata metadata = EntityParser.ENTITIES.get(entityName);

if (metadata == null) {
return;
}

for (EntityFieldMetadata field : metadata.getFields()) {
if (field.getColumnDetails()
.getDatabaseType()
.toLowerCase()
.contains("blob")) {
String propertyName = field.getPropertyName();
Object value = data.get(propertyName);
if (value instanceof java.sql.Blob) {
try {
java.sql.Blob blob = (java.sql.Blob) value;
byte[] bytes = blob.getBytes(1, (int) blob.length());
data.put(propertyName, bytes);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,29 @@ type ClassDecoratorContext = {
addInitializer(fn: () => void): void;
};

export type ColumnTypes =
// Numeric types
| 'integer' | 'long' | 'short' | 'byte' | 'float' | 'double' | 'big_integer' | 'big_decimal'
// String types
| 'string' | 'char' | 'text' | 'nstring' | 'ntext'
// Date/Time types
| 'date' | 'time' | 'timestamp' | 'calendar' | 'calendar_date' | 'instant'
// Boolean types
| 'boolean' | 'true_false' | 'yes_no' | 'numeric_boolean'
// Binary types
| 'binary' | 'blob' | 'clob' | 'materialized_blob' | 'materialized_clob'
// Other types
| 'serializable' | 'any' | 'object' | 'uuid-char' | 'uuid-binary' | 'json' | 'jsonb' | 'xml';

// --- Metadata Models ---
export interface ColumnOptions {
name?: string;
type?: string;
type?: ColumnTypes | (string & {});
length?: number;
nullable?: boolean;
defaultValue?: string;
precision?: number;
scale?: number;
}

export interface OneToManyOptions {
Expand Down Expand Up @@ -61,7 +77,7 @@ interface PropertyMetadata {
}

export interface EntityConstructor extends Function {
new (...args: any[]): any;
new(...args: any[]): any;
$entity_name: string;
$table_name: string;
$id_name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export abstract class Repository<T extends Record<string, any>> {
// Caches entity metadata (name, table, id) onto the constructor function for static access
if (!this.entityConstructor.$entity_name) {
// Assumes store methods return non-null strings
this.entityConstructor.$entity_name = (store as any).getEntityName(this.entityConstructor.name);
this.entityConstructor.$table_name = (store as any).getTableName(this.entityConstructor.name);
this.entityConstructor.$id_name = (store as any).getIdName(this.entityConstructor.name);
this.entityConstructor.$id_column = (store as any).getIdColumn(this.entityConstructor.name);
this.entityConstructor.$entity_name = store.getEntityName(this.entityConstructor.name);
this.entityConstructor.$table_name = store.getTableName(this.entityConstructor.name);
this.entityConstructor.$id_name = store.getIdName(this.entityConstructor.name);
this.entityConstructor.$id_column = store.getIdColumn(this.entityConstructor.name);
}
}

Expand All @@ -62,8 +62,8 @@ export abstract class Repository<T extends Record<string, any>> {
*/
public findAll(options: Options = {}): T[] {
// Assume store.list returns T[] but we explicitly cast it to T[]
const list: T[] = (store as any).list(this.getEntityName(), options);
(translator as any).translateList(list, options.language, this.getTableName());
const list: T[] = store.list(this.getEntityName(), options);
translator.translateList(list, options.language, this.getTableName());
return list;
}

Expand All @@ -72,8 +72,8 @@ export abstract class Repository<T extends Record<string, any>> {
*/
public findById(id: number | string, options: Options = {}): T | undefined {
// Assume store.get returns T or null/undefined
const entity: T | null = (store as any).get(this.getEntityName(), id);
(translator as any).translateEntity(entity, id, options.language, this.getTableName());
const entity: T | null = store.get(this.getEntityName(), id);
translator.translateEntity(entity, id, options.language, this.getTableName());
return entity ?? undefined;
}

Expand All @@ -82,7 +82,7 @@ export abstract class Repository<T extends Record<string, any>> {
* @returns The generated ID (string or number).
*/
public create(entity: T): string | number {
const id = (store as any).save(this.getEntityName(), entity);
const id = store.save(this.getEntityName(), entity);
this.triggerEvent({
operation: "create",
table: this.getTableName(),
Expand All @@ -107,7 +107,7 @@ export abstract class Repository<T extends Record<string, any>> {
// Retrieve the entity state before update for the event payload
const previousEntity = this.findById(id);

(store as any).update(this.getEntityName(), entity);
store.update(this.getEntityName(), entity);

this.triggerEvent({
operation: "update",
Expand All @@ -132,18 +132,18 @@ export abstract class Repository<T extends Record<string, any>> {

// If no ID is present, save (create)
if (id === null || id === undefined) {
return (store as any).save(this.getEntityName(), entity);
return store.save(this.getEntityName(), entity);
}

// If ID is present, check existence
const existingEntity = (store as any).get(this.getEntityName(), id);
const existingEntity = store.get(this.getEntityName(), id);

if (existingEntity) {
this.update(entity);
return id;
} else {
// ID exists, but entity does not -> save (create with provided ID)
return (store as any).save(this.getEntityName(), entity);
return store.save(this.getEntityName(), entity);
}
}

Expand All @@ -152,9 +152,9 @@ export abstract class Repository<T extends Record<string, any>> {
*/
public deleteById(id: number | string): void {
// Retrieve entity before removal for the event payload
const entity = (store as any).get(this.getEntityName(), id);
const entity = store.get(this.getEntityName(), id);

(store as any).remove(this.getEntityName(), id);
store.remove(this.getEntityName(), id);

this.triggerEvent({
operation: "delete",
Expand All @@ -172,7 +172,7 @@ export abstract class Repository<T extends Record<string, any>> {
* Counts the number of entities matching the given options.
*/
public count(options?: Options): number {
return (store as any).count(this.getEntityName(), options);
return store.count(this.getEntityName(), options);
}

/**
Expand Down
Loading
Loading