ucampostgresvro 0.1.2__py3-none-any.whl → 0.2.0__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 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
@@ -150,6 +151,57 @@ 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
  ) -> bool:
@@ -268,6 +320,60 @@ class DB:
268
320
  LOG.info("INFO: deployment ID information is fetched successfully")
269
321
  return self.cursor.fetchall()
270
322
 
323
+ def get_deployment_id_by_id(
324
+ self,
325
+ deployment_id: int,
326
+ table_name: str = DEFAULT_TABLES.get("deploymentid"),
327
+ ) -> any:
328
+ """Retreive the information from the deployment table.
329
+
330
+ Args:
331
+ deployment_id (int): primary key of the Deployment ID need to be fetched.
332
+ table_name (str, optional): table name of the deploymentid. Defaults to DEFAULT_TABLES.get("deploymentid").
333
+
334
+ Raises:
335
+ DbException: Raise exception in case of retrieving information
336
+
337
+ Returns:
338
+ any: Retreive the data from the Deployment ID database.
339
+ """
340
+ with self.connection:
341
+ try:
342
+ self.cursor.execute(
343
+ f"SELECT * FROM {table_name} where id = '{deployment_id}';"
344
+ )
345
+ LOG.info("INFO: deployment ID information is fetched successfully")
346
+ return self.cursor.fetchone()
347
+ except Exception as e:
348
+ LOG.error(
349
+ f"Error: Unable to fetch deployment id from table '{table_name}': {e}"
350
+ )
351
+ raise DbException(
352
+ f"Error: Unable to fetch deployment id from table '{table_name}': {e}"
353
+ )
354
+
355
+ def get_deployment_id_primary_key(
356
+ self,
357
+ deployment_id: str,
358
+ table_name: str = DEFAULT_TABLES.get("deploymentid"),
359
+ ) -> any:
360
+ """Retreive the primary key from the deployment table.
361
+
362
+ Args:
363
+ deployment_id (str): Deployment ID need to be fetched.
364
+ table_name (str): table name of the deploymentid.
365
+
366
+ Returns:
367
+ any: Retreive the data from the Deployment ID database.
368
+ """
369
+ with self.connection:
370
+ if deployment_id:
371
+ self.cursor.execute(
372
+ f"SELECT * FROM {table_name} where deploymentID = '{deployment_id}';"
373
+ )
374
+ LOG.info("INFO: deployment ID information is fetched successfully")
375
+ return self.cursor.fetchone()[0]
376
+
271
377
  def insert_project(
272
378
  self,
273
379
  project_number: str,
@@ -281,7 +387,7 @@ class DB:
281
387
  project_number (str): payment order information.
282
388
  paid_by (int): primary key of the vrauser.
283
389
  amount (float): amount paid for the purchase.
284
- table_name (str): table name of the purchaseOrder.
390
+ table_name (str): table name of the project.
285
391
 
286
392
  Raises:
287
393
  DbException: Exception for the provided inputs.
@@ -315,7 +421,7 @@ class DB:
315
421
 
316
422
  def update_project(
317
423
  self,
318
- old_project_number: str,
424
+ project_id: int,
319
425
  new_project_number: str,
320
426
  new_paid_by: int,
321
427
  new_amount: float,
@@ -324,11 +430,11 @@ class DB:
324
430
  """Updation of the the project detail in project table
325
431
 
326
432
  Args:
327
- old_project_number (str): old payment order information.
328
- new_project_number (str): new payment order information to replace old payment order.
433
+ project_id (int): project id to be updated.
434
+ new_project_number (str): new project information to replace old project.
329
435
  new_paid_by (int): new primary key of the vrauser.
330
- new_amount (float): new amount paid for the purchase.
331
- table_name (str): table name of the purchaseOrder.
436
+ new_amount (float): new amount paid for the project.
437
+ table_name (str): table name of the project.
332
438
 
333
439
  Returns:
334
440
  bool: True for the success and False for the failure.
@@ -338,24 +444,24 @@ class DB:
338
444
  self.cursor.execute(
339
445
  f"UPDATE {table_name} SET \
340
446
  project_number ='{new_project_number}', paid_by='{new_paid_by}', amount='{new_amount}' \
341
- WHERE project_number='{old_project_number}';"
447
+ WHERE id='{project_id}';"
342
448
  )
343
449
  LOG.info(
344
- f"INFO: Updation of the project {old_project_number} has been peformed successfully"
450
+ f"INFO: Updation of the project {project_id} has been peformed successfully"
345
451
  )
346
452
  return True
347
453
  except Exception as e:
348
- LOG.error(f"Error: purchaseOrder Updating in table {table_name} : {e}")
454
+ LOG.error(f"Error: Project Updating in table {table_name} : {e}")
349
455
  return False
350
456
 
351
457
  def remove_project(
352
- self, project_number: str, table_name: str = DEFAULT_TABLES.get("proj")
458
+ self, project_id: int, table_name: str = DEFAULT_TABLES.get("proj")
353
459
  ) -> bool:
354
460
  """Removal of the project.
355
461
 
356
462
  Args:
357
- project_number (str): project which need to be removed.
358
- table_name (str): table name of the purchaseOrder.
463
+ project_id (int): project id which need to be removed.
464
+ table_name (str): table name of the project.
359
465
 
360
466
  Returns:
361
467
  bool: True for the success and False for the failure.
@@ -363,44 +469,92 @@ class DB:
363
469
  with self.connection:
364
470
  try:
365
471
  self.cursor.execute(
366
- f"DELETE from {table_name} WHERE project_number='{project_number}';"
472
+ f"DELETE from {table_name} WHERE id='{project_id}';"
367
473
  )
368
474
  LOG.info(
369
- f"INFO: Removing of the project '{project_number}' has been performed successfully."
475
+ f"INFO: Removing of the project '{project_id}' has been performed successfully."
370
476
  )
371
477
  return True
372
478
  except Exception as e:
373
- LOG.error(
374
- f"Error: purchaseOrder removing from table '{table_name}': {e}"
375
- )
479
+ LOG.error(f"Error: project removing from table '{table_name}': {e}")
376
480
  raise DbException(
377
- f"Error: purchaseOrder removing from table '{table_name}': {e}"
481
+ f"Error: project removing from table '{table_name}': {e}"
378
482
  )
379
483
 
380
484
  def get_project(
381
485
  self,
382
- project_number: Optional[str] = None,
486
+ project: Optional[int] = None,
383
487
  table_name: str = DEFAULT_TABLES.get("proj"),
384
488
  ) -> any:
385
489
  """Retreive the information from the project table.
386
490
 
387
491
  Args:
388
- project_number (Optional[str], optional): project which need to be fetched. Defaults to None.
389
- table_name (str): table name of the purchaseOrder.
492
+ project (Optional[int], optional): project which need to be fetched. Defaults to None.
493
+ table_name (str): table name of the project.
390
494
 
391
495
  Returns:
392
496
  any: Retreive the data from the project database.
393
497
  """
394
498
  with self.connection:
395
- if project_number:
499
+ if project:
396
500
  self.cursor.execute(
397
- f"SELECT * FROM {table_name} where project_number = '{project_number}';"
501
+ f"SELECT * FROM {table_name} where project_number = '{project}';"
398
502
  )
399
503
  else:
400
504
  self.cursor.execute(f"SELECT * FROM {table_name};")
401
505
  LOG.info("INFO: project information has been fetched successfully.")
402
506
  return self.cursor.fetchall()
403
507
 
508
+ def get_project_by_id(
509
+ self,
510
+ project_id: int,
511
+ table_name: str = DEFAULT_TABLES.get("proj"),
512
+ ) -> any:
513
+ """Retreive the information from the project table.
514
+
515
+ Args:
516
+ project_id (Optional[int], optional): project which need to be fetched. Defaults to None.
517
+ table_name (str): table name of the project.
518
+
519
+ Returns:
520
+ any: Retreive the data from the project database.
521
+ """
522
+ with self.connection:
523
+ self.cursor.execute(
524
+ f"SELECT * FROM {table_name} where id = '{project_id}';"
525
+ )
526
+ LOG.info("INFO: project information has been fetched successfully.")
527
+ return self.cursor.fetchone()
528
+
529
+ def get_project_primary_key(
530
+ self,
531
+ project: str,
532
+ datestmp: datetime = None,
533
+ table_name: str = DEFAULT_TABLES.get("proj"),
534
+ ) -> any:
535
+ """Retreive the primary key of the project from the project table.
536
+
537
+ Args:
538
+ project (str): project which need to be fetched.
539
+ datestmp (date, optional): date for which the value to be retrieved. Defaults to None.
540
+ table_name (str, optional): table name of the project. Defaults to DEFAULT_TABLES.get("proj").
541
+
542
+ Returns:
543
+ any: Retreive the data from the project database.
544
+ """
545
+ with self.connection:
546
+ if datestmp:
547
+ LOG.info(f"Date query : {datestmp}")
548
+ self.cursor.execute(
549
+ f"SELECT * FROM {table_name} where project_number = '{project}' and date >= '{datestmp}';"
550
+ )
551
+ else:
552
+ self.cursor.execute(
553
+ f"SELECT * FROM {table_name} where project_number = '{project}';"
554
+ )
555
+ LOG.info("INFO: project information has been fetched successfully.")
556
+ return self.cursor.fetchone()[0]
557
+
404
558
  def insert_grant(
405
559
  self,
406
560
  grant_number: str,
@@ -446,7 +600,7 @@ class DB:
446
600
 
447
601
  def update_grant(
448
602
  self,
449
- old_grant: str,
603
+ grant_id: int,
450
604
  new_grant: str,
451
605
  new_paid_by: int,
452
606
  new_amount: float,
@@ -455,7 +609,7 @@ class DB:
455
609
  """ "Updation of the the grant detail in grant table
456
610
 
457
611
  Args:
458
- old_grant (str): old grant information.
612
+ grant_id (id): grant id to be updated.
459
613
  new_grant (str): new grant information to replace old grant.
460
614
  new_paid_by (int): new primary key of the vrauser.
461
615
  new_amount (float): new amount paid for the purchase.
@@ -468,25 +622,25 @@ class DB:
468
622
  try:
469
623
  self.cursor.execute(
470
624
  f"UPDATE {table_name} SET grant_number ='{new_grant}', paid_by='{new_paid_by}', \
471
- amount='{new_amount}' WHERE grant_number='{old_grant}';"
625
+ amount='{new_amount}' WHERE id='{grant_id}';"
472
626
  )
473
627
  LOG.info(
474
- f"INFO: Updation of the grant {old_grant} has been performed successfully."
628
+ f"INFO: Updation of the grant {grant_id} has been performed successfully."
475
629
  )
476
630
  return True
477
631
  except Exception as e:
478
632
  LOG.error(
479
- f"Error: grant Updating of '{old_grant}' in table '{table_name}': {e}"
633
+ f"Error: grant Updating of '{grant_id}' in table '{table_name}': {e}"
480
634
  )
481
635
  return False
482
636
 
483
637
  def remove_grant(
484
- self, grant_number: str, table_name: str = DEFAULT_TABLES.get("grant")
638
+ self, grant_id: int, table_name: str = DEFAULT_TABLES.get("grant")
485
639
  ) -> bool:
486
640
  """Removal of the grant.
487
641
 
488
642
  Args:
489
- grant_number (str): grant number which need to be replaced.
643
+ grant_id (int): grant number which need to be replaced.
490
644
  table_name (str): table name of the grant.
491
645
 
492
646
  Returns:
@@ -494,30 +648,28 @@ class DB:
494
648
  """
495
649
  with self.connection:
496
650
  try:
497
- self.cursor.execute(
498
- f"DELETE from {table_name} WHERE grant_number='{grant_number}';"
499
- )
651
+ self.cursor.execute(f"DELETE from {table_name} WHERE id='{grant_id}';")
500
652
  LOG.info(
501
- f"INFO: Removal of the grant {grant_number} has been performed successfully."
653
+ f"INFO: Removal of the grant {grant_id} has been performed successfully."
502
654
  )
503
655
  return True
504
656
  except Exception as e:
505
657
  LOG.error(
506
- f"Error: Removal of grant {grant_number} from table {table_name}: {e}"
658
+ f"Error: Removal of grant {grant_id} from table {table_name}: {e}"
507
659
  )
508
660
  raise DbException(
509
- f"Error: Removal of grant {grant_number} from table {table_name}: {e}"
661
+ f"Error: Removal of grant {grant_id} from table {table_name}: {e}"
510
662
  )
511
663
 
512
664
  def get_grant(
513
665
  self,
514
- grant_number: str | None = None,
666
+ grant_number: int | None = None,
515
667
  table_name: str = DEFAULT_TABLES.get("grant"),
516
668
  ) -> any:
517
669
  """Retreive the information from the grant table.
518
670
 
519
671
  Args:
520
- grant_number (str | None, optional): grant which need to be fetched. Defaults to None.
672
+ grant_number (int | None, optional): primary key of the grant which need to be fetched. Defaults to None.
521
673
  table_name (str): table name of the grant.
522
674
 
523
675
  Returns:
@@ -533,6 +685,54 @@ class DB:
533
685
  LOG.info("INFO: grant information has been fetched successfully.")
534
686
  return self.cursor.fetchall()
535
687
 
688
+ def get_grant_by_id(
689
+ self,
690
+ grant_id: int,
691
+ table_name: str = DEFAULT_TABLES.get("grant"),
692
+ ) -> any:
693
+ """Retreive the information from the grant table.
694
+
695
+ Args:
696
+ grant_id (int | None, optional): primary key of the grant which need to be fetched. Defaults to None.
697
+ table_name (str): table name of the grant.
698
+
699
+ Returns:
700
+ any: Retreive the data from the grant database.
701
+ """
702
+ with self.connection:
703
+ self.cursor.execute(f"SELECT * FROM {table_name} where id = '{grant_id}';")
704
+ LOG.info("INFO: grant information has been fetched successfully.")
705
+ return self.cursor.fetchone()
706
+
707
+ def get_grant_primary_key(
708
+ self,
709
+ grant_number: str,
710
+ datestmp: datetime = None,
711
+ table_name: str = DEFAULT_TABLES.get("grant"),
712
+ ) -> any:
713
+ """Retreive the information from the grant table.
714
+
715
+ Args:
716
+ grant_number (str | None, optional): grant which need to be fetched. Defaults to None.
717
+ datestmp (datetime, optional): date for which the value to be retrieved. Defaults to None.
718
+ table_name (str): table name of the grant.
719
+
720
+ Returns:
721
+ any: Retreive the data from the grant database.
722
+ """
723
+ with self.connection:
724
+ if datestmp:
725
+ LOG.info(f"Date query : {datestmp}")
726
+ self.cursor.execute(
727
+ f"SELECT * FROM {table_name} where grant_number = '{grant_number}' and date >= '{datestmp}';"
728
+ )
729
+ else:
730
+ self.cursor.execute(
731
+ f"SELECT * FROM {table_name} where grant_number = '{grant_number}';"
732
+ )
733
+ LOG.info("INFO: grant information has been fetched successfully.")
734
+ return self.cursor.fetchone()[0]
735
+
536
736
  def insert_costing(
537
737
  self,
538
738
  deployment_id: int,
@@ -601,23 +801,21 @@ class DB:
601
801
 
602
802
  def update_costing(
603
803
  self,
604
- deployment_id: int,
605
- typee: str,
606
- old_grant_id: Optional[int] = None,
607
- old_project_id: Optional[int] = None,
608
- grant_id: Optional[int] = None,
609
- project_id: Optional[int] = None,
804
+ old_costing_id: int,
805
+ new_deployment_id: int,
806
+ new_typee: str,
807
+ new_grant_id: Optional[int] = None,
808
+ new_project_id: Optional[int] = None,
610
809
  table_name: str = DEFAULT_TABLES.get("costing"),
611
810
  ) -> bool:
612
811
  """Updation of the costing database entry.
613
812
 
614
813
  Args:
615
- deployment_id (int): primary key of the deployment id.
616
- typee (str): type of the license.
617
- old_grant_id (Optional[int], optional): primary key of the old grant id. Defaults to None.
618
- old_project_id (Optional[int], optional): primary key of the old payment order. Defaults to None.
619
- grant_id (Optional[int], optional): primary key of the grant id. Defaults to None.
620
- project_id (Optional[int], optional): primary key of the grant id. Defaults to None.
814
+ old_costing_id (int): primary key of the costing id.
815
+ new_deployment_id (int): primary key of the deployment id.
816
+ new_typee (str): type of the license.
817
+ new_grant_id (Optional[int], optional): primary key of the grant id. Defaults to None.
818
+ new_project_id (Optional[int], optional): primary key of the project id. Defaults to None.
621
819
  table_name (str): table name of the costing.
622
820
 
623
821
  Raises:
@@ -626,67 +824,40 @@ class DB:
626
824
  Returns:
627
825
  bool: True for the success and False for the failure.
628
826
  """
629
- if old_project_id and old_grant_id:
630
- LOG.info(
631
- "Error: Please do not provide deployment_id. type, and, (project_id/ grant_id) for costing"
632
- )
827
+ if new_grant_id and new_project_id:
633
828
  raise DbException(
634
- "Error: Please do not provide deployment_id. type, and, (project_id/ grant_id) for costing"
829
+ f"Error: Specify either grant_id or project_id for updating the table {table_name}"
635
830
  )
636
- elif old_project_id:
637
- with self.connection:
638
- try:
639
- grant_id = "NULL" if not grant_id else f"'{grant_id}'"
640
- project_id = "NULL" if not project_id else f"'{project_id}'"
641
- self.cursor.execute(
642
- f"UPDATE {table_name} SET \
643
- deployment_id ='{deployment_id}', type='{typee}', project_id={project_id}, \
644
- grant_id={grant_id} WHERE project_id='{old_project_id}';"
645
- )
646
- LOG.info(
647
- "INFO: updation of the costing has been performed successfully."
648
- )
649
- return True
650
- except Exception as e:
651
- LOG.error(
652
- f"Error: Updation of costing has failed in table '{table_name}': \n {e}"
653
- )
654
- return False
655
- elif old_grant_id:
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
831
+ with self.connection:
832
+ try:
833
+ grant_id = "NULL" if not new_grant_id else f"'{new_grant_id}'"
834
+ project_id = "NULL" if not new_project_id else f"'{new_project_id}'"
835
+ self.cursor.execute(
836
+ f"UPDATE {table_name} SET \
837
+ deployment_id ='{new_deployment_id}', type='{new_typee}', project_id={project_id}, \
838
+ grant_id={grant_id} WHERE id='{old_costing_id}';"
839
+ )
840
+ LOG.info(
841
+ "INFO: updation of the costing has been performed successfully."
842
+ )
843
+ return True
844
+ except Exception as e:
845
+ LOG.error(
846
+ f"Error: Updation of costing has failed in table '{table_name}': \n {e}"
847
+ )
848
+ raise DbException(
849
+ f"Error: Updation of costing has failed in table '{table_name}'"
850
+ )
674
851
 
675
852
  def remove_costing(
676
853
  self,
677
- deployment_id: int,
678
- typee: str,
679
- project_id: Optional[int] = None,
680
- grant_id: Optional[int] = None,
854
+ costing_id: int,
681
855
  table_name: str = DEFAULT_TABLES.get("costing"),
682
856
  ) -> bool:
683
857
  """Removal of the costing detail from costing database.
684
858
 
685
859
  Args:
686
- deployment_id (int): primary key of the deployment id.
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.
860
+ costing_id (int): primary key of the costing id.
690
861
  table_name (str): table name of the costing.
691
862
 
692
863
  Raises:
@@ -696,49 +867,20 @@ class DB:
696
867
  bool: True for the success and False for the failure.
697
868
  """
698
869
  with self.connection:
699
- if grant_id and project_id:
700
- LOG.error(
701
- "Error: Please do not provide both project_id and grant_id for costing"
870
+ try:
871
+ self.cursor.execute(
872
+ f"DELETE from {table_name} WHERE id = '{costing_id}';"
702
873
  )
703
- raise DbException(
704
- "Error: Please do not provide both project_id and grant_id for costing"
874
+ LOG.info(
875
+ "INFO: Removing of the costing has been performed successfully."
705
876
  )
706
- elif deployment_id and grant_id:
707
- try:
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:
877
+ return True
878
+ except Exception as e:
737
879
  LOG.error(
738
- "Error: Please provide correct deployment_id, type, and, (project_id/grant_id) for costing"
880
+ f"Error: Removing of costing has failed in table {table_name}: \n {e}"
739
881
  )
740
882
  raise DbException(
741
- "Error: Please provide correct deployment_id, type, and, (project_id/grant_id) for costing"
883
+ f"Error: Removing of costing has failed in table {table_name}: \n {e}"
742
884
  )
743
885
 
744
886
  def get_costing(
@@ -785,6 +927,73 @@ class DB:
785
927
  LOG.info("INFO: costing information has been performed successfully")
786
928
  return self.cursor.fetchall()
787
929
 
930
+ def get_costing_by_id(
931
+ self,
932
+ costing_id: int,
933
+ table_name: str = DEFAULT_TABLES.get("costing"),
934
+ ) -> any:
935
+ """Retreive the information from the costing table.
936
+
937
+ Args:
938
+ costing_id (int): primary key of the costing id.
939
+ table_name (str): table name of the costing.
940
+
941
+ Returns:
942
+ any: Retreive the data from the costing database.
943
+ """
944
+ with self.connection:
945
+ try:
946
+ self.cursor.execute(
947
+ f"SELECT * FROM {table_name} where id = '{costing_id}';"
948
+ )
949
+ LOG.info("INFO: costing information has been performed successfully")
950
+ return self.cursor.fetchone()
951
+ except Exception as e:
952
+ LOG.error(
953
+ f"Error: Unable to fetch costing id from table '{table_name}': {e}"
954
+ )
955
+ raise DbException(
956
+ f"Error: Unable to fetch costing id from table '{table_name}': {e}"
957
+ )
958
+
959
+ def get_costing_primary_key(
960
+ self,
961
+ deployment_id: int,
962
+ typee: str,
963
+ project_id: Optional[int] = None,
964
+ grant_id: Optional[int] = None,
965
+ table_name: str = DEFAULT_TABLES.get("costing"),
966
+ ) -> any:
967
+ """Retreive the primary key from the costing table.
968
+
969
+ Args:
970
+ deployment_id (int): primary key of the deployment id.
971
+ typee (str): type of the license.
972
+ project_id (Optional[int], optional): primary key of the payment order. Defaults to None.
973
+ grant_id (Optional[int], optional): primary key of the grant id. Defaults to None.
974
+ table_name (str): table name of the costing.
975
+
976
+ Returns:
977
+ any: Retreive the data from the costing database.
978
+ """
979
+ with self.connection:
980
+ if deployment_id and project_id and typee:
981
+ self.cursor.execute(
982
+ f"SELECT * FROM {table_name} where deployment_id = '{deployment_id}' and type = '{typee}' \
983
+ and project_id = '{project_id}';"
984
+ )
985
+ elif deployment_id and grant_id and typee:
986
+ self.cursor.execute(
987
+ f"SELECT * FROM {table_name} where deployment_id = '{deployment_id}' and type = '{typee}' \
988
+ and grant_id = '{grant_id}';"
989
+ )
990
+ else:
991
+ raise DbException(
992
+ "please provide project_id or grant_id along with deplyment_id and type"
993
+ )
994
+ LOG.info("INFO: costing information has been performed successfully")
995
+ return self.cursor.fetchone()[0]
996
+
788
997
  def closedb(self) -> None:
789
998
  """
790
999
  To close the databse connection.