Example #1
0
    def get_db_prep_value(self, value, connection: BaseDatabaseWrapper, prepared=False) -> str:
        if not value:  # 避免 更新时 重新生成 单号
            cursor = connection.cursor()

            cursor.execute("select order_num from df_order_info ORDER by id DESC limit 0, 1")
            row = cursor.fetchone()  # 获取查询记录   返回是tuple

            current_date = date.strftime(date.today(), '%Y%m%d')

            if row:  # 空元组是没有记录的
                cn = row[0]
                date_, number = cn[:8], cn[8:]
                if date_ == current_date:
                    number = str(int(number)+1).rjust(4, '0')
                    return '%s%s' % (date_, number)

            return '%s0001' % current_date

        return value
Example #2
0
 def _get_infodictlist_for_db(
         cls, connection: BaseDatabaseWrapper, vendor: str, db_name: str,
         schema_name: str) -> List[Dict[str, Any]]:
     log.debug("Fetching/caching database structure (for database {}, "
               "schema {})...".format(db_name, schema_name))
     if db_name is None:
         raise ValueError("Use '', not None, for a blank db_name")
     if schema_name is None:
         raise ValueError("Use '', not None, for a blank schema_name")
     if vendor == VENDOR_MICROSOFT:
         if not db_name:
             raise ValueError("No db_name specified; required for MSSQL")
         if not schema_name:
             raise ValueError("No schema_name specified; required for MSSQL")  # noqa
         sql, args = cls._get_info_microsoft(db_name, [schema_name])
     elif vendor == VENDOR_POSTGRESQL:
         if db_name:
             raise ValueError("db_name specified; must be '' for PostgreSQL")  # noqa
         if not schema_name:
             raise ValueError("No schema_name specified; required for PostgreSQL")  # noqa
         sql, args = cls._get_info_postgres([schema_name])
     elif vendor == VENDOR_MYSQL:
         if db_name:
             raise ValueError("db_name specified; must be '' for MySQL")
         if not schema_name:
             raise ValueError("No schema_name specified; required for MySQL")  # noqa
         sql, args = cls._get_info_mysql(db_and_schema_name=schema_name)
     else:
         raise ValueError(
             "Don't know how to get metadata for "
             "connection.vendor=='{}'".format(vendor))
     # We execute this one directly, rather than using the Query class,
     # since this is a system rather than a per-user query.
     cursor = connection.cursor()
     # log.debug("sql = {}, args = {}".format(sql, repr(args)))
     cursor.execute(sql, args)
     results = dictfetchall(cursor)  # list of OrderedDicts
     # log.debug("results = {}".format(repr(results)))
     log.debug("... done")
     return results
Example #3
0
def check_supports_pg_trgm(connection: BaseDatabaseWrapper) -> bool:
    """Determine if trigram similarity support is present.

    Attempts to run a query against the extensions table in PostgreSQL to
    look for the pg_trgm extension. Failures assume a lack of support.

    Args:
        connection: The database connection.

    Returns:
        bool: True if trigram support is present.
    """
    try:
        with connection.cursor() as cursor:
            cursor.execute("""
                SELECT 1
                FROM pg_extension
                WHERE extname = 'pg_trgm' LIMIT 1;
            """)
            return bool(cursor.fetchone())
    except DatabaseError:
        return False