postgres-pr 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ $LOAD_PATH.unshift '../../lib'
2
+ $LOAD_PATH.unshift '/tmp/og-0.5.0/lib'
3
+ require 'og'
4
+ require 'glue/logger'
5
+
6
+ $DBG = true
7
+
8
+ class User
9
+ end
10
+
11
+ class Comment
12
+ prop_accessor :body, String
13
+ belongs_to :user, User
14
+ end
15
+
16
+ class User
17
+ prop_accessor :name, String
18
+ has_many :comments, Comment
19
+ end
20
+
21
+ if __FILE__ == $0
22
+ config = {
23
+ :address => "localhost",
24
+ :database => "mneumann",
25
+ :backend => "psql",
26
+ :user => "mneumann",
27
+ :password => "",
28
+ :connection_count => 1
29
+ }
30
+ $log = Logger.new(STDERR)
31
+ $og = Og::Database.new(config)
32
+
33
+ $og.get_connection
34
+
35
+ u1 = User.new
36
+ u1.name = "Michael Neumann"
37
+ u1.save!
38
+
39
+ u2 = User.new
40
+ u2.name = "John User"
41
+ u2.save!
42
+
43
+ c1 = Comment.new
44
+ c1.body = "og test"
45
+ c1.user = u1
46
+ c1.save!
47
+
48
+ p User.all
49
+ p Comment.all
50
+ end
data/lib/buffer.rb CHANGED
@@ -7,12 +7,19 @@ class Buffer
7
7
  class Error < RuntimeError; end
8
8
  class EOF < Error; end
9
9
 
10
- def initialize(size)
10
+ def self.from_string(str)
11
+ new(str)
12
+ end
13
+
14
+ def self.of_size(size)
11
15
  raise ArgumentError if size < 0
16
+ new('#' * size)
17
+ end
12
18
 
13
- @size = size
19
+ def initialize(content)
20
+ @size = content.size
21
+ @content = content
14
22
  @position = 0
15
- @content = "#" * @size
16
23
  end
17
24
 
18
25
  def size
@@ -58,10 +65,11 @@ class Buffer
58
65
  write(str)
59
66
  n -= str.size
60
67
  end
68
+ raise if n < 0
61
69
  end
62
70
 
63
71
  def write_cstring(cstr)
64
- raise ArgumentError, "Invalid Ruby/cstring" if cstr.include?("\000")
72
+ raise ArgumentError, "Invalid Ruby/cstring" if cstr.include?(0)
65
73
  write(cstr)
66
74
  write("\000")
67
75
  end
@@ -34,7 +34,7 @@ class Message
34
34
  raise ParseError unless length >= 4
35
35
 
36
36
  # initialize buffer
37
- buffer = Buffer.new(startup ? length : 1+length)
37
+ buffer = Buffer.of_size(startup ? length : 1+length)
38
38
  buffer.write_byte(type) unless startup
39
39
  buffer.write_int32_network(length)
40
40
  buffer.copy_from_stream(stream, length-4)
@@ -53,7 +53,7 @@ class Message
53
53
  end
54
54
 
55
55
  def dump(body_size=0)
56
- buffer = Buffer.new(5 + body_size)
56
+ buffer = Buffer.of_size(5 + body_size)
57
57
  buffer.write_byte(self.message_type)
58
58
  buffer.write_int32_network(4 + body_size)
59
59
  yield buffer if block_given?
@@ -462,7 +462,7 @@ class StartupMessage < Message
462
462
  def dump
463
463
  sz = @params.inject(4 + 4) {|sum, kv| sum + kv[0].size + 1 + kv[1].size + 1} + 1
464
464
 
465
- buffer = Buffer.new(sz)
465
+ buffer = Buffer.of_size(sz)
466
466
  buffer.write_int32_network(sz)
467
467
  buffer.write_int32_network(@proto_version)
468
468
  @params.each_pair {|key, value|
@@ -498,7 +498,7 @@ class SSLRequest < Message
498
498
 
499
499
  def dump
500
500
  sz = 4 + 4
501
- buffer = Buffer.new(sz)
501
+ buffer = Buffer.of_size(sz)
502
502
  buffer.write_int32_network(sz)
503
503
  buffer.write_int32_network(@ssl_request_code)
504
504
  raise DumpError unless buffer.at_end?
@@ -29,24 +29,69 @@ class PGconn
29
29
  end
30
30
 
31
31
  alias exec query
32
+
33
+ def self.escape(str)
34
+ # TODO: correct?
35
+ str.gsub(/\\/){ '\\\\' }.gsub(/'/){ '\\\'' }
36
+ end
37
+
32
38
  end
33
39
 
34
40
  class PGresult
35
- attr_reader :fields, :result
41
+ include Enumerable
42
+
43
+ def each(&block)
44
+ @result.each(&block)
45
+ end
36
46
 
47
+ def [](index)
48
+ @result[index]
49
+ end
50
+
37
51
  def initialize(res)
38
52
  @res = res
39
53
  @fields = @res.fields.map {|f| f.name}
40
54
  @result = @res.rows
41
55
  end
42
56
 
43
- include Enumerable
57
+ # TODO: status, getlength, cmdstatus
44
58
 
45
- def each(&block)
46
- @result.each(&block)
59
+ attr_reader :result, :fields
60
+
61
+ def num_tuples
62
+ @result.size
47
63
  end
48
64
 
49
- def [](index)
50
- @result[index]
65
+ def num_fields
66
+ @fields.size
67
+ end
68
+
69
+ def fieldname(index)
70
+ @fields[index]
71
+ end
72
+
73
+ def fieldnum(name)
74
+ @fields.index(name)
75
+ end
76
+
77
+ def type(index)
78
+ raise
79
+ # TODO: correct?
80
+ @res.fields[index].type_oid
81
+ end
82
+
83
+ def size(index)
84
+ raise
85
+ # TODO: correct?
86
+ @res.fields[index].typlen
87
+ end
88
+
89
+ def getvalue(tup_num, field_num)
90
+ @result[tup_num][field_num]
91
+ end
92
+
93
+ # free the result set
94
+ def clear
95
+ @res = @fields = @result = nil
51
96
  end
52
97
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.1
3
3
  specification_version: 1
4
4
  name: postgres-pr
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.2
7
- date: 2004-11-23
6
+ version: 0.3.0
7
+ date: 2004-12-07
8
8
  summary: A pure Ruby interface to the PostgreSQL database
9
9
  require_paths:
10
10
  - lib
@@ -29,18 +29,20 @@ files:
29
29
  - lib/binary_writer.rb
30
30
  - lib/byteorder.rb
31
31
  - lib/postgres-pr
32
- - lib/binary_reader.rb
33
- - lib/postgres.rb
34
- - lib/buffer.rb
35
- - lib/postgres-pr/connection.rb
36
32
  - lib/postgres-pr/typeconv
37
- - lib/postgres-pr/message.rb
38
- - lib/postgres-pr/postgres-compat.rb
39
33
  - lib/postgres-pr/typeconv/array.rb
40
34
  - lib/postgres-pr/typeconv/bytea.rb
41
35
  - lib/postgres-pr/typeconv/conv.rb
42
36
  - lib/postgres-pr/typeconv/TC_conv.rb
37
+ - lib/postgres-pr/connection.rb
38
+ - lib/postgres-pr/message.rb
39
+ - lib/postgres-pr/postgres-compat.rb
40
+ - lib/binary_reader.rb
41
+ - lib/postgres.rb
42
+ - lib/buffer.rb
43
43
  - test/TC_message.rb
44
+ - examples/og
45
+ - examples/og/test.rb
44
46
  - examples/test_connection.rb
45
47
  - examples/client.rb
46
48
  - examples/server.rb