psycopg 3.0.9等于psycopg2中的复合脚轮是什么?

发布于 2025-01-24 19:44:00 字数 1799 浏览 4 评论 0原文

   import psycopg
   from psycopg.types.composite import CompositeInfo, register_composite

   def insert_data(self):
      pg = psycopg.connect(self.connection_string)
      cursor = pg.cursor()
      
      class TradeComposite(CompositeInfo):
        def __init__(self, name, oid, attrs, array_oid=None, schema=None):
          self.name = name
          self.schema = schema
          self.oid = oid
          self.array_oid = array_oid

          self.attnames = [a[0] for a in attrs]
          self.atttypes = [a[1] for a in attrs]
          # self._create_type(name, self.attnames)
          # self.typecaster = _ext.new_type((oid,), name, self.parse)
          # if array_oid:
          #     self.array_typecaster = _ext.new_array_type(
          #         (array_oid,), "%sARRAY" % name, self.typecaster)
          # else:
          #     self.array_typecaster = None
        def make(self, values):
          return tuple(values)
      
      info = CompositeInfo.fetch(pg, 'insertable_unique_trade')
      register_composite(info, cursor, factory=TradeComposite)

      def handle_messages():
          trades = [
          (55, 'acctname', 0, 69, 66376.07596266, 68, 66386.70210866087, 68, 0.0, 1.00016009, '6043080995', 1641444405.716349), 
          (55, 'acctname', 0, 69, 2.05263549, 68, 6750.199985115853, 68, 0.0, 3288.55270115, '6215459421', 1642183827.224316)
          ]
          cursor.execute("SELECT insert_unique_trades(%s::insertable_unique_trade[])::insertable_unique_trade[]", [trades])
          results = cursor.fetchall()
          pg.commit()

复合类型是在PSQL脚本中使用创建创建的,

我可以使用pg.commit()插入数据,但我无法执行Cursor 。 Psycopg2复合脚轮在Psycopg 3.0.9中的等效实现?我不得不从psycopg2 2.9.3升级到psycopg 3.0.9。

   import psycopg
   from psycopg.types.composite import CompositeInfo, register_composite

   def insert_data(self):
      pg = psycopg.connect(self.connection_string)
      cursor = pg.cursor()
      
      class TradeComposite(CompositeInfo):
        def __init__(self, name, oid, attrs, array_oid=None, schema=None):
          self.name = name
          self.schema = schema
          self.oid = oid
          self.array_oid = array_oid

          self.attnames = [a[0] for a in attrs]
          self.atttypes = [a[1] for a in attrs]
          # self._create_type(name, self.attnames)
          # self.typecaster = _ext.new_type((oid,), name, self.parse)
          # if array_oid:
          #     self.array_typecaster = _ext.new_array_type(
          #         (array_oid,), "%sARRAY" % name, self.typecaster)
          # else:
          #     self.array_typecaster = None
        def make(self, values):
          return tuple(values)
      
      info = CompositeInfo.fetch(pg, 'insertable_unique_trade')
      register_composite(info, cursor, factory=TradeComposite)

      def handle_messages():
          trades = [
          (55, 'acctname', 0, 69, 66376.07596266, 68, 66386.70210866087, 68, 0.0, 1.00016009, '6043080995', 1641444405.716349), 
          (55, 'acctname', 0, 69, 2.05263549, 68, 6750.199985115853, 68, 0.0, 3288.55270115, '6215459421', 1642183827.224316)
          ]
          cursor.execute("SELECT insert_unique_trades(%s::insertable_unique_trade[])::insertable_unique_trade[]", [trades])
          results = cursor.fetchall()
          pg.commit()

the composite type was created in the psql script with CREATE TYPE insertable_unique_trade AS

I can insert the data to the db with pg.commit() but i cant perform cursor.fetchall() because of the (I think) TradeComposite class. Whats the equivalent implementation of psycopg2 composite casters in psycopg 3.0.9? i had to upgrade from psycopg2 2.9.3 to psycopg 3.0.9.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那伤。 2025-01-31 19:44:00

您可以简单地使用此信息:

type_info = CompositeInfo.fetch(conn=self.Connection, name="your_type")
register_composite(info=type_info, context=self.Connection, factory=None)

您将获得结果类型为命名tuple。

You can simply use this:

type_info = CompositeInfo.fetch(conn=self.Connection, name="your_type")
register_composite(info=type_info, context=self.Connection, factory=None)

And you will get the result type as a namedtuple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文