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