HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
ftests_dbi_insert_on_update.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 
4 """
5 HCE project, Python bindings, Distributed Tasks Manager application.
6 Event objects definitions.
7 
8 @package: dtm
9 @file ftests_dbi_insert_on_update.py
10 @author Oleksii <developers.hce@gmail.com>
11 @link: http://hierarchical-cluster-engine.com/
12 @copyright: Copyright &copy; 2013-2014 IOIX Ukraine
13 @license: http://hierarchical-cluster-engine.com/license/
14 @since: 0.1
15 """
16 
17 
18 from dbi.dbi import DBI
19 from dbi.dbi import CONSTANTS
20 from Resources import Resources as dbResources
21 from dtm.EventObjects import Resource as eventResource
22 
23 class DemoResourcesManager(object):
24 
25 
26  def __init__(self, config_dic):
27  # create dbi instance
28  self.dbi = DBI(config_dic)
29 
30 
31  def updateResourcesData(self, resource):
32  # insert object
33  original_resource = dbResources(resource)
34  self.dbi.insert(original_resource)
35  if self.dbi.getErrorCode() != CONSTANTS.DBI_SUCCESS_CODE:
36  # handle insertion's error
37  print "insert original resource error!"
38  return
39  else:
40  # do ptocessing after insertion
41  print "original resource: %s" % original_resource
42  pass
43 
44 
45  # fetch object
46  fetched_resource = dbResources(resource)
47  fetched_original_resource = self.dbi.fetch(fetched_resource, "nodeId=%s"%fetched_resource.nodeId)
48  if self.dbi.getErrorCode()!=CONSTANTS.DBI_SUCCESS_CODE and fetched_original_resource!=original_resource:
49  # handle fetch's error
50  print "fetch original resource error!"
51  return
52  else:
53  # do ptocessing after insertion
54  pass
55  print "fetched original resource: %s" % fetched_original_resource
56 
57 
58 
59  # update otiginal resource
60  updated_resource = dbResources(resource)
61  updated_resource.name = "updated"
62  print "updated resource: %s" % fetched_original_resource
63  returned_updated_resource = self.dbi.insertOnUpdate(updated_resource, "nodeId=%s"%updated_resource.nodeId)
64  if self.dbi.getErrorCode() != CONSTANTS.DBI_SUCCESS_CODE:
65  # handle fetch's error
66  print "insert on update error!"
67  return
68  else:
69  # do ptocessing after insertion
70  pass
71  print "returned updated resource: %s" % returned_updated_resource
72 
73 
74 
75  # fetch updated object
76  fetched_resource = dbResources(resource)
77  fetched_updated_resource = self.dbi.fetch(fetched_resource, "nodeId=%s"%fetched_resource.nodeId)
78  if self.dbi.getErrorCode() != CONSTANTS.DBI_SUCCESS_CODE:
79  # handle update's error
80  print "delete one object error!"
81  return
82  else:
83  # do ptocessing after insertion
84  pass
85  print "fetched updated resource: %s" % fetched_updated_resource
86 
87 
88 
89 
90 
91 
92 if __name__ == '__main__':
93  # config section
94  config_dic = dict()
95  config_dic["db_name"] = "/del.db"
96  # create ResourcesManager instance
97  demoResourceManager = DemoResourcesManager(config_dic)
98  event_Resource = eventResource("100")
99  demoResourceManager.updateResourcesData(event_Resource)
Definition: dbi.py:1