Skip to content

Commit

Permalink
Added parent_entity type
Browse files Browse the repository at this point in the history
  • Loading branch information
boskoramen committed Sep 22, 2024
1 parent 878914b commit 2c90a32
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 47 deletions.
64 changes: 24 additions & 40 deletions code/__DEFINES/ndatabase.dm
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@
*/

#define FIELD_INT(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_INT; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_INT; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_INT(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_INT; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_INT; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -175,14 +175,14 @@
}

#define FIELD_BIGINT(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_BIGINT; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_BIGINT; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_BIGINT(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_BIGINT; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_BIGINT; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -191,14 +191,14 @@
}

#define FIELD_CHAR(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_CHAR; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_CHAR; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_CHAR(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_CHAR; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_CHAR; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -207,14 +207,14 @@
}

#define FIELD_STRING_SMALL(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_SMALL; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_SMALL; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_STRING_SMALL(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_SMALL; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_SMALL; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -223,14 +223,14 @@
}

#define FIELD_STRING_MEDIUM(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_MEDIUM; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_MEDIUM; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_STRING_MEDIUM(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_MEDIUM; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_MEDIUM; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -239,14 +239,14 @@
}

#define FIELD_STRING_LARGE(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_LARGE; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_LARGE; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_STRING_LARGE(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_LARGE; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_LARGE; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -255,14 +255,14 @@
}

#define FIELD_STRING_MAX(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_MAX; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_MAX; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_STRING_MAX(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_MAX; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_STRING_MAX; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -271,30 +271,14 @@
}

#define FIELD_DATE(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_DATE; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_DATE; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_DATE(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_DATE; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}

#define FIELD_DATE(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_DATE; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_DATE(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_DATE; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_DATE; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -303,14 +287,14 @@
}

#define FIELD_TEXT(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_TEXT; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_TEXT; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_TEXT(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_TEXT; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_TEXT; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -319,14 +303,14 @@
}

#define FIELD_BLOB(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_BLOB; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_BLOB; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_BLOB(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_BLOB; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_BLOB; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -335,14 +319,14 @@
}

#define FIELD_DECIMAL(entity, field) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_DECIMAL; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_DECIMAL; parent_entity_type = /datum/entity/##entity; }; \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
..(); \
LAZYSET(field_types, #field, /datum/db_field/##entity/##field); \
}
#define FIELD_DEFAULT_VALUE_DECIMAL(entity, field, default) \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_DECIMAL; }; \
/datum/db_field/##entity/##field{ name = #field; field_type = DB_FIELDTYPE_DECIMAL; parent_entity_type = /datum/entity/##entity; }; \
/datum/db_field/##entity/##field/New() { . = ..(); value = default; } \
/datum/entity/##entity/var/datum/db_field/##entity/##field/##field; \
/datum/entity_meta/##entity/setup_field_types() { \
Expand All @@ -351,9 +335,9 @@
}

/// Defines an entity link where `child_entity` refers to `parent_entity` through its column `foreign_key`.
/// `foreign_key` should NOT be a string but instead the actual foreign key field name.
/// `foreign_key` should NOT be a string but instead the actual foreign key field type.
#define DEFINE_ENTITY_LINK(parent_entity, child_entity, foreign_key) \
/datum/controller/subsytem/entity_manager/get_entity_links(list/datum/entity_link/entity_links) { \
..(); \
entity_links += new /datum/entity_link(parent_entity, child_entity, #foreign_key); \
entity_links += new /datum/entity_link(parent_entity, child_entity, foreign_key); \
}
6 changes: 4 additions & 2 deletions code/datums/_ndatabase/code/entity/db_field.dm
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/datum/db_field

Check failure on line 1 in code/datums/_ndatabase/code/entity/db_field.dm

View workflow job for this annotation

GitHub Actions / Run Linters

DME Validator

File should not be included
/// The type on the DB, should be a subtype of /datum/db_field_type (see: `__DEFINES/ndatabase.dm`).
var/field_type
/// The name of the field on the DB, should only contain lowercase letters and underscores.
var/name
/// The type on the DB, should be a subtype of /datum/db_field_type (see: `__DEFINES/ndatabase.dm`).
var/field_type
/// The actual value of the field (not necessarily persisted to DB)
var/value
/// The typepath of the entity datum that holds the field
var/parent_entity_type
3 changes: 2 additions & 1 deletion code/datums/_ndatabase/code/entity/link.dm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
var/parent_entity
/// Entity that is referencing a different entity
var/child_entity
/// Column on child entity to reference parent entity
/// Column on child entity to reference parent entity.
/// A typepath to the db_field datum that represents the column
var/child_foreign_key

var/datum/entity_meta/parent_meta
Expand Down
6 changes: 2 additions & 4 deletions code/datums/_ndatabase/subsystems/entity_manager.dm
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ GLOBAL_REAL(SSentity_manager, /datum/controller/subsystem/entity_manager)
for(var/datum/entity_link/link as anything in entity_links)
var/datum/entity_meta/parent = tables[link.parent_entity]
var/datum/entity_meta/child = tables[link.child_entity]
if(link.child_name)
parent.inbound_links[link.child_entity] = link
if(link.parent_name)
child.outbound_links[link.parent_entity] = link
parent.inbound_links[link.child_entity] = link
child.outbound_links[link.parent_entity] = link
link.parent_meta = parent
link.child_meta = child

Expand Down

0 comments on commit 2c90a32

Please sign in to comment.