annotate commons/core/sql/TableAdaptator.py @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # http://www.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 ## Abstract class, Ancestor of Table*Adaptator
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 class TableAdaptator( object ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 ## Constructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 # @param iDb DbMySql instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 # @param table str table name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 def __init__( self, iDb = None, table = "" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 self._iDb = iDb
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 self._table = table
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 ## Set connector to database
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 # @param iDb database instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 def setDbConnector( self, iDb ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self._iDb = iDb
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 ## Set table
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 # @param table string table name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 def setTable( self, table ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 self._table = table
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 ## Return the table name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 def getTable( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 return self._table
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 ## Return the number of rows in the table
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 def getSize( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 return self._iDb.getSize( self._table )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 ## Test if table is empty
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 def isEmpty( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 return self._iDb.isEmpty( self._table )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 ## Insert an instance of Map or Set or Match or Path or Seq instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 # @param obj a Map or Set or Match or Path or Seq instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 # @param delayed boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 def insert(self, obj, delayed = False):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 if obj.isEmpty():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 self._escapeAntislash(obj)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 sql_cmd = self._genSqlCmdForInsert(obj, delayed)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 self._iDb.execute(sql_cmd)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 ## Insert a list of Map or Set or Match or Path instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 # @param l a list of object instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 # @param delayed boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 def insertList(self, l, delayed = False):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 for i in l:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 self.insert(i, delayed)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 ## Give the data contained in the table as a list of coord object instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 # @return lObject list of coord object instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 def getListOfAllCoordObject( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 sqlCmd = "SELECT * FROM %s" % ( self._table )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 lObjs = self._iDb.getObjectListWithSQLCmd( sqlCmd, self._getInstanceToAdapt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 return lObjs
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 ## Generate sql command for GetListOverlappingCoord method
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 # @param obj Map, Set or Match instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 # @param delayed boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 # @return sqlCmd string generated sql command
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 def _genSqlCmdForInsert(self, obj, delayed):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 sqlCmd = 'INSERT '
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 if delayed :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 sqlCmd += ' DELAYED '
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 type2Insert, attr2Insert = self._getTypeAndAttr2Insert(obj)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 sqlCmd += 'INTO %s VALUES (' % (self._table)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 sqlCmd += ",".join(type2Insert)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 sqlCmd += ")"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 sqlCmd = sqlCmd % attr2Insert
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 return sqlCmd
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 def _getTypeAndAttr2Insert(self, obj):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 pass
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 def _getInstanceToAdapt(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 pass
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 def _escapeAntislash(self, obj):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 pass