This file contains 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
#!/bin/bash | |
echo "Hello world" |
This file contains 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
version: '2' | |
networks: | |
roachnet: | |
services: | |
master: | |
image: cockroachdb/cockroach:v2.0.3 | |
command: start --insecure | |
ports: |
This file contains 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
$ docker exec -it goroach_master_1 ./cockroach sql --insecure | |
> create database blog_db; | |
> set database = blog_db; | |
> CREATE TABLE "posts" ( | |
"id" SERIAL, | |
"name" STRING(100), | |
"category" STRING(50), | |
"author" STRING(50), |
This file contains 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
func init() { | |
roachConnection, err := sql.Open("postgres", "postgresql://root@localhost:26257/blog_db?sslmode=disable") | |
if err != nil { | |
panic("Could not establish connection to CockroachDB") | |
} | |
} |
This file contains 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
func createRecord(post Post) error { | |
qryString := fmt.Sprintf( | |
"INSERT INTO posts (name, category, author, created_at, updated_at) VALUES ('%s', '%s', '%s', NOW(), NOW())", | |
post.Name, post.Category, post.Author) | |
_, err := roachConnection.Exec(qryString) | |
return err | |
} |
This file contains 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
func readRecords() []Post { | |
var result []Post | |
rows, err := roachConnection.Query("select * FROM posts;") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer rows.Close() | |
for rows.Next() { |
This file contains 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
func deleteRecord(id int64) error { | |
qryString := fmt.Sprintf("DELETE from posts WHERE id = %d", id) | |
fmt.Printf("Query: %v\n", qryString) | |
_, err := roachConnection.Exec(qryString) | |
return err | |
} |
This file contains 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
func updateRecord(fieldName string, newVal string, condFieldName string, condFieldValue int64) error { | |
qryString := fmt.Sprintf("UPDATE posts set %s='%s' WHERE %s=%d", fieldName, newVal, condFieldName, condFieldValue) | |
fmt.Printf("Query: %v\n", qryString) | |
_, err := roachConnection.Exec(qryString) | |
return err | |
} |
This file contains 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
require 'open-uri' | |
require 'nokogiri' | |
document = Nokogiri::HTML(open(URL)) |
This file contains 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
results = [] | |
products = document.xpath("//ul[@class='products']/li") | |
products.each do |p| | |
json_product = {} | |
json_product[:title] = p.xpath("div/div[@class='product-header']").text.strip | |
json_product[:description] = p.xpath("div/div[@class='product-body']/p[contains(@class, 'description')]").text.strip | |
json_product[:price] = p.xpath("div/div[@class='product-footer']/p[contains(@class, 'price')]").text.strip | |
results << json_product | |
end |
OlderNewer