Example #1
0
    def delete(self, sql, params=None, cursor_mode=CursorMode.CURSOR_MODE):

        if sql.upper().startswith('DROP') | sql.upper().startswith(
                'CREATE') | sql.upper().startswith('INSERT') | sql.upper(
                ).startswith('SELECT') | sql.upper().startswith('UPDATE'):
            raise DBError(key=DBErrorType.OPERATE_NOT_SUPPORT_ERROR,
                          message="Not support this operate")

        conn = None
        cursor = None
        try:
            delete_result = self.__execute(sql=sql,
                                           params=params,
                                           cursor_mode=cursor_mode)
            conn = delete_result["conn"]
            cursor = delete_result["cursor"]

            self.__commit_connection()
            return cursor.rowcount
        except DBError as e:
            raise e
        except Exception as e:
            reraise(DBError(e, sql=sql, params=params), sys.exc_info()[2])
        finally:
            if cursor:
                cursor.close()
            if conn:
                self.__close_connection()
Example #2
0
    def insert(self,
               sql,
               params=None,
               execute_mode=ExecuteMode.ONE_MODE,
               cursor_mode=CursorMode.CURSOR_MODE):

        if sql.upper().startswith('DROP') | sql.upper().startswith(
                'CREATE') | sql.upper().startswith('SELECT') | sql.upper(
                ).startswith('UPDATE') | sql.upper().startswith('DELETE'):
            raise DBError(key=DBErrorType.OPERATE_NOT_SUPPORT_ERROR,
                          message="Not support this operate")

        conn = None
        cursor = None
        try:
            insert_result = self.__execute(sql=sql,
                                           params=params,
                                           execute_mode=execute_mode,
                                           cursor_mode=cursor_mode)
            conn = insert_result["conn"]
            cursor = insert_result["cursor"]
            rowcount = cursor.rowcount

            if execute_mode == ExecuteMode.ONE_MODE:
                result = cursor.lastrowid
            else:
                result = rowcount

            self.__commit_connection()
            return result
        except DBError as e:
            raise e
        except Exception as e:
            reraise(DBError(e, sql=sql, params=params), sys.exc_info()[2])
        finally:
            if cursor:
                cursor.close()
            if conn:
                self.__close_connection()
Example #3
0
    def select(self, sql, params=None, cursor_mode=CursorMode.CURSOR_MODE):

        if sql.upper().startswith('DROP') | sql.upper().startswith(
                'CREATE') | sql.upper().startswith('INSERT') | sql.upper(
                ).startswith('UPDATE') | sql.upper().startswith('DELETE'):
            raise DBError(key=DBErrorType.OPERATE_NOT_SUPPORT_ERROR,
                          message="Not support this operate")

        conn = None
        cursor = None
        try:
            select_result = self.__execute(sql=sql,
                                           params=params,
                                           cursor_mode=cursor_mode)
            conn = select_result["conn"]
            cursor = select_result["cursor"]

            result = []
            column_values = cursor.fetchall()
            if column_values is not None and len(column_values) > 0:
                column_names = [i[0] for i in cursor.description]

                for i, row in enumerate(column_values):
                    tmp = {}
                    for j, value in enumerate(row):
                        tmp[column_names[j]] = value
                    result.append(tmp)
            return result

        except DBError as e:
            raise e
        except Exception as e:
            reraise(DBError(e, sql=sql, params=params), sys.exc_info()[2])
        finally:
            if cursor:
                cursor.close()
            if conn:
                self.__close_connection()