小编vit*_*y-t的帖子

正则表达式/解析 PostgreSQL 名称

通过 PostgreSQL名称和标识符,我试图找出以下内容:

\n\n

是否可以实现正则表达式(或以其他方式解析它)来检查 PostgreSQL 名称/标识符是否需要用双引号引起来?如果可能的话,还要检查它是否是 Unicode 样式的名称。

\n\n

我需要将标识符名称注入 SQL 查询中,并且仅当名称需要时,我才想将其括在双引号中,因为我读到,在不需要的地方添加引号实际上会更改名称格式(请参阅下面的更新) )。

\n\n

更新1:

\n\n

最终,我需要实现以下功能:

\n\n
function prepareSQLName(name) {\n\n    if (/* this is a regular name*/) {\n        return name;\n    }\n\n    if (/* this is a unicode name */) {\n        return \'U&"\' + name + \'"\';\n    } else {\n        /* needs double quotes only */\n        return \'"\' + name + \'"\';\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

主要关注点之一 - 处理非英文名称。例如,以下是一个完全有效的 PostgreSQL(俄语),不需要将任何内容括在双引号中:

\n\n
create table \xd0\xb6\xd0\xb8\xd1\x80\xd0\xb0\xd1\x84\xd1\x8b(\xd0\xb2\xd0\xb5\xd1\x81 int, \xd1\x80\xd0\xbe\xd1\x81\xd1\x82 int, \xd0\xb2\xd0\xbe\xd0\xb7\xd1\x80\xd0\xb0\xd1\x81\xd1\x82 int, \xd0\xb8\xd0\xbc\xd1\x8f …
Run Code Online (Sandbox Code Playgroud)

regex sql postgresql node.js

4
推荐指数
1
解决办法
4629
查看次数

TypeScript中的可选前导参数

在将JavaScript库协议转换为TypeScript时,我偶然发现了使用前导可选参数,而不是常规/尾随参数.

JavaScript中的方法:

db.task(function (context) {
    // executing task
});
Run Code Online (Sandbox Code Playgroud)

有一个可以在前面注入的任务的可选名称:

db.task('myTaskName', function (context) {
    // executing task
});
Run Code Online (Sandbox Code Playgroud)

这样做是为了使代码更具可读性,将任务名称放在前面,而不是最后的某个地方,这看起来是错误的/不直观的.

如何在TypeScript中围绕这些参数进行编码?

我知道我可以将两个参数声明为可选参数,但情况并非如此,因为回调函数需要作为第一个或第二个参数.如果它使这更简单,我们可以说 - 最后一个参数必须是回调函数.

javascript typescript

4
推荐指数
2
解决办法
974
查看次数

参数化查询的查询格式

我正在使用pg-promisePostgreSQL 中的 like 子句执行选择查询。不幸的是,查询失败,错误代码为08P01和错误消息

绑定消息提供 1 个参数,但准备好的语句 "" 需要 0

查询如下

select user_name, user_id from users where user_name like '$1#%'
Run Code Online (Sandbox Code Playgroud)

我使用参数化查询作为

var userQuery:pgp.ParameterizedQuery = new pgp.ParameterizedQuery("<above_query>", [userName]);
Run Code Online (Sandbox Code Playgroud)

用于执行查询的 API 是

each(query:TQuery, values:any, cb:(row:any, index:number, data:Array<any>)=>void, thisArg?:any):XPromise<Array<any>>;
Run Code Online (Sandbox Code Playgroud)

我查看了 pg-promise 示例,但它没有将 LIKE 子句与参数化查询一起使用。

环境细节是

PG承诺:4.3.2

PostgreSQL:9.6

节点:5.7.1

更新:1 我可以使用queryAPI(纯文本 sql)运行查询,但不能使用each. 任何人都可以解释为什么 LIKE 子句each在使用ParameterizedAPI 时失败。

postgresql parameterized-query pg-promise

4
推荐指数
1
解决办法
1万
查看次数

Node.js 6 和匿名对象

有人知道为什么从 Node.js 6.0 开始console.log有时会渲染带有前导anonymous词的对象吗?

anonymous {val: 123}
Run Code Online (Sandbox Code Playgroud)

这是什么意思,如何反击?

我从我正在使用的一些库中获取对象。

console node.js

4
推荐指数
1
解决办法
2637
查看次数

NodeJS MySQL连接字符串

我使用MySQL作为带Yeoman生成器的DB创建ExpressJS MVC应用程序,并且config.js我想更改MySQL连接字符串,但我不知道在字符串中指定密码.

我的字符串是 mysql://root@localhost:3306/

请帮我.

mysql node.js express yeoman

4
推荐指数
1
解决办法
6947
查看次数

pg-promise:将函数作为参数传递给func()

我正在使用pg-promise访问我们的postgres数据库.我想调用一个foo(geom)接受几何数据类型(PostGIS)的存储过程.我只有lats/lngs开头,所以我想用postGIS转换它们.

这看起来像这样:

db.func('foo', 'ST_MakePoint(' + location.lat + ', ' + location.lng + ')')
  .then((result) => {
    console.log(bar);
  });
Run Code Online (Sandbox Code Playgroud)

我现在收到错误,抱怨我的几何体无效(转换不会发生).我相信ST_MakePoint我所拥有的价值观的作品.我猜它在数据库上执行时将其解释为字符串而不是函数调用.

我应该如何传递此参数才能使其正常工作?

javascript postgresql pg-promise

4
推荐指数
1
解决办法
628
查看次数

如何为WebStorm配置eslint缩进?

如何设置"indent".eslintr.json以匹配WebStorm使用的默认?

在此处输入图片说明

到目前为止,根据官方文档,我尝试过的所有内容都无法与之匹配:

  • "indent": ["error", 2] -给很多 Expected indentation of 2 spaces but found 4
  • "indent": ["error", 4] -给很多 Expected indentation of 4 spaces but found 8
  • "indent": ["error", 8] -给很多 Expected indentation of 8 spaces but found 4

我完整的eslint配置:

{
  "env": {
    "es6": true,
    "node": true,
    "jasmine": true
  },
  "extends": "eslint:recommended",
  "parserOptions": {
  },
  "rules": {
    "no-else-return": "error",
    "no-multi-spaces": "error",
    "no-whitespace-before-property": "error",
    "camelcase": "error",
    "new-cap": "error",
    "no-console": "error",
    "comma-dangle": "error",
    "no-var": "error", …
Run Code Online (Sandbox Code Playgroud)

javascript webstorm eslint

4
推荐指数
1
解决办法
3593
查看次数

pg-promise - 错误运算符不存在:bigint = bigint[]

我正在尝试运行查询:

let query =
    `
        DELETE FROM
            ${table_name}
        WHERE
            _id IN ($1::bigint[])
            AND
            account_id = $2
    `
let fields =
    [
        _ids,
        account_id,
    ]
Run Code Online (Sandbox Code Playgroud)

但它给了我错误:

operator does not exist: bigint = bigint[]
Run Code Online (Sandbox Code Playgroud)

_ids 是一个数组。

笔记

我在实施答案时遇到的错误是:

GraphQLError: Int cannot represent non-integer value: []
Run Code Online (Sandbox Code Playgroud)

这只是一个 GraphQL 错误,与 postgres 无关。

javascript postgresql node.js pg-promise

4
推荐指数
1
解决办法
3281
查看次数

省略列名/将对象直接插入到node-postgres中

我想传递以列名作为key 的字典,从而避免在查询本身中声明列名(直接键入它们)。


假设我有一个User包含 2 个列名的表:

  • idUser(INT)
  • fullName(VARCHAR)

要使用node-postgres创建记录,我需要在查询中声明列名称,如下所示:

    var idUser   = 2;
    var fullName = "John Doe";
    var query = 'INSERT INTO User(idUser, age) VALUES ($1, $2)';

    database.query(query, [idUser, fullName], function(error, result) {
      callback(error, result.rows);
      database.end();
    });
Run Code Online (Sandbox Code Playgroud)

我更希望有一种方法可以只传递字典并让它从键中推断出列名 - 如果有一个简单的技巧我想听听。

例如这样的事情:

    var values = {
      idUser  : 2,
      fullName: "John Doe"
    };
    var query = 'INSERT INTO User VALUES ($1)';

    database.query(query, [values], function(error, result) {
      callback(error, result.rows);
      database.end();
    });
Run Code Online (Sandbox Code Playgroud)

postgresql node.js node-postgres

3
推荐指数
1
解决办法
3977
查看次数

从连接获取 PostgreSQL 服务器版本?

现代 PostgreSQL 连接协议中是否有任何内容可以指示服务器版本?

如果没有,是否存在端点可以针对开放连接执行的特殊低级请求,以提取包含该版本的服务器详细信息?

我正在研究node-postgres的可能扩展,它会在每次新连接时自动提供服务器版本。我想知道这是否可能。

对于管理连接的基本驱动程序来说,必须SELECT version()在每个新连接上执行然后解析它,级别太高了。它应该在协议级别完成。

postgresql node-postgres

3
推荐指数
1
解决办法
1237
查看次数