Esempio n. 1
0
    def _load_fragment_data_by_title(self, db_connection, wikia, title):
        self.wikia = wikia

        sql = ("""
            SELECT f.fragment_id, f.article, f.title, f2.title as parent_fragment_title
            FROM fragments AS f
            LEFT OUTER JOIN fragments AS f2
            ON (f.parent_fragment_id = f2.fragment_id)
            WHERE
            f.title = %(article_title)s AND
            f.wikia_id = %(wikia_id)s
        """)

        params = {
            'article_title': title,
            'wikia_id': self.wikia.wikia_id
        }

        fragment_data = fetchone(db_connection, sql, params)
        if fragment_data is None:
            raise FragmentNotFoundException()

        # parse the data into our fields
        self.fragment_id = str(fragment_data['fragment_id'])
        self.article = str(fragment_data['article'])

        if fragment_data['parent_fragment_title'] is not None:
            self.parent_fragment_title = str(fragment_data['parent_fragment_title'])
        else:
            self.parent_fragment_title = None

        self.title = str(fragment_data['title'])
Esempio n. 2
0
    def _save_fragment(self, db_connection):
        '''
        :param db_connection: an active database connection
        NOTE: this function will update this object's fragment_id, if needed
        :return:
        '''

        # First, go fetch the parent fragment's id, if it exists
        # Yes, I realize that this could probably be combined into a single query with the next one
        sql = ("""
            SELECT f.fragment_id
            FROM fragments AS f
            WHERE
            f.title = %(parent_fragment_title)s AND
            f.wikia_id = %(wikia_id)s
        """)

        params = {
            'parent_fragment_title': self.parent_fragment_title,
            'wikia_id': self.wikia.wikia_id
        }

        row = fetchone(db_connection, sql, params)
        parent_fragment_id = None
        if row is not None:
            parent_fragment_id = row['fragment_id']

        sql = ("""
            INSERT INTO fragments
            (fragment_id, wikia_id, article, title, parent_fragment_id)
            VALUES
            (%(fragment_id)s, %(wikia_id)s, %(article)s, %(title)s, %(parent_fragment_id)s)
            ON DUPLICATE KEY UPDATE
            wikia_id = VALUES(wikia_id),
            article = VALUES(article),
            title = VALUES(title),
            parent_fragment_id = VALUES(parent_fragment_id)
        """)

        params = {
            'fragment_id': self.fragment_id,
            'wikia_id': self.wikia.wikia_id,
            'article': self.article,
            'title': self.title,
            'parent_fragment_id': parent_fragment_id
        }

        lastrowid = insert(db_connection, sql, params)
        if lastrowid != 0:
            self.fragment_id = str(lastrowid)
Esempio n. 3
0
    def load_by_name(self, db_connection, name):
        sql = ("""
            SELECT w.wikia_id
            FROM wikias AS w
            WHERE
            w.wikia_name = %(wikia_name)s
        """)

        params = {
            'wikia_name': name
        }

        wikia_data = fetchone(db_connection, sql, params)
        if wikia_data is None:
            raise WikiaNotFoundException()

        self.wikia_name = name
        self.wikia_id = wikia_data['wikia_id']