Skip to content

Commit

Permalink
Fix special column names in postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
damms005 committed Oct 7, 2024
1 parent fb169c2 commit 3d30bad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/services/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,24 @@ export const SqlService = {
limitConstraint += offset ? ` OFFSET ${offset}` : '';

const whereString = where.length ? `WHERE ${where.join(' AND ')}` : '';
let sql;
let loggedSql = '';
let rows

const sql = `SELECT * FROM ${delimiter}${table}${delimiter} ${whereString} ${limitConstraint}`

try {
rows = await sequelize.query(
`SELECT * FROM ${delimiter}${table}${delimiter} ${whereString} ${limitConstraint}`, {
rows = await sequelize.query(sql, {
type: QueryTypes.SELECT,
raw: true,
replacements,
logging: query => { sql = query }
logging: query => loggedSql = `${loggedSql}\n${query}`
});
} catch (error) {
reportError(String(error));
return
}

return { rows, sql };
return { rows, sql: loggedSql };
},

async getTotalRows(dialect: Dialect, sequelize: Sequelize | null, table: string, columns: Column[], whereClause?: Record<string, any>): Promise<number | undefined> {
Expand Down Expand Up @@ -107,7 +108,7 @@ function buildWhereClause(dialect: Dialect, whereClause: Record<string, any>, co

const isStringablePostgresComparison = /(uuid|integer|smallint|bigint|int\d)/.test(targetColumn.type) && dialect === 'postgres';
if (isStringablePostgresComparison) {
column = `${column}::text`;
column = `"${column}"::text`;
delimiter = ''
}

Expand Down
18 changes: 10 additions & 8 deletions triage/postgres-triage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ CREATE TABLE book (
published_date DATE,
isbn VARCHAR(13),
pages INT,
available BOOLEAN DEFAULT TRUE
available BOOLEAN DEFAULT TRUE,
"user" UUID,
"order" INT
);
INSERT INTO book (title, author, published_date, isbn, pages, available) VALUES
('The Great Gatsby', 'F. Scott Fitzgerald', '1925-04-10', '9780743273565', 180, TRUE),
('1984', 'George Orwell', '1949-06-08', '9780451524935', 328, FALSE),
('To Kill a Mockingbird', 'Harper Lee', '1960-07-11', '9780061120084', 281, TRUE),
('Pride and Prejudice', 'Jane Austen', '1813-01-28', '9781503290563', 279, TRUE),
('The Catcher in the Rye', 'J.D. Salinger', '1951-07-16', '9780316769488', 214, FALSE);
INSERT INTO book (title, author, published_date, isbn, pages, available, "user", "order") VALUES
('The Great Gatsby', 'F. Scott Fitzgerald', '1925-04-10', '9780743273565', 180, TRUE, uuid_generate_v4(), 1),
('1984', 'George Orwell', '1949-06-08', '9780451524935', 328, FALSE, uuid_generate_v4(), 2),
('To Kill a Mockingbird', 'Harper Lee', '1960-07-11', '9780061120084', 281, TRUE, uuid_generate_v4(), 3),
('Pride and Prejudice', 'Jane Austen', '1813-01-28', '9781503290563', 279, TRUE, uuid_generate_v4(), 4),
('The Catcher in the Rye', 'J.D. Salinger', '1951-07-16', '9780316769488', 214, FALSE, uuid_generate_v4(), 5);
EOF

Expand All @@ -50,6 +52,6 @@ connect(
)
EXAMPLE_CONNECTION

echo "You can connect to it from inside the container like: `psql sample_db postgres`"
echo "You can connect to it from inside the container like: 'psql sample_db postgres'"
echo "To stop the PostgreSQL container, run the following command:"
echo "docker stop postgres-devdb-triage && docker rm postgres-devdb-triage"

0 comments on commit 3d30bad

Please sign in to comment.