-
Notifications
You must be signed in to change notification settings - Fork 978
Look ma, multiple DB support (Part I of the DB saga) #2924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
58e44cd
wallet: Expand select query field lists from macros
cdecker 257e569
wallet: Remove printf-like db_select variant
cdecker 7c8cb7e
wallet: Change db_select_prepare to check for select instead
cdecker 9737571
wallet: Add macro to annotate SQL statements
cdecker 3a32fbc
wallet: Annotate migrations using the SQL macro
cdecker 1668290
db: Make the `db` struct private and provide accessors instead
cdecker 0864fa9
wallet: Add tooling to extract SQL queries and generate driver info
cdecker eea1c70
wallet: Look up the db_config for the given driver in db_open
cdecker ce41328
wallet: Move the db_fatal definition so we can use it in drivers
cdecker 7e5a14c
wallet: Move the struct db definition to db_common.h
cdecker 3a49547
wallet: Add read-only flag to extracted queries
cdecker ec3e5cb
db: Implement skaffolding for the dispatch of DB-specific functions
cdecker d726142
db: Implement the sqlite3 driver
cdecker 89dae2c
db: Switch to new DB asbtraction for DB migrations
cdecker 643446b
wallet: Call db_stmt_free from the db_stmt destructor automatically
cdecker 91500cd
db: Track whether a db_stmt has been executed
cdecker 4207120
db: Implement basic query capabilities
cdecker 74bafc4
db: Add method to count changed rows of a db_stmt
cdecker a09a1e1
db: Add setup and teardown function to DB
cdecker f18eaa0
sqlite3: Move begin transaction and commit into the driver
cdecker 55f53d8
db: Migrate to DB abstraction layer in db.c
cdecker 7e24a75
db: Add more type-safe bindings to the interface
cdecker fa413ed
db: Add type-safe column access functions
cdecker 1f87560
db: Add DB-specific db_last_insert_id
cdecker 4aacd62
db: Migrate invoices.c to new abstraction layer
cdecker 58cdb46
db: Migrate wallet.c to the new abstraction layer
cdecker 3449729
db: Move tracking of pending statements into the `struct db`
cdecker c431f63
db: Move statement expansion into the driver
cdecker 755df7c
db: Switch to indirect `db_last_insert_id` version
cdecker 7402aea
db: Switch to indirect db close
cdecker 340d0de
db: Remove sqlite3 from db.c and db.h
cdecker 969f469
db: Extract db config lookup into its own function
cdecker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| from mako.template import Template | ||
|
|
||
| import sys | ||
|
|
||
|
|
||
| class Sqlite3Rewriter(object): | ||
| def rewrite(self, query): | ||
| return query | ||
|
|
||
|
|
||
| rewriters = { | ||
| "sqlite3": Sqlite3Rewriter(), | ||
| } | ||
|
|
||
| template = Template("""#ifndef LIGHTNINGD_WALLET_GEN_DB_${f.upper()} | ||
| #define LIGHTNINGD_WALLET_GEN_DB_${f.upper()} | ||
|
|
||
| #include <config.h> | ||
| #include <wallet/db_common.h> | ||
|
|
||
| #if HAVE_${f.upper()} | ||
|
|
||
| struct db_query db_${f}_queries[] = { | ||
|
|
||
| % for elem in queries: | ||
| { | ||
| .name = "${elem['name']}", | ||
| .query = "${elem['query']}", | ||
| .placeholders = ${elem['placeholders']}, | ||
| .readonly = ${elem['readonly']}, | ||
| }, | ||
| % endfor | ||
| }; | ||
|
|
||
| #define DB_${f.upper()}_QUERY_COUNT ${len(queries)} | ||
|
|
||
| #endif /* HAVE_${f.upper()} */ | ||
|
|
||
| #endif /* LIGHTNINGD_WALLET_GEN_DB_${f.upper()} */ | ||
| """) | ||
|
|
||
|
|
||
| def extract_queries(pofile): | ||
| # Given a po-file, extract all queries and their associated names, and | ||
| # return them as a list. | ||
|
|
||
| def chunk(pofile): | ||
| # Chunk a given file into chunks separated by an empty line | ||
| with open(pofile, 'r') as f: | ||
| chunk = [] | ||
| for line in f: | ||
| line = line.strip() | ||
| if line.strip() == "": | ||
| yield chunk | ||
| chunk = [] | ||
| else: | ||
| chunk.append(line.strip()) | ||
| if chunk != []: | ||
| yield chunk | ||
|
|
||
| queries = [] | ||
| for c in chunk(pofile): | ||
| name = c[0][3:] | ||
|
|
||
| # Skip other comments | ||
| i = 1 | ||
| while c[i][0] == '#': | ||
| i += 1 | ||
|
|
||
| # Strip header and surrounding quotes | ||
| query = c[i][7:][:-1] | ||
|
|
||
| queries.append({ | ||
| 'name': name, | ||
| 'query': query, | ||
| 'placeholders': query.count('?'), | ||
| 'readonly': "true" if query.upper().startswith("SELECT") else "false", | ||
| }) | ||
| return queries | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| if len(sys.argv) != 3: | ||
| print("Usage:\n\t{} <statements.po-file> <output-dialect>".format(sys.argv[0])) | ||
| sys.exit(1) | ||
|
|
||
| dialect = sys.argv[2] | ||
|
|
||
| if dialect not in rewriters: | ||
| print("Unknown dialect {}. The following are available: {}".format( | ||
| dialect, | ||
| ", ".join(rewriters.keys()) | ||
| )) | ||
| sys.exit(1) | ||
|
|
||
| rewriter = rewriters[dialect] | ||
|
|
||
| queries = extract_queries(sys.argv[1]) | ||
| queries = rewriter.rewrite(queries) | ||
|
|
||
| print(template.render(f=dialect, queries=queries)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| sqlparse==0.3.0 | ||
| mako==1.0.14 | ||
| mrkd==0.1.5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put template in external file?