main
commit
dcac1c8b1b
Binary file not shown.
|
|
@ -0,0 +1,5 @@
|
||||||
|
module 0_setup
|
||||||
|
|
||||||
|
go 1.18
|
||||||
|
|
||||||
|
require github.com/mattn/go-sqlite3 v1.14.15
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
|
|
@ -0,0 +1,74 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
dbpath := flag.String("db", "./db.db", "path to db file")
|
||||||
|
q := flag.String("q", "show tables", "query to execute")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
query := strings.Trim(strings.TrimSpace(*q), ";")
|
||||||
|
if len(query) == 0 {
|
||||||
|
panic("refusing empty query")
|
||||||
|
}
|
||||||
|
query += ";"
|
||||||
|
log.Printf("%s", query)
|
||||||
|
|
||||||
|
db, err := sql.Open("sqlite3", *dbpath)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(strings.ToUpper(strings.TrimSpace(query)), "SELECT") {
|
||||||
|
rows, err := db.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
columns, err := rows.Columns()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ptrs := make([]any, len(columns))
|
||||||
|
for i := range ptrs {
|
||||||
|
var v interface{}
|
||||||
|
ptrs[i] = &v
|
||||||
|
}
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
if err := rows.Scan(ptrs...); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
result := make([]string, len(columns))
|
||||||
|
for i := range ptrs {
|
||||||
|
ptr := ptrs[i].(*interface{})
|
||||||
|
result[i] = fmt.Sprintf("%s:%+v", columns[i], *ptr)
|
||||||
|
}
|
||||||
|
log.Println(result)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
stmt, err := db.Prepare(query)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := stmt.Exec()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
affected, err := result.RowsAffected()
|
||||||
|
log.Printf("%d: %v", affected, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
main() {
|
||||||
|
set -e
|
||||||
|
set -o pipefail
|
||||||
|
|
||||||
|
local bin=./0_setup
|
||||||
|
local cmds=(
|
||||||
|
'DROP TABLE IF EXISTS a'
|
||||||
|
'CREATE TABLE a(
|
||||||
|
x INT,
|
||||||
|
y VARCHAR(20),
|
||||||
|
PRIMARY KEY (x)
|
||||||
|
)'
|
||||||
|
'DROP TABLE IF EXISTS b'
|
||||||
|
'CREATE TABLE b(
|
||||||
|
x INT,
|
||||||
|
y VARCHAR(20),
|
||||||
|
PRIMARY KEY (x)
|
||||||
|
)'
|
||||||
|
'SELECT * FROM a'
|
||||||
|
'INSERT INTO a
|
||||||
|
(x, y)
|
||||||
|
VALUES
|
||||||
|
(4, "a")
|
||||||
|
'
|
||||||
|
'SELECT * FROM a'
|
||||||
|
'SELECT * FROM b'
|
||||||
|
'INSERT INTO b
|
||||||
|
(x, y)
|
||||||
|
VALUES
|
||||||
|
(5, "b")
|
||||||
|
'
|
||||||
|
'SELECT * FROM b'
|
||||||
|
'UPDATE b
|
||||||
|
SET x=4
|
||||||
|
WHERE y="b"
|
||||||
|
'
|
||||||
|
'SELECT * FROM b'
|
||||||
|
'SELECT * FROM a JOIN b WHERE a.x == b.x'
|
||||||
|
'SELECT a.y, b.y FROM a JOIN b WHERE a.x == b.x'
|
||||||
|
)
|
||||||
|
|
||||||
|
for cmd in "${cmds[@]}"; do
|
||||||
|
echo =================
|
||||||
|
$bin -q "$cmd"
|
||||||
|
echo
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
main() {
|
||||||
|
set -e
|
||||||
|
set -o pipefail
|
||||||
|
|
||||||
|
local bin=../0_setup/0_setup
|
||||||
|
local cmds=(
|
||||||
|
'DROP TABLE IF EXISTS events'
|
||||||
|
'
|
||||||
|
CREATE TABLE events (
|
||||||
|
sensor_id integer not null,
|
||||||
|
event_type integer not null
|
||||||
|
);
|
||||||
|
'
|
||||||
|
'INSERT INTO events VALUES (2, 2)'
|
||||||
|
'INSERT INTO events VALUES (2, 4)'
|
||||||
|
'INSERT INTO events VALUES (2, 2)'
|
||||||
|
'INSERT INTO events VALUES (3, 2)'
|
||||||
|
'INSERT INTO events VALUES (2, 3)'
|
||||||
|
"$@"
|
||||||
|
)
|
||||||
|
|
||||||
|
for cmd in "${cmds[@]}"; do
|
||||||
|
echo =================
|
||||||
|
$bin -q "$cmd"
|
||||||
|
echo
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
||||||
Binary file not shown.
|
|
@ -0,0 +1,4 @@
|
||||||
|
task1: ezpz
|
||||||
|
task2: that one was like "if you turn on bulbs in order X, Y, Z; then how many seconds are all at+before the current bulb active?" I left a comment explaining "this is probably the google one where you have O(1) space by doing negatives; that'll do". Looks like my performance was bad (as I said in comment n**2). No surprise given it's space efficient. Looks like time complexity is Codility's priority. Surprised I failed 1 of 5 correctness, but that should be fatal.
|
||||||
|
task3: I LITERALLY DIDNT SORT YET GOT 100% CORRECT that'd disgusting
|
||||||
|
task4: I missed a multi-choice question. Bullshit. They were so fuckin easy and I bothered to double check. I don't trust proctor here.
|
||||||
Loading…
Reference in New Issue