Skip to content

Commit

Permalink
DOC: fix SQL example
Browse files Browse the repository at this point in the history
  • Loading branch information
frankenjoe committed Jan 17, 2024
1 parent f5f2d86 commit 9513282
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
6 changes: 1 addition & 5 deletions docs/legacy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ Legacy backends
===============

The file structure on the backend
has changed for
:class:`audbackend.FileSystem`
and :class:`audbackend.Artifactory`
in version 1.0.0
of :mod:`audbackend`.
has changed with version 1.0.0.

Before,
a file ``/sub/file.txt``
Expand Down
61 changes: 22 additions & 39 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ stored on our backend:
* ``content``: the binary content
* ``date``: the date when the file was added
* ``owner``: the owner of the file
* ``version``: the version of the file

.. jupyter-execute::

Expand All @@ -423,8 +422,7 @@ stored on our backend:
content BLOB NOT NULL,
date TEXT NOT NULL,
owner TEXT NOT NULL,
version TEXT NOT NULL,
PRIMARY KEY (path, version)
PRIMARY KEY (path)
);
'''
with self._db as db:
Expand Down Expand Up @@ -470,14 +468,13 @@ if a file exists.
def _exists(
self,
path: str,
version: str,
) -> bool:
with self._db as db:
query = f'''
SELECT EXISTS (
SELECT 1
FROM data
WHERE path="{path}" AND version="{version}"
WHERE path="{path}"
);
'''
result = db.execute(query).fetchone()[0] == 1
Expand All @@ -499,20 +496,19 @@ a file to our backend.
self,
src_path: str,
dst_path: str,
version: str,
checksum: str,
verbose: bool,
):
with self._db as db:
with open(src_path, 'rb') as file:
content = file.read()
query = '''
INSERT INTO data (path, checksum, content, date, owner, version)
VALUES (?, ?, ?, ?, ?, ?)
INSERT INTO data (path, checksum, content, date, owner)
VALUES (?, ?, ?, ?, ?)
'''
owner = getpass.getuser()
date = datetime.datetime.today().strftime('%Y-%m-%d')
data = (dst_path, checksum, content, date, owner, version)
data = (dst_path, checksum, content, date, owner)
db.execute(query, data)


Expand All @@ -537,13 +533,12 @@ to access its meta information.
def _checksum(
self,
path: str,
version: str,
) -> str:
with self._db as db:
query = f'''
SELECT checksum
FROM data
WHERE path="{path}" AND version="{version}"
WHERE path="{path}"
'''
checksum = db.execute(query).fetchone()[0]
return checksum
Expand All @@ -556,13 +551,12 @@ to access its meta information.
def _date(
self,
path: str,
version: str,
) -> str:
with self._db as db:
query = f'''
SELECT date
FROM data
WHERE path="{path}" AND version="{version}"
WHERE path="{path}"
'''
date = db.execute(query).fetchone()[0]
return date
Expand All @@ -575,13 +569,12 @@ to access its meta information.
def _owner(
self,
path: str,
version: str,
) -> str:
with self._db as db:
query = f'''
SELECT owner
FROM data
WHERE path="{path}" AND version="{version}"
WHERE path="{path}"
'''
owner = db.execute(query).fetchone()[0]
return owner
Expand All @@ -601,14 +594,13 @@ from the backend.
self,
src_path: str,
dst_path: str,
version: str,
verbose: bool,
):
with self._db as db:
query = f'''
SELECT content
FROM data
WHERE path="{src_path}" AND version="{version}"
WHERE path="{src_path}"
'''
content = db.execute(query).fetchone()[0]
with open(dst_path, 'wb') as fp:
Expand Down Expand Up @@ -637,28 +629,19 @@ we provide a listing method.
def _ls(
self,
path: str,
) -> typing.List[typing.Tuple[str, str]]:
) -> typing.List[str]:

with self._db as db:
if path.endswith('/'):
# path is sub-path;
# list all files and versions under sub-path
query = f'''
SELECT path, version
FROM data
WHERE path
LIKE ? || "%"
'''
ls = db.execute(query, [path]).fetchall()
else:
# path is file
# list all versions of file
query = f'''
SELECT path, version
FROM data
WHERE path="{path}"
'''
ls = db.execute(query).fetchall()

# list all files and versions under sub-path
query = f'''
SELECT path
FROM data
WHERE path
LIKE ? || "%"
'''
ls = db.execute(query, [path]).fetchall()
ls = [x[0] for x in ls]

if not ls and not path == '/':
# path has to exists if not root
Expand All @@ -667,6 +650,7 @@ we provide a listing method.
os.strerror(errno.ENOENT),
path,
)

return ls


Expand All @@ -691,13 +675,12 @@ requires another method.
def _remove_file(
self,
path: str,
version: str,
):
with self._db as db:
query = f'''
DELETE
FROM data
WHERE path="{path}" AND version="{version}"
WHERE path="{path}"
'''
db.execute(query)

Expand Down

0 comments on commit 9513282

Please sign in to comment.