Beispiel #1
0
def del_table(_conn: r.net.DefaultConnection,
              _name_table: str,
              _name_db: str = rtm_cfg_name_db,
              _expr: bool = False):
    p = False
    while True:
        try:
            if not cdn(_name_db):
                cdw_prevent_creation_or_deletion_if_string_check_fail(
                    _name_db, True, False)
                return None
            if not ctn(_name_table):
                cdw_prevent_creation_or_deletion_if_string_check_fail(
                    _name_table, False, False)
                return None

            if not check_db(_conn, _name_db):
                return None

            if not check_table(_conn, _name_table, _name_db):
                return None

            if _expr:
                return r.db(_name_db).table_drop(_name_table)
            else:
                return r.db(_name_db).table_drop(_name_table).run(_conn)
        except r.errors.ReqlDriverError:
            _conn.reconnect()

            if not p:
                print("\n{}{}\n{}".format(
                    "there is no database connection and/or there is no ",
                    "internet connection", "re - trying database connection"))
            p = True
Beispiel #2
0
def del_doc(_conn: r.net.DefaultConnection,
            _value: str,
            _name_column: str,
            _name_table: str,
            _name_db: str = rtm_cfg_name_db,
            _expr: bool = False):
    """ Delete document based on column and its value. If there are more then
    one document has the same value on its column then multiple documents will
    be deleted.
    """
    p = False
    while True:
        try:
            if not cdn(_name_db):
                cdw_prevent_creation_or_deletion_if_string_check_fail(
                    _name_db, True, False)
                return None
            if not ctn(_name_table):
                cdw_prevent_creation_or_deletion_if_string_check_fail(
                    _name_table, False, False)
                return None

            if not check_db(_conn, _name_db):
                return None

            if not check_table(_conn, _name_table, _name_db):
                return None

            if not check_doc(_conn, _value, _name_column, _name_table,
                             _name_db):
                return None

            if _expr:
                return r.db(_name_db).table(_name_table)\
                    .filter({ _name_column:_value }).delete()
            else:
                return r.db(_name_db).table(_name_table)\
                    .filter({ _name_column:_value }).delete().run(_conn)
        except r.errors.ReqlDriverError:
            _conn.reconnect()

            if not p:
                print("\n{}{}\n{}".format(
                    "there is no database connection and/or there is no ",
                    "internet connection", "re - trying database connection"))
            p = True
Beispiel #3
0
def check_db(_conn: r.net.DefaultConnection, _name_db: str = rtm_cfg_name_db):
    p = False
    while True:
        try:
            if r.db_list().contains(_name_db).run(_conn):
                if not cdn(_name_db):
                    cw_db_mod(_name_db)
                return True
            break
        except r.errors.ReqlDriverError:
            _conn.reconnect()

            if not p:
                print("\n{}{}\n{}".format(
                    "there is no database connection and/or there is no ",
                    "internet connection", "re - trying database connection"))
            p = True

    return False
Beispiel #4
0
def create_doc(_conn: r.net.DefaultConnection,
               _dict: dict,
               _name_table: str,
               _name_db: str = rtm_cfg_name_db,
               _unique_column: list = [],
               _expr: bool = False):
    p = False
    while True:
        try:
            if not cdn(_name_db):
                cdw_prevent_creation_or_deletion_if_string_check_fail(
                    _name_db, True, True)
                return None
            if not ctn(_name_table):
                cdw_prevent_creation_or_deletion_if_string_check_fail(
                    _name_table, False, True)
                return None
            """Make sure the document's value is unique based on `_unique_column`."""
            if check_db(_conn, _name_db) and\
               check_table(_conn, _name_table, _name_db):
                for i in _unique_column:
                    if check_doc(_conn, _dict[i], i, _name_table, _name_db):
                        return None

            if _expr:
                return r.db(_name_db).table(_name_table).insert(_dict)
            else:
                return r.db(_name_db).table(_name_table).insert(_dict).run(
                    _conn)
        except r.errors.ReqlDriverError:
            _conn.reconnect()

            if not p:
                print("\n{}{}\n{}".format(
                    "there is no database connection and/or there is no ",
                    "internet connection", "re - trying database connection"))
            p = True
Beispiel #5
0
    def test_check_db_name(self):
        true = [
            "db", "db_1", "db_alpha", "db_alpha_1", "dbalpha", "dbalpha_1"
        ]

        false = [
            " db ", " Db ", " db 1 ", " Db 1 ", " db alpha ", " Db alpha ",
            " db alpha 1 ", " Db alpha 1 ", "db!", "Db!", "db!alpha",
            "db!Alpha", "Db!alpha", "Db!Alpha", "db!alpha!1", "db!Alpha!1",
            "Db!alpha!1", "Db!Alpha!1", "db1", "Db1", "dbAlpha", "DbAlpha",
            "dbAlpha1", "DbAlpha1"
        ]

        for i in true:
            self.assertTrue(cdn(i))

        for i in false:
            self.assertFalse(cdn(i))



    def test_check_table_name(self):
        true = [
            "table", "table_alpha", "table_1", "tableAlpha", "tableAlpha_1",
            "table_alpha_1"
        ]

        false = [
            " table ", " Table ", " table1 ", " Table1 ", "!table!",
            "!Table!", "!table!1!", "!Table!1!", "table1", "Table1"