代码之家  ›  专栏  ›  技术社区  ›  Scott

SQLite3 gem中的类型转换做什么?

  •  1
  • Scott  · 技术社区  · 7 年前

    type_translation = true 在gem的网站上没有记录初始化方法。它有什么作用?

    2 回复  |  直到 7 年前
        1
  •  3
  •   anothermh ScarletMerlin    7 年前

    答案可以通过以下方式找到: examining the source code :

    # Return the type translator employed by this database instance. Each
    # database instance has its own type translator; this allows for different
    # type handlers to be installed in each instance without affecting other
    # instances. Furthermore, the translators are instantiated lazily, so that
    # if a database does not use type translation, it will not be burdened by
    # the overhead of a useless type translator. (See the Translator class.)
    

    当你看到 Translator class :

    # The Translator class encapsulates the logic and callbacks necessary for
    # converting string data to a value of some specified type. Every Database
    # instance may have a Translator instance, in order to assist in type
    # translation (Database#type_translation).
    

    因此,当 type_translation = true ,sqlite3将使用该数据库的类型转换器来协助在数据类型之间序列化数据。

        2
  •  3
  •   Beartech    7 年前

    从该链接: https://github.com/sparklemotion/sqlite3-ruby/blob/master/lib/sqlite3/database.rb

    “…Database类还提供类型转换服务,通过该服务,SQLite3数据类型(均表示为字符串)可以转换为其相应的类型(如其表的模式中所定义)。这种转换仅在从数据库查询数据时发生——插入和更新都仍然是无类型的…”

    https://sqlite.org/datatype3.html 它解释了sqlite3中如何处理数据类型。