Last active
February 14, 2018 15:47
-
-
Save tym-xqo/978a1da88deb8987b2ba6390f02ed855 to your computer and use it in GitHub Desktop.
Frontmatter handler for SQL files
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
import re | |
import sqlite3 | |
import unittest | |
import frontmatter | |
from frontmatter import YAMLHandler | |
class SQLHandler(YAMLHandler): | |
""" Subclass of YAMLHandler that uses SQL comment indicators `/* <comment> */` as frontmatter | |
boundary. Allows inclusion of YAML frontmatter in SQL scripts that can still be passed to | |
a database query planner without initial parsing. | |
""" | |
FM_BOUNDARY = re.compile(r'^(?:/\*|\*/)$', re.MULTILINE) | |
START_DELIMITER = '\*' | |
END_DELIMITER = '*/' | |
class TestSQLHandler(unittest.TestCase): | |
def test_frontmatter_parsing(self): | |
s = frontmatter.load('test.sql', handler=SQLHandler()) | |
self.assertEqual(s['title'], 'test') | |
def test_sql_submission(self): | |
with open('test.sql', 'r') as f: | |
q = f.read() | |
conn = sqlite3.connect(':memory:') | |
c = conn.cursor() | |
r = c.execute(q).fetchall() | |
self.assertEqual(r[0][0], 1) | |
if __name__ == '__main__': | |
unittest.main() |
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
/* | |
title: test | |
foo: bar | |
date: 2018-02-14T01:29:12Z | |
*/ | |
select 1 as foo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment