Python檢查寫入MySQL的數據是否符合字段的數據類型定義

MySQL等傳統數據庫都有字段數據類型定義的設計。定義好字段類型以後,往字段裏面插入/更新的數據,就要複合其數據類型的定義了,不然會寫入報錯。程序中往數據庫裏寫入數據的時候,爲了避免寫入錯誤的發生,往往在寫入之前需要判斷一下寫入數據是否複合數據庫定義。以下是自己寫的MySQL / MariaDB中一些主要的常規數據類型的Python檢查。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


"""
create_author : 蛙鱖雞鸛狸猿
create_time   : 2019-05-10
program       : *_* check validity of data to be written into MySQL / MariaDB *_*
"""


import re


def check_tinyint_column(column, if_none=False, if_sign=False):
    """
    Check if it is valid of data to be written to satisfy TINYINT(MySQL / MariaDB) type columns.
    :param column: column(data) to write into MySQL / MariaDB.
    :param if_none: whether none value is valid or not.
    :param if_sign: whether integer values signed or not.
    :return: Boolean.
    """
    if if_none:
        if re.match("^$", column):
            return True
        else:
            pass
    elif not if_none:
        if re.match("^$", column):
            return False
        else:
            pass
    try:
        int(column)
    except ValueError:
        return False
    min_check = None
    max_check = None
    if if_sign:
        min_check = -128
        max_check = 127
    elif not if_sign:
        min_check = 0
        max_check = 255
    int_column = int(column)
    if max_check >= int_column >= min_check:
        return True
    else:
        return False


def check_smallint_column(column, if_none=False, if_sign=False):
    """
    Check if it is valid of data to be written to satisfy SMALLINT(MySQL / MariaDB) type columns.
    :param column: column(data) to write into MySQL / MariaDB.
    :param if_none: whether none value is valid or not.
    :param if_sign: whether integer values signed or not.
    :return: Boolean.
    """
    if if_none:
        if re.match("^$", column):
            return True
        else:
            pass
    elif not if_none:
        if re.match("^$", column):
            return False
        else:
            pass
    try:
        int(column)
    except ValueError:
        return False
    min_check = None
    max_check = None
    if if_sign:
        min_check = -32768
        max_check = 32767
    elif not if_sign:
        min_check = 0
        max_check = 65535
    int_column = int(column)
    if max_check >= int_column >= min_check:
        return True
    else:
        return False


def check_mediumint_column(column, if_none=False, if_sign=False):
    """
    Check if it is valid of data to be written to satisfy MEDIUMINT(MySQL / MariaDB) type columns.
    :param column: column(data) to write into MySQL / MariaDB.
    :param if_none: whether none value is valid or not.
    :param if_sign: whether integer values signed or not.
    :return: Boolean.
    """
    if if_none:
        if re.match("^$", column):
            return True
        else:
            pass
    elif not if_none:
        if re.match("^$", column):
            return False
        else:
            pass
    try:
        int(column)
    except ValueError:
        return False
    min_check = None
    max_check = None
    if if_sign:
        min_check = -8388608
        max_check = 8388607
    elif not if_sign:
        min_check = 0
        max_check = 16777215
    int_column = int(column)
    if max_check >= int_column >= min_check:
        return True
    else:
        return False


def check_int_column(column, if_none=False, if_sign=False):
    """
    Check if it is valid of data to be written to satisfy INT(MySQL / MariaDB) type columns.
    :param column: column(data) to write into MySQL / MariaDB.
    :param if_none: whether none value is valid or not.
    :param if_sign: whether integer values signed or not.
    :return: Boolean.
    """
    if if_none:
        if re.match("^$", column):
            return True
        else:
            pass
    elif not if_none:
        if re.match("^$", column):
            return False
        else:
            pass
    try:
        int(column)
    except ValueError:
        return False
    min_check = None
    max_check = None
    if if_sign:
        min_check = -2147483648
        max_check = 2147483647
    elif not if_sign:
        min_check = 0
        max_check = 4294967295
    int_column = int(column)
    if max_check >= int_column >= min_check:
        return True
    else:
        return False


def check_bigint_column(column, if_none=False, if_sign=False):
    """
    Check if it is valid of data to be written to satisfy BIGINT(MySQL / MariaDB) type columns.
    :param column: column(data) to write into MySQL / MariaDB.
    :param if_none: whether none value is valid or not.
    :param if_sign: whether integer values signed or not.
    :return: Boolean.
    """
    if if_none:
        if re.match("^$", column):
            return True
        else:
            pass
    elif not if_none:
        if re.match("^$", column):
            return False
        else:
            pass
    try:
        int(column)
    except ValueError:
        return False
    min_check = None
    max_check = None
    if if_sign:
        min_check = -9223372036854775808
        max_check = 9223372036854775807
    elif not if_sign:
        min_check = 0
        max_check = 18446744073709551615
    int_column = int(column)
    if max_check >= int_column >= min_check:
        return True
    else:
        return False


def check_char_column(column, length, if_none=False):
    """
    Check if it is valid of data to be written to satisfy CHAR(MySQL / MariaDB) type columns.
    :param column: column(data) to write into MySQL / MariaDB.
    :param length: design of data length.
    :param if_none: whether none value is valid or not.
    :return: Boolean.
    """
    pattern = None
    if if_none:
        pattern = "(^.{%d}$)|(^$)" % length
    elif not if_none:
        pattern = "^.{%d}$" % length
    if re.match(pattern, column):
        return True
    else:
        return False


def check_varchar_column(column, length, if_none=False):
    """
    Check if it is valid of data to be written to satisfy VARCHAR(MySQL / MariaDB) type columns.
    :param column: column(data) to write into MySQL / MariaDB.
    :param length: design of data length.
    :param if_none: whether none value is valid or not.
    :return: Boolean.
    """
    pos = None
    if if_none:
        pos = 0
    elif not if_none:
        pos = 1
    pattern = "^.{%d,%d}$" % (pos, length)
    if re.match(pattern, column):
        return True
    else:
        return False


def check_datetime_column(column, if_none=False):
    """
    Check if it is valid of data to be written to satisfy DATETIME / TIMESTAMP(MySQL / MariaDB) type columns.
    :param column: column(data) to write into MySQL / MariaDB.
    :param if_none: whether none value is valid or not.
    :return: Boolean.
    """
    pattern = None
    if if_none:
        pattern = r"(^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$)|(^$)"
    elif not if_none:
        pattern = r"^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$"
    if re.match(pattern, column):
        return True
    else:
        return False

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章