common_sqlite
SQLite utility functions that are independent of API version.
DatabaseError
Bases: Exception
General LUTE database error.
Source code in lute/io/_db/common_sqlite.py
13 14 15 16 | |
compare_cols(cols1, cols2)
Compare whether two sets of columns are identical.
The comparison is unidirectional - This function tests for columns present
in cols2 which are not present in cols1, but NOT vice versa. Switch the
order of the arguments in order to retrieve the other comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cols1
|
Dict[str, str]
|
Dictionary of first set of column names and types. |
required |
cols2
|
Dict[str, str]
|
Dictionary of second set of column names and types. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
diff |
Dict[str, str] | None
|
Any columns present in |
Source code in lute/io/_db/common_sqlite.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
does_table_exist(con, table_name)
Check whether a table exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
Connection
|
Database connection. |
required |
table_name
|
str
|
The table to check for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
does_exist |
bool
|
Whether the table exists. |
Source code in lute/io/_db/common_sqlite.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
execute_in_transaction(con, script)
Execute a multi-statement SQL script without committing.
sqlite3.Connection.executescript always commits any pending transaction
first, so it cannot be used inside a migration. This splits the script into
complete statements (TRIGGER bodies included) and executes them one by one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
Connection
|
Database connection. |
required |
script
|
str
|
The SQL statements to execute. |
required |
Raises:
| Type | Description |
|---|---|
DatabaseError
|
If the script ends with an incomplete statement. |
Source code in lute/io/_db/common_sqlite.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
get_all_rows_for_table(con, table_name)
Return all rows for a requested table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
Connection
|
Database connection. |
required |
table_name
|
str
|
The table's name. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
rows |
List[Tuple[Any, ...]]
|
ALL rows for a table. |
Source code in lute/io/_db/common_sqlite.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
get_table_cols(con, table_name)
Retrieve the columns currently present in a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
Connection
|
Database connection. |
required |
table_name
|
str
|
The table's name. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
cols |
Dict[str, str]
|
A dictionary of column names and types. |
Source code in lute/io/_db/common_sqlite.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
get_tables(con)
Retrieve a list of all tables in a database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
Connection
|
Database connection. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tables |
List[str]
|
A list of database tables. |
Source code in lute/io/_db/common_sqlite.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
get_user_version(con)
Retrieve the schema version stored in the database file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
Connection
|
Database connection. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
user_version |
int
|
The value of |
Source code in lute/io/_db/common_sqlite.py
136 137 138 139 140 141 142 143 144 145 | |
set_user_version(con, user_version)
Set the schema version stored in the database file.
This is transactional: it is undone if the enclosing transaction is rolled back.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
Connection
|
Database connection. |
required |
user_version
|
int
|
The new value of |
required |
Source code in lute/io/_db/common_sqlite.py
148 149 150 151 152 153 154 155 156 157 158 159 160 | |
write_transaction(con)
Run a block of writes as one transaction.
The write lock is taken at the start (BEGIN IMMEDIATE), so concurrent
writers wait for each other instead of interleaving. The block is committed
if it completes, and rolled back entirely on any exception, which is then
re-raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
Connection
|
Database connection, not in a transaction. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
con |
Connection
|
The same connection. |
Source code in lute/io/_db/common_sqlite.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |