Skip to content

Commit e96395b

Browse files
author
colinmcneil
committed
Insert code into db
1 parent 797072b commit e96395b

File tree

5 files changed

+92
-19
lines changed

5 files changed

+92
-19
lines changed
File renamed without changes.

prompts/pylint/3-5-violations-sql-run.md renamed to prompts/pylint/4-run-violation-insert.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ tools:
88
database:
99
type: string
1010
description: the path to the database
11-
sql:
12-
type: string
13-
description: the sql statement to run
1411
container:
1512
image: vonwig/sqlite:latest
1613
command:
@@ -30,40 +27,40 @@ docker run -it --rm -v thread:/thread \
3027
```
3128
docker run -it --rm -v thread:/thread \
3229
vonwig/sqlite:latest \
33-
/thread/db.dqlite \
30+
/thread/db.sqlite \
3431
".schema RANGES"
3532
```
3633

3734
```
3835
docker run -it --rm -v thread:/thread \
3936
vonwig/sqlite:latest \
40-
/thread/db.dqlite \
37+
/thread/db.sqlite \
4138
".schema VIOLATIONS"
4239
```
4340

4441
```
4542
docker run -it --rm -v thread:/thread \
4643
vonwig/sqlite:latest \
47-
/thread/db.dqlite \
44+
/thread/db.sqlite \
4845
".open /thread/insert.sql"
4946
```
5047

5148
```
5249
docker run -it --rm -v thread:/thread \
5350
vonwig/sqlite:latest \
54-
/thread/db.dqlite \
51+
/thread/db.sqlite \
5552
"SELECT * FROM VIOLATIONS INNER JOIN RANGES ON VIOLATIONS.RANGE = RANGES.ID WHERE RANGES.PATH = 'src/app.py'"
5653
```
5754

5855
```
5956
docker run -it --rm -v thread:/thread \
6057
vonwig/sqlite:latest \
61-
/thread/db.dqlite \
58+
/thread/db.sqlite \
6259
"SELECT * FROM RANGES WHERE RANGES.PATH = 'src/app.py'"
6360
```
6461

6562

6663
# prompt user
6764

68-
Run the sqlite command with the path to the database set to `/thread/db.dqlite`.
65+
Run the sqlite command with the path to the database set to `/thread/db.sqlite`.
6966

prompts/pylint/5-6-top-level-ranges.md renamed to prompts/pylint/5-1-insert-top-level-ranges.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,16 @@ An example parsed file:
118118
```
119119
to get the ranges of each top level node into capturing groups.
120120
Write the ranges to `/thread/ranges.sql` in the form of sql
121-
`INSERT INTO RANGES (LINE, END_LINE, COLUMN, END_COLUMN, PATH) values (...)`
121+
`INSERT INTO RANGES (START_LINE, END_LINE, START_COLUMN, END_COLUMN, PATH) values (...)`
122122

123123
Example output of script would be the creation of file `/thread/ranges.sql` with contents:
124124

125125
```sql
126-
INSERT INTO RANGES (LINE, COLUMN, END_LINE, END_COLUMN, NODE_TYPE, PATH) values (0,0,0,21,'comment', 'main.py')
127-
INSERT INTO RANGES (LINE, COLUMN, END_LINE, END_COLUMN, NODE_TYPE, PATH) values (2,0,2,37,'import_from_statement', 'main.py')
128-
INSERT INTO RANGES (LINE, COLUMN, END_LINE, END_COLUMN, NODE_TYPE, PATH) values (4,0,5,17,'if_statement', 'main.py')
126+
INSERT INTO RANGES (START_LINE, START_COLUMN, END_LINE, END_COLUMN, PATH) values (0,0,0,21, 'main.py')
127+
INSERT INTO RANGES (START_LINE, START_COLUMN, END_LINE, END_COLUMN, PATH) values (2,0,2,37, 'main.py')
128+
INSERT INTO RANGES (START_LINE, START_COLUMN, END_LINE, END_COLUMN, PATH) values (4,0,5,17, 'main.py')
129129
```
130130

131131
5. Execute the code in the javascript sandbox.
132132

133-
6. Finally, run `.read /thread/ranges.sql` in db.sqlite
133+
6. Finally, run `.read /thread/ranges.sql` in `/thread/db.sqlite`

prompts/pylint/5-2-insert-code.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
tools:
3+
- name: sqlite
4+
description: run the sqlite command
5+
parameters:
6+
type: object
7+
properties:
8+
database:
9+
type: string
10+
description: the path to the database
11+
sql:
12+
type: string
13+
description: the sql statement to run
14+
container:
15+
image: vonwig/sqlite:latest
16+
command:
17+
- "{{database}}"
18+
- "{{sql|safe}}"
19+
- name: run-javascript-sandbox
20+
description: execute javascript code in node
21+
parameters:
22+
type: object
23+
properties:
24+
javascript:
25+
type: string
26+
description: the javascript code to run
27+
container:
28+
image: vonwig/javascript-runner
29+
command:
30+
- "{{javascript|safe}}"
31+
---
32+
33+
# prompt user
34+
35+
We are using database `/thread/db.sqlite`.
36+
37+
0. Use JS sandbox to delete folder `/thread/code_insert` and create it again
38+
39+
1. Drop and re-create the `CODE` table with the following schema:
40+
```
41+
(ID,RANGE_ID,CODE_AT_RANGE)
42+
```
43+
44+
where RANGE_ID is a foreign key to the `RANGES` table.
45+
ID should just be a regular autoincrementing primary key
46+
CODE_AT_RANGE is text.
47+
48+
2. Prepare a script similar to the following:
49+
```
50+
.mode csv
51+
.headers on
52+
.once /thread/code_insert/all_ranges.csv
53+
<Query>
54+
```
55+
where query selects everything from the RANGES table where END_LINE is not null. Use the javascript sandbox with fs to write this script to `/thread/code_insert/query_ranges.sql`.
56+
57+
3. Execute the script against the DB: `.read /thread/code_insert/query_ranges.sql`
58+
59+
4. Now that we have `/thread/code_insert/all_ranges.csv`,
60+
61+
we get something like
62+
```csv
63+
ID,PATH,START_LINE,END_LINE,START_COLUMN,END_COLUMN
64+
125,wsgi_skylines.py,11,104,0,61
65+
```
66+
67+
So, write a JS script to
68+
- Read the CSV file manually (no modules) and iterate over the rows
69+
- Read the contents at PATH with fs and slice out the content between `START_LINE` and `END_LINE`
70+
- Write a line of SQL to populate the row in the `CODE` table. This INSERT statement should then be appended to `/thread/code_insert/insert_code.sql`.
71+
72+
The RANGE_ID should be the first value in the CSV row
73+
The CODE_AT_RANGE is the sliced content at PATH
74+
75+
Example `/thread/code_insert/insert_code.sql` file:
76+
77+
```sql
78+
INSERT INTO CODE (RANGE_ID, CODE_AT_RANGE) (125, "<Contents of wsgi_skylines from line 11 to 104>")
79+
```
80+

prompts/pylint/4.md renamed to prompts/pylint/generic_query.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,4 @@ tools:
2020

2121
# prompt user
2222

23-
1. Run `.read /thread/insert.sql` against the database `/thread/db.sqlite`.
24-
25-
2. After executing the SQL, count the number of rows in the `VIOLATIONS` and `RANGES` tables:
26-
- `SELECT count(*) FROM VIOLATIONS;`
27-
- `SELECT count(*) FROM RANGES;`
23+
How many rows are in the ranges and violations tables? Use database `/thread/db.sqlite`

0 commit comments

Comments
 (0)