Products.ZSQLiteDA 0.8.1__tar.gz → 0.9.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- products_zsqliteda-0.9.0/CHANGES.rst +54 -0
- products_zsqliteda-0.9.0/LICENSE.txt +44 -0
- products_zsqliteda-0.9.0/MANIFEST.in +2 -0
- products_zsqliteda-0.9.0/PKG-INFO +146 -0
- products_zsqliteda-0.9.0/Products/ZSQLiteDA/DA.py +430 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products/ZSQLiteDA/__init__.py +6 -0
- products_zsqliteda-0.9.0/Products/ZSQLiteDA/db.py +585 -0
- products_zsqliteda-0.9.0/Products/ZSQLiteDA/dtml/browse.dtml +111 -0
- products_zsqliteda-0.9.0/Products/ZSQLiteDA/dtml/connectionAdd.dtml +107 -0
- products_zsqliteda-0.9.0/Products/ZSQLiteDA/dtml/connectionEdit.dtml +188 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products/ZSQLiteDA/dtml/connectionStatus.dtml +4 -4
- products_zsqliteda-0.9.0/Products.ZSQLiteDA.egg-info/PKG-INFO +146 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products.ZSQLiteDA.egg-info/SOURCES.txt +2 -1
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products.ZSQLiteDA.egg-info/requires.txt +1 -0
- products_zsqliteda-0.9.0/README.rst +58 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/setup.py +23 -2
- Products.ZSQLiteDA-0.8.1/MANIFEST.in +0 -2
- Products.ZSQLiteDA-0.8.1/PKG-INFO +0 -32
- Products.ZSQLiteDA-0.8.1/Products/ZSQLiteDA/DA.py +0 -291
- Products.ZSQLiteDA-0.8.1/Products/ZSQLiteDA/db.py +0 -235
- Products.ZSQLiteDA-0.8.1/Products/ZSQLiteDA/dtml/browse.dtml +0 -16
- Products.ZSQLiteDA-0.8.1/Products/ZSQLiteDA/dtml/connectionAdd.dtml +0 -116
- Products.ZSQLiteDA-0.8.1/Products/ZSQLiteDA/dtml/connectionEdit.dtml +0 -100
- Products.ZSQLiteDA-0.8.1/Products.ZSQLiteDA.egg-info/PKG-INFO +0 -32
- Products.ZSQLiteDA-0.8.1/Products.ZSQLiteDA.egg-info/entry_points.txt +0 -3
- Products.ZSQLiteDA-0.8.1/README.rst +0 -17
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products/ZSQLiteDA/images/DBAdapterFolder_icon.gif +0 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products/ZSQLiteDA/images/table.gif +0 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products/__init__.py +0 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products.ZSQLiteDA.egg-info/dependency_links.txt +0 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products.ZSQLiteDA.egg-info/namespace_packages.txt +0 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products.ZSQLiteDA.egg-info/not-zip-safe +0 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/Products.ZSQLiteDA.egg-info/top_level.txt +0 -0
- {Products.ZSQLiteDA-0.8.1 → products_zsqliteda-0.9.0}/setup.cfg +0 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Changelog
|
|
2
|
+
=========
|
|
3
|
+
|
|
4
|
+
0.9.0 (2026-12-04)
|
|
5
|
+
------------------
|
|
6
|
+
|
|
7
|
+
Security
|
|
8
|
+
~~~~~~~~
|
|
9
|
+
|
|
10
|
+
- Database directories are restricted to an allowlist. Without a
|
|
11
|
+
``directories-file`` in the ``Products.ZSQLiteDA`` product config, only
|
|
12
|
+
the instance ``var/sqlite`` directory can be used. Older releases could
|
|
13
|
+
open an existing database from any absolute path. A listed directory is
|
|
14
|
+
mount-only unless its line is prefixed with ``create``.
|
|
15
|
+
- SQL ``ATTACH DATABASE`` statements are rejected so queries cannot bypass
|
|
16
|
+
the database-directory allowlist.
|
|
17
|
+
- A database file name must be a plain file name. Paths, ``.`` and ``..``,
|
|
18
|
+
and control characters are rejected.
|
|
19
|
+
- A non-empty file is opened only when it starts with a SQLite header.
|
|
20
|
+
Empty files are still accepted, so a new database can be created.
|
|
21
|
+
|
|
22
|
+
Queries
|
|
23
|
+
~~~~~~~
|
|
24
|
+
|
|
25
|
+
- A Z SQL Method can run several statements in one call by putting
|
|
26
|
+
``<dtml-var sql_delimiter>`` between them. Zope renders that variable
|
|
27
|
+
as a null byte, and the adapter runs the pieces as one batch. Rows
|
|
28
|
+
from every ``SELECT`` come back together, also when the batch contains
|
|
29
|
+
``INSERT``, ``UPDATE`` or ``DELETE``, as long as the columns match.
|
|
30
|
+
If one statement fails, the whole batch is rolled back.
|
|
31
|
+
- Each query still commits on its own and does not join the Zope
|
|
32
|
+
transaction. A later error or a request retry neither rolls the SQL
|
|
33
|
+
back nor skips it.
|
|
34
|
+
|
|
35
|
+
ZMI
|
|
36
|
+
~~~
|
|
37
|
+
|
|
38
|
+
- The Add, Properties, Status and Browse tabs follow the Zope 4 ZMI
|
|
39
|
+
layout.
|
|
40
|
+
- The Add and Properties forms use the existing database or create a new
|
|
41
|
+
one. Properties can switch the allowlisted directory and refresh the
|
|
42
|
+
database list for that directory.
|
|
43
|
+
- The Browse tab lists tables and views in an accordion and shows a
|
|
44
|
+
formatted ``CREATE`` statement that can be copied. A ``--`` comment no
|
|
45
|
+
longer swallows the comma before the next column.
|
|
46
|
+
- The Properties tab shows the package version.
|
|
47
|
+
|
|
48
|
+
Packaging
|
|
49
|
+
~~~~~~~~~
|
|
50
|
+
|
|
51
|
+
- Declare ``Zope >= 4`` and require Python 3.7 or newer.
|
|
52
|
+
- Drop ``Products.ZSQLiteDA-configure.zcml``. It included a package that
|
|
53
|
+
does not exist.
|
|
54
|
+
- Ship this changelog in the sdist.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Zope Public License (ZPL) Version 2.1
|
|
2
|
+
|
|
3
|
+
A copyright notice accompanies this license document that identifies the
|
|
4
|
+
copyright holders.
|
|
5
|
+
|
|
6
|
+
This license has been certified as open source. It has also been designated as
|
|
7
|
+
GPL compatible by the Free Software Foundation (FSF).
|
|
8
|
+
|
|
9
|
+
Redistribution and use in source and binary forms, with or without
|
|
10
|
+
modification, are permitted provided that the following conditions are met:
|
|
11
|
+
|
|
12
|
+
1. Redistributions in source code must retain the accompanying copyright
|
|
13
|
+
notice, this list of conditions, and the following disclaimer.
|
|
14
|
+
|
|
15
|
+
2. Redistributions in binary form must reproduce the accompanying copyright
|
|
16
|
+
notice, this list of conditions, and the following disclaimer in the
|
|
17
|
+
documentation and/or other materials provided with the distribution.
|
|
18
|
+
|
|
19
|
+
3. Names of the copyright holders must not be used to endorse or promote
|
|
20
|
+
products derived from this software without prior written permission from the
|
|
21
|
+
copyright holders.
|
|
22
|
+
|
|
23
|
+
4. The right to distribute this software or to use it for any purpose does not
|
|
24
|
+
give you the right to use Servicemarks (sm) or Trademarks (tm) of the
|
|
25
|
+
copyright
|
|
26
|
+
holders. Use of them is covered by separate agreement with the copyright
|
|
27
|
+
holders.
|
|
28
|
+
|
|
29
|
+
5. If any files are modified, you must cause the modified files to carry
|
|
30
|
+
prominent notices stating that you changed the files and the date of any
|
|
31
|
+
change.
|
|
32
|
+
|
|
33
|
+
Disclaimer
|
|
34
|
+
|
|
35
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESSED
|
|
36
|
+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
37
|
+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
38
|
+
EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
39
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
40
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
41
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
42
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
43
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
44
|
+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: Products.ZSQLiteDA
|
|
3
|
+
Version: 0.9.0
|
|
4
|
+
Summary: SQLite database adapter for Zope4
|
|
5
|
+
Home-page: https://github.com/nakagami/Products.ZSQLiteDA
|
|
6
|
+
Author: Hajime Nakagami
|
|
7
|
+
Author-email: nakagami@gmail.com
|
|
8
|
+
License: ZPL
|
|
9
|
+
Keywords: SQLite
|
|
10
|
+
Classifier: Programming Language :: Python
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Framework :: Zope :: 4
|
|
13
|
+
Classifier: Framework :: Zope :: 5
|
|
14
|
+
Classifier: License :: OSI Approved :: Zope Public License
|
|
15
|
+
Requires-Python: >=3.7
|
|
16
|
+
Description-Content-Type: text/x-rst
|
|
17
|
+
License-File: LICENSE.txt
|
|
18
|
+
Requires-Dist: Zope>=4
|
|
19
|
+
Requires-Dist: Products.ZSQLMethods
|
|
20
|
+
Dynamic: author
|
|
21
|
+
Dynamic: author-email
|
|
22
|
+
Dynamic: classifier
|
|
23
|
+
Dynamic: description
|
|
24
|
+
Dynamic: description-content-type
|
|
25
|
+
Dynamic: home-page
|
|
26
|
+
Dynamic: keywords
|
|
27
|
+
Dynamic: license
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
Dynamic: requires-dist
|
|
30
|
+
Dynamic: requires-python
|
|
31
|
+
Dynamic: summary
|
|
32
|
+
|
|
33
|
+
ZSQLiteDA
|
|
34
|
+
=========
|
|
35
|
+
|
|
36
|
+
The ZSQLiteDA https://github.com/nakagami/Products.ZSQLiteDA provides
|
|
37
|
+
support for SQLite database adapter objects in the Zope 4 and 5.
|
|
38
|
+
|
|
39
|
+
Installation
|
|
40
|
+
-----------------
|
|
41
|
+
|
|
42
|
+
buildout.cfg::
|
|
43
|
+
|
|
44
|
+
eggs =
|
|
45
|
+
...
|
|
46
|
+
Products.ZSQLiteDA
|
|
47
|
+
|
|
48
|
+
To add or use additional databases, simply add (or link) additional
|
|
49
|
+
database file to your 'var/sqlite' directory.
|
|
50
|
+
|
|
51
|
+
Database directories
|
|
52
|
+
--------------------
|
|
53
|
+
|
|
54
|
+
By default the adapter may only create and open SQLite databases in the
|
|
55
|
+
instance ``var/sqlite`` directory, keeping it confined to the instance's own
|
|
56
|
+
data area.
|
|
57
|
+
|
|
58
|
+
To allow other directories, point the product at a list file from
|
|
59
|
+
``zope.conf``::
|
|
60
|
+
|
|
61
|
+
<product-config Products.ZSQLiteDA>
|
|
62
|
+
directories-file /path/to/instance/etc/zsqliteda-directories.conf
|
|
63
|
+
</product-config>
|
|
64
|
+
|
|
65
|
+
The file lists one directory per line. Blank lines and lines starting with
|
|
66
|
+
``#`` are ignored. A listed directory is *mount only* (existing databases may
|
|
67
|
+
be opened there) unless the line is prefixed with ``create``, which also
|
|
68
|
+
permits creating new databases in that directory::
|
|
69
|
+
|
|
70
|
+
# one directory per line; prefix with "create " to allow new databases
|
|
71
|
+
/srv/shared/sqlite
|
|
72
|
+
create /data/app databases
|
|
73
|
+
|
|
74
|
+
The instance ``var/sqlite`` directory is always allowed (create + mount) and
|
|
75
|
+
never needs to be listed.
|
|
76
|
+
|
|
77
|
+
.. note::
|
|
78
|
+
|
|
79
|
+
Without a ``directories-file`` the adapter is restricted to ``var/sqlite``.
|
|
80
|
+
This is stricter than older releases, which allowed opening existing
|
|
81
|
+
databases from anywhere. Connections that point outside ``var/sqlite`` must
|
|
82
|
+
have their directory added to the list file.
|
|
83
|
+
|
|
84
|
+
Transactions
|
|
85
|
+
------------
|
|
86
|
+
|
|
87
|
+
Each query is committed on its own. The adapter does not enlist in the Zope
|
|
88
|
+
transaction, so a later error in the request, or a request retry, does not
|
|
89
|
+
roll back SQL that has already run and does not prevent it from running
|
|
90
|
+
again. This is the same behaviour as previous releases.
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
Changelog
|
|
94
|
+
=========
|
|
95
|
+
|
|
96
|
+
0.9.0 (2026-12-04)
|
|
97
|
+
------------------
|
|
98
|
+
|
|
99
|
+
Security
|
|
100
|
+
~~~~~~~~
|
|
101
|
+
|
|
102
|
+
- Database directories are restricted to an allowlist. Without a
|
|
103
|
+
``directories-file`` in the ``Products.ZSQLiteDA`` product config, only
|
|
104
|
+
the instance ``var/sqlite`` directory can be used. Older releases could
|
|
105
|
+
open an existing database from any absolute path. A listed directory is
|
|
106
|
+
mount-only unless its line is prefixed with ``create``.
|
|
107
|
+
- SQL ``ATTACH DATABASE`` statements are rejected so queries cannot bypass
|
|
108
|
+
the database-directory allowlist.
|
|
109
|
+
- A database file name must be a plain file name. Paths, ``.`` and ``..``,
|
|
110
|
+
and control characters are rejected.
|
|
111
|
+
- A non-empty file is opened only when it starts with a SQLite header.
|
|
112
|
+
Empty files are still accepted, so a new database can be created.
|
|
113
|
+
|
|
114
|
+
Queries
|
|
115
|
+
~~~~~~~
|
|
116
|
+
|
|
117
|
+
- A Z SQL Method can run several statements in one call by putting
|
|
118
|
+
``<dtml-var sql_delimiter>`` between them. Zope renders that variable
|
|
119
|
+
as a null byte, and the adapter runs the pieces as one batch. Rows
|
|
120
|
+
from every ``SELECT`` come back together, also when the batch contains
|
|
121
|
+
``INSERT``, ``UPDATE`` or ``DELETE``, as long as the columns match.
|
|
122
|
+
If one statement fails, the whole batch is rolled back.
|
|
123
|
+
- Each query still commits on its own and does not join the Zope
|
|
124
|
+
transaction. A later error or a request retry neither rolls the SQL
|
|
125
|
+
back nor skips it.
|
|
126
|
+
|
|
127
|
+
ZMI
|
|
128
|
+
~~~
|
|
129
|
+
|
|
130
|
+
- The Add, Properties, Status and Browse tabs follow the Zope 4 ZMI
|
|
131
|
+
layout.
|
|
132
|
+
- The Add and Properties forms use the existing database or create a new
|
|
133
|
+
one. Properties can switch the allowlisted directory and refresh the
|
|
134
|
+
database list for that directory.
|
|
135
|
+
- The Browse tab lists tables and views in an accordion and shows a
|
|
136
|
+
formatted ``CREATE`` statement that can be copied. A ``--`` comment no
|
|
137
|
+
longer swallows the comma before the next column.
|
|
138
|
+
- The Properties tab shows the package version.
|
|
139
|
+
|
|
140
|
+
Packaging
|
|
141
|
+
~~~~~~~~~
|
|
142
|
+
|
|
143
|
+
- Declare ``Zope >= 4`` and require Python 3.7 or newer.
|
|
144
|
+
- Drop ``Products.ZSQLiteDA-configure.zcml``. It included a package that
|
|
145
|
+
does not exist.
|
|
146
|
+
- Ship this changelog in the sdist.
|
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
##############################################################################
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
|
|
4
|
+
#
|
|
5
|
+
# This software is subject to the provisions of the Zope Public License,
|
|
6
|
+
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
|
|
7
|
+
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
|
|
8
|
+
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
9
|
+
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
|
|
10
|
+
# FOR A PARTICULAR PURPOSE
|
|
11
|
+
#
|
|
12
|
+
##############################################################################
|
|
13
|
+
import os
|
|
14
|
+
import json
|
|
15
|
+
import logging
|
|
16
|
+
from urllib.parse import urlencode
|
|
17
|
+
from AccessControl.Permissions import change_database_connections, \
|
|
18
|
+
open_close_database_connection, view_management_screens
|
|
19
|
+
from AccessControl.SecurityInfo import ClassSecurityInfo
|
|
20
|
+
import Shared.DC.ZRDB.Connection
|
|
21
|
+
from App.special_dtml import HTMLFile
|
|
22
|
+
from sqlite3 import OperationalError as sqlite3_OperationalError
|
|
23
|
+
|
|
24
|
+
from . import __version__
|
|
25
|
+
from .db import DB, DEFAULT_DATA_DIR, check_database, \
|
|
26
|
+
create_db_file, manage_DataSources, safe_db_name, format_sql, \
|
|
27
|
+
configured_dirs, can_create_in, can_mount_from
|
|
28
|
+
|
|
29
|
+
logger = logging.getLogger('Products.ZSQLiteDA')
|
|
30
|
+
|
|
31
|
+
database_type = 'SQLite'
|
|
32
|
+
|
|
33
|
+
data_sources = manage_DataSources
|
|
34
|
+
|
|
35
|
+
addConnectionForm = HTMLFile('dtml/connectionAdd', globals())
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def extract_error(e):
|
|
39
|
+
try:
|
|
40
|
+
return e.args[0]
|
|
41
|
+
except Exception:
|
|
42
|
+
return str(e)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _add_form(self, REQUEST, message=''):
|
|
46
|
+
"""The Add form. ``message`` is a bootstrap alert above the fields."""
|
|
47
|
+
data_dir = DEFAULT_DATA_DIR
|
|
48
|
+
if REQUEST is not None and REQUEST.get('data_dir'):
|
|
49
|
+
data_dir = REQUEST['data_dir']
|
|
50
|
+
try:
|
|
51
|
+
sources = data_sources(data_dir=data_dir)
|
|
52
|
+
except sqlite3_OperationalError:
|
|
53
|
+
data_dir = DEFAULT_DATA_DIR
|
|
54
|
+
sources = data_sources(data_dir=data_dir)
|
|
55
|
+
return addConnectionForm(
|
|
56
|
+
self, self, REQUEST,
|
|
57
|
+
database_type=database_type,
|
|
58
|
+
data_dir=data_dir,
|
|
59
|
+
data_sources=sources,
|
|
60
|
+
manage_tabs_message=message)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def manage_addZSQLiteConnectionForm(self, REQUEST, *args, **kw) -> HTMLFile:
|
|
64
|
+
"""Add a connection form"""
|
|
65
|
+
|
|
66
|
+
return _add_form(self, REQUEST)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def manage_addZSQLiteConnection(self, id: str, title: str,
|
|
70
|
+
data_dir: str,
|
|
71
|
+
connection: str = '',
|
|
72
|
+
new_database: str = '',
|
|
73
|
+
mode: str = '',
|
|
74
|
+
REQUEST=None) -> 'Connection.manage_main':
|
|
75
|
+
"""Add a DB connection to a folder"""
|
|
76
|
+
|
|
77
|
+
if not id:
|
|
78
|
+
if REQUEST is None:
|
|
79
|
+
raise sqlite3_OperationalError(
|
|
80
|
+
'Please provide an id for the database adapter!')
|
|
81
|
+
return _add_form(
|
|
82
|
+
self, REQUEST, 'Please provide an id for the database adapter!')
|
|
83
|
+
|
|
84
|
+
# Existing vs. create-new is driven by the mode radio, with a fallback to
|
|
85
|
+
# whichever field carries a value so a scripted or JS-less POST still does
|
|
86
|
+
# the right thing. The two inputs use distinct field names ('connection'
|
|
87
|
+
# and 'new_database'), so the form can never submit a duplicate that
|
|
88
|
+
# ZPublisher would fold into a list.
|
|
89
|
+
creating = (mode == 'new') or (not connection and bool(new_database))
|
|
90
|
+
if creating:
|
|
91
|
+
connection = new_database
|
|
92
|
+
|
|
93
|
+
db_path = os.path.join(data_dir, connection)
|
|
94
|
+
|
|
95
|
+
try:
|
|
96
|
+
safe_db_name(connection)
|
|
97
|
+
if creating and connection and os.path.exists(db_path):
|
|
98
|
+
raise sqlite3_OperationalError(
|
|
99
|
+
f'The database file “{connection}” already exists\n'
|
|
100
|
+
f'in {data_dir}!')
|
|
101
|
+
if connection and not creating and not os.path.exists(db_path):
|
|
102
|
+
# 'use existing' must not silently fall through to creating a file
|
|
103
|
+
raise sqlite3_OperationalError(
|
|
104
|
+
f'The database “{connection}” does not exist in “{data_dir}”. '
|
|
105
|
+
'Select an existing database or create a new one.')
|
|
106
|
+
check_database(dbpath=db_path)
|
|
107
|
+
except sqlite3_OperationalError as e:
|
|
108
|
+
if REQUEST is None:
|
|
109
|
+
raise
|
|
110
|
+
# Stay on the Add form. MessageDialog is the old full-page dialog and
|
|
111
|
+
# would leave the ZMI layout.
|
|
112
|
+
return _add_form(self, REQUEST, extract_error(e))
|
|
113
|
+
# Note - ZSQLiteDA's connect immediately check is alway false
|
|
114
|
+
self._setObject(id, Connection(
|
|
115
|
+
id, title, connection, REQUEST=REQUEST, data_dir=data_dir))
|
|
116
|
+
if REQUEST is not None:
|
|
117
|
+
return self.manage_main(self, REQUEST)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class Connection(Shared.DC.ZRDB.Connection.Connection):
|
|
121
|
+
" "
|
|
122
|
+
|
|
123
|
+
security = ClassSecurityInfo()
|
|
124
|
+
_isAnSQLConnection = True
|
|
125
|
+
data_dir = DEFAULT_DATA_DIR
|
|
126
|
+
|
|
127
|
+
def __init__(self, id: str, title: str, connection_string: str = '',
|
|
128
|
+
REQUEST=None, data_dir: str = None) -> None:
|
|
129
|
+
if data_dir is None:
|
|
130
|
+
data_dir = (REQUEST.get('data_dir', DEFAULT_DATA_DIR)
|
|
131
|
+
if REQUEST is not None else DEFAULT_DATA_DIR)
|
|
132
|
+
create_db_file(data_dir=data_dir, connection_string=connection_string)
|
|
133
|
+
self.data_dir = data_dir
|
|
134
|
+
super().__init__(id, title, connection_string)
|
|
135
|
+
|
|
136
|
+
def data_dir_is_default(self): # used in the DTML forms
|
|
137
|
+
return self.data_dir == DEFAULT_DATA_DIR
|
|
138
|
+
|
|
139
|
+
manage_options = Shared.DC.ZRDB.Connection.Connection.manage_options + (
|
|
140
|
+
{'label': 'Browse', 'action': 'manage_browse'},
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
manage_browse = HTMLFile('dtml/browse', globals())
|
|
144
|
+
|
|
145
|
+
database_type = database_type
|
|
146
|
+
da_version = __version__
|
|
147
|
+
id = f'{database_type}_database_connection'
|
|
148
|
+
meta_type = title = f'Z {database_type} Database Connection'
|
|
149
|
+
zmi_icon = 'fas fa-database'
|
|
150
|
+
|
|
151
|
+
manage_main = HTMLFile('dtml/connectionStatus', globals())
|
|
152
|
+
|
|
153
|
+
manage_properties = HTMLFile(
|
|
154
|
+
'dtml/connectionEdit', globals(),
|
|
155
|
+
data_sources=data_sources,
|
|
156
|
+
DEFAULT_DATA_DIR=DEFAULT_DATA_DIR
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
@security.protected(change_database_connections)
|
|
160
|
+
def manage_edit(self, title: str, data_dir: str, connection_string: str = '',
|
|
161
|
+
new_database: str = '', check=None, REQUEST=None) -> str:
|
|
162
|
+
"""Change the connection's title, data directory and database.
|
|
163
|
+
|
|
164
|
+
``data_dir`` must be in the allowlist, except that a connection whose
|
|
165
|
+
stored directory pre-dates the allowlist stays editable in place (e.g.
|
|
166
|
+
to fix its title) without being re-opened. ``connection_string`` selects
|
|
167
|
+
an existing database in that directory; ``new_database`` creates one.
|
|
168
|
+
"""
|
|
169
|
+
def fail(message):
|
|
170
|
+
# Programmatic callers (REQUEST is None) get an exception rather
|
|
171
|
+
# than a management page they cannot render (connectionEdit.dtml
|
|
172
|
+
# dereferences REQUEST).
|
|
173
|
+
if REQUEST is None:
|
|
174
|
+
raise sqlite3_OperationalError(message)
|
|
175
|
+
return self._properties_message(REQUEST, message, 'danger')
|
|
176
|
+
|
|
177
|
+
for name in (new_database, connection_string):
|
|
178
|
+
try:
|
|
179
|
+
safe_db_name(name)
|
|
180
|
+
except sqlite3_OperationalError as e:
|
|
181
|
+
return fail(extract_error(e))
|
|
182
|
+
|
|
183
|
+
dir_changed = (data_dir != self.data_dir)
|
|
184
|
+
|
|
185
|
+
# A connection whose stored directory is not on the allowlist (e.g. one
|
|
186
|
+
# created before the allowlist existed) may still be edited in place:
|
|
187
|
+
# its title can be changed, but it is not re-opened, its directory may
|
|
188
|
+
# not be changed to another non-allowed one, and no database may be
|
|
189
|
+
# created there. Anything that actually opens a database still goes
|
|
190
|
+
# through connect() -> check_database, so this stays secure by default.
|
|
191
|
+
if not can_mount_from(data_dir):
|
|
192
|
+
if dir_changed or new_database:
|
|
193
|
+
return fail(
|
|
194
|
+
f'This adapter may not use databases in “{data_dir}”. '
|
|
195
|
+
'Choose an allowed directory, or add this one to the file '
|
|
196
|
+
'named by “directories-file” in the '
|
|
197
|
+
'<product-config Products.ZSQLiteDA> section of zope.conf.')
|
|
198
|
+
self.edit(title, self.connection_string, None)
|
|
199
|
+
if REQUEST is not None:
|
|
200
|
+
return self._properties_message(
|
|
201
|
+
REQUEST, f'“{self.id}” has been edited.')
|
|
202
|
+
return
|
|
203
|
+
|
|
204
|
+
if not os.path.exists(data_dir):
|
|
205
|
+
return fail(
|
|
206
|
+
f'The directory “{data_dir}” does not exist.\n'
|
|
207
|
+
'Please create it on the file system.')
|
|
208
|
+
|
|
209
|
+
if new_database:
|
|
210
|
+
if os.path.exists(os.path.join(data_dir, new_database)):
|
|
211
|
+
return fail(
|
|
212
|
+
f'The database file “{new_database}” already exists\n'
|
|
213
|
+
f'in {data_dir}!')
|
|
214
|
+
try:
|
|
215
|
+
create_db_file(data_dir=data_dir, connection_string=new_database)
|
|
216
|
+
except sqlite3_OperationalError as e:
|
|
217
|
+
return fail(extract_error(e))
|
|
218
|
+
connection_string = new_database
|
|
219
|
+
elif connection_string and not os.path.exists(
|
|
220
|
+
os.path.join(data_dir, connection_string)):
|
|
221
|
+
if dir_changed:
|
|
222
|
+
# The directory was changed and the previously selected database
|
|
223
|
+
# is not in the new one: adopt the directory but drop the stale
|
|
224
|
+
# selection, rather than binding to an unrelated same-named file
|
|
225
|
+
# or failing outright.
|
|
226
|
+
connection_string = ''
|
|
227
|
+
else:
|
|
228
|
+
return fail(
|
|
229
|
+
f'The database “{connection_string}” does not exist in '
|
|
230
|
+
f'“{data_dir}”. Select a database that exists in that '
|
|
231
|
+
'directory.')
|
|
232
|
+
|
|
233
|
+
if connection_string:
|
|
234
|
+
try:
|
|
235
|
+
check_database(dbpath=os.path.join(data_dir, connection_string))
|
|
236
|
+
except sqlite3_OperationalError as e:
|
|
237
|
+
if not dir_changed and self.connection_string == connection_string:
|
|
238
|
+
# the currently-bound database has become invalid: clear it
|
|
239
|
+
self.edit(title, '', check)
|
|
240
|
+
return fail(extract_error(e))
|
|
241
|
+
|
|
242
|
+
self.data_dir = data_dir
|
|
243
|
+
|
|
244
|
+
if self.connected():
|
|
245
|
+
self.manage_close_connection()
|
|
246
|
+
|
|
247
|
+
# Re-open right away when a database is selected, so the Status and
|
|
248
|
+
# Browse tabs reflect the new connection immediately. Honour an explicit
|
|
249
|
+
# ``check`` from a programmatic caller; the UI form sends none, so it
|
|
250
|
+
# defaults to opening whenever a database is selected. An empty
|
|
251
|
+
# connection_string stays closed (connecting to '' would point at the
|
|
252
|
+
# directory and fail).
|
|
253
|
+
connect_now = bool(connection_string) if check is None else bool(check)
|
|
254
|
+
self.edit(title, connection_string,
|
|
255
|
+
1 if (connect_now and connection_string) else None)
|
|
256
|
+
|
|
257
|
+
if REQUEST is not None:
|
|
258
|
+
return self._properties_message(
|
|
259
|
+
REQUEST, f'“{self.id}” has been edited.')
|
|
260
|
+
|
|
261
|
+
def _properties_message(self, REQUEST, message, message_type='success'):
|
|
262
|
+
"""Redirect to Properties so the tab matches the page.
|
|
263
|
+
|
|
264
|
+
Rendering the template directly leaves the browser on ``manage_edit``.
|
|
265
|
+
That address matches no tab, so the first tab (Status) is highlighted
|
|
266
|
+
while the Properties form is still on screen.
|
|
267
|
+
"""
|
|
268
|
+
query = urlencode({
|
|
269
|
+
'manage_tabs_message': message,
|
|
270
|
+
'manage_tabs_type': message_type,
|
|
271
|
+
})
|
|
272
|
+
return REQUEST.RESPONSE.redirect(
|
|
273
|
+
f'{self.absolute_url()}/manage_properties?{query}')
|
|
274
|
+
|
|
275
|
+
def connected(self) -> str:
|
|
276
|
+
if hasattr(self, '_v_database_connection'):
|
|
277
|
+
return self._v_database_connection.opened
|
|
278
|
+
return ''
|
|
279
|
+
|
|
280
|
+
def connect(self, s: str) -> 'Connection':
|
|
281
|
+
# connect() is the authoritative allowlist/validation point. The Add and
|
|
282
|
+
# Edit forms validate up front for early feedback, but a legacy object
|
|
283
|
+
# whose stored data_dir predates the allowlist (or a crafted name) must
|
|
284
|
+
# not be openable through the Status tab, an implicit __call__, or a
|
|
285
|
+
# ZODB reconnect either.
|
|
286
|
+
safe_db_name(s)
|
|
287
|
+
dbpath = os.path.join(self.data_dir, s)
|
|
288
|
+
check_database(dbpath=dbpath)
|
|
289
|
+
# Opening must never *create* a database. check_database deliberately
|
|
290
|
+
# accepts a not-yet-existing file in a create-allowed directory so the
|
|
291
|
+
# Add/Edit "create new" paths can pre-create it via create_db_file();
|
|
292
|
+
# but reaching connect() with the file missing means it was removed
|
|
293
|
+
# underneath us, so surface that instead of letting sqlite3.connect()
|
|
294
|
+
# silently fabricate an empty database and mask the data loss.
|
|
295
|
+
if s and not os.path.exists(dbpath):
|
|
296
|
+
raise sqlite3_OperationalError(
|
|
297
|
+
f'The database “{s}” no longer exists in “{self.data_dir}”.')
|
|
298
|
+
# close any previous connection first, otherwise the old sqlite3
|
|
299
|
+
# handle leaks when reconnecting (e.g. Open clicked twice, or after edit)
|
|
300
|
+
self.manage_close_connection()
|
|
301
|
+
self._v_database_connection = DB(dbpath)
|
|
302
|
+
# keep the base class's _v_connected marker in sync, so title_or_id()
|
|
303
|
+
# / title_and_id() report "(connected)" like a stock ZRDB connection
|
|
304
|
+
self._v_connected = self._v_database_connection.opened
|
|
305
|
+
return self
|
|
306
|
+
|
|
307
|
+
@security.protected(open_close_database_connection)
|
|
308
|
+
def manage_open_connection(self, REQUEST=None):
|
|
309
|
+
"""Open the connection. A failure — e.g. a directory that is no longer
|
|
310
|
+
in the allowlist, or an invalid database — is shown as a red alert
|
|
311
|
+
rather than the base class's default green 'success' banner."""
|
|
312
|
+
msg, msg_type = '', 'success'
|
|
313
|
+
try:
|
|
314
|
+
self.connect(self.connection_string)
|
|
315
|
+
except Exception as exc:
|
|
316
|
+
# log with traceback like the base class does, so a failed open is
|
|
317
|
+
# diagnosable even when there is no REQUEST to show the alert on
|
|
318
|
+
logger.error('Error opening database connection.', exc_info=True)
|
|
319
|
+
msg, msg_type = str(exc), 'danger'
|
|
320
|
+
if REQUEST is not None:
|
|
321
|
+
return self.manage_main(self, REQUEST,
|
|
322
|
+
manage_tabs_message=msg,
|
|
323
|
+
manage_tabs_type=msg_type)
|
|
324
|
+
|
|
325
|
+
@security.protected(open_close_database_connection)
|
|
326
|
+
def manage_close_connection(self, REQUEST=None):
|
|
327
|
+
"""Close the connection and drop the stale volatile handle, so that
|
|
328
|
+
__call__ reconnects on demand instead of returning a closed connection."""
|
|
329
|
+
result = super().manage_close_connection(REQUEST)
|
|
330
|
+
try:
|
|
331
|
+
del self._v_database_connection
|
|
332
|
+
except AttributeError:
|
|
333
|
+
pass
|
|
334
|
+
return result
|
|
335
|
+
|
|
336
|
+
@security.protected(view_management_screens)
|
|
337
|
+
def manage_browse_data(self):
|
|
338
|
+
# Data for the Browse tab: one entry per table/view with its columns
|
|
339
|
+
# and CREATE statement, as plain built-in types so the DTML template
|
|
340
|
+
# can iterate them directly.
|
|
341
|
+
if not self.connected():
|
|
342
|
+
return []
|
|
343
|
+
c = self._v_database_connection
|
|
344
|
+
result = []
|
|
345
|
+
for d in c.tables():
|
|
346
|
+
name = d['TABLE_NAME']
|
|
347
|
+
result.append({
|
|
348
|
+
'name': name,
|
|
349
|
+
'kind': d['TABLE_TYPE'],
|
|
350
|
+
'columns': c.columns(name),
|
|
351
|
+
'sql': format_sql(c.sqlscript(name)),
|
|
352
|
+
})
|
|
353
|
+
return result
|
|
354
|
+
|
|
355
|
+
@security.protected(view_management_screens)
|
|
356
|
+
def manage_listDataSources(self, data_dir=None, REQUEST=None):
|
|
357
|
+
"""JSON: the database files in `data_dir` plus whether the directory
|
|
358
|
+
allows creating / mounting databases, used by the Properties form to
|
|
359
|
+
refresh its picker when the data directory is changed."""
|
|
360
|
+
if data_dir is None:
|
|
361
|
+
data_dir = self.data_dir
|
|
362
|
+
result = {
|
|
363
|
+
'data_dir': data_dir,
|
|
364
|
+
'can_create': can_create_in(data_dir),
|
|
365
|
+
'can_mount': can_mount_from(data_dir),
|
|
366
|
+
'databases': [],
|
|
367
|
+
'error': '',
|
|
368
|
+
}
|
|
369
|
+
if result['can_mount']:
|
|
370
|
+
try:
|
|
371
|
+
result['databases'] = [name for name, _ in
|
|
372
|
+
data_sources(data_dir=data_dir)]
|
|
373
|
+
except sqlite3_OperationalError as e:
|
|
374
|
+
result['error'] = extract_error(e)
|
|
375
|
+
else:
|
|
376
|
+
result['error'] = 'This directory is not in the allowlist.'
|
|
377
|
+
if REQUEST is not None:
|
|
378
|
+
REQUEST.RESPONSE.setHeader('Content-Type', 'application/json')
|
|
379
|
+
return json.dumps(result)
|
|
380
|
+
|
|
381
|
+
@security.protected(view_management_screens)
|
|
382
|
+
def zsql_dir_options(self):
|
|
383
|
+
# directories offered in the Properties data-directory dropdown: the
|
|
384
|
+
# configured allowlist plus this connection's current directory (which
|
|
385
|
+
# may pre-date the allowlist), sorted.
|
|
386
|
+
return sorted(set(configured_dirs()) | {self.data_dir})
|
|
387
|
+
|
|
388
|
+
@security.protected(view_management_screens)
|
|
389
|
+
def zsql_can_create_here(self):
|
|
390
|
+
# whether new databases may be created in this connection's directory
|
|
391
|
+
return can_create_in(self.data_dir)
|
|
392
|
+
|
|
393
|
+
@security.protected(view_management_screens)
|
|
394
|
+
def zsql_can_mount_here(self):
|
|
395
|
+
# whether this connection's directory may be used at all
|
|
396
|
+
return can_mount_from(self.data_dir)
|
|
397
|
+
|
|
398
|
+
@security.protected(view_management_screens)
|
|
399
|
+
def zsql_databases(self):
|
|
400
|
+
# database files for the Properties picker's initial render: empty when
|
|
401
|
+
# the current directory is not allowed or cannot be read, so a legacy or
|
|
402
|
+
# missing data_dir is never enumerated and never crashes the render.
|
|
403
|
+
# manage_listDataSources then drives the allowlist-aware refresh.
|
|
404
|
+
names = []
|
|
405
|
+
if can_mount_from(self.data_dir):
|
|
406
|
+
try:
|
|
407
|
+
names = [name for name, _ in data_sources(data_dir=self.data_dir)]
|
|
408
|
+
except sqlite3_OperationalError:
|
|
409
|
+
names = []
|
|
410
|
+
# Always include the currently-bound database, even if it is a dot-file
|
|
411
|
+
# (hidden from the listing) or has since disappeared, so the picker
|
|
412
|
+
# always has an option matching connection_string and saving the form
|
|
413
|
+
# cannot silently rebind the adapter to a different file (or wipe the
|
|
414
|
+
# binding when the select would otherwise be empty).
|
|
415
|
+
cs = self.connection_string
|
|
416
|
+
if cs and cs not in names:
|
|
417
|
+
names.append(cs)
|
|
418
|
+
return sorted(names)
|
|
419
|
+
|
|
420
|
+
def manage_wizard(self, tables):
|
|
421
|
+
pass
|
|
422
|
+
|
|
423
|
+
def manage_join(self, tables, select_cols, join_cols, REQUEST=None):
|
|
424
|
+
pass
|
|
425
|
+
|
|
426
|
+
def manage_insert(self, table, cols, REQUEST=None):
|
|
427
|
+
pass
|
|
428
|
+
|
|
429
|
+
def manage_update(self, table, keys, cols, REQUEST=None):
|
|
430
|
+
pass
|
|
@@ -10,6 +10,12 @@
|
|
|
10
10
|
# FOR A PARTICULAR PURPOSE
|
|
11
11
|
#
|
|
12
12
|
##############################################################################
|
|
13
|
+
# Single source of truth for the package version: setup.py parses this
|
|
14
|
+
# line, DA.py imports it for the ZMI display. Must stay above the DA
|
|
15
|
+
# import -- DA.py does ``from . import __version__`` while this module
|
|
16
|
+
# is still initialising.
|
|
17
|
+
__version__ = '0.9.0'
|
|
18
|
+
|
|
13
19
|
from . import DA
|
|
14
20
|
from App.ImageFile import ImageFile
|
|
15
21
|
|