示例#1
0
文件: pg.py 项目: sbg/seventweets
 def get_tweet(id_: int, cursor: pg8000.Cursor) -> TwResp:
     """
     Returns single tweet from database.
     :param id_: ID of tweet to get.
     :param cursor: Database cursor.
     :return: Tweet with provided ID.
     """
     cursor.execute(
         f'''
         SELECT {TWEET_COLUMN_ORDER}
         FROM tweets WHERE id=%s;
     ''', (id_, ))
     return cursor.fetchone()
示例#2
0
文件: pg.py 项目: sbg/seventweets
 def get_node(name: str, cursor: pg8000.Cursor) -> NdResp:
     """
     :param name: Name of the node to get.
     :param cursor: Database cursor.
     :return: Node with provided name.
     """
     cursor.execute(
         f'''
         SELECT {NODE_COLUMN_ORDER}
         FROM nodes
         WHERE name=%s;
     ''', (name, ))
     return cursor.fetchone()
示例#3
0
文件: pg.py 项目: sbg/seventweets
    def insert_tweet(tweet: str, cursor: pg8000.Cursor) -> TwResp:
        """
        Inserts new tweet and returns id of the created row

        :param tweet: Content of the tweet to add.
        :param cursor: Database cursor.
        :return: ID of the tweet that was created.
        """
        cursor.execute(
            f'''
            INSERT INTO tweets (tweet) VALUES (%s)
            RETURNING {TWEET_COLUMN_ORDER};
        ''', (tweet, ))
        return cursor.fetchone()
示例#4
0
文件: pg.py 项目: sbg/seventweets
    def insert_node(name: str, address: str, cursor: pg8000.Cursor) -> NdResp:
        """
        Inserts new nodes to database.

        :param name: Name of new node.
        :param address: Address of new node.
        :param cursor: Database cursor.
        :return: Node that was inserted.
        """
        cursor.execute(
            f'''
            INSERT INTO nodes (name, address)
            VALUES (%s, %s)
            RETURNING {NODE_COLUMN_ORDER};
        ''', (name, address))
        return cursor.fetchone()
示例#5
0
文件: pg.py 项目: sbg/seventweets
    def create_retweet(server: str, ref: str, cursor: pg8000.Cursor) -> TwResp:
        """
        Creates retweet in database that references server and tweet ID
        provided in parameters.

        :param server: Server name of original tweet.
        :param ref: Tweet reference (ID) on original server.
        :param cursor: Database cursor.
        :return: Newly created tweet.
        """
        cursor.execute(
            f'''
            INSERT INTO tweets (type, reference)
            VALUES (%s, %s)
            RETURNING {TWEET_COLUMN_ORDER};
        ''', ('retweet', f'{server}#{ref}'))
        return cursor.fetchone()
示例#6
0
文件: pg.py 项目: sbg/seventweets
    def update_node(name: str, address: str, cursor: pg8000.Cursor) -> NdResp:
        """
        Updates existing node.

        :param name: Name of the node to update.
        :param address: New address to set for the node.
        :param cursor: Database cursor.
        :return: Node that was updated.
        """
        cursor.execute(
            f'''
            UPDATE nodes
            SET
                address = %s
            WHERE
                name = %s
            RETURNING {NODE_COLUMN_ORDER};
        ''', (address, name))
        return cursor.fetchone()
示例#7
0
文件: pg.py 项目: sbg/seventweets
    def count_tweets(type_: str, cursor: pg8000.Cursor) -> int:
        """
        Returns number of tweets of specified type.

        :param type_: Type of tweet to count.
        :param cursor: Database cursor.
        """
        where = ''
        params = []
        if type_:
            where = 'WHERE type=%s'
            params.append(type_)
        cursor.execute(
            f'''
            SELECT count(*)
            FROM tweets
            {where}
        ''', tuple(params))
        return cursor.fetchone()[0]
示例#8
0
文件: pg.py 项目: sbg/seventweets
    def modify_tweet(id_: int, new_content: str,
                     cursor: pg8000.Cursor) -> TwResp:
        """
        Updates tweet content.

        :param id_: ID of tweet to update.
        :param new_content: New tweet content.
        :param cursor: Database cursor.
        :return:
            Tweet that was update, if tweet with provided ID was found, None
            otherwise.
        """
        cursor.execute(
            f'''
            UPDATE tweets SET
            tweet=%s,
            modified_at=%s
            WHERE id=%s
            RETURNING {TWEET_COLUMN_ORDER};
        ''', (new_content, datetime.utcnow(), id_))
        return cursor.fetchone()