Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

跨库查询列表语法 #423

Closed
transtone opened this issue Jul 20, 2022 · 7 comments
Closed

跨库查询列表语法 #423

transtone opened this issue Jul 20, 2022 · 7 comments

Comments

@transtone
Copy link
Contributor

transtone commented Jul 20, 2022

用的postgresql,写在代码中的默认 schema 是 apijson , 此时要查询另外一个 schema dict 中的 user_list 表数据,应该怎么查呢?

{
    "[]": {	
	"user_list": {        
		"@column": "id,user_name",
                "@schema": "dict",
	},
        "count": 3,
    },
}

这样写会报错:
数组 []:{} 中每个 key:{} 都必须是表 TableKey:{} 或 数组 arrayKey[]:{}

===============

经测试,发现有这样的现象:

  1. 修改Access表的内容(name, alias)之后,必须重启程序,否则不生效。
  2. 表的名称不能有下划线,如果有下划线,必须用 alias 。

满足上面两个条件之后,则可以正常查询。
对于1,用了数据库表还必须重启程序才能生效的做法十分不理解。
对于2,没看到faq里有提到,表名不能包含下划线。

@TommyLemon
Copy link
Collaborator

1.你没认真看完常见问题 issue,里面提到调接口热重载方式

2.不是下划线问题,是首字母没大写问题,这点不管是首页 README 注意事项、通用文档、常见问题 issue 全都有说明

@transtone
Copy link
Contributor Author

1.你没认真看完常见问题 issue,里面提到调接口热重载方式

2.不是下划线问题,是首字母没大写问题,这点不管是首页 README 注意事项、通用文档、常见问题 issue 全都有说明

感谢回复。

  1. 接口热重载,也就是自动重启,那也是需要重启,只不过是自动触发重启。
  2. 也就是说首字母不是大写的表,都需要alias了,和是不是下划线没有本质区别。

这些使用中经常遇到的问题,我看还是有人问的,apijson-doc 是否可以把这些都加进来呢?
image

@transtone
Copy link
Contributor Author

transtone commented Jul 21, 2022

这个截图我觉得可以说明一些问题,使用者关注的重点只是很小的字,也不加粗,而无用的废话又粗又大。表面上在FAQ里有了,可不仔细找还真找不到,对有心尝鲜的童鞋很不友好呀。

个人觉得,应该反复强调的是那些FAQ本身,而不是去当一个教育家,时时刻刻教大家做人。
image

@TommyLemon
Copy link
Collaborator

TommyLemon commented Jul 21, 2022

我提到 3 处,你找到了第 4 处。

首页注意事项不够显眼吗?
https://github.com/Tencent/APIJSON#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9
image

常见问题上方
#36
image

通用文档
3.设计规范
3.1 操作方法
https://github.com/Tencent/APIJSON/blob/master/Document.md#31-%E6%93%8D%E4%BD%9C%E6%96%B9%E6%B3%95
image

另外配套的接口工具 APIAuto 也有静态检查,是没用它还是没看旁边注释?
http://apijson.cn/api/
image
image

apijson-doc原本是其他热心的开发者贡献的,最近不怎么维护了,APIJSON 官方组织 fork 了一份维护
https://github.com/APIJSON/apijson-doc

你要是觉得还不够可以提个 PR 优化文档,或者改源码。

@TommyLemon
Copy link
Collaborator

TommyLemon commented Jul 21, 2022

1.并没有重启,只是重新加载了配置数据,你可以看 APIJSONController.reload 实现代码

2.MySQL 默认大小写不敏感,SELECT * FROM `Moment` 也可以访问到 `moment` 表。
对于 PostgreSQL, Oracle 等默认大小写敏感的数据库,可以设置为不敏感,或者在 APIJSON 项目
DemoSQLConfig 重写 getSQLTable,直接 toLowerCase/toUpperCase
#343

@transtone
Copy link
Contributor Author

transtone commented Jul 21, 2022

1.并没有重启,只是重新加载了配置数据,你可以看 APIJSONController.reload 实现代码

2.MySQL 默认大小写不敏感,SELECT * FROM Moment 也可以访问到 moment 表。 对于 PostgreSQL, Oracle 等默认大小写敏感的数据库,可以设置为不敏感,或者在 APIJSON 项目 DemoSQLConfig 重写 getSQLTable,直接 toLowerCase/toUpperCase #343

可以给一个重写 getSqLTable 的例子吗?

    public DemoSQLConfig() {
        super();
    }

    @Override
    public String getSQLTable() {
        String table = super.getSQLTable();
        return table.toLowerCase();
    }

这样改写之后,必须要将 Access 改成 access 才能正常启动。
启动后仍然不能用小写查询:

	"msg": "dataList[]/sys_user_info:{} 不合法!数组 []:{} 中每个 key:{} 都必须是表 TableKey:{} 或 数组 arrayKey[]:{} !",
/**判断是否为首字母大写的代码名称
	 * @param s
	 * @return
	 */
	public static boolean isBigName(String s) {
		if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) {
			return false;
		}
		return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches();
	}

..................................
public static boolean isTableKey(String key) {
   return StringUtil.isBigName(key);
}
.........................

 //APIJSON Object
			boolean isTableKey = JSONRequest.isTableKey(Pair.parseEntry(key, true).getKey());
			if (type == TYPE_ITEM && isTableKey == false) {
				throw new IllegalArgumentException(parentPath + "/" + key + ":{} 不合法!"
						+ "数组 []:{} 中每个 key:{} 都必须是表 TableKey:{} 或 数组 arrayKey[]:{} !");
			}

代码里面 isTableKey 已经限制了必须首字母大写,单改一处肯定不行。

============================

改造了一下,能够查询出来了:

  1. 将执行sql时的表名转换为小写。(ps:这样就不兼容又有大写又有小写的情况了,不知道怎么弄。)
    public DemoSQLConfig() {
        super();
    }

    @Override
    public String getSQLTable() {
        String table = super.getSQLTable();
        String[] str = {"Acess", "Document", "Request", "Response"};
        if (Arrays.asList(str).contains(table)) {
            return table;
        } else {
            return table.toLowerCase();
        }
    }
  1. 请求时使用大写,骗过isTableKey,在处理时不报错。
{
    "[]": {	
	"User_list": {        
		"@column": "id,user_name",
                "@schema": "dict",
	},
        "count": 3,
    },
}

@TommyLemon
Copy link
Collaborator

TommyLemon commented Sep 4, 2022

Access 表里可以配置 name 和 alias,单个指定请求传参表名和数据库真是表名的映射关系
https://github.com/APIJSON/apijson-framework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants