mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-04 20:28:10 +00:00
Community: Add bind variable support for oracle adb docloader (#30937)
PR title: Community: Add bind variable support for oracle adb docloader Description: This PR adds support of using bind variable to oracle adb doc loader class, including minor document change. Issue: N/A Dependencies: No new dependencies.
This commit is contained in:
parent
9418c0d8a5
commit
335f089d6a
@ -36,10 +36,7 @@
|
||||
"pip install oracledb"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"pycharm": {
|
||||
"is_executing": true
|
||||
}
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
@ -51,10 +48,7 @@
|
||||
"from settings import s"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"pycharm": {
|
||||
"is_executing": true
|
||||
}
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
@ -97,16 +91,14 @@
|
||||
"doc_2 = doc_loader_2.load()"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"pycharm": {
|
||||
"is_executing": true
|
||||
}
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"With TLS authentication, wallet_location and wallet_password are not required."
|
||||
"With TLS authentication, wallet_location and wallet_password are not required.\n",
|
||||
"Bind variable option is provided by argument \"parameters\"."
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
@ -117,6 +109,8 @@
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"SQL_QUERY = \"select channel_id, channel_desc from sh.channels where channel_desc = :1 fetch first 5 rows only\"\n",
|
||||
"\n",
|
||||
"doc_loader_3 = OracleAutonomousDatabaseLoader(\n",
|
||||
" query=SQL_QUERY,\n",
|
||||
" user=s.USERNAME,\n",
|
||||
@ -124,6 +118,7 @@
|
||||
" schema=s.SCHEMA,\n",
|
||||
" config_dir=s.CONFIG_DIR,\n",
|
||||
" tns_name=s.TNS_NAME,\n",
|
||||
" parameters=[\"Direct Sales\"],\n",
|
||||
")\n",
|
||||
"doc_3 = doc_loader_3.load()\n",
|
||||
"\n",
|
||||
@ -133,6 +128,7 @@
|
||||
" password=s.PASSWORD,\n",
|
||||
" schema=s.SCHEMA,\n",
|
||||
" connection_string=s.CONNECTION_STRING,\n",
|
||||
" parameters=[\"Direct Sales\"],\n",
|
||||
")\n",
|
||||
"doc_4 = doc_loader_4.load()"
|
||||
],
|
||||
|
@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
from langchain_core.documents import Document
|
||||
|
||||
@ -31,6 +31,7 @@ class OracleAutonomousDatabaseLoader(BaseLoader):
|
||||
wallet_password: Optional[str] = None,
|
||||
connection_string: Optional[str] = None,
|
||||
metadata: Optional[List[str]] = None,
|
||||
parameters: Optional[Union[list, tuple, dict]] = None,
|
||||
):
|
||||
"""
|
||||
init method
|
||||
@ -44,6 +45,7 @@ class OracleAutonomousDatabaseLoader(BaseLoader):
|
||||
:param wallet_password: password of wallet
|
||||
:param connection_string: connection string to connect to adb instance
|
||||
:param metadata: metadata used in document
|
||||
:param parameters: bind variable to use in query
|
||||
"""
|
||||
# Mandatory required arguments.
|
||||
self.query = query
|
||||
@ -67,6 +69,9 @@ class OracleAutonomousDatabaseLoader(BaseLoader):
|
||||
# metadata column
|
||||
self.metadata = metadata
|
||||
|
||||
# parameters, e.g bind variable
|
||||
self.parameters = parameters
|
||||
|
||||
# dsn
|
||||
self.dsn: Optional[str]
|
||||
self._set_dsn()
|
||||
@ -96,7 +101,10 @@ class OracleAutonomousDatabaseLoader(BaseLoader):
|
||||
cursor = connection.cursor()
|
||||
if self.schema:
|
||||
cursor.execute(f"alter session set current_schema={self.schema}")
|
||||
cursor.execute(self.query)
|
||||
if self.parameters:
|
||||
cursor.execute(self.query, self.parameters)
|
||||
else:
|
||||
cursor.execute(self.query)
|
||||
columns = [col[0] for col in cursor.description]
|
||||
data = cursor.fetchall()
|
||||
data = [
|
||||
|
Loading…
Reference in New Issue
Block a user