<commit_before>#
# Copyright (c) 2005-2006 rPath, Inc.
#
# All Rights Reserved
#
import sys, time
import weakref

from conary.dbstore import sqlerrors

from mint.mint_error import MintError

class ItemNotFound(MintError):
    def __init__(self, item = "item"):
        self.item = item

    def __str__(self):
        return "requested %s not found" % self.item

class DuplicateItem(MintError):
    def __init__(self, item = "item"):
        self.item = item

    def __str__(self):
        return "duplicate item in %s" % self.item

class UpToDateException(MintError):
    def __init__(self, table = "Unknown Table"):
        self.table = table

    def __str__(self):
            return "The table '%s' is not up to date" % self.table

def concat(db, *items):
    if db.driver == "mysql":
        return "CONCAT(%s)" % ", ".join(items)
    elif db.driver == "sqlite":
        return " || ".join(items)
    raise Exception("Unsupported database")

class TableObject(object):
    """A simple base class defining a object-oriented interface to an SQL table.
       @cvar server: Internal L{mint.server.MintServer} object to modify the object in
                      the database.
       @cvar id: The database primary key of the current object.
    """
    __slots__ = ('server', 'id')

    def getItem(self, id):
        """Abstract method to retrieve information about an object from the database
           and fill the object members with that information.

           @param id: database primary key"""
        raise NotImplementedError

    def __init__(self, server, id):
        """@param server: a L{mint.server.MintServer} object
                          for manipulation of the item
                          represented by this object.
           @param id: database primary key of the item
                      to be represented by this object.
        """
        self.id = id
        self.server = server
        self.refresh()

    def refresh(self):
        """Refreshes the object's internal fields of data about the item by forcing
           a call to L{getItem}."""
        data = self.getItem(self.id)
        for key, val in data.items():
            self.__setattr__(key, val)

    def getId(self):
        """@return: database primary key of the item represented by this object"""
        return self.id

class DatabaseTable(object):
    """
    @cvar name: The name of the table as created by the createSQL string.
    @cvar fields: List of SQL fields as created by the createSQL string.
    @cvar createSQL: SQL statement to create the table for this object.
    @cvar indexes: A dictionary of indeces mapped to the SQL queries to create
    those indeces
    """

    schemaVersion = 28
    name = "Table"
    fields = []
    createSQL = "CREATE TABLE Table ();"
    createSQL_mysql = None
    indexes = {}
    views = {}

    def __init__(self, db):
        """@param db: database connection object. database object will be
        stored by weak reference only. an instance of it must exist outside
        of DatabaseTable objects at all times. This is normally not an issue,
        since MintServer object holds the db and all DatabaseTables objects."""
        assert(self.name and self.fields and self.createSQL)
        self.db = db

        cu = self.db.cursor()

        # create missing tables
        if self.name not in self.db.tables:
            cu.execute(self.createSQL % self.db.keywords)
            self.initialCreation = True
        else:
            self.initialCreation = False

        # Don't commit here.  Commits must be handled up stream to enable
        # upgrading
        self.upToDate = self.versionCheck()

        # create missing indexes, but only after upgrading.  Missing indeces
        # may be created through the regular index routines instead of being
        # explicitly created in the upgrade code.  If indeces are created
        # before the upgrade check is done, a column not found exception
        # could be raised if a new index is created referencing a new column
        # created in the upgrade procedures.

        if self.upToDate:
            self.db.loadSchema()
            indexes = set(self.db.tables[self.name])
            missingIndexes = set(self.indexes.keys()) - indexes

            for index in missingIndexes:
                cu.execute(self.indexes[index])

            for view in self.views.keys():
                if view.lower() not in [x.lower() for x in self.db.views.keys()]:
                    cu.execute("CREATE VIEW %s AS %s" % (view, self.views[view]))

    def __getattribute__(self, name):
        if name == 'db':
            return object.__getattribute__(self, name)()
        return object.__getattribute__(self, name)

    def __setattr__(self, name, val):
        if name == 'db' and not isinstance(val, weakref.ref):
            return object.__setattr__(self, name, weakref.ref(val))
        return object.__setattr__(self, name, val)

    def getDBVersion(self):
        cu = self.db.cursor()

        cu.execute("SELECT COALESCE(MAX(version), 0) FROM DatabaseVersion")
        version = cu.fetchone()[0]

        if not version:
            cu.execute("""INSERT INTO DatabaseVersion(version, timestamp)
                              VALUES(?,?)""", self.schemaVersion, time.time())
            version = self.schemaVersion
        return version

    def versionCheck(self):<commit_msg>only create views once we're fully up-to-date<commit_after>#
# Copyright (c) 2005-2006 rPath, Inc.
#
# All Rights Reserved
#
import sys, time
import weakref

