【问题标题】:How to access tables in other schema in korma?如何在 korma 中访问其他模式中的表?
【发布时间】:2014-11-07 03:14:12
【问题描述】:

在 SQL 中,访问其他模式中的表很简单:

select * 
from other_schema.t
where ...

如何在 korma 中做到这一点?我实际上要做的是访问information_schema.tables 表。所以用defdb 定义另一个db 不会有帮助。

我尝试定义实体,但是失败了。

(defentity information_schema.tables)

【问题讨论】:

    标签: clojure korma sqlkorma


    【解决方案1】:

    我必须知道在定义实体时有一种方法可以指定基表。指定基表时,允许使用. 设置架构。

    (defentity tables
      (table :information_schema.tables))
    

    这适用于访问information_schema.tables 表,无需定义另一个数据库。

    【讨论】:

      【解决方案2】:

      您应该能够通过定义另一个数据库来做到这一点。我可以像这样创建一个数据库:

      CREATE database my_db;
      USE my_db;
      CREATE TABLE stuff (
        things VARCHAR(255)
      );
      INSERT INTO stuff (things) VALUES ("some things");
      

      现在我定义了两个 Korma 数据库和实体,并查询它们:

      (defdb my-db (mysql {:host "localhost" 
                           :port 3306 
                           :db "my_db"
                           :user "root"
                           :password nil}))
      
      (defdb information-schema (mysql {:host "localhost" 
                                        :port 3306 
                                        :db "information_schema" 
                                        :user "root" 
                                        :password nil}))
      
      
      (defentity stuff)
      
      (defentity information-schema)
      
      (select stuff
              (database my-db))
      
      ;; => ({:things "some things"})
      
      (select TABLES 
              (database information-schema) 
              (fields :TABLE_SCHEMA :TABLE_NAME) 
              (where {:TABLE_SCHEMA "my_db"}))
      
      ;; => ({:TABLE_NAME "stuff", :TABLE_SCHEMA "my_db"})
      

      【讨论】:

      • 感谢您回答我的问题。不幸的是,我不知道root 用户密码,所以我无法应用您的建议。无论如何,我找到了更合适的方式。请参考我自己的回答。
      • 您不需要 root 用户,每个用户都会看到来自 information_schema 的结果,因为他们有权访问的对象,但我不想通过添加 GRANT 语句来混淆示例
      猜你喜欢
      • 1970-01-01
      • 2010-10-26
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多