def _update(self,**kw): """UPDATE Query. Like insert generate a preconfigured sql string and quote the values. Check if a preconfigured WHERE string is preformatted and charge it to the query update.""" if self._where_string is None: raise DBQueryError(e) conn = Query() conn.setConnection() dataValues = dict([(column, value) for column, value in kw.iteritems()]) keys = dataValues.keys() values = [ dataValues[k] for k in keys] sqlString = 'UPDATE "{table}" SET {set} {where};'.format( table = self.__class__.__name__.lower(), set = " , ".join(key+"=%s" for key in keys), where = self._where_string ) final_values = sum([values, self._where_value], []) curr = conn.getCursor() try: curr.execute(sqlString,final_values) conn.commit() except IntegrityError, e: raise DBQueryError(e)
def insert(self,**kw): """INSERT Query. The **kw correspind to the column name and instance the attributes automatically. Generate a correspondent INSERT Query without preconfigured SQL String.""" conn = Query() conn.setConnection() dataValues = dict([(column, value) for column, value in kw.iteritems()]) keys = dataValues.keys() values = [ dataValues[k] for k in keys] sqlString = 'INSERT INTO "{table}"({ins_key}) VALUES ({ins_values});'.format( table = self.__class__.__name__.lower(), ins_key = " , ".join(key for key in keys), ins_values = ",".join("%s" for key in keys) ) curr = conn.getCursor() try: curr.execute(sqlString,values) conn.commit() except IntegrityError, e: raise DBQueryError(e)
def get(self,**kw): """Select Query. If the query don't return a value raise a ObjectDoesNotExist. THis method is used to obtain One RowSet.""" conn = Query() conn.setConnection() dataValues = dict([(column, value) for column, value in kw.iteritems()]) keys = dataValues.keys() values = [ dataValues[k] for k in keys] sqlString = 'SELECT * FROM "{table}" WHERE {where};'.format( table = self.__class__.__name__.lower(), where = " AND ".join(key+"=%s" for key in keys) ) curr = conn.getCursor() try: curr.execute(sqlString,values) conn.commit() results = curr.fetchone() except IntegrityError, e: raise DBQueryError(e)
def sql(self,sql,values,fetch=None,multiple=None): """Generic SQL Method. **ARGS** * *sql:* SQL String preformatted * *value:* dictionary with the values * *fetch:* if is not None the sql return One Row Result * *miltiple:* if is not None the sql return a Dictionary of Rows.""" conn = Query() conn.setConnection() curr = conn.getCursor() results = None try: curr.execute(sql,values) if multiple is not None: results = curr.fetchall() conn.commit() if fetch is not None: results = curr.fetchone() except IntegrityError, e: raise DBQueryError(e)