|
42 | 42 | from ._utils import _TrackedQueryCancellationTimer
|
43 | 43 | from .bind_upload_agent import BindUploadAgent, BindUploadError
|
44 | 44 | from .constants import (
|
| 45 | + CMD_TYPE_DOWNLOAD, |
| 46 | + CMD_TYPE_UPLOAD, |
45 | 47 | FIELD_NAME_TO_ID,
|
46 | 48 | PARAMETER_PYTHON_CONNECTOR_QUERY_RESULT_FORMAT,
|
47 | 49 | FileTransferType,
|
@@ -1730,6 +1732,151 @@ def get_result_batches(self) -> list[ResultBatch] | None:
|
1730 | 1732 | )
|
1731 | 1733 | return self._result_set.batches
|
1732 | 1734 |
|
| 1735 | + def _download( |
| 1736 | + self, |
| 1737 | + stage_location: str, |
| 1738 | + target_directory: str, |
| 1739 | + options: dict[str, Any], |
| 1740 | + _do_reset: bool = True, |
| 1741 | + ) -> None: |
| 1742 | + """Downloads from the stage location to the target directory. |
| 1743 | +
|
| 1744 | + Args: |
| 1745 | + stage_location (str): The location of the stage to download from. |
| 1746 | + target_directory (str): The destination directory to download into. |
| 1747 | + options (dict[str, Any]): The download options. |
| 1748 | + _do_reset (bool, optional): Whether to reset the cursor before |
| 1749 | + downloading, by default we will reset the cursor. |
| 1750 | + """ |
| 1751 | + from .file_transfer_agent import SnowflakeFileTransferAgent |
| 1752 | + |
| 1753 | + if _do_reset: |
| 1754 | + self.reset() |
| 1755 | + |
| 1756 | + # Interpret the file operation. |
| 1757 | + ret = self.connection._file_operation_parser.parse_file_operation( |
| 1758 | + stage_location=stage_location, |
| 1759 | + local_file_name=None, |
| 1760 | + target_directory=target_directory, |
| 1761 | + command_type=CMD_TYPE_DOWNLOAD, |
| 1762 | + options=options, |
| 1763 | + ) |
| 1764 | + |
| 1765 | + # Execute the file operation based on the interpretation above. |
| 1766 | + file_transfer_agent = SnowflakeFileTransferAgent( |
| 1767 | + self, |
| 1768 | + "", # empty command because it is triggered by directly calling this util not by a SQL query |
| 1769 | + ret, |
| 1770 | + ) |
| 1771 | + file_transfer_agent.execute() |
| 1772 | + self._init_result_and_meta(file_transfer_agent.result()) |
| 1773 | + |
| 1774 | + def _upload( |
| 1775 | + self, |
| 1776 | + local_file_name: str, |
| 1777 | + stage_location: str, |
| 1778 | + options: dict[str, Any], |
| 1779 | + _do_reset: bool = True, |
| 1780 | + ) -> None: |
| 1781 | + """Uploads the local file to the stage location. |
| 1782 | +
|
| 1783 | + Args: |
| 1784 | + local_file_name (str): The local file to be uploaded. |
| 1785 | + stage_location (str): The stage location to upload the local file to. |
| 1786 | + options (dict[str, Any]): The upload options. |
| 1787 | + _do_reset (bool, optional): Whether to reset the cursor before |
| 1788 | + uploading, by default we will reset the cursor. |
| 1789 | + """ |
| 1790 | + from .file_transfer_agent import SnowflakeFileTransferAgent |
| 1791 | + |
| 1792 | + if _do_reset: |
| 1793 | + self.reset() |
| 1794 | + |
| 1795 | + # Interpret the file operation. |
| 1796 | + ret = self.connection._file_operation_parser.parse_file_operation( |
| 1797 | + stage_location=stage_location, |
| 1798 | + local_file_name=local_file_name, |
| 1799 | + target_directory=None, |
| 1800 | + command_type=CMD_TYPE_UPLOAD, |
| 1801 | + options=options, |
| 1802 | + ) |
| 1803 | + |
| 1804 | + # Execute the file operation based on the interpretation above. |
| 1805 | + file_transfer_agent = SnowflakeFileTransferAgent( |
| 1806 | + self, |
| 1807 | + "", # empty command because it is triggered by directly calling this util not by a SQL query |
| 1808 | + ret, |
| 1809 | + ) |
| 1810 | + file_transfer_agent.execute() |
| 1811 | + self._init_result_and_meta(file_transfer_agent.result()) |
| 1812 | + |
| 1813 | + def _download_stream( |
| 1814 | + self, stage_location: str, decompress: bool = False |
| 1815 | + ) -> IO[bytes]: |
| 1816 | + """Downloads from the stage location as a stream. |
| 1817 | +
|
| 1818 | + Args: |
| 1819 | + stage_location (str): The location of the stage to download from. |
| 1820 | + decompress (bool, optional): Whether to decompress the file, by |
| 1821 | + default we do not decompress. |
| 1822 | +
|
| 1823 | + Returns: |
| 1824 | + IO[bytes]: A stream to read from. |
| 1825 | + """ |
| 1826 | + # Interpret the file operation. |
| 1827 | + ret = self.connection._file_operation_parser.parse_file_operation( |
| 1828 | + stage_location=stage_location, |
| 1829 | + local_file_name=None, |
| 1830 | + target_directory=None, |
| 1831 | + command_type=CMD_TYPE_DOWNLOAD, |
| 1832 | + options=None, |
| 1833 | + has_source_from_stream=True, |
| 1834 | + ) |
| 1835 | + |
| 1836 | + # Set up stream downloading based on the interpretation and return the stream for reading. |
| 1837 | + return self.connection._stream_downloader.download_as_stream(ret, decompress) |
| 1838 | + |
| 1839 | + def _upload_stream( |
| 1840 | + self, |
| 1841 | + input_stream: IO[bytes], |
| 1842 | + stage_location: str, |
| 1843 | + options: dict[str, Any], |
| 1844 | + _do_reset: bool = True, |
| 1845 | + ) -> None: |
| 1846 | + """Uploads content in the input stream to the stage location. |
| 1847 | +
|
| 1848 | + Args: |
| 1849 | + input_stream (IO[bytes]): A stream to read from. |
| 1850 | + stage_location (str): The location of the stage to upload to. |
| 1851 | + options (dict[str, Any]): The upload options. |
| 1852 | + _do_reset (bool, optional): Whether to reset the cursor before |
| 1853 | + uploading, by default we will reset the cursor. |
| 1854 | + """ |
| 1855 | + from .file_transfer_agent import SnowflakeFileTransferAgent |
| 1856 | + |
| 1857 | + if _do_reset: |
| 1858 | + self.reset() |
| 1859 | + |
| 1860 | + # Interpret the file operation. |
| 1861 | + ret = self.connection._file_operation_parser.parse_file_operation( |
| 1862 | + stage_location=stage_location, |
| 1863 | + local_file_name=None, |
| 1864 | + target_directory=None, |
| 1865 | + command_type=CMD_TYPE_UPLOAD, |
| 1866 | + options=options, |
| 1867 | + has_source_from_stream=input_stream, |
| 1868 | + ) |
| 1869 | + |
| 1870 | + # Execute the file operation based on the interpretation above. |
| 1871 | + file_transfer_agent = SnowflakeFileTransferAgent( |
| 1872 | + self, |
| 1873 | + "", # empty command because it is triggered by directly calling this util not by a SQL query |
| 1874 | + ret, |
| 1875 | + source_from_stream=input_stream, |
| 1876 | + ) |
| 1877 | + file_transfer_agent.execute() |
| 1878 | + self._init_result_and_meta(file_transfer_agent.result()) |
| 1879 | + |
1733 | 1880 |
|
1734 | 1881 | class DictCursor(SnowflakeCursor):
|
1735 | 1882 | """Cursor returning results in a dictionary."""
|
|
0 commit comments