Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/models/src/sql/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ function detectUnmatchedPairs(obfuscated, adapter) {
* @returns {string}
*/
export default function normalize(sql, adapter) {
// The MongoDB agents record each operation as a statement whose argument
// values are already replaced with placeholders, for example
// db.users.updateOne({"_id": ?}, {"$set": {"name": ?}}). The generic
// obfuscation below would treat the quoted keys as string literals and a
// pair of $operators as a dollar-quoted string, so leave it as it is.
if (adapter === 'mongodb') return sql;

/** @type {RegExp[]} */ let regexp;
switch (adapter) {
case 'mysql':
Expand Down
28 changes: 28 additions & 0 deletions packages/models/tests/unit/sql.normalize.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import normalize from '../../src/sql/normalize';

describe('normalize SQL', () => {
test('replaces literals for a known dialect', () => {
expect(normalize("SELECT * FROM users WHERE id = 1 AND name = 'bob'", 'postgres')).toEqual(
'SELECT * FROM users WHERE id = ? AND name = ?'
);
});

test('replaces literals with the fallback for an unknown dialect', () => {
expect(normalize('SELECT * FROM users WHERE id = 1', 'something')).toEqual(
'SELECT * FROM users WHERE id = ?'
);
});

test('leaves MongoDB statements alone, since the agent already normalized them', () => {
const statements = [
'db.users.find({"a": ?})',
'db.users.updateOne({"_id": ?}, {"$set": {"name": ?}, "$inc": {"n": ?}}, {"upsert": ?})',
'db.orders.aggregate([{"$match": {"status": ?}}, {"$group": {"_id": ?, "total": {"$sum": ?}}}])',
'db.getCollection("with-dash").insertOne({"a": ?})',
'db.users.distinct("email", {"deleted": ?})',
];
statements.forEach((statement) => {
expect(normalize(statement, 'mongodb')).toEqual(statement);
});
});
});
Loading