Skip to content

Commit

Permalink
Add PostgreSQL for Python
Browse files Browse the repository at this point in the history
  • Loading branch information
dfalbel committed Feb 21, 2025
1 parent 10028e9 commit ca691ae
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
71 changes: 71 additions & 0 deletions extensions/positron-connections/src/drivers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function registerConnectionDrivers(context: vscode.ExtensionContext) {
new RPostgreSQLDriver(context),
new PythonSQLiteDriver(context),
new RSparkDriver(context),
new PythonPostgreSQLDriver(context),
];

for (const driver of drivers) {
Expand Down Expand Up @@ -351,3 +352,73 @@ conn = sqlite3.connect(${JSON.stringify(dbname) ?? JSON.stringify('')})
`;
}
}

class PythonPostgreSQLDriver extends PythonDriver implements positron.ConnectionsDriver {

constructor(context: vscode.ExtensionContext) {
super();
const iconPath = path.join(context.extensionPath, 'media', 'logo', 'postgre.svg');
const iconData = readFileSync(iconPath, 'base64');
this.metadata.base64EncodedIconSvg = iconData;
}

driverId: string = 'py-postgres';
metadata: positron.ConnectionsDriverMetadata = {
languageId: 'python',
name: 'PostgresSQL',
inputs: [
{
'id': 'dbname',
'label': 'Database Name',
'type': 'string',
'value': 'localhost'
},
{
'id': 'host',
'label': 'Host',
'type': 'string',
'value': 'localhost'
},
{
'id': 'port',
'label': 'Port',
'type': 'number',
'value': '5432'
},
{
'id': 'user',
'label': 'User',
'type': 'string',
'value': 'postgres'
},
{
'id': 'password',
'label': 'Password',
'type': 'string',
'value': 'password'
},
]
};

generateCode(inputs: positron.ConnectionsInput[]) {
const dbname = inputs.find(input => input.id === 'dbname')?.value;
const host = inputs.find(input => input.id === 'host')?.value;
const port = inputs.find(input => input.id === 'port')?.value;
const user = inputs.find(input => input.id === 'user')?.value;
const password = inputs.find(input => input.id === 'password')?.value;

const connection_string = `postgresql+psycopg2://${user}:${password}@${host}:${port}/${dbname}`;

return `import sqlalchemy
conn = sqlalchemy.create_engine(sqlalchemy.URL.create(
"postgresql+psycopg2",
username=${JSON.stringify(user)},
password=${JSON.stringify(password)},
host=${JSON.stringify(host)},
database=${JSON.stringify(dbname)},
port=${JSON.stringify(port)}
))
%connection_show conn
`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ class SQLAlchemyConnection(Connection):
def __init__(self, conn: sqlalchemy.Engine):
self.conn = conn
self.display_name = f"SQLAlchemy ({conn.name})"
self.host = conn.url.render_as_string()
self.host = conn.url.render_as_string(hide_password=False)
self.type = "SQLAlchemy"
self.code = (
"import sqlalchemy\n"
Expand Down

0 comments on commit ca691ae

Please sign in to comment.