本文整理匯總了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);