hive_sql 1.0.0.pre.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +58 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +41 -0
  5. data/Rakefile +538 -0
  6. data/bin/hive_sql +23 -0
  7. data/hive_sql.gemspec +41 -0
  8. data/lib/hive_sql.rb +101 -0
  9. data/lib/hive_sql/models/account.rb +119 -0
  10. data/lib/hive_sql/models/block.rb +18 -0
  11. data/lib/hive_sql/models/comment.rb +142 -0
  12. data/lib/hive_sql/models/community.rb +78 -0
  13. data/lib/hive_sql/models/community_role.rb +37 -0
  14. data/lib/hive_sql/models/community_subscriber.rb +23 -0
  15. data/lib/hive_sql/models/connection.rb +18 -0
  16. data/lib/hive_sql/models/dynamic_global_properties.rb +45 -0
  17. data/lib/hive_sql/models/follower.rb +17 -0
  18. data/lib/hive_sql/models/reblog.rb +20 -0
  19. data/lib/hive_sql/models/sql_base.rb +28 -0
  20. data/lib/hive_sql/models/tag.rb +15 -0
  21. data/lib/hive_sql/models/token.rb +27 -0
  22. data/lib/hive_sql/models/transaction.rb +76 -0
  23. data/lib/hive_sql/models/tx/account_create.rb +26 -0
  24. data/lib/hive_sql/models/tx/account_recover.rb +20 -0
  25. data/lib/hive_sql/models/tx/account_update.rb +22 -0
  26. data/lib/hive_sql/models/tx/account_witness_proxy.rb +31 -0
  27. data/lib/hive_sql/models/tx/account_witness_vote.rb +22 -0
  28. data/lib/hive_sql/models/tx/claim_reward_balance.rb +21 -0
  29. data/lib/hive_sql/models/tx/comment.rb +24 -0
  30. data/lib/hive_sql/models/tx/comments_option.rb +23 -0
  31. data/lib/hive_sql/models/tx/convert.rb +20 -0
  32. data/lib/hive_sql/models/tx/custom.rb +25 -0
  33. data/lib/hive_sql/models/tx/custom/community.rb +32 -0
  34. data/lib/hive_sql/models/tx/custom/follow.rb +48 -0
  35. data/lib/hive_sql/models/tx/custom/reblog.rb +22 -0
  36. data/lib/hive_sql/models/tx/custom/witness.rb +11 -0
  37. data/lib/hive_sql/models/tx/delegate_vesting_share.rb +20 -0
  38. data/lib/hive_sql/models/tx/delete_comment.rb +19 -0
  39. data/lib/hive_sql/models/tx/escrow_approve.rb +23 -0
  40. data/lib/hive_sql/models/tx/escrow_dispute.rb +22 -0
  41. data/lib/hive_sql/models/tx/escrow_release.rb +25 -0
  42. data/lib/hive_sql/models/tx/escrow_transfer.rb +28 -0
  43. data/lib/hive_sql/models/tx/feed.rb +20 -0
  44. data/lib/hive_sql/models/tx/limit_order.rb +26 -0
  45. data/lib/hive_sql/models/tx/pow.rb +19 -0
  46. data/lib/hive_sql/models/tx/transfer.rb +24 -0
  47. data/lib/hive_sql/models/tx/update_proposal_vote.rb +31 -0
  48. data/lib/hive_sql/models/tx/vote.rb +21 -0
  49. data/lib/hive_sql/models/tx/withdraw.rb +19 -0
  50. data/lib/hive_sql/models/tx/withdraw_vesting_route.rb +21 -0
  51. data/lib/hive_sql/models/tx/witness_update.rb +24 -0
  52. data/lib/hive_sql/models/vo/author_reward.rb +22 -0
  53. data/lib/hive_sql/models/vo/comment_benefactor_reward.rb +23 -0
  54. data/lib/hive_sql/models/vo/curation_reward.rb +21 -0
  55. data/lib/hive_sql/models/vo/fill_convert_request.rb +21 -0
  56. data/lib/hive_sql/models/vo/fill_order.rb +23 -0
  57. data/lib/hive_sql/models/vo/fill_transfer_from_savings.rb +23 -0
  58. data/lib/hive_sql/models/vo/fill_vesting_withdraw.rb +21 -0
  59. data/lib/hive_sql/models/vo/interest.rb +19 -0
  60. data/lib/hive_sql/models/vo/liquidity_reward.rb +19 -0
  61. data/lib/hive_sql/models/vo/producer_reward.rb +19 -0
  62. data/lib/hive_sql/models/vo/proposal_pay.rb +20 -0
  63. data/lib/hive_sql/models/vo/return_vesting_delegation.rb +20 -0
  64. data/lib/hive_sql/models/vo/shutdown_witness.rb +18 -0
  65. data/lib/hive_sql/models/vo/sps_fund.rb +18 -0
  66. data/lib/hive_sql/models/witness.rb +48 -0
  67. data/lib/hive_sql/version.rb +3 -0
  68. metadata +413 -0
