Exemplo n.º 1
0
    def execute(self, *args):
        super().execute()

        input_valid = IOInput(self._io)
        input_valid()

        for k, v in self.__dict__.items():
            self._logger.info("%s : %s" % (k, v))

        param_valid = EssentialParameters(self.__class__.__name__,
                                          [self._tblname])
        param_valid()

        tbl_valid = SqliteTableExistence(self._dbname, self._tblname)
        tbl_valid()

        def dict_factory(cursor, row):
            d = {}
            for i, col in enumerate(cursor.description):
                d[col[0]] = row[i]
            return d

        self._sqlite_adptr.connect(self._dbname)
        cur = self._sqlite_adptr.fetch(sql=self.__get_query(),
                                       row_factory=dict_factory)
        for r in cur:
            self._s.save(r)
Exemplo n.º 2
0
    def execute(self, *args):
        super().execute()

        param_valid = EssentialParameters(self.__class__.__name__,
                                          [self._tblname])
        param_valid()

        tbl_valid = SqliteTableExistence(self._dbname, self._tblname)
        tbl_valid()

        output_valid = IOOutput(self._io)
        output_valid()

        # get table column definition
        self._sqlite_adptr.connect(self._dbname)
        column_def = self.__get_column_def()

        if self._refresh is True:
            self.__refresh_table(column_def)

        # database transaction
        def insert():
            self._logger.info("Start to insert")
            insert_rows = []
            with open(self._s.cache_file, "r", encoding="utf-8") as f:
                for i, l_str in enumerate(f, 1):
                    l_dict = ast.literal_eval(l_str)
                    insert_rows.append(l_dict)

                    # Check only once
                    if i == 1:
                        self.__valid_column_def(column_def, l_dict)

                    # execute bulk insert
                    if i % self._insert_cnt == 0:
                        self._sqlite_adptr.execute_many_insert(
                            self._tblname, column_def, insert_rows,
                            self._replace_into)
                        insert_rows.clear()

                if len(insert_rows) > 0:
                    self._sqlite_adptr.execute_many_insert(
                        self._tblname, column_def, insert_rows,
                        self._replace_into)
                    insert_rows.clear()

            self._logger.info("Finish to insert")

        super().execute(insert)
        self._s.remove()
Exemplo n.º 3
0
    def test_table_existence_ok_with_bool(self):
        """
        SqliteTableExistence invalid case
        """
        # create test db and insert dummy data
        os.makedirs(self.__db_dir)
        db_file = os.path.join(self.__db_dir, "spam.db")
        conn = sqlite3.connect(db_file)
        conn.execute("create table spam_table (id, name, age);")
        conn.execute("insert into spam_table (id, name, age) values(1,1,1);")
        conn.commit()
        conn.close()

        valid = SqliteTableExistence(db_file, "spam_table", True)
        exists_tbl = valid()
        shutil.rmtree(self.__db_dir)
        assert exists_tbl is True
Exemplo n.º 4
0
    def test_table_existence_ng_with_exc(self):
        """
        SqliteTableExistende invalid case
        """
        # create test db and insert dummy data
        os.makedirs(self.__db_dir)
        db_file = os.path.join(self.__db_dir, "spam.db")
        conn = sqlite3.connect(db_file)
        conn.execute("create table spam_table (id, name, age);")
        conn.execute("insert into spam_table (id, name, age) values(1,1,1);")
        conn.commit()
        conn.close()

        with pytest.raises(SqliteInvalid) as excinfo:
            valid = SqliteTableExistence(db_file, "spam_table2")
            valid()
        shutil.rmtree(self.__db_dir)
        assert "not found" in str(excinfo.value)