-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsta.rb
66 lines (57 loc) · 1.68 KB
/
insta.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'pg'
require 'json'
require 'hashie'
require "yaml"
class PostgresDirect
# Create the connection instance.
def connect
@conn = PG.connect(
:dbname => 'zfriss',
:user => 'zfriss',
:password => '')
end
# Create our test table (assumes it doesn't already exist)
def createUserTable
@conn.exec("CREATE TABLE users (id serial NOT NULL, name character varying(255), CONSTRAINT users_pkey PRIMARY KEY (id)) WITH (OIDS=FALSE);");
end
# When we're done, we're going to drop our test table.
def dropUserTable
@conn.exec("DROP TABLE users")
end
# Prepared statements prevent SQL injection attacks. However, for the connection, the prepared statements
# live and apparently cannot be removed, at least not very easily. There is apparently a significant
# performance improvement using prepared statements.
def prepareInsertUserStatement
@conn.prepare("update_insta", "UPDATE instagram_items set full_data = $1 WHERE id = $2")
end
# Add a user with the prepared statement.
def updateInsta(full_data, id)
@conn.exec_prepared("update_insta", [full_data, id])
end
# Get our data back
def queryUserTable
@conn.exec( "SELECT * FROM instagram_items WHERE ID > 1" ) do |result|
result.each do |row|
yield row if block_given?
end
end
end
# Disconnect the back-end connection.
def disconnect
@conn.close
end
end
def main
p = PostgresDirect.new()
p.connect
begin
p.prepareInsertUserStatement
p.queryUserTable {|row| p.updateInsta(YAML.load(row['full_data']).to_json, row['id'])}
rescue Exception => e
puts e.message
puts e.backtrace.inspect
ensure
p.disconnect
end
end
main