opensampl.load.table_factory
Database table factory for handling CRUD operations with conflict resolution.
TableFactory
Factory class for handling database table operations with conflict resolution.
Source code in opensampl/load/table_factory.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
__init__(name, session)
Initialize Table Factory Object for db table matching given name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the database table. |
required |
session
|
Session
|
SQLAlchemy database session. |
required |
Source code in opensampl/load/table_factory.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
create_col_filter(data, cols)
Create a SQLAlchemy filter expression for the given columns and data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict[str, Any]
|
Dictionary containing the data values. |
required |
cols
|
list[str]
|
List of column names to create filter for. |
required |
Returns:
Type | Description |
---|---|
SQLAlchemy filter expression or None if columns are missing. |
Source code in opensampl/load/table_factory.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
extract_unique_constraints()
Identify unique constraints that can be used to match existing entries.
Returns:
Type | Description |
---|---|
Tuple containing identifiable constraint and list of unique constraints. |
Source code in opensampl/load/table_factory.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
find_by_field(column_name, data)
Get the entries where column = data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column_name
|
str
|
Name of the column to filter by. |
required |
data
|
Any
|
Value to match against. |
required |
Returns:
Type | Description |
---|---|
List of model instances matching the criteria. |
Raises:
Type | Description |
---|---|
ValueError
|
If column name does not exist in the table. |
Source code in opensampl/load/table_factory.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
find_existing(data)
Find an existing record that matches the provided data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict[str, Any]
|
Dictionary containing the data to match against. |
required |
Returns:
Type | Description |
---|---|
Optional[Base]
|
Existing model instance or None if not found. |
Raises:
Type | Description |
---|---|
ValueError
|
If no identifiable fields are provided. |
Source code in opensampl/load/table_factory.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
print_filter_debug(filter_expr, label)
Print debug information for a filter expression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filter_expr
|
Optional[Union[BinaryExpression, BooleanClauseList]]
|
The SQLAlchemy filter expression. |
required |
label
|
str
|
Label for the debug output. |
required |
Source code in opensampl/load/table_factory.py
92 93 94 95 96 97 98 99 100 101 102 103 |
|
resolve_table()
Retrieve the SQLAlchemy model class for the given table name.
Returns:
Type | Description |
---|---|
The corresponding SQLAlchemy model class. |
Raises:
Type | Description |
---|---|
ValueError
|
If table name is not found in metadata. |
Source code in opensampl/load/table_factory.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
write(data, if_exists='update')
Write data to the table that the factory refers to.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict[str, Any]
|
The data to write to the table. |
required |
if_exists
|
conflict_actions
|
How to handle conflicts with existing entries. One of: - 'update': Only update fields that are provided and non-default (default) - 'error': Raise an error if entry exists - 'replace': Replace all non-primary-key fields with new values - 'ignore': Skip if entry exists |
'update'
|
Returns:
Type | Description |
---|---|
The created or updated model instance. |
Raises:
Type | Description |
---|---|
ValueError
|
If entry exists and if_exists is 'error'. |
Source code in opensampl/load/table_factory.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|