当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript sqlstring.format函数代码示例

本文整理汇总了TypeScript中sqlstring.format函数的典型用法代码示例。如果您正苦于以下问题:TypeScript format函数的具体用法?TypeScript format怎么用?TypeScript format使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了format函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: tableName

export function listQuery<F> (v: Model<F> | IResourceObject, opts?: IListQueryOpts) {
  const _opts = opts || {}
  const page = _opts.page !== undefined ? _opts.page : 0
  const size = _opts.size !== undefined ? _opts.size : DEFAULT_PAGE_SIZE
  const sort = _opts.sort !== undefined ? _opts.sort : []
  const cond = _opts.cond

  const type = v.type
  const id = v.id
  const table = tableName(type)
  if (!id) {
    throw ErrInvalidID
  }

  return SqlString.format(
    `SELECT * FROM ?? INNER JOIN (
      SELECT ?? FROM ??
      ${cond !== undefined ? 'WHERE ' + cond.build() : ''}
      LIMIT ? OFFSET ?
    ) AS ?? USING (??)
    ${sort.length !== 0 ? 'ORDER BY \`' + sort.join('\`, \`') + '\`' : ''}
    ;`,
    [ table, 'id', table, size, page * size, 'result', 'id' ]
  )
}
开发者ID:perpengt,项目名称:jsonapi,代码行数:25,代码来源:query.ts

示例2: fieldMap

export function insertQuery<F> (v: Model<F> | IResourceObject) {
  const id = v.id
  if (!id) {
    throw ErrIDRequired
  }
  const map = fieldMap(v, { id })
  return SqlString.format(
    `INSERT INTO ?? (??) VALUES (?);`,
    [ tableName(v), _.keysIn(map), _.valuesIn(map) ]
  )
}
开发者ID:perpengt,项目名称:jsonapi,代码行数:11,代码来源:query.ts

示例3: u

export function updateQuery<F> (
  v: Model<F> | IResourceIdentifier | IResourceObject,
  u: ((fields: F) => F) | _.Dictionary<any>,
  w: QueryCond
) {
  let diff: _.Dictionary<any>
  if (_.isFunction(u)) {
    if (v instanceof Model) {
      const before = v.fields
      const after = u(_.cloneDeep(v.fields))
      diff = difference(after, before)
    } else {
      throw ErrNotUpdatableObject
    }
  } else {
    diff = u
  }
  return SqlString.format(
    `UPDATE ?? SET ? WHERE ${w.build()};`,
    [ tableName(v), diff ]
  )
}
开发者ID:perpengt,项目名称:jsonapi,代码行数:22,代码来源:query.ts

示例4:

import * as SqlString from "sqlstring";

// Code samples taken from:
// https://github.com/mysqljs/sqlstring/blob/master/README.md

const userId = "some user provided value";
const sql1 = "SELECT * FROM users WHERE id = " + SqlString.escape(userId);

const userId2 = 1;
const sql2 = SqlString.format("SELECT * FROM users WHERE id = ?", [userId2]);

const userId3 = 1;
const sql3 = SqlString.format(
  "UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?",
  ["a", "b", "c", userId3],
);

const post = { id: 1, title: "Hello MySQL" };
const sql4 = SqlString.format("INSERT INTO posts SET ?", post);

const sorter = "date";
const sql5 =
  "SELECT * FROM posts ORDER BY " + SqlString.escapeId("posts." + sorter);

const sorter2 = "date.2";
const sql6 =
  "SELECT * FROM posts ORDER BY " + SqlString.escapeId(sorter2, true);

const userId4 = 1;
const inserts = ["users", "id", userId4];
const sql7 = SqlString.format("SELECT * FROM ?? WHERE ?? = ?", inserts);
开发者ID:Jeremy-F,项目名称:DefinitelyTyped,代码行数:31,代码来源:sqlstring-tests.ts


注:本文中的sqlstring.format函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。