本文整理汇总了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' ]
)
}
示例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) ]
)
}
示例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 ]
)
}
示例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);