from conary.dbstore import sqlerrors

from mint.mint_error import MintError

class ItemNotFound(MintError):
    def __init__(self, item = "item"):
        self.item = item

    def __str__(self):
        return "requested %s not found" % self.item

class DuplicateItem(MintError):
    def __init__(self, item = "item"):
        self.item = item

    def __str__(self):
        return "duplicate item in %s" % self.item

class UpToDateException(MintError):
    def __init__(self, table = "Unknown Table"):
        self.table = table

    def __str__(self):
            return "The table '%s' is not up to date" % self.table

def concat(db, *items):
    if db.driver == "mysql":
        return "CONCAT(%s)" % ", ".join(items)
    elif db.driver == "sqlite":
        return " || ".join(items)
    raise Exception("Unsupported database")

class TableObject(object):
    """A simple base class defining a object-oriented interface to an SQL table.
       @cvar server: Internal L{mint.server.MintServer} object to modify the object in
                      the database.
       @cvar id: The database primary key of the current object.
    """
    __slots__ = ('server', 'id')

    def getItem(self, id):
        """Abstract method to retrieve information about an object from the database
           and fill the object members with that information.

           @param id: database primary key"""
        raise NotImplementedError

    def __init__(self, server, id):
        """@param server: a L{mint.server.MintServer} object
                          for manipulation of the item
                          represented by this object.
           @param id: database primary key of the item
                      to be represented by this object.
        """
        self.id = id
        self.server = server
        self.refresh()

    def refresh(self):
        """Refreshes the object's internal fields of data about the item by forcing
           a call to L{getItem}."""
        data = self.getItem(self.id)
        for key, val in data.items():
            self.__setattr__(key, val)

    def getId(self):
        """@return: database primary key of the item represented by this object"""
        return self.id

class DatabaseTable(object):
    """
    @cvar name: The name of the table as created by the createSQL string.
    @cvar fields: List of SQL fields as created by the createSQL string.
    @cvar createSQL: SQL statement to create the table for this object.
    @cvar indexes: A dictionary of indeces mapped to the SQL queries to create
    those indeces
    """

    schemaVersion = 28
    name = "Table"
    fields = []
    createSQL = "CREATE TABLE Table ();"
    createSQL_mysql = None
    indexes = {}
    views = {}

    def __init__(self, db):
        """@param db: database connection object. database object will be
        stored by weak reference only. an instance of it must exist outside
        of DatabaseTable objects at all times. This is normally not an issue,
        since MintServer object holds the db and all DatabaseTables objects."""
        assert(self.name and self.fields and self.createSQL)
        self.db = db

        cu = self.db.cursor()

        # create missing tables
        if self.name not in self.db.tables:
            cu.execute(self.createSQL % self.db.keywords)
            self.initialCreation = True
        else:
            self.initialCreation = False

        # Don't commit here.  Commits must be handled up stream to enable
        # upgrading
        self.upToDate = self.versionCheck()

        # create missing indexes, but only after upgrading.  Missing indeces
        # may be created through the regular index routines instead of being
        # explicitly created in the upgrade code.  If indeces are created
        # before the upgrade check is done, a column not found exception
        # could be raised if a new index is created referencing a new column
        # created in the upgrade procedures.

        if self.upToDate:
            self.db.loadSchema()
            indexes = set(self.db.tables[self.name])
            missingIndexes = set(self.indexes.keys()) - indexes

            for index in missingIndexes:
                cu.execute(self.indexes[index])

            if self.schemaVersion == self.getDBVersion():
                for view in self.views.keys():
                    if view.lower() not in [x.lower() for x in self.db.views.keys()]:
                        cu.execute("CREATE VIEW %s AS %s" % (view, self.views[view]))

    def __getattribute__(self, name):
        if name == 'db':
            return object.__getattribute__(self, name)()
        return object.__getattribute__(self, name)

    def __setattr__(self, name, val):
        if name == 'db' and not isinstance(val, weakref.ref):
            return object.__setattr__(self, name, weakref.ref(val))
        return object.__setattr__(self, name, val)

    def getDBVersion(self):
        cu = self.db.cursor()

        cu.execute("SELECT COALESCE(MAX(version), 0) FROM DatabaseVersion")
        version = cu.fetchone()[0]

        if not version:
            cu.execute("""INSERT INTO DatabaseVersion(version, timestamp)
                              VALUES(?,?)""", self.schemaVersion, time.time())
            version = self.schemaVersion
        return version

    def versionCheck(self):
{'fraction_non_alphanumeric': 0.09129162092938163, 'fraction_numerical': 0.0021953896816684962, 'mean_word_length': 2.2587183308494785, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1497971', 'n_tokens_mistral': 2970, 'n_tokens_neox': 2871, 'n_words': 1086}