@@ -0,0 +1,78 @@
1
+ module HiveSQL
2
+ class Community < HiveSQL::SqlBase
3
+
4
+ self.table_name = :Communities
5
+
6
+ has_many :roles, foreign_key: :community, class_name: 'HiveSQL::CommunityRole', primary_key: :name
7
+ has_many :guest_roles, -> { guests }, foreign_key: :community, class_name: 'HiveSQL::CommunityRole', primary_key: :name
8
+ has_many :member_roles, -> { members }, foreign_key: :community, class_name: 'HiveSQL::CommunityRole', primary_key: :name
9
+ has_many :mod_roles, -> { mods }, foreign_key: :community, class_name: 'HiveSQL::CommunityRole', primary_key: :name
10
+ has_many :admin_roles, -> { admins }, foreign_key: :community, class_name: 'HiveSQL::CommunityRole', primary_key: :name
11
+ has_many :muted_roles, -> { muted }, foreign_key: :community, class_name: 'HiveSQL::CommunityRole', primary_key: :name
12
+
13
+ has_many :role_accounts, through: :roles, source: :account_record
14
+ has_many :guests, through: :guest_roles, source: :account_record
15
+ has_many :members, through: :member_roles, source: :account_record
16
+ has_many :mods, through: :mod_roles, source: :account_record
17
+ has_many :admins, through: :admin_roles, source: :account_record
18
+ has_many :muted, through: :muted_roles, source: :account_record
19
+
20
+ has_many :subscribers, foreign_key: :community, class_name: 'HiveSQL::CommunitySubscriber', primary_key: :name
21
+ has_many :subscriber_accounts, through: :subscribers, source: :account_record
22
+
23
+ has_many :comments, foreign_key: :category, class_name: 'HiveSQL::Comment', primary_key: :name
24
+ has_many :authors, -> { distinct }, through: :comments, source: :author_record
25
+ has_many :tags, -> { distinct }, through: :comments, source: :tags
26
+
27
+ scope :mode, lambda { |type| where(type: type) }
28
+ scope :topics, -> { mode 1 }
29
+ scope :journals, -> { mode 2 }
30
+ scope :councils, -> { mode 3 }
31
+
32
+ scope :language, lambda { |language| where(language: language) }
33
+ scope :nsfw, lambda { |nsfw = true| where(nsfw: nsfw) }
34
+
35
+ scope :tagged, lambda { |tag|
36
+ comments = HiveSQL::Comment.where("category LIKE 'hive-%'")
37
+ comments = comments.joins(:community_record)
38
+ comments = comments.tagged(tag, exclude_category: true)
39
+
40
+ where(name: comments.select(:category))
41
+ }
42
+
43
+ scope :query, lambda { |query, options = {include_roles: false}|
44
+ query_clause = []
45
+
46
+ query_clause << "LOWER([Communities].[name]) LIKE ?"
47
+ query_clause << "LOWER([Communities].[title]) LIKE ?"
48
+ query_clause << "LOWER([Communities].[about]) LIKE ?"
49
+ query_clause << "LOWER([Communities].[description]) LIKE ?"
50
+
51
+ if !!options.fetch(:include_roles, false)
52
+ query_clause << "[Communities].[name] IN ( SELECT [CommunitiesRoles].[community] FROM [CommunitiesRoles] WHERE [CommunitiesRoles].[account] LIKE ?)"
53
+ query_clause << "[Communities].[name] IN ( SELECT [CommunitiesRoles].[community] FROM [CommunitiesRoles] WHERE LOWER([CommunitiesRoles].[title]) LIKE ?)"
54
+ end
55
+
56
+ query = ["%#{query.downcase}%"] * query_clause.size
57
+ where(query_clause.join(' OR '), *query)
58
+ }
59
+
60
+ def tag_names
61
+ tags.pluck(:tag)
62
+ end
63
+ end
64
+ end
65
+
66
+ # Structure
67
+ #
68
+ # HiveSQL::Community(
69
+ # name varchar,
70
+ # type varchar,
71
+ # title varchar,
72
+ # about varchar,
73
+ # description varchar,
74
+ # flag_text varchar,
75
+ # language varchar,
76
+ # nsfw boolean,
77
+ # TS timestamp,
78
+ # )
@@ -0,0 +1,37 @@
1
+ module HiveSQL
2
+ class CommunityRole < HiveSQL::SqlBase
3
+
4
+ self.table_name = :CommunitiesRoles
5
+
6
+ belongs_to :community_record, foreign_key: :community, class_name: 'HiveSQL::Community', primary_key: :name
7
+ belongs_to :account_record, foreign_key: :account, class_name: 'HiveSQL::Account', primary_key: :name
8
+
9
+ scope :account, lambda { |account| where(account: account) }
10
+
11
+ scope :mode, lambda { |mode| where(role: mode) }
12
+ scope :guests, -> { mode 'guest' }
13
+ scope :members, -> { mode 'member' }
14
+ scope :mods, -> { mode 'mod' }
15
+ scope :admins, -> { mode 'admin' }
16
+ scope :muted, -> { mode 'muted' }
17
+
18
+ scope :community, lambda {|community| where(community: community)}
19
+ scope :account, lambda {|account| where(account: account)}
20
+
21
+ scope :query, lambda { |query, options = {include_roles: false}|
22
+ where("community IN(?) OR account LIKE ? OR LOWER(title) LIKE ?",
23
+ Community.query(query, options).select(:name),
24
+ "%#{query}%",
25
+ "%#{query.downcase}%")
26
+ }
27
+ end
28
+ end
29
+
30
+ # Structure
31
+ #
32
+ # HiveSQL::CommunityRole(
33
+ # community varchar,
34
+ # account varchar,
35
+ # role varchar,
36
+ # title varchar
37
+ # )
@@ -0,0 +1,23 @@
1
+ module HiveSQL
2
+ class CommunitySubscriber < HiveSQL::SqlBase
3
+
4
+ self.table_name = :CommunitiesSubscribers
5
+
6
+ belongs_to :community_record, foreign_key: :community, class_name: 'HiveSQL::Community', primary_key: :name
7
+ belongs_to :account_record, foreign_key: :subscriber, class_name: 'HiveSQL::Account', primary_key: :name
8
+
9
+ scope :community, lambda {|community| where(community: community)}
10
+ scope :subscriber, lambda {|subscriber| where(subscriber: subscriber)}
11
+
12
+ scope :query, lambda { |query, options = {include_roles: false}|
13
+ where(community: Community.query(query, options).select(:name))
14
+ }
15
+ end
16
+ end
17
+
18
+ # Structure
19
+ #
20
+ # HiveSQL::CommunitySubscriber(
21
+ # community varchar,
22
+ # subscriber varchar
23
+ # )
@@ -0,0 +1,18 @@
1
+ require 'rest-client'
2
+ require 'nokogiri'
3
+
4
+ module HiveSQL
5
+ class Connection < HiveSQL::SqlBase
6
+
7
+ self.table_name = nil
8
+
9
+ def self.tables
10
+ self.connection.tables
11
+ end
12
+
13
+ def self.columns(model_name)
14
+ "HiveSQL::#{model_name}".constantize.columns.map(&:name)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ module HiveSQL
2
+ class DynamicGlobalProperties < HiveSQL::SqlBase
3
+
4
+ self.table_name = :DynamicGlobalProperties
5
+
6
+ end
7
+ end
8
+
9
+ # Structure
10
+ #
11
+ # HiveSQL::DynamicGlobalProperties(
12
+ # ID: integer,
13
+ # head_block_number: integer,
14
+ # head_block_id: varchar,
15
+ # time: timestamp,
16
+ # current_witness: varchar,
17
+ # total_pow: integer,
18
+ # num_pow_witnesses: integer,
19
+ # virtual_supply: money,
20
+ # current_supply: money,
21
+ # confidential_supply: money,
22
+ # current_sbd_supply: money,
23
+ # confidential_sbd_supply: money,
24
+ # total_vesting_fund_steem: money,
25
+ # total_vesting_fund_steem_symbol: varchar,
26
+ # total_vesting_shares: money,
27
+ # total_vesting_shares_symbol: varchar,
28
+ # total_reward_fund_steem: money,
29
+ # total_reward_shares2: varchar,
30
+ # pending_rewarded_vesting_shares: varchar,
31
+ # pending_rewarded_vesting_steem: varchar,
32
+ # sbd_interest_rate: integer,
33
+ # sbd_print_rate: integer,
34
+ # average_block_size: integer,
35
+ # maximum_block_size: integer,
36
+ # current_aslot: integer,
37
+ # recent_slots_filled: varchar,
38
+ # participation_count: integer,
39
+ # last_irreversible_block_num: integer,
40
+ # max_virtual_bandwidth: varchar,
41
+ # vote_power_reserve_rate: integer,
42
+ # current_reserve_ratio: integer,
43
+ # vote_regeneration_per_day: integer,
44
+ # steem_per_vest: money
45
+ # )
@@ -0,0 +1,17 @@
1
+ module HiveSQL
2
+ class Follower < HiveSQL::SqlBase
3
+
4
+ self.table_name = :Followers
5
+
6
+ belongs_to :follower_account, foreign_key: :follower, class_name: 'Account', primary_key: :name
7
+ belongs_to :following_account, foreign_key: :following, class_name: 'Account', primary_key: :name
8
+
9
+ end
10
+ end
11
+
12
+ # Structure
13
+ #
14
+ # HiveSQL::Follower(
15
+ # follower varchar,
16
+ # following varchar
17
+ # )
@@ -0,0 +1,20 @@
1
+ module HiveSQL
2
+ class Reblog < HiveSQL::SqlBase
3
+
4
+ self.table_name = :Reblogs
5
+
6
+ belongs_to :account, foreign_key: :account, class_name: 'Account', primary_key: :name
7
+ belongs_to :author_account, foreign_key: :author, class_name: 'Account', primary_key: :name
8
+
9
+ def comment; Comment::find_by(author: author, permlink: permlink); end
10
+ end
11
+ end
12
+
13
+ # Structure
14
+ #
15
+ # HiveSQL::Follower(
16
+ # account varchar,
17
+ # author varchar,
18
+ # permlink varchar,
19
+ # timestamp: datetime
20
+ # )
@@ -0,0 +1,28 @@
1
+ require "active_record"
2
+
3
+ module HiveSQL
4
+ class SqlBase < ActiveRecord::Base
5
+
6
+ establish_connection({
7
+ adapter: "sqlserver",
8
+ host: ENV['HIVESQL_HOST'] || 'sql.hivesql.io',
9
+ username: ENV['HIVESQL_USERNAME'] || '',
10
+ password: ENV['HIVESQL_PASSWORD'] || '',
11
+ timeout: 60
12
+ })
13
+
14
+ self.table_name = nil
15
+ self.inheritance_column = nil
16
+ self.abstract_class = true
17
+
18
+ scope :before, lambda { |before, field = 'timestamp'| where("#{field} < ?", before) }
19
+ scope :after, lambda { |after, field = 'timestamp'| where("#{field} > ?", after) }
20
+ scope :today, -> { after(1.day.ago) }
21
+ scope :yesterday, -> { before(1.day.ago).after(2.days.ago) }
22
+
23
+ def tx
24
+ HiveSQL::Transaction.find_by(tx_id: self.tx_id)
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ module HiveSQL
2
+ class Tag < HiveSQL::SqlBase
3
+
4
+ self.table_name = :Tags
5
+
6
+ belongs_to :comment, foreign_key: 'comment_ID', class_name: 'HiveSQL::Comment'
7
+ end
8
+ end
9
+
10
+ # Structure
11
+ #
12
+ # HiveSQL::Tag(
13
+ # comment_ID integer,
14
+ # tag varchar
15
+ # )
@@ -0,0 +1,27 @@
1
+ module HiveSQL
2
+ class Token < HiveSQL::SqlBase
3
+
4
+ self.table_name = :Tokens
5
+
6
+ scope :before, lambda { |before, field = 'time'| where("#{field} < ?", before) }
7
+ scope :after, lambda { |after, field = 'time'| where("#{field} > ?", after) }
8
+ scope :today, -> { after(1.day.ago) }
9
+ scope :yesterday, -> { before(1.day.ago).after(2.days.ago) }
10
+
11
+ end
12
+ end
13
+
14
+ # Structure
15
+ #
16
+ # HiveSQL::Token(
17
+ # id: integer,
18
+ # hour: boolean,
19
+ # symbol: varchar,
20
+ # time: smalldatetime,
21
+ # open: real,
22
+ # close: real,
23
+ # high: real,
24
+ # low: real,
25
+ # volumefrom: real,
26
+ # volumeto: real
27
+ # )
@@ -0,0 +1,76 @@
1
+ module HiveSQL
2
+ class Transaction < HiveSQL::SqlBase
3
+
4
+ self.table_name = :Transactions
5
+ self.primary_key = :tx_id
6
+
7
+ belongs_to :block, foreign_key: :block_num
8
+ has_many :block_transactions, through: :block, source: :transactions
9
+
10
+ scope :before, lambda { |before, field = 'expiration'| where("#{field} < ?", before) }
11
+ scope :after, lambda { |after, field = 'expiration'| where("#{field} > ?", after) }
12
+ scope :today, -> { after(1.day.ago) }
13
+ scope :yesterday, -> { before(1.day.ago).after(2.days.ago) }
14
+
15
+ scope :type, lambda { |type| where(type: type) }
16
+
17
+ # So you have a Transaction#tx_id and you want to know what the operation was
18
+ # that lead to it. Well, that's tricky because all of the ops are in their
19
+ # own tables. This method will (slowly) try to find the appropriate table.
20
+ def op
21
+ retries = 0
22
+ puts type
23
+ # Here, we map the type to class name, if supported. Most of them can be
24
+ # mapped automatically, e.g. "vote" => "Vote" but some types share tables
25
+ # with one another. We also use timestamps to narrow the search
26
+ # parameters, for all the good it does. We use the expiration minus the
27
+ # maximum TaPoS window.
28
+ op_type = case type
29
+ when 'account_create_with_delegation', 'create_claimed_account' then 'AccountCreate'
30
+ when 'comment_options' then 'CommentOption'
31
+ when 'custom_json' then 'Custom'
32
+ when 'delegate_vesting_shares' then 'DelegateVestingShare'
33
+ when 'feed_publish' then 'Feed'
34
+ when 'limit_order_create', 'limit_order_create2' then 'LimitOrder'
35
+ when 'Pow2' then 'Pow'
36
+ when 'set_withdraw_vesting_route' then 'WithdrawVestingRoute'
37
+ when 'transfer_from_savings', 'transfer_to_vesting' then 'Transfer'
38
+ when 'withdraw_vesting' then 'Withdraw'
39
+ when *%w(
40
+ cancel_transfer_from_savings change_recovery_account claim_account
41
+ decline_voting_rights limit_order_cancel recover_account
42
+ request_account_recovery witness_set_properties
43
+ ) then raise "Unsupported: #{type}"
44
+ else; type.split('_').collect(&:capitalize).join
45
+ end
46
+
47
+ tapos_window_start = expiration - 28800.seconds
48
+ ops = Tx.const_get(op_type).where(tx_id: self).
49
+ where("timestamp BETWEEN ? AND ?", tapos_window_start, expiration)
50
+
51
+ loop do
52
+ retries += 1
53
+ op = ops.first
54
+
55
+ return op if !!op
56
+ break if retries > 10
57
+
58
+ sleep 3
59
+ end
60
+
61
+ raise "Unable to find #{type} for tx_id: #{tx_id}"
62
+ end
63
+ end
64
+ end
65
+
66
+ # Structure
67
+ #
68
+ # HiveSQL::Transaction(
69
+ # tx_id: integer,
70
+ # block_num: integer,
71
+ # transaction_num: integer,
72
+ # ref_block_num: integer,
73
+ # ref_block_prefix: integer,
74
+ # expiration: datetime,
75
+ # type: varchar
76
+ # )
@@ -0,0 +1,26 @@
1
+ module HiveSQL
2
+ module Tx
3
+ class AccountCreate < HiveSQL::SqlBase
4
+
5
+ self.table_name = :TxAccountCreates
6
+
7
+ end
8
+ end
9
+ end
10
+
11
+ # Structure
12
+ #
13
+ # HiveSQL::Tx::AccountCreate(
14
+ # ID: integer,
15
+ # tx_id: integer,
16
+ # fee: money,
17
+ # delegation: money,
18
+ # creator: varchar,
19
+ # new_account_name: varchar,
20
+ # owner_key: varchar,
21
+ # active_key: varchar,
22
+ # posting_key: varchar,
23
+ # memo_key: varchar_max,
24
+ # json_metadata: varchar_max,
25
+ # timestamp: datetime
26
+ # )
@@ -0,0 +1,20 @@
1
+ module HiveSQL
2
+ module Tx
3
+ class AccountRecover < HiveSQL::SqlBase
4
+
5
+ self.table_name = :TxAccountRecovers
6
+
7
+ end
8
+ end
9
+ end
10
+
11
+ # Structure
12
+ #
13
+ # HiveSQL::Tx::AccountRecover(
14
+ # ID: integer,
15
+ # tx_id: integer,
16
+ # recovery_account: varchar,
17
+ # account_to_recover: varchar,
18
+ # recovered: boolean,
19
+ # timestamp: datetime
20
+ # )