Model¶
API Status
API documentation is auto-generated from docstrings. Ensure docstrings are comprehensive.
Overview¶
[Module purpose and key concepts]
Reference¶
model
¶
This module provides the class Model.
The class Model is responsible for the connection to the PostgreSQL database.
Once connected, you can use the
get_relation_class <#half_orm.model.Model.get_relation_class>
_
method to generate a class to access any relation (table/view) in your database.
Example
from half_orm.model import Model model = Model('my_config_file') class MyTable(model.get_relation_class('my_schema.my_table')): ... # Your business code goes here
Note
The default schema is public
in PostgreSQL, so to reference a table
my_table
in this schema you'll have to use pubic.my_table
.
Classes¶
Model
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_file
|
str
|
the configuration file that contains the informations to connect to the database. |
required |
scope
|
Optional[str]
|
used to agregate several modules in a package.
See |
None
|
Note
The config_file is searched in the HALFORM_CONF_DIR
variable if specified,
then in /etc/half_orm
. The file format is as follows:
| [database]
| name = <postgres db name>
| user = <postgres user>
| password = <postgres password>
| host = <host name | localhost>
| port = <port | 5432>
name is the only mandatory entry if you are using an
ident login with a local account <https://www.postgresql.org/docs/current/auth-ident.html>
_.
Source code in half_orm/model.py
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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
|
Functions¶
__connect(config_file=None, reload=False)
¶
Setup a new connection to the database.
The reconnect method is an alias to the __connect
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_file
|
str
|
If a config_file is provided, the connection is made with the new parameters, allowing to change role. The database name must be the same. |
None
|
reload
|
bool
|
If set to True, reloads the metadata from the database. Usefull if the model has changed. |
False
|
Source code in half_orm/model.py
__init__(config_file, scope=None)
¶
Model constructor
Use @config_file in your scripts. The @dbname parameter is reserved to the __factory metaclass.
Source code in half_orm/model.py
__load_config(config_file)
¶
Load the config file
Raises:
Type | Description |
---|---|
MissingConfigFile
|
If the config_file is not found in HALFORM_CONF_DIR. |
MalformedConfigFile
|
if the name is missing in the config_file. |
RuntimeError
|
If the reconnection is attempted on another database. |
Source code in half_orm/model.py
call_procedure(proc_name, *args, **kwargs)
¶
Executes a PostgreSQL procedure <https://www.postgresql.org/docs/current/sql-call.html>
_.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
The list of parameters to be passed to the postgresql function. |
()
|
|
**kwargs
|
The list of named parameters to be passed to the postgresql function. |
{}
|
Returns:
Type | Description |
---|---|
None | List[tuple]: None or a list of tuples. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If you mix ***args** and ****kwargs**. |
Note
You can't mix args and kwargs with the call_procedure method!
Source code in half_orm/model.py
classes()
¶
Returns the all the classes of the model
Source code in half_orm/model.py
desc()
¶
Returns the list of the relations of the model.
Each element in the list contains:
- the relation type: 'r' relation, 'v' view, 'm' materialized view, 'p' partition;
- a tuple identifying the relation: (db name>,
, ); - a list of tuples indentifying the inherited relations.
Example
from half_orm.model import Model halftest = Model('halftest') halftest.desc() [('r', ('halftest', 'actor', 'person'), []), ('r', ('halftest', 'blog', 'comment'), []), ('r', ('halftest', 'blog', 'event'), [('halftest', 'blog', 'post')]), ('r', ('halftest', 'blog', 'post'), []), ('v', ('halftest', 'blog.view', 'post_comment'), [])]
Source code in half_orm/model.py
disconnect()
¶
execute_function(fct_name, *args, **kwargs)
¶
Executes a PostgreSQL function <https://www.postgresql.org/docs/current/sql-syntax-calling-funcs.html>
_.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
The list of parameters to be passed to the postgresql function. |
()
|
|
**kwargs
|
The list of named parameters to be passed to the postgresql function. |
{}
|
Returns:
Type | Description |
---|---|
List[tuple]
|
List[tuple]: a list of tuples. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If you mix ***args** and ****kwargs**. |
Note
You can't mix args and kwargs with the execute_function method!
Source code in half_orm/model.py
execute_query(query, values=(), mogrify=False)
¶
Executes a raw SQL query.
Warning
This method calls the psycopg2
cursor.execute <https://www.psycopg.org/docs/cursor.html?highlight=execute#cursor.execute>
_
function.
Please read the psycopg2 documentation on
passing parameters to SQL queries <https://www.psycopg.org/docs/usage.html#query-parameters>
_.
Source code in half_orm/model.py
get_relation_class(relation_name, fields_aliases=None)
¶
This method is a factory that generates a class that inherits the Relation <#half_orm.relation.Relation>
_ class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
relation_name
|
string
|
the full name ( |
required |
Raises:
Type | Description |
---|---|
ValueError
|
if the schema is missing in relation_name |
UnknownRelationError
|
if the relation is not found in the database |
Returns:
Type | Description |
---|---|
a class that inherits the |
Examples:
A class inheriting the Relation <#half_orm.relation.Relation>
_ class is returned:
>>> Person = model.get_relation_class('actor.person')
>>> Person
A prefered way to create a class: >>> class Person(model.get_relation_class('actor.person)): >>> # Your code goes here
A MissingSchemaInName <#half_orm.model_errors.MissingSchemaInName>
_ is raised when the schema name is missing:
>>> model.get_relation_class('person')
[...]MissingSchemaInName: do you mean 'public.person'?
An UnknownRelation <#half_orm.model_errors.UnknownRelation>
_ is raised if the relation is not found in the model:
>>> model.get_relation_class('public.person')
[...]UnknownRelation: 'public.person' does not exist in the database halftest.
Source code in half_orm/model.py
has_relation(qtn)
¶
Checks if the qtn is a relation in the database
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the relation exists in the database, False otherwise. |
Example
model.has_relation('public.person') False model.has_relation('actor.person') True
Source code in half_orm/model.py
ping()
¶
Checks if the connection is still established. Attempts a new connection otherwise.
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the connection is established, False otherwise. |