Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jdbc获取特定查询的元数据_Jdbc_Clojure - Fatal编程技术网

jdbc获取特定查询的元数据

jdbc获取特定查询的元数据,jdbc,clojure,Jdbc,Clojure,在clojure/JavaJDBC中,我了解到使用getMetaData可以返回大量有关通过jdbc连接到的数据库的有趣信息。可以根据目录、架构和表名对其进行筛选 (defn get-db-metadata [db-spec ] (with-connection (get-db-connection-map db-spec) ; get columns returs the following: ; TABLE_CAT String =

在clojure/JavaJDBC中,我了解到使用getMetaData可以返回大量有关通过jdbc连接到的数据库的有趣信息。可以根据目录、架构和表名对其进行筛选

(defn get-db-metadata 
    [db-spec ]
    (with-connection (get-db-connection-map db-spec)  
      ; get columns returs the following:
      ;  TABLE_CAT String       => table catalog (may be null)
      ;  TABLE_SCHEM String     => table schema (may be null)
      ;  TABLE_NAME String      => table name
      ;  COLUMN_NAME String     => column name
      ;  DATA_TYPE int          => SQL type from java.sql.Types
      ;  TYPE_NAME String       => Data source dependent type name, for a UDT the type name is fully qualified
      ;  COLUMN_SIZE int        => column size. For char or date types this is the maximum number of characters, for numeric or decimal types this is precision.
      ;  BUFFER_LENGTH          => not used.
      ;  DECIMAL_DIGITS int     => the number of fractional digits
      ;  NUM_PREC_RADIX int     => Radix (typically either 10 or 2)
      ;  NULLABLE int           => is NULL allowed.
      ;  columnNoNulls          => might not allow NULL values
      ;  columnNullable         => definitely allows NULL values
      ;  columnNullableUnknown  => nullability unknown
      ;  REMARKS String         => comment describing column (may be null)
      ;  COLUMN_DEF String      => default value (may be null)
      ;  SQL_DATA_TYPE int      => unused
      ;  SQL_DATETIME_SUB int   => unused
      ;  CHAR_OCTET_LENGTH int  => for char types the maximum number of bytes in the column
      ;  ORDINAL_POSITION int   => index of column in table (starting at 1)
      ;  IS_NULLABLE String     => "NO" means column definitely does not allow NULL values; "YES" means the column might allow NULL values. An empty string means nobody knows.
      ;  SCOPE_CATLOG String    => catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF)
      ;  SCOPE_SCHEMA String    => schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF)
      ;  SCOPE_TABLE String     => table name that this the scope of a reference attribure (null if the DATA_TYPE isn't REF)
      ;  SOURCE_DATA_TYPE short => source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types (null if DATA_TYPE isn't DISTINCT or user-generated REF)
      (into #{}
            (map #(str (% :table_name) "." (% :column_name) "\n")
                 (resultset-seq (->
                                  (connection)
                                  (.getMetaData)
                                  ; Params in are catalog, schemapattern, tablenamepattern  
                                  ;(.getColumns "stuff" "public" nil "%")
                                  (.getColumns "db_catalog" "schema_name" "some_table" "%")
                                  )
                                )
                 )
            )
      )
    )
我感兴趣的不是数据库中每个表的信息,而是特定查询返回的结果集的信息。在这一点上,我特别需要知道检索到的特定列的最大长度,例如以固定宽度格式打印到屏幕上

到目前为止我所考虑的(我肯定不是很理想):

  • 正在尝试分析in-bound sql语句以确定哪些表 正在查询,然后获取这些表的元数据 明确地这将被证明是复杂的结果 包含函数的select语句,或公共表表达式等。 我认为它可能很快变得混乱(而且不准确)

  • 还可以基于视图创建临时视图 入站查询。。。然后我可以获取这个视图上的元数据。这 但是,如果我只与 我正在使用的数据库。通常情况下,我相信我正在努力做的事情

  • 获取结果,然后为每个列返回 最大长度的值,然后创建我的固定宽度网格 因此。如果我看的是大的,那就不太好了 结果集

  • 有没有更好的方法来确定我从查询返回的所有内容的类型?其他程序如何做到这一点?似乎我应该能够在发出查询请求时获取元数据:

    (defn fetch-results 
      "Treat lazy result sets in whole for returning a database query"  
      [db-spec query] 
      (with-connection 
        (get-db-connection-map db-spec) 
        (with-query-results res query 
          ; (get the medata here somehow for columns returned ????)
          (doall res))
        )
      )
    

    提前谢谢

    您可以在scala中这样做,这可能对您有所帮助:

    var stmt: PreparedStatement = null
    var rs: ResultSetMetaData = null
    try {
      stmt = conn.prepareStatement(query)
      rs = stmt.getMetaData()
    } finally {
      cleanup(stmt)
    }
    }
    

    我不知道clojure,但是你能不能只使用Thank Mark返回的对象,我将尝试让它工作,因为它似乎是关于效率的合适选择。仅供参考,在clojure中,我还不太确定如何做,因为在查询结果中,结果集处理似乎已经为我做好了。。。我会坚持下去,并及时发布和回复(希望如此)。我遇到了打印表,这在一定程度上有帮助。。。。虽然它不是一种元数据方法,也没有按顺序打印列。坚持下去,尽管这有一些帮助。[连结]