How to use the psycopg2.sql.SQL function in psycopg2

To help you get started, we’ve selected a few psycopg2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github LMFDB / lmfdb / lmfdb / backend / table.py View on Github external
def set_sort(self, sort, resort=True, commit=True):
        """
        Change the default sort order for this table

        INPUT:

        - ``sort`` -- a list of columns or pairs (col, direction) where direction is 1 or -1.
        - ``resort`` -- whether to resort the table ids after changing the sort.
        """
        self._set_sort(sort)
        with DelayCommit(self, commit, silence=True):
            if sort:
                updater = SQL("UPDATE meta_tables SET sort = %s WHERE name = %s")
                values = [sort, self.search_table]
            else:
                updater = SQL("UPDATE meta_tables SET sort = NULL WHERE name = %s")
                values = [self.search_table]
            self._execute(updater, values)
            self._break_order()
            if resort:
                self.resort()

            # add an index for the default sort
            if not any(index["columns"] == sort for index_name, index in self.list_indexes().items()):
                self.create_index(sort)
            self.log_db_change("set_sort", sort=sort)
github LMFDB / lmfdb / lmfdb / backend / table.py View on Github external
If None, will use ``self.extra_table``; another common input is ``self.extra_table + "_tmp"``.
        """
        print("resorting disabled")
        return
        self._check_locks()
        with DelayCommit(self, silence=True):
            if self._id_ordered and (search_table is not None or self._out_of_order):
                now = time.time()
                search_table, extra_table = self._identify_tables(search_table, extra_table)
                newid = "newid"
                while newid in self.search_cols or newid in self.extra_cols:
                    newid += "_"
                newid = Identifier(newid)
                oldid = Identifier("id")
                addcol = SQL("ALTER TABLE {0} ADD COLUMN {1} bigint")
                dropcol = SQL("ALTER TABLE {0} DROP COLUMN {1}")
                movecol = SQL("ALTER TABLE {0} RENAME COLUMN {1} TO {2}")
                pkey = SQL("ALTER TABLE {0} ADD PRIMARY KEY ({1})")
                self._execute(addcol.format(search_table, newid))
                updater = SQL(
                    "UPDATE {0} SET {1} = newsort.newid "
                    "FROM (SELECT id, ROW_NUMBER() OVER(ORDER BY {2}) AS newid FROM {0}) "
                    "newsort WHERE {0}.id = newsort.id"
                )
                updater = updater.format(search_table, newid, self._sort)
                self._execute(updater)
                if extra_table is not None:
                    self._execute(addcol.format(extra_table, newid))
                    updater = SQL(
                        "UPDATE {0} SET {1} = search_table.{1} "
                        "FROM (SELECT id, {1} FROM {2}) search_table "
                        "WHERE {0}.id = search_table.id"
github LMFDB / lmfdb / lmfdb / verify / verification.py View on Github external
- ``join2`` -- a column or list of columns (default: `None`) on ``other_table`` on which we will join the two tables. If `None`, we take ``join2`` = ``join1``, see `_make_join`
        - ``constraint`` -- a dictionary, as passed to the search method
        - ``subselect_wrapper`` -- a string, e.g., "ARRAY" to convert the inner select query
        - ``extra`` -- SQL object to append to the subquery.  This can hold additional constraints or set the sort order for the inner select query
        """
        # WARNING: since it uses _run_query, this whole function is not safe against SQL injection,
        # so should only be run locally in data validation
        join = self._make_join(join1, join2)
        col = self._make_sql(col, "t1")
        if isinstance(quantity, string_types):
            quantity = SQL("t2.{0}").format(Identifier(quantity))
        # This is unsafe
        subselect_wrapper = SQL(subselect_wrapper)
        if extra is None:
            extra = SQL("")
        condition = SQL("{0} != {1}(SELECT {2} FROM {3} t2 WHERE {4}{5})").format(
            col,
            subselect_wrapper,
            quantity,
            Identifier(other_table),
            join,
            extra)
        return self._run_query(condition, constraint, values, table=SQL("{0} t1").format(Identifier(self.table.search_table)))
github VOLTTRON / volttron / volttron / platform / dbutils / postgresqlfuncts.py View on Github external
def collect_aggregate(self, topic_ids, agg_type, start=None, end=None):
        if (isinstance(agg_type, str) and
                agg_type.upper() not in self.get_aggregation_list()):
            raise ValueError('Invalid aggregation type {}'.format(agg_type))
        query = [
            SQL('SELECT {}(CAST(value_string as float)), COUNT(value_string)'.format(
                agg_type.upper())),
            SQL('FROM {}').format(Identifier(self.data_table)),
            SQL('WHERE topic_id in ({})').format(
                SQL(', ').join(Literal(tid) for tid in topic_ids)),
        ]
        if start is not None:
            query.append(SQL(' AND ts >= {}').format(Literal(start)))
        if end is not None:
            query.append(SQL(' AND ts < {}').format(Literal(end)))
        rows = self.select(SQL('\n').join(query))
        return rows[0] if rows else (0, 0)
github california-civic-data-coalition / django-calaccess-processed-data / calaccess_processed_campaignfinance / managers / base.py View on Github external
"""
        Execute custom sql.

        Args:
            file_name (str): Name of .sql file.
            params (dict): Map of named placeholder in sql to parameter.
            composables (dict): Map of named placeholder in sql to psycopg2 sql Composable.

        Log the number of rows and operation performed.
        """
        # Get the path to the SQL file
        raw_sql = self.get_sql(file_name)

        # Compile with any composable variables
        if composables:
            composed_sql = sql.SQL(raw_sql).format(**composables)
        else:
            composed_sql = raw_sql

        # Open a database connection
        with connection.cursor() as cursor:
            # Run the SQL
            cursor.execute(composed_sql, params)
            # Get the row row_count
            row_count = cursor.rowcount

        # Log the result
        operation = self._extract_operation_from_sql(raw_sql)
        logger.debug('{} {} {}'.format(
            row_count,
            "row" + pluralize(row_count),
            operation
github LMFDB / lmfdb / lmfdb / backend / table.py View on Github external
def _break_order(self):
        """
        This function should be called when the id ordering is invalidated by an insertion or update.
        """
        if not self._out_of_order:
            # Only need to interact with database in this case.
            updater = SQL("UPDATE meta_tables SET out_of_order = true WHERE name = %s")
            self._execute(updater, [self.search_table], silent=True)
            self._out_of_order = True
github LMFDB / lmfdb / lmfdb / db_backend.py View on Github external
def _indexes_touching(self, columns):
        """
        Utility function for determining which indexes reference any of the given columns.
        """
        selecter = SQL("SELECT index_name FROM meta_indexes WHERE table_name = %s")
        if columns:
            selecter = SQL("{0} AND ({1})").format(selecter, SQL(" OR ").join(SQL("columns @> %s") * len(columns)))
            columns = [[col] for col in columns]
        return self._execute(selecter, [self.search_table] + columns, silent=True)
github LMFDB / lmfdb / lmfdb / db_backend.py View on Github external
If None, will use ``self.extra_table``; another common input is ``self.extra_table + "_tmp"``.
        """
        if self._id_ordered and (search_table is not None or self._out_of_order):
            now = time.time()
            search_table, extra_table = self._identify_tables(search_table, extra_table)
            newid = "newid"
            while newid in self._search_cols or newid in self._extra_cols:
                newid += "_"
            newid = Identifier(newid)
            oldid = Identifier("id")
            addcol = SQL("ALTER TABLE {0} ADD COLUMN {1} bigint")
            dropcol = SQL("ALTER TABLE {0} DROP COLUMN {1}")
            movecol = SQL("ALTER TABLE {0} RENAME COLUMN {1} TO {2}")
            pkey = SQL("ALTER TABLE {0} ADD PRIMARY KEY ({1})")
            self._execute(addcol.format(search_table, newid), silent=True, commit=False)
            updater = SQL("UPDATE {0} SET {1} = newsort.newid FROM (SELECT id, ROW_NUMBER() OVER(ORDER BY {2}) AS newid FROM {0}) newsort WHERE {0}.id = newsort.id")
            updater = updater.format(search_table, newid, self._sort)
            self._execute(updater, silent=True, commit=False)
            if extra_table is not None:
                self._execute(addcol.format(extra_table, newid), silent=True, commit=False)
                updater = SQL("UPDATE {0} SET {1} = search_table.{1} FROM (SELECT id, {1} FROM {2}) search_table WHERE {0}.id = search_table.id")
                updater = updater.format(extra_table, newid, search_table)
                self._execute(updater, silent=True, commit=False)
                self._execute(dropcol.format(extra_table, oldid), silent=True, commit=False)
                self._execute(movecol.format(extra_table, newid, oldid), silent=True, commit=False)
                self._execute(pkey.format(extra_table, oldid), silent=True, commit=False)
            self._execute(dropcol.format(search_table, oldid), silent=True, commit=False)
            self._execute(movecol.format(search_table, newid, oldid), silent=True, commit=False)
            self._execute(pkey.format(search_table, oldid), silent=True, commit=False)
            self._set_ordered(commit=False)
            print "Resorted %s in %.3f secs"%(self.search_table, time.time() - now)
            if commit: