pgb 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 060fc2513c44822e85176d57cd22e8ce31c36b65b1103366048fef1a2aaba5d1
4
- data.tar.gz: 2ae4bee33b32d211311629d680b78901a0794c7cd2898b44774795e3646c1c64
3
+ metadata.gz: 3c0c8b5ca1f08c57f115c1c42dca22b2bb73a3307f91e162fc2209410eea9851
4
+ data.tar.gz: 409321568093c7babb7ec83507c03cced5d2aa71a95e77b149d7e2b854a6ee48
5
5
  SHA512:
6
- metadata.gz: b3a661d7941ea09a150a93d211c0b373296b4804158cb9afe34282d16d3a3ca0fecbbe3af7c1c2c7633d9db2ffbadec3adf78855159b706cca0e2ca917992fe4
7
- data.tar.gz: e46d3993fafba112ef23040653373b9373c8e4ff864027011c63e91cd6f502af9de3c67c9ab33b7b99ea1092849086cbb4c43a502ea7d81c4505d83e0a6cb084
6
+ metadata.gz: 52372c0f53a9816695f933077ecc47b82d6523dfeeac68656548990255dfb5eb24565bb23ce145a54ece88b50ed0b2e8b31075db4e074341b50f65eadcd856cb
7
+ data.tar.gz: 01f9f30f469668d5da76dc80f1b159ec3e8dfdfcadf92967df2b16440365f7dd249bd1b95563c2c6291ac2f5f995c2c12d0c3b018fd78425e72ba6f80f0ae418
data/.rubocop.yml CHANGED
@@ -15,7 +15,7 @@ Style/Documentation:
15
15
  Enabled: false
16
16
 
17
17
  Metrics/MethodLength:
18
- Max: 20
18
+ Enabled: false
19
19
 
20
20
  Lint/MissingSuper:
21
21
  Enabled: false
@@ -64,3 +64,9 @@ Layout/MultilineAssignmentLayout:
64
64
 
65
65
  Style/CommentAnnotation:
66
66
  RequireColon: false
67
+
68
+ Metrics/CyclomaticComplexity:
69
+ Enabled: false
70
+
71
+ Style/MissingElse:
72
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.1] - 2022-12-12
4
+
5
+ - Caller logging
6
+
3
7
  ## [0.1.0] - 2022-12-11
4
8
 
5
9
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pgb (0.1.0)
4
+ pgb (0.1.1)
5
5
  pg (~> 1.4)
6
6
  zeitwerk (~> 2.6)
7
7
 
data/lib/pgb/model.rb CHANGED
@@ -65,16 +65,53 @@ module PGB
65
65
 
66
66
  private
67
67
 
68
- # TODO Types
69
68
  # TODO Column in schema, but not in result
70
69
  # TODO Column in result, but not in schema
71
70
  # TODO Enums
72
71
  # TODO Custom type read / write
73
72
  def parse(row)
74
73
  object = new
75
- columns.each { |column| object.public_send("#{column}=", row.fetch(column)) }
74
+ columns.each { |column| object.public_send("#{column}=", to_ruby_type(row, column)) }
76
75
  object
77
76
  end
77
+
78
+ # TODO More types
79
+ # TODO Numeric types in old rubies?
80
+ # TODO Auto ? for booleans? (non-null only?)
81
+ # TODO Compare objects by schema, invalidate old ones?
82
+ # TODO Cut extract schema keys
83
+ def to_ruby_type(row, column)
84
+ value = row.fetch(column)
85
+ return unless value
86
+
87
+ type = schema.fetch(column).fetch('data_type')
88
+ case type
89
+ when 'bigint', 'integer', 'smallint'
90
+ Integer(value)
91
+ when 'character varying'
92
+ value
93
+ when 'boolean'
94
+ parse_boolean(value)
95
+ when 'jsonb'
96
+ JSON.parse(value)
97
+ when 'timestamp without time zone', 'date', 'USER-DEFINED'
98
+ # TODO USER-DEFINED = citext, enum, others
99
+ "#{value} ! TODO"
100
+ else
101
+ raise Error, "Unknown db type: #{type}"
102
+ end
103
+ end
104
+
105
+ def parse_boolean(value)
106
+ case value
107
+ when 't'
108
+ true
109
+ when 'f'
110
+ false
111
+ else
112
+ raise Error, "Unknown boolean db representation: #{value.inspect}"
113
+ end
114
+ end
78
115
  end
79
116
  end
80
117
  end
data/lib/pgb/user.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PGB
4
+ # TODO Remove from git
5
+ class User < Model
6
+ end
7
+ end
data/lib/pgb/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PGB
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
data/lib/pgb.rb CHANGED
@@ -20,6 +20,8 @@
20
20
  # TODO CI
21
21
  # TODO Push gem
22
22
  # TODO Check code is compatible with ruby 2.7
23
+ # TODO Performance
24
+ # TODO Clean up changelog when somewhat stable
23
25
 
24
26
  require 'pg'
25
27
  require 'zeitwerk'
@@ -38,6 +40,9 @@ loader.setup
38
40
  PGB.loader = loader
39
41
 
40
42
  module PGB
43
+ SUPPORTED_PG_VERSIONS = (11..15).freeze
44
+ WORKING_DIR = File.expand_path(__dir__)
45
+
41
46
  class Error < StandardError
42
47
  end
43
48
 
@@ -57,9 +62,12 @@ module PGB
57
62
  # TODO Check postgres version on first call
58
63
  # TODO Look through everything
59
64
  # TODO Enable all rubocop cops
65
+ # TODO Coloring if supported
60
66
  def execute(query)
61
- sql = query.is_a?(Query) || query.is_a?(Command) ? query.to_sql : query
62
- puts sql
67
+ sql = query.is_a?(Query) || query.is_a?(Command) ? query.to_sql : query.strip.tr("\n", ' ')
68
+ # TODO Weird
69
+ connection
70
+ log(sql)
63
71
  connection.exec(sql).to_a
64
72
  end
65
73
 
@@ -67,16 +75,43 @@ module PGB
67
75
  def connection
68
76
  @connection ||= begin
69
77
  connection = PG.connect(dbname: 'hubstaff_development')
78
+ ensure_pg_version_supported(connection)
70
79
  connection
71
80
  end
72
81
  end
73
82
 
83
+ # TODO Different backtrace order in different versions?
84
+ # TODO Disable sql logging?
85
+ # TODO Disable caller logging?
86
+ def log(sql)
87
+ log_line = sql
88
+
89
+ frame = caller.find { !_1['/lib/ruby/gems/'] }
90
+ if frame
91
+ frame = frame.gsub("#{WORKING_DIR}/", '').strip
92
+ log_line += " -- #{frame}" unless frame.empty?
93
+ end
94
+
95
+ puts log_line
96
+ end
97
+
98
+ # TODO Check expected response format
99
+ def ensure_pg_version_supported(connection)
100
+ sql = 'SELECT version()'
101
+ log(sql)
102
+ version = Integer(connection.exec(sql).first.fetch('version').match(/PostgreSQL (\d+)\./)[1])
103
+ unless SUPPORTED_PG_VERSIONS.include?(version)
104
+ raise Error, "Unsupported Postgres version: #{version}, supported: #{SUPPORTED_PG_VERSIONS}"
105
+ end
106
+ end
107
+
74
108
  # TODO Extract
75
109
  # TODO Model is not a table name (e.g. query)?
76
110
  # TODO Do not select all columns
77
111
  # TODO Request as PGB query
78
112
  # TODO Support getting schema for all tables
79
113
  # TODO Remove line breaks for raw queries?
114
+ # TODO Check expected response format
80
115
  def schema(table_name)
81
116
  table_name = table_name.to_s
82
117
  @schema ||= begin
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Gubitskiy
@@ -73,6 +73,7 @@ files:
73
73
  - lib/pgb/shortcuts.rb
74
74
  - lib/pgb/sql_displayable.rb
75
75
  - lib/pgb/type_cast.rb
76
+ - lib/pgb/user.rb
76
77
  - lib/pgb/version.rb
77
78
  homepage: https://github.com/enthrops/pgb
78
79
  licenses:
@@ -91,6 +92,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
92
  - - ">="
92
93
  - !ruby/object:Gem::Version
93
94
  version: '2.7'
95
+ - - "<"
96
+ - !ruby/object:Gem::Version
97
+ version: '3.2'
94
98
  required_rubygems_version: !ruby/object:Gem::Requirement
95
99
  requirements:
96
100
  - - ">="