예제 #1
0
 def execute(self,*args,**kw):
     ## For when sql is executed manually (bypassing AdvancedTable)
     if isinstance(self.row_factory,objects.AdvancedRow_Factory) and not self.row_factory.parent:
         with Utilities.temp_row_factory(self,objects.dict_factory):
             return super().execute(*args,**kw)
     ## Otherwise, continue as normal
     return super().execute(*args,**kw)
예제 #2
0
 def gettablestats(self,tablename):
     """ Returns the information stored in sqlite_master as a dict for the given table """
     if isinstance(tablename,Table.Table):
         tablename = str(tablename.name)
     if not isinstance(tablename,str):
         raise TypeError("Invalid tablename")
     with Utilities.temp_row_factory(self,objects.dict_factory):
         return self.execute("""SELECT rowid,* from sqlite_master WHERE name = ? AND type = "table";""",(tablename,)).fetchone()
예제 #3
0
 def test_dropcolumn(self):
     """ Checks that string works """
     self.db.addtables(self.table)
     dropcol = Utilities.generate_dropcolumn(self.table, "a")
     self.db.executescript(dropcol)
     advtable = self.db.getadvancedtable("a")
     self.assertEqual(len(advtable.columns), 1)
     self.assertIn("b", advtable.columns)
     self.assertNotIn("a", advtable.columns)
예제 #4
0
    def test_match_text(self):
        """ Checks for expected output string """
        self.assertRegex(
            Utilities.generate_dropcolumn(self.table, "a"),
            re.compile(
                """BEGIN TRANSACTION;
\s*CREATE TEMPORARY TABLE a__temporary__\(\s*b TEXT\s*\);\s*INSERT INTO a__temporary__ SELECT b FROM a;
\s*DROP TABLE a;\s*CREATE TABLE a\(\s*b TEXT\s*\);\s*INSERT INTO a SELECT b FROM a__temporary__;
\s*DROP TABLE a__temporary__;\s*COMMIT;""".replace(" ", "\s+"),
                re.IGNORECASE | re.VERBOSE))
예제 #5
0
 def test_dataintegrity(self):
     """ Makes sure that remaining columns have their correct values """
     input = [{"a": i, "b": str(i * 3)} for i in range(100)]
     self.db.row_factory = dict_factory
     self.db.addtables(self.table)
     advtable = self.db.getadvancedtable("a")
     advtable.addmultiple(*input)
     self.assertEqual(advtable.selectall(), input)
     dropcol = Utilities.generate_dropcolumn(self.table, "a")
     self.db.executescript(dropcol)
     advtable = self.db.getadvancedtable("a")
     for i in input:
         del i['a']
     self.assertEqual(advtable.selectall(), input)
예제 #6
0
 def test_gettablebyid(self):
     """ Tests that tables can be gotten by id """
     utils.setupadditionaltables(self)
     with Utilities.temp_row_factory(self.connection, sql.dict_factory):
         tableresults = self.connection.execute(
             """SELECT rowid,tbl_name FROM sqlite_master WHERE type='table';"""
         ).fetchall()
     ## Ensure gettablebyid also sets row_factory
     self.connection.row_factory = objects.advancedrow_factory
     for table in tableresults:
         with self.subTest(table=table):
             result = self.connection.gettablebyid(table['rowid'])
             self.assertEqual(result.name, table['tbl_name'])
             self.assertFalse(result.row_factory is None)
예제 #7
0
    def test_temp_row_factory(self):
        """ Tests the temp_row_factory Context Manager works as advertised """
        utils.setupconnection(self)
        row = self.connection.execute(" SELECT 1 AS myvalue;").fetchone()
        self.assertFalse(isinstance(row,dict))
        self.assertEqual(row[0],1)

        with Utilities.temp_row_factory(self.connection, sql.dict_factory):
            row = self.connection.execute(" SELECT 1 AS myvalue;").fetchone()
            self.assertTrue(isinstance(row,dict))
            self.assertEqual(row['myvalue'],1)

        row = self.connection.execute(" SELECT 1 AS myvalue;").fetchone()
        self.assertFalse(isinstance(row,dict))
        self.assertEqual(row[0],1)
예제 #8
0
    def gettablebyid(self,rowid):
        """ Returns a Table Object representing the table with the given rowid.

        rowid should be an integer which represents the table's rowid in the sqlite_master table.
        Raises a ValueError if the table does not exist.
        """
        if not isinstance(rowid,int):
            raise TypeError("rowid should be an integer")
        with Utilities.temp_row_factory(self,objects.dict_factory):
            tableentry = self.execute("""SELECT sql FROM sqlite_master WHERE type="table" AND rowid=?;""",(rowid,)).fetchone()
        if not tableentry:
            raise ValueError(f"Table {tablename} does not exist.")

        if self.parser: return self.parser(tableentry['sql'],database = self).obj.to_advancedtable()
        return Table.AdvancedTable(tableentry['sql'],database = self)