ucampostgresvro 0.1.2__py3-none-any.whl → 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ucampostgresvro/DBA.py +384 -167
- ucampostgresvro/__init__.py +1 -1
- ucampostgresvro/__main__.py +49 -19
- ucampostgresvro/tests/dbconnect.py +5 -5
- ucampostgresvro/tests/test_DB.py +129 -21
- ucampostgresvro/utils.py +2 -0
- {ucampostgresvro-0.1.2.dist-info → ucampostgresvro-0.2.1.dist-info}/METADATA +97 -37
- ucampostgresvro-0.2.1.dist-info/RECORD +16 -0
- ucampostgresvro-0.1.2.dist-info/RECORD +0 -16
- {ucampostgresvro-0.1.2.dist-info → ucampostgresvro-0.2.1.dist-info}/LICENSE +0 -0
- {ucampostgresvro-0.1.2.dist-info → ucampostgresvro-0.2.1.dist-info}/WHEEL +0 -0
ucampostgresvro/DBA.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import logging
|
2
|
+
from datetime import datetime
|
2
3
|
from typing import Optional
|
3
4
|
|
4
5
|
import psycopg2
|
@@ -33,7 +34,7 @@ class DB:
|
|
33
34
|
|
34
35
|
def insert_vrauser(
|
35
36
|
self, crsid: str, name: str, table_name: str = DEFAULT_TABLES.get("user")
|
36
|
-
) ->
|
37
|
+
) -> int:
|
37
38
|
"""Insertion of the vrauser detail.
|
38
39
|
|
39
40
|
Args:
|
@@ -45,21 +46,21 @@ class DB:
|
|
45
46
|
DbException: Exception for the provided inputs.
|
46
47
|
|
47
48
|
Returns:
|
48
|
-
|
49
|
+
int: Primary key of the user.
|
49
50
|
"""
|
50
51
|
with self.connection:
|
51
52
|
if crsid and name:
|
52
53
|
try:
|
53
54
|
self.cursor.execute(
|
54
|
-
f"INSERT INTO {table_name} (crsid, name) VALUES ('{crsid}', '{name}');"
|
55
|
+
f"INSERT INTO {table_name} (crsid, name) VALUES ('{crsid}', '{name}') RETURNING id;"
|
55
56
|
)
|
56
57
|
LOG.info(
|
57
58
|
f"INFO: {table_name} insersion successful: CRSID {crsid} and Name {name}"
|
58
59
|
)
|
59
|
-
return
|
60
|
+
return self.cursor.fetchone()[0]
|
60
61
|
except Exception as e:
|
61
62
|
LOG.error(f"Error: {table_name} insertion : {e}")
|
62
|
-
|
63
|
+
raise DbException(f"Error: {table_name} insertion fail")
|
63
64
|
else:
|
64
65
|
LOG.error(
|
65
66
|
f"Error: Please provide both crid and name for {table_name} insertion"
|
@@ -150,9 +151,60 @@ class DB:
|
|
150
151
|
LOG.info(f"INFO: {table_name} information is fetched successfully")
|
151
152
|
return self.cursor.fetchall()
|
152
153
|
|
154
|
+
def get_vrauser_by_id(
|
155
|
+
self,
|
156
|
+
user_id: int,
|
157
|
+
table_name: str = DEFAULT_TABLES.get("user"),
|
158
|
+
) -> any:
|
159
|
+
"""Retreive the information from the vrauser table.
|
160
|
+
|
161
|
+
Args:
|
162
|
+
user_id (int): primary key of user to fetch information.
|
163
|
+
table_name (str): table name of the user.
|
164
|
+
|
165
|
+
Raises:
|
166
|
+
DbException: Raise exception in case of retrieving information
|
167
|
+
|
168
|
+
Returns:
|
169
|
+
any: retreive the data from the vrauser database.
|
170
|
+
"""
|
171
|
+
with self.connection:
|
172
|
+
try:
|
173
|
+
self.cursor.execute(
|
174
|
+
f"SELECT * FROM {table_name} where id = '{user_id}';"
|
175
|
+
)
|
176
|
+
LOG.info(f"INFO: {table_name} information is fetched successfully")
|
177
|
+
return self.cursor.fetchone()
|
178
|
+
except Exception as e:
|
179
|
+
LOG.error(
|
180
|
+
f"Error: Unable to fetch user id from table '{table_name}': {e}"
|
181
|
+
)
|
182
|
+
raise DbException(
|
183
|
+
f"Error: Unable to fetch user id from table '{table_name}': {e}"
|
184
|
+
)
|
185
|
+
|
186
|
+
def get_vrauser_primary_key(
|
187
|
+
self,
|
188
|
+
crsid: Optional[str] = None,
|
189
|
+
table_name: str = DEFAULT_TABLES.get("user"),
|
190
|
+
) -> any:
|
191
|
+
"""Retreive the information from the vrauser table.
|
192
|
+
|
193
|
+
Args:
|
194
|
+
crsid (Optional[str], optional): CRSID need to be fetched. Defaults to None.
|
195
|
+
table_name (str): table name of the user.
|
196
|
+
|
197
|
+
Returns:
|
198
|
+
any: retreive the data from the vrauser database.
|
199
|
+
"""
|
200
|
+
with self.connection:
|
201
|
+
self.cursor.execute(f"SELECT * FROM {table_name} where crsid = '{crsid}';")
|
202
|
+
LOG.info(f"INFO: {table_name} information is fetched successfully")
|
203
|
+
return self.cursor.fetchone()[0]
|
204
|
+
|
153
205
|
def insert_deployment_id(
|
154
206
|
self, deployment_id: str, table_name: str = DEFAULT_TABLES.get("deploymentid")
|
155
|
-
) ->
|
207
|
+
) -> int:
|
156
208
|
"""Insertion of the deployment detail.
|
157
209
|
|
158
210
|
Args:
|
@@ -163,21 +215,23 @@ class DB:
|
|
163
215
|
DbException: Exception for the provided inputs.
|
164
216
|
|
165
217
|
Returns:
|
166
|
-
|
218
|
+
int: primary key of the deployment.
|
167
219
|
"""
|
168
220
|
with self.connection:
|
169
221
|
if deployment_id:
|
170
222
|
try:
|
171
223
|
self.cursor.execute(
|
172
|
-
f"INSERT INTO {table_name} (deploymentID) VALUES ('{deployment_id}');"
|
224
|
+
f"INSERT INTO {table_name} (deploymentID) VALUES ('{deployment_id}') RETURNING id;"
|
173
225
|
)
|
174
226
|
LOG.info(
|
175
227
|
f"INFO: deployment ID {deployment_id} inserted successfully"
|
176
228
|
)
|
177
|
-
return
|
229
|
+
return self.cursor.fetchone()[0]
|
178
230
|
except Exception as e:
|
179
231
|
LOG.error(f"Error: deployment ID insertion in {table_name}: {e}")
|
180
|
-
|
232
|
+
raise DbException(
|
233
|
+
f"Error: deployment ID insertion in {table_name}: {e}"
|
234
|
+
)
|
181
235
|
else:
|
182
236
|
LOG.error(
|
183
237
|
f"Error: Please provide deployment ID for {table_name} insertion"
|
@@ -268,43 +322,99 @@ class DB:
|
|
268
322
|
LOG.info("INFO: deployment ID information is fetched successfully")
|
269
323
|
return self.cursor.fetchall()
|
270
324
|
|
325
|
+
def get_deployment_id_by_id(
|
326
|
+
self,
|
327
|
+
deployment_id: int,
|
328
|
+
table_name: str = DEFAULT_TABLES.get("deploymentid"),
|
329
|
+
) -> any:
|
330
|
+
"""Retreive the information from the deployment table.
|
331
|
+
|
332
|
+
Args:
|
333
|
+
deployment_id (int): primary key of the Deployment ID need to be fetched.
|
334
|
+
table_name (str, optional): table name of the deploymentid. Defaults to DEFAULT_TABLES.get("deploymentid").
|
335
|
+
|
336
|
+
Raises:
|
337
|
+
DbException: Raise exception in case of retrieving information
|
338
|
+
|
339
|
+
Returns:
|
340
|
+
any: Retreive the data from the Deployment ID database.
|
341
|
+
"""
|
342
|
+
with self.connection:
|
343
|
+
try:
|
344
|
+
self.cursor.execute(
|
345
|
+
f"SELECT * FROM {table_name} where id = '{deployment_id}';"
|
346
|
+
)
|
347
|
+
LOG.info("INFO: deployment ID information is fetched successfully")
|
348
|
+
return self.cursor.fetchone()
|
349
|
+
except Exception as e:
|
350
|
+
LOG.error(
|
351
|
+
f"Error: Unable to fetch deployment id from table '{table_name}': {e}"
|
352
|
+
)
|
353
|
+
raise DbException(
|
354
|
+
f"Error: Unable to fetch deployment id from table '{table_name}': {e}"
|
355
|
+
)
|
356
|
+
|
357
|
+
def get_deployment_id_primary_key(
|
358
|
+
self,
|
359
|
+
deployment_id: str,
|
360
|
+
table_name: str = DEFAULT_TABLES.get("deploymentid"),
|
361
|
+
) -> any:
|
362
|
+
"""Retreive the primary key from the deployment table.
|
363
|
+
|
364
|
+
Args:
|
365
|
+
deployment_id (str): Deployment ID need to be fetched.
|
366
|
+
table_name (str): table name of the deploymentid.
|
367
|
+
|
368
|
+
Returns:
|
369
|
+
any: Retreive the data from the Deployment ID database.
|
370
|
+
"""
|
371
|
+
with self.connection:
|
372
|
+
if deployment_id:
|
373
|
+
self.cursor.execute(
|
374
|
+
f"SELECT * FROM {table_name} where deploymentID = '{deployment_id}';"
|
375
|
+
)
|
376
|
+
LOG.info("INFO: deployment ID information is fetched successfully")
|
377
|
+
return self.cursor.fetchone()[0]
|
378
|
+
|
271
379
|
def insert_project(
|
272
380
|
self,
|
273
381
|
project_number: str,
|
274
382
|
paid_by: int,
|
275
383
|
amount: float,
|
276
384
|
table_name: str = DEFAULT_TABLES.get("proj"),
|
277
|
-
) ->
|
385
|
+
) -> int:
|
278
386
|
"""Insertion of the project table.
|
279
387
|
|
280
388
|
Args:
|
281
389
|
project_number (str): payment order information.
|
282
390
|
paid_by (int): primary key of the vrauser.
|
283
391
|
amount (float): amount paid for the purchase.
|
284
|
-
table_name (str): table name of the
|
392
|
+
table_name (str): table name of the project.
|
285
393
|
|
286
394
|
Raises:
|
287
395
|
DbException: Exception for the provided inputs.
|
288
396
|
|
289
397
|
Returns:
|
290
|
-
|
398
|
+
int: primary key of the project.
|
291
399
|
"""
|
292
400
|
with self.connection:
|
293
401
|
if project_number:
|
294
402
|
try:
|
295
403
|
self.cursor.execute(
|
296
404
|
f"INSERT INTO {table_name} (project_number, paid_by, amount) VALUES \
|
297
|
-
('{project_number}', '{paid_by}', '{amount}');"
|
405
|
+
('{project_number}', '{paid_by}', '{amount}') RETURNING id;"
|
298
406
|
)
|
299
407
|
LOG.info(
|
300
408
|
f"INFO: Insertion of {project_number} and {amount} by {paid_by} is performed successfully"
|
301
409
|
)
|
302
|
-
return
|
410
|
+
return self.cursor.fetchone()[0]
|
303
411
|
except Exception as e:
|
304
412
|
LOG.error(
|
305
413
|
f"Error: project insertion in a table '{table_name}':\n {e}"
|
306
414
|
)
|
307
|
-
|
415
|
+
raise DbException(
|
416
|
+
f"Error: project insertion in a table '{table_name}':\n {e}"
|
417
|
+
)
|
308
418
|
else:
|
309
419
|
LOG.error(
|
310
420
|
"Error: Please provide project_number, paid_by, amount for Payment Oder"
|
@@ -315,7 +425,7 @@ class DB:
|
|
315
425
|
|
316
426
|
def update_project(
|
317
427
|
self,
|
318
|
-
|
428
|
+
project_id: int,
|
319
429
|
new_project_number: str,
|
320
430
|
new_paid_by: int,
|
321
431
|
new_amount: float,
|
@@ -324,11 +434,11 @@ class DB:
|
|
324
434
|
"""Updation of the the project detail in project table
|
325
435
|
|
326
436
|
Args:
|
327
|
-
|
328
|
-
new_project_number (str): new
|
437
|
+
project_id (int): project id to be updated.
|
438
|
+
new_project_number (str): new project information to replace old project.
|
329
439
|
new_paid_by (int): new primary key of the vrauser.
|
330
|
-
new_amount (float): new amount paid for the
|
331
|
-
table_name (str): table name of the
|
440
|
+
new_amount (float): new amount paid for the project.
|
441
|
+
table_name (str): table name of the project.
|
332
442
|
|
333
443
|
Returns:
|
334
444
|
bool: True for the success and False for the failure.
|
@@ -338,24 +448,24 @@ class DB:
|
|
338
448
|
self.cursor.execute(
|
339
449
|
f"UPDATE {table_name} SET \
|
340
450
|
project_number ='{new_project_number}', paid_by='{new_paid_by}', amount='{new_amount}' \
|
341
|
-
WHERE
|
451
|
+
WHERE id='{project_id}';"
|
342
452
|
)
|
343
453
|
LOG.info(
|
344
|
-
f"INFO: Updation of the project {
|
454
|
+
f"INFO: Updation of the project {project_id} has been peformed successfully"
|
345
455
|
)
|
346
456
|
return True
|
347
457
|
except Exception as e:
|
348
|
-
LOG.error(f"Error:
|
458
|
+
LOG.error(f"Error: Project Updating in table {table_name} : {e}")
|
349
459
|
return False
|
350
460
|
|
351
461
|
def remove_project(
|
352
|
-
self,
|
462
|
+
self, project_id: int, table_name: str = DEFAULT_TABLES.get("proj")
|
353
463
|
) -> bool:
|
354
464
|
"""Removal of the project.
|
355
465
|
|
356
466
|
Args:
|
357
|
-
|
358
|
-
table_name (str): table name of the
|
467
|
+
project_id (int): project id which need to be removed.
|
468
|
+
table_name (str): table name of the project.
|
359
469
|
|
360
470
|
Returns:
|
361
471
|
bool: True for the success and False for the failure.
|
@@ -363,51 +473,99 @@ class DB:
|
|
363
473
|
with self.connection:
|
364
474
|
try:
|
365
475
|
self.cursor.execute(
|
366
|
-
f"DELETE from {table_name} WHERE
|
476
|
+
f"DELETE from {table_name} WHERE id='{project_id}';"
|
367
477
|
)
|
368
478
|
LOG.info(
|
369
|
-
f"INFO: Removing of the project '{
|
479
|
+
f"INFO: Removing of the project '{project_id}' has been performed successfully."
|
370
480
|
)
|
371
481
|
return True
|
372
482
|
except Exception as e:
|
373
|
-
LOG.error(
|
374
|
-
f"Error: purchaseOrder removing from table '{table_name}': {e}"
|
375
|
-
)
|
483
|
+
LOG.error(f"Error: project removing from table '{table_name}': {e}")
|
376
484
|
raise DbException(
|
377
|
-
f"Error:
|
485
|
+
f"Error: project removing from table '{table_name}': {e}"
|
378
486
|
)
|
379
487
|
|
380
488
|
def get_project(
|
381
489
|
self,
|
382
|
-
|
490
|
+
project: Optional[int] = None,
|
383
491
|
table_name: str = DEFAULT_TABLES.get("proj"),
|
384
492
|
) -> any:
|
385
493
|
"""Retreive the information from the project table.
|
386
494
|
|
387
495
|
Args:
|
388
|
-
|
389
|
-
table_name (str): table name of the
|
496
|
+
project (Optional[int], optional): project which need to be fetched. Defaults to None.
|
497
|
+
table_name (str): table name of the project.
|
390
498
|
|
391
499
|
Returns:
|
392
500
|
any: Retreive the data from the project database.
|
393
501
|
"""
|
394
502
|
with self.connection:
|
395
|
-
if
|
503
|
+
if project:
|
396
504
|
self.cursor.execute(
|
397
|
-
f"SELECT * FROM {table_name} where project_number = '{
|
505
|
+
f"SELECT * FROM {table_name} where project_number = '{project}';"
|
398
506
|
)
|
399
507
|
else:
|
400
508
|
self.cursor.execute(f"SELECT * FROM {table_name};")
|
401
509
|
LOG.info("INFO: project information has been fetched successfully.")
|
402
510
|
return self.cursor.fetchall()
|
403
511
|
|
512
|
+
def get_project_by_id(
|
513
|
+
self,
|
514
|
+
project_id: int,
|
515
|
+
table_name: str = DEFAULT_TABLES.get("proj"),
|
516
|
+
) -> any:
|
517
|
+
"""Retreive the information from the project table.
|
518
|
+
|
519
|
+
Args:
|
520
|
+
project_id (Optional[int], optional): project which need to be fetched. Defaults to None.
|
521
|
+
table_name (str): table name of the project.
|
522
|
+
|
523
|
+
Returns:
|
524
|
+
any: Retreive the data from the project database.
|
525
|
+
"""
|
526
|
+
with self.connection:
|
527
|
+
self.cursor.execute(
|
528
|
+
f"SELECT * FROM {table_name} where id = '{project_id}';"
|
529
|
+
)
|
530
|
+
LOG.info("INFO: project information has been fetched successfully.")
|
531
|
+
return self.cursor.fetchone()
|
532
|
+
|
533
|
+
def get_project_primary_key(
|
534
|
+
self,
|
535
|
+
project: str,
|
536
|
+
datestmp: datetime = None,
|
537
|
+
table_name: str = DEFAULT_TABLES.get("proj"),
|
538
|
+
) -> any:
|
539
|
+
"""Retreive the primary key of the project from the project table.
|
540
|
+
|
541
|
+
Args:
|
542
|
+
project (str): project which need to be fetched.
|
543
|
+
datestmp (date, optional): date for which the value to be retrieved. Defaults to None.
|
544
|
+
table_name (str, optional): table name of the project. Defaults to DEFAULT_TABLES.get("proj").
|
545
|
+
|
546
|
+
Returns:
|
547
|
+
any: Retreive the data from the project database.
|
548
|
+
"""
|
549
|
+
with self.connection:
|
550
|
+
if datestmp:
|
551
|
+
LOG.info(f"Date query : {datestmp}")
|
552
|
+
self.cursor.execute(
|
553
|
+
f"SELECT * FROM {table_name} where project_number = '{project}' and date >= '{datestmp}';"
|
554
|
+
)
|
555
|
+
else:
|
556
|
+
self.cursor.execute(
|
557
|
+
f"SELECT * FROM {table_name} where project_number = '{project}';"
|
558
|
+
)
|
559
|
+
LOG.info("INFO: project information has been fetched successfully.")
|
560
|
+
return self.cursor.fetchone()[0]
|
561
|
+
|
404
562
|
def insert_grant(
|
405
563
|
self,
|
406
564
|
grant_number: str,
|
407
565
|
paid_by: int,
|
408
566
|
amount: float,
|
409
567
|
table_name: str = DEFAULT_TABLES.get("grant"),
|
410
|
-
) ->
|
568
|
+
) -> int:
|
411
569
|
"""Insertion of the grant detail.
|
412
570
|
|
413
571
|
Args:
|
@@ -421,23 +579,23 @@ class DB:
|
|
421
579
|
DbException: Exception for the provided inputs.
|
422
580
|
|
423
581
|
Returns:
|
424
|
-
|
582
|
+
int: Primary key of the grant.
|
425
583
|
"""
|
426
584
|
with self.connection:
|
427
585
|
if grant_number:
|
428
586
|
try:
|
429
587
|
self.cursor.execute(
|
430
588
|
f"INSERT INTO {table_name} (grant_number, paid_by, amount) \
|
431
|
-
VALUES ('{grant_number}', '{paid_by}', '{amount}');"
|
589
|
+
VALUES ('{grant_number}', '{paid_by}', '{amount}') RETURNING id;"
|
432
590
|
)
|
433
591
|
LOG.info(
|
434
592
|
f"INFO: Insertion of the grant {grant_number} and {amount} by \
|
435
593
|
{paid_by} has been performed successfully."
|
436
594
|
)
|
437
|
-
return
|
595
|
+
return self.cursor.fetchone()[0]
|
438
596
|
except Exception as e:
|
439
597
|
LOG.error(f"Error: grant Insert in table '{table_name}': {e}")
|
440
|
-
|
598
|
+
raise DbException(f"Error: grant Insert in table '{table_name}': {e}")
|
441
599
|
else:
|
442
600
|
LOG.error(
|
443
601
|
"Error: Please provide grant_number, paid_by, amount for grants"
|
@@ -446,7 +604,7 @@ class DB:
|
|
446
604
|
|
447
605
|
def update_grant(
|
448
606
|
self,
|
449
|
-
|
607
|
+
grant_id: int,
|
450
608
|
new_grant: str,
|
451
609
|
new_paid_by: int,
|
452
610
|
new_amount: float,
|
@@ -455,7 +613,7 @@ class DB:
|
|
455
613
|
""" "Updation of the the grant detail in grant table
|
456
614
|
|
457
615
|
Args:
|
458
|
-
|
616
|
+
grant_id (id): grant id to be updated.
|
459
617
|
new_grant (str): new grant information to replace old grant.
|
460
618
|
new_paid_by (int): new primary key of the vrauser.
|
461
619
|
new_amount (float): new amount paid for the purchase.
|
@@ -468,25 +626,25 @@ class DB:
|
|
468
626
|
try:
|
469
627
|
self.cursor.execute(
|
470
628
|
f"UPDATE {table_name} SET grant_number ='{new_grant}', paid_by='{new_paid_by}', \
|
471
|
-
amount='{new_amount}' WHERE
|
629
|
+
amount='{new_amount}' WHERE id='{grant_id}';"
|
472
630
|
)
|
473
631
|
LOG.info(
|
474
|
-
f"INFO: Updation of the grant {
|
632
|
+
f"INFO: Updation of the grant {grant_id} has been performed successfully."
|
475
633
|
)
|
476
634
|
return True
|
477
635
|
except Exception as e:
|
478
636
|
LOG.error(
|
479
|
-
f"Error: grant Updating of '{
|
637
|
+
f"Error: grant Updating of '{grant_id}' in table '{table_name}': {e}"
|
480
638
|
)
|
481
639
|
return False
|
482
640
|
|
483
641
|
def remove_grant(
|
484
|
-
self,
|
642
|
+
self, grant_id: int, table_name: str = DEFAULT_TABLES.get("grant")
|
485
643
|
) -> bool:
|
486
644
|
"""Removal of the grant.
|
487
645
|
|
488
646
|
Args:
|
489
|
-
|
647
|
+
grant_id (int): grant number which need to be replaced.
|
490
648
|
table_name (str): table name of the grant.
|
491
649
|
|
492
650
|
Returns:
|
@@ -494,30 +652,28 @@ class DB:
|
|
494
652
|
"""
|
495
653
|
with self.connection:
|
496
654
|
try:
|
497
|
-
self.cursor.execute(
|
498
|
-
f"DELETE from {table_name} WHERE grant_number='{grant_number}';"
|
499
|
-
)
|
655
|
+
self.cursor.execute(f"DELETE from {table_name} WHERE id='{grant_id}';")
|
500
656
|
LOG.info(
|
501
|
-
f"INFO: Removal of the grant {
|
657
|
+
f"INFO: Removal of the grant {grant_id} has been performed successfully."
|
502
658
|
)
|
503
659
|
return True
|
504
660
|
except Exception as e:
|
505
661
|
LOG.error(
|
506
|
-
f"Error: Removal of grant {
|
662
|
+
f"Error: Removal of grant {grant_id} from table {table_name}: {e}"
|
507
663
|
)
|
508
664
|
raise DbException(
|
509
|
-
f"Error: Removal of grant {
|
665
|
+
f"Error: Removal of grant {grant_id} from table {table_name}: {e}"
|
510
666
|
)
|
511
667
|
|
512
668
|
def get_grant(
|
513
669
|
self,
|
514
|
-
grant_number:
|
670
|
+
grant_number: int | None = None,
|
515
671
|
table_name: str = DEFAULT_TABLES.get("grant"),
|
516
672
|
) -> any:
|
517
673
|
"""Retreive the information from the grant table.
|
518
674
|
|
519
675
|
Args:
|
520
|
-
grant_number (
|
676
|
+
grant_number (int | None, optional): primary key of the grant which need to be fetched. Defaults to None.
|
521
677
|
table_name (str): table name of the grant.
|
522
678
|
|
523
679
|
Returns:
|
@@ -533,6 +689,54 @@ class DB:
|
|
533
689
|
LOG.info("INFO: grant information has been fetched successfully.")
|
534
690
|
return self.cursor.fetchall()
|
535
691
|
|
692
|
+
def get_grant_by_id(
|
693
|
+
self,
|
694
|
+
grant_id: int,
|
695
|
+
table_name: str = DEFAULT_TABLES.get("grant"),
|
696
|
+
) -> any:
|
697
|
+
"""Retreive the information from the grant table.
|
698
|
+
|
699
|
+
Args:
|
700
|
+
grant_id (int | None, optional): primary key of the grant which need to be fetched. Defaults to None.
|
701
|
+
table_name (str): table name of the grant.
|
702
|
+
|
703
|
+
Returns:
|
704
|
+
any: Retreive the data from the grant database.
|
705
|
+
"""
|
706
|
+
with self.connection:
|
707
|
+
self.cursor.execute(f"SELECT * FROM {table_name} where id = '{grant_id}';")
|
708
|
+
LOG.info("INFO: grant information has been fetched successfully.")
|
709
|
+
return self.cursor.fetchone()
|
710
|
+
|
711
|
+
def get_grant_primary_key(
|
712
|
+
self,
|
713
|
+
grant_number: str,
|
714
|
+
datestmp: datetime = None,
|
715
|
+
table_name: str = DEFAULT_TABLES.get("grant"),
|
716
|
+
) -> any:
|
717
|
+
"""Retreive the information from the grant table.
|
718
|
+
|
719
|
+
Args:
|
720
|
+
grant_number (str | None, optional): grant which need to be fetched. Defaults to None.
|
721
|
+
datestmp (datetime, optional): date for which the value to be retrieved. Defaults to None.
|
722
|
+
table_name (str): table name of the grant.
|
723
|
+
|
724
|
+
Returns:
|
725
|
+
any: Retreive the data from the grant database.
|
726
|
+
"""
|
727
|
+
with self.connection:
|
728
|
+
if datestmp:
|
729
|
+
LOG.info(f"Date query : {datestmp}")
|
730
|
+
self.cursor.execute(
|
731
|
+
f"SELECT * FROM {table_name} where grant_number = '{grant_number}' and date >= '{datestmp}';"
|
732
|
+
)
|
733
|
+
else:
|
734
|
+
self.cursor.execute(
|
735
|
+
f"SELECT * FROM {table_name} where grant_number = '{grant_number}';"
|
736
|
+
)
|
737
|
+
LOG.info("INFO: grant information has been fetched successfully.")
|
738
|
+
return self.cursor.fetchone()[0]
|
739
|
+
|
536
740
|
def insert_costing(
|
537
741
|
self,
|
538
742
|
deployment_id: int,
|
@@ -540,7 +744,7 @@ class DB:
|
|
540
744
|
project_id: Optional[int] = None,
|
541
745
|
grant_id: Optional[int] = None,
|
542
746
|
table_name: str = DEFAULT_TABLES.get("costing"),
|
543
|
-
) ->
|
747
|
+
) -> int:
|
544
748
|
"""Insertion of the costing detail.
|
545
749
|
|
546
750
|
Args:
|
@@ -555,7 +759,7 @@ class DB:
|
|
555
759
|
DbException: Exception for the provided inputs.
|
556
760
|
|
557
761
|
Returns:
|
558
|
-
|
762
|
+
int: Primary key of the costing.
|
559
763
|
"""
|
560
764
|
with self.connection:
|
561
765
|
if project_id and grant_id:
|
@@ -569,28 +773,32 @@ class DB:
|
|
569
773
|
try:
|
570
774
|
self.cursor.execute(
|
571
775
|
f"INSERT INTO {table_name} (deployment_id, type, project_id) VALUES \
|
572
|
-
('{deployment_id}', '{typee}', '{project_id}');"
|
776
|
+
('{deployment_id}', '{typee}', '{project_id}') RETURNING id;"
|
573
777
|
)
|
574
778
|
LOG.info("INFO: Costing insertion has been performed successfully")
|
575
|
-
return
|
779
|
+
return self.cursor.fetchone()[0]
|
576
780
|
except Exception as e:
|
577
781
|
LOG.error(
|
578
782
|
f"Error: cost removal failed in table '{table_name}': {e}"
|
579
783
|
)
|
580
|
-
|
784
|
+
raise DbException(
|
785
|
+
f"Error: cost removal failed in table '{table_name}': {e}"
|
786
|
+
)
|
581
787
|
elif deployment_id and grant_id and typee:
|
582
788
|
try:
|
583
789
|
self.cursor.execute(
|
584
790
|
f"INSERT INTO {table_name} (deployment_id, type, grant_id) VALUES \
|
585
|
-
('{deployment_id}', '{typee}', '{grant_id}');"
|
791
|
+
('{deployment_id}', '{typee}', '{grant_id}') RETURNING id;"
|
586
792
|
)
|
587
793
|
LOG.info("INFO: Insertion of costing has been successfully")
|
588
|
-
return
|
794
|
+
return self.cursor.fetchone()[0]
|
589
795
|
except Exception as e:
|
590
796
|
LOG.error(
|
591
797
|
f"Error: cost removal failed in table '{table_name}': {e}"
|
592
798
|
)
|
593
|
-
|
799
|
+
raise DbException(
|
800
|
+
f"Error: cost removal failed in table '{table_name}': {e}"
|
801
|
+
)
|
594
802
|
else:
|
595
803
|
LOG.error(
|
596
804
|
"Error: Please provide correct deployment_id, type, and, (project_id/grant_id) for costing"
|
@@ -601,23 +809,21 @@ class DB:
|
|
601
809
|
|
602
810
|
def update_costing(
|
603
811
|
self,
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
project_id: Optional[int] = None,
|
812
|
+
old_costing_id: int,
|
813
|
+
new_deployment_id: int,
|
814
|
+
new_typee: str,
|
815
|
+
new_grant_id: Optional[int] = None,
|
816
|
+
new_project_id: Optional[int] = None,
|
610
817
|
table_name: str = DEFAULT_TABLES.get("costing"),
|
611
818
|
) -> bool:
|
612
819
|
"""Updation of the costing database entry.
|
613
820
|
|
614
821
|
Args:
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
project_id (Optional[int], optional): primary key of the grant id. Defaults to None.
|
822
|
+
old_costing_id (int): primary key of the costing id.
|
823
|
+
new_deployment_id (int): primary key of the deployment id.
|
824
|
+
new_typee (str): type of the license.
|
825
|
+
new_grant_id (Optional[int], optional): primary key of the grant id. Defaults to None.
|
826
|
+
new_project_id (Optional[int], optional): primary key of the project id. Defaults to None.
|
621
827
|
table_name (str): table name of the costing.
|
622
828
|
|
623
829
|
Raises:
|
@@ -626,67 +832,40 @@ class DB:
|
|
626
832
|
Returns:
|
627
833
|
bool: True for the success and False for the failure.
|
628
834
|
"""
|
629
|
-
if
|
630
|
-
LOG.info(
|
631
|
-
"Error: Please do not provide deployment_id. type, and, (project_id/ grant_id) for costing"
|
632
|
-
)
|
835
|
+
if new_grant_id and new_project_id:
|
633
836
|
raise DbException(
|
634
|
-
"Error:
|
837
|
+
f"Error: Specify either grant_id or project_id for updating the table {table_name}"
|
635
838
|
)
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
with self.connection:
|
657
|
-
try:
|
658
|
-
grant_id = "NULL" if not grant_id else f"'{grant_id}'"
|
659
|
-
project_id = "NULL" if not project_id else f"'{project_id}'"
|
660
|
-
self.cursor.execute(
|
661
|
-
f"UPDATE {table_name} SET deployment_id ='{deployment_id}', type='{typee}', \
|
662
|
-
grant_id={grant_id}, project_id={project_id} WHERE grant_id='{old_grant_id}';"
|
663
|
-
)
|
664
|
-
LOG.info(
|
665
|
-
"INFO: updation of the costing has been performed successfully."
|
666
|
-
)
|
667
|
-
return True
|
668
|
-
except Exception as e:
|
669
|
-
LOG.error(f"Error: Updation of costing has failed: \n {e}")
|
670
|
-
return False
|
671
|
-
else:
|
672
|
-
LOG.error("Error: updation of the costing has been failed")
|
673
|
-
return False
|
839
|
+
with self.connection:
|
840
|
+
try:
|
841
|
+
grant_id = "NULL" if not new_grant_id else f"'{new_grant_id}'"
|
842
|
+
project_id = "NULL" if not new_project_id else f"'{new_project_id}'"
|
843
|
+
self.cursor.execute(
|
844
|
+
f"UPDATE {table_name} SET \
|
845
|
+
deployment_id ='{new_deployment_id}', type='{new_typee}', project_id={project_id}, \
|
846
|
+
grant_id={grant_id} WHERE id='{old_costing_id}';"
|
847
|
+
)
|
848
|
+
LOG.info(
|
849
|
+
"INFO: updation of the costing has been performed successfully."
|
850
|
+
)
|
851
|
+
return True
|
852
|
+
except Exception as e:
|
853
|
+
LOG.error(
|
854
|
+
f"Error: Updation of costing has failed in table '{table_name}': \n {e}"
|
855
|
+
)
|
856
|
+
raise DbException(
|
857
|
+
f"Error: Updation of costing has failed in table '{table_name}'"
|
858
|
+
)
|
674
859
|
|
675
860
|
def remove_costing(
|
676
861
|
self,
|
677
|
-
|
678
|
-
typee: str,
|
679
|
-
project_id: Optional[int] = None,
|
680
|
-
grant_id: Optional[int] = None,
|
862
|
+
costing_id: int,
|
681
863
|
table_name: str = DEFAULT_TABLES.get("costing"),
|
682
864
|
) -> bool:
|
683
865
|
"""Removal of the costing detail from costing database.
|
684
866
|
|
685
867
|
Args:
|
686
|
-
|
687
|
-
typee (str): type of the license.
|
688
|
-
project_id (Optional[int], optional): primary key of the payment order. Defaults to None.
|
689
|
-
grant_id (Optional[int], optional): primary key of the grant id. Defaults to None.
|
868
|
+
costing_id (int): primary key of the costing id.
|
690
869
|
table_name (str): table name of the costing.
|
691
870
|
|
692
871
|
Raises:
|
@@ -696,49 +875,20 @@ class DB:
|
|
696
875
|
bool: True for the success and False for the failure.
|
697
876
|
"""
|
698
877
|
with self.connection:
|
699
|
-
|
700
|
-
|
701
|
-
"
|
878
|
+
try:
|
879
|
+
self.cursor.execute(
|
880
|
+
f"DELETE from {table_name} WHERE id = '{costing_id}';"
|
702
881
|
)
|
703
|
-
|
704
|
-
"
|
882
|
+
LOG.info(
|
883
|
+
"INFO: Removing of the costing has been performed successfully."
|
705
884
|
)
|
706
|
-
|
707
|
-
|
708
|
-
self.cursor.execute(
|
709
|
-
f"DELETE from {table_name} WHERE deployment_id = '{deployment_id}' and type = '{typee}' \
|
710
|
-
and grant_id = '{grant_id}';"
|
711
|
-
)
|
712
|
-
LOG.info(
|
713
|
-
"INFO: Removing of the costing has been performed successfully."
|
714
|
-
)
|
715
|
-
return True
|
716
|
-
except Exception as e:
|
717
|
-
LOG.error(
|
718
|
-
f"Error: Removing of costing has failed in table {table_name}: \n {e}"
|
719
|
-
)
|
720
|
-
return False
|
721
|
-
elif deployment_id and project_id:
|
722
|
-
try:
|
723
|
-
self.cursor.execute(
|
724
|
-
f"DELETE from {table_name} WHERE deployment_id = '{deployment_id}' and type = '{typee}' \
|
725
|
-
and project_id = '{project_id}';"
|
726
|
-
)
|
727
|
-
LOG.info(
|
728
|
-
"INFO: Removing of the costing has been performed successfully."
|
729
|
-
)
|
730
|
-
return True
|
731
|
-
except Exception as e:
|
732
|
-
LOG.error(
|
733
|
-
f"Error: Removing of costing has failed in table '{table_name}': \n {e}"
|
734
|
-
)
|
735
|
-
return False
|
736
|
-
else:
|
885
|
+
return True
|
886
|
+
except Exception as e:
|
737
887
|
LOG.error(
|
738
|
-
"Error:
|
888
|
+
f"Error: Removing of costing has failed in table {table_name}: \n {e}"
|
739
889
|
)
|
740
890
|
raise DbException(
|
741
|
-
"Error:
|
891
|
+
f"Error: Removing of costing has failed in table {table_name}: \n {e}"
|
742
892
|
)
|
743
893
|
|
744
894
|
def get_costing(
|
@@ -785,6 +935,73 @@ class DB:
|
|
785
935
|
LOG.info("INFO: costing information has been performed successfully")
|
786
936
|
return self.cursor.fetchall()
|
787
937
|
|
938
|
+
def get_costing_by_id(
|
939
|
+
self,
|
940
|
+
costing_id: int,
|
941
|
+
table_name: str = DEFAULT_TABLES.get("costing"),
|
942
|
+
) -> any:
|
943
|
+
"""Retreive the information from the costing table.
|
944
|
+
|
945
|
+
Args:
|
946
|
+
costing_id (int): primary key of the costing id.
|
947
|
+
table_name (str): table name of the costing.
|
948
|
+
|
949
|
+
Returns:
|
950
|
+
any: Retreive the data from the costing database.
|
951
|
+
"""
|
952
|
+
with self.connection:
|
953
|
+
try:
|
954
|
+
self.cursor.execute(
|
955
|
+
f"SELECT * FROM {table_name} where id = '{costing_id}';"
|
956
|
+
)
|
957
|
+
LOG.info("INFO: costing information has been performed successfully")
|
958
|
+
return self.cursor.fetchone()
|
959
|
+
except Exception as e:
|
960
|
+
LOG.error(
|
961
|
+
f"Error: Unable to fetch costing id from table '{table_name}': {e}"
|
962
|
+
)
|
963
|
+
raise DbException(
|
964
|
+
f"Error: Unable to fetch costing id from table '{table_name}': {e}"
|
965
|
+
)
|
966
|
+
|
967
|
+
def get_costing_primary_key(
|
968
|
+
self,
|
969
|
+
deployment_id: int,
|
970
|
+
typee: str,
|
971
|
+
project_id: Optional[int] = None,
|
972
|
+
grant_id: Optional[int] = None,
|
973
|
+
table_name: str = DEFAULT_TABLES.get("costing"),
|
974
|
+
) -> any:
|
975
|
+
"""Retreive the primary key from the costing table.
|
976
|
+
|
977
|
+
Args:
|
978
|
+
deployment_id (int): primary key of the deployment id.
|
979
|
+
typee (str): type of the license.
|
980
|
+
project_id (Optional[int], optional): primary key of the payment order. Defaults to None.
|
981
|
+
grant_id (Optional[int], optional): primary key of the grant id. Defaults to None.
|
982
|
+
table_name (str): table name of the costing.
|
983
|
+
|
984
|
+
Returns:
|
985
|
+
any: Retreive the data from the costing database.
|
986
|
+
"""
|
987
|
+
with self.connection:
|
988
|
+
if deployment_id and project_id and typee:
|
989
|
+
self.cursor.execute(
|
990
|
+
f"SELECT * FROM {table_name} where deployment_id = '{deployment_id}' and type = '{typee}' \
|
991
|
+
and project_id = '{project_id}';"
|
992
|
+
)
|
993
|
+
elif deployment_id and grant_id and typee:
|
994
|
+
self.cursor.execute(
|
995
|
+
f"SELECT * FROM {table_name} where deployment_id = '{deployment_id}' and type = '{typee}' \
|
996
|
+
and grant_id = '{grant_id}';"
|
997
|
+
)
|
998
|
+
else:
|
999
|
+
raise DbException(
|
1000
|
+
"please provide project_id or grant_id along with deplyment_id and type"
|
1001
|
+
)
|
1002
|
+
LOG.info("INFO: costing information has been performed successfully")
|
1003
|
+
return self.cursor.fetchone()[0]
|
1004
|
+
|
788
1005
|
def closedb(self) -> None:
|
789
1006
|
"""
|
790
1007
|
To close the databse connection.
|