hive_sql 1.0.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,6 +29,11 @@ module HiveSQL
29
29
  has_many :subscriptions, foreign_key: :subscriber, class_name: 'HiveSQL::CommunitySubscriber', primary_key: :name
30
30
  has_many :subscribed_communities, through: :subscriptions, source: :community_record
31
31
 
32
+ has_many :proposals, foreign_key: :creator, class_name: 'HiveSQL::Proposal', primary_key: :name
33
+
34
+ has_many :proposal_votes, foreign_key: :voter, class_name: 'HiveSQL::ProposalApproval', primary_key: :name
35
+ has_many :approved_proposals, through: :proposal_votes, source: :proposal
36
+
32
37
  scope :before, lambda { |before, field = 'created'| where("#{field} < ?", before) }
33
38
  scope :after, lambda { |after, field = 'created'| where("#{field} > ?", after) }
34
39
  scope :today, -> { after(1.day.ago) }
@@ -1,3 +1,5 @@
1
+ require 'open-uri'
2
+
1
3
  module HiveSQL
2
4
  class Block < HiveSQL::SqlBase
3
5
 
@@ -5,7 +7,17 @@ module HiveSQL
5
7
  self.primary_key = :block_num
6
8
 
7
9
  has_many :transactions, foreign_key: :block_num
8
-
10
+
11
+ # You have the actual trx_id that the blockchain knows about and you would
12
+ # like to get this corresponding block. Well, HiveSQL doesn't store that,
13
+ # so we have to do an RPC lookup.
14
+ scope :trx_id, lambda { |trx_id|
15
+ block_num = JSON[open("http://anyx.io/v1/account_history_api/get_transaction?id=#{trx_id}").read].tap do |tx|
16
+ tx['block_num']
17
+ end
18
+
19
+ where(block_num: block_num)
20
+ }
9
21
  end
10
22
  end
11
23
 
@@ -25,8 +25,11 @@ module HiveSQL
25
25
 
26
26
  scope :normalized_json, -> { where("json_metadata LIKE '{%}' AND ISJSON(json_metadata) > 0") }
27
27
 
28
- scope :app, lambda { |app|
29
- normalized_json.where("JSON_VALUE(json_metadata, '$.app') LIKE ?", "#{app}/%")
28
+ scope :app, lambda { |app, forward = true|
29
+ r = normalized_json.where("JSON_VALUE(json_metadata, '$.app') LIKE ?", "#{app}/%")
30
+ r = where.not(id: r.select(:id)) unless forward
31
+
32
+ r
30
33
  }
31
34
 
32
35
  scope :app_version, lambda { |version|
@@ -37,12 +40,15 @@ module HiveSQL
37
40
  scope :author, lambda {|author| joins(:author_record).where(author: author)}
38
41
 
39
42
  scope :tagged, lambda { |tag, options = {exclude_category: false}|
43
+ tag = [tag].flatten
40
44
  exclude_category = !!options[:exclude_category]
45
+ tagged_posts = HiveSQL::Tag.where(tag: tag).select(:comment_ID)
46
+ tagged_posts = tagged_posts.where("comment_ID > ?", minimum(:ID))
41
47
 
42
48
  if exclude_category
43
- where(id: HiveSQL::Tag.where(tag: tag).select(:comment_ID))
49
+ where(id: tagged_posts.select(:comment_ID))
44
50
  else
45
- where("ID IN(?) OR category = ?", HiveSQL::Tag.where(tag: tag).select(:comment_ID), tag)
51
+ where("ID IN(?) OR category = ?", tagged_posts.select(:comment_ID), tag.first)
46
52
  end
47
53
  }
48
54
 
@@ -66,6 +72,32 @@ module HiveSQL
66
72
  where("JSON_VALUE(beneficiaries, '$.account') IN(?)", [account].flatten)
67
73
  }
68
74
 
75
+ scope :query, lambda { |query, forward = true|
76
+ query = [query].flatten.map{|q| "%#{q}%"}
77
+ condition = []
78
+ sub_conditions = []
79
+
80
+ sub_conditions << "LOWER(author) LIKE ?"
81
+ sub_conditions << "LOWER(permlink) LIKE ?"
82
+ sub_conditions << "LOWER(category) LIKE ?"
83
+ sub_conditions << "LOWER(parent_author) LIKE ?"
84
+ sub_conditions << "LOWER(parent_permlink) LIKE ?"
85
+ sub_conditions << "LOWER(title) LIKE ?"
86
+ sub_conditions << "LOWER(body) LIKE ?"
87
+ sub_conditions << "LOWER(json_metadata) LIKE ?"
88
+ sub_conditions << "LOWER(beneficiaries) LIKE ?"
89
+ sub_conditions << "LOWER(root_title) LIKE ?"
90
+
91
+ query.each do |q|
92
+ condition += sub_conditions
93
+ end
94
+
95
+ r = where(condition.join(' OR '), *(query * sub_conditions.size))
96
+ r = where.not(id: r.select(:id)) unless forward
97
+
98
+ r
99
+ }
100
+
69
101
  def self.find_by_author(author)
70
102
  where(author: author).first
71
103
  end
@@ -85,6 +117,10 @@ module HiveSQL
85
117
  def tag_names
86
118
  tags.pluck(:tag)
87
119
  end
120
+
121
+ def hydrated_json_metadata
122
+ JSON[json_metadata] rescue {}
123
+ end
88
124
  end
89
125
  end
90
126
 
@@ -0,0 +1,24 @@
1
+ module HiveSQL
2
+ class Delegation < HiveSQL::SqlBase
3
+
4
+ self.table_name = :Delegations
5
+
6
+ belongs_to :delegator_account, foreign_key: :delegator, primary_key: 'name', class_name: 'HiveSQL::Account'
7
+ belongs_to :delegatee_account, foreign_key: :delegatee, primary_key: 'name', class_name: 'HiveSQL::Account'
8
+
9
+ scope :delegator, lambda {|delegator| where(delegator: delegator)}
10
+ scope :delegatee, lambda {|delegatee| where(delegatee: delegatee)}
11
+
12
+ def mvests
13
+ vests / 1e6
14
+ end
15
+ end
16
+ end
17
+
18
+ # Structure
19
+ #
20
+ # HiveSQL::Delegation(
21
+ # delegator: varchar,
22
+ # delegatee: varchar,
23
+ # vests: money
24
+ # )
@@ -18,17 +18,23 @@ end
18
18
  # num_pow_witnesses: integer,
19
19
  # virtual_supply: money,
20
20
  # current_supply: money,
21
+ # current_supply_symbol: varchar,
21
22
  # confidential_supply: money,
23
+ # confidential_supply_symbol: varchar,
22
24
  # current_hbd_supply: money,
25
+ # current_hbd_supply_symbol: varchar,
23
26
  # confidential_hbd_supply: money,
24
27
  # total_vesting_fund_hive: money,
25
28
  # total_vesting_fund_hive_symbol: varchar,
26
29
  # total_vesting_shares: money,
27
30
  # total_vesting_shares_symbol: varchar,
28
31
  # total_reward_fund_hive: money,
32
+ # total_reward_fund_hive_symbol: varchar,
29
33
  # total_reward_shares2: varchar,
30
34
  # pending_rewarded_vesting_shares: varchar,
35
+ # pending_rewarded_vesting_shares_symbol: varchar,
31
36
  # pending_rewarded_vesting_hive: varchar,
37
+ # pending_rewarded_vesting_hive_symbol: varchar,
32
38
  # hbd_interest_rate: integer,
33
39
  # hbd_print_rate: integer,
34
40
  # average_block_size: integer,
@@ -41,5 +47,24 @@ end
41
47
  # vote_power_reserve_rate: integer,
42
48
  # current_reserve_ratio: integer,
43
49
  # vote_regeneration_per_day: integer,
44
- # hive_per_vest: money
50
+ # hive_per_vest: money,
51
+ # required_actions_partition_percent: integer,
52
+ # target_votes_per_period: integer,
53
+ # delegation_return_period: integer,
54
+ # reverse_auction_seconds: integer,
55
+ # available_account_subsidies: integer,
56
+ # hbd_stop_percent: integer,
57
+ # hbd_start_percent: integer,
58
+ # next_maintenance_time: timestamp,
59
+ # last_budget_time: timestamp,
60
+ # content_reward_percent: integer,
61
+ # vesting_reward_percent: integer,
62
+ # sps_fund_percent: integer,
63
+ # sps_interval_ledger: integer,
64
+ # sps_interval_ledger_symbol: varchar,
65
+ # downvote_pool_percent: integer,
66
+ # smt_creation_fee: money,
67
+ # smt_creation_fee_symbol: varchar,
68
+ # price_hive_usd: money,
69
+ # price_hbd_usd: money
45
70
  # )
@@ -0,0 +1,26 @@
1
+ module HiveSQL
2
+ class Proposal < HiveSQL::SqlBase
3
+ self.table_name = :Proposals
4
+
5
+ belongs_to :creator_record, foreign_key: :creator, class_name: 'HiveSQL::Account', primary_key: :name
6
+ belongs_to :receiver_record, foreign_key: :receiver, class_name: 'HiveSQL::Account', primary_key: :name
7
+ has_many :approvals, foreign_key: :proposal_id, class_name: 'HiveSQL::ProposalApproval', primary_key: :id
8
+ has_many :voters, through: :approvals, source: :voter
9
+ end
10
+ end
11
+
12
+ # Structure
13
+ #
14
+ # HiveSQL::Follower(
15
+ # id integer,
16
+ # creator varchar,
17
+ # receiver varchar,
18
+ # start_date timestamp,
19
+ # end_date timestamp,
20
+ # daily_pay money,
21
+ # daily_pay_symbol varchar(3),
22
+ # subject varchar,
23
+ # permlink varchar,
24
+ # total_votes integer,
25
+ # deleted boolean
26
+ # )
@@ -0,0 +1,15 @@
1
+ module HiveSQL
2
+ class ProposalApproval < HiveSQL::SqlBase
3
+ self.table_name = :ProposalsApprovals
4
+
5
+ belongs_to :proposal, foreign_key: :proposal_id, class_name: 'Proposal', primary_key: :id
6
+ belongs_to :voter_record, foreign_key: :voter, class_name: 'Account', primary_key: :name
7
+ end
8
+ end
9
+
10
+ # Structure
11
+ #
12
+ # HiveSQL::Follower(
13
+ # proposal_id integer,
14
+ # voter varchar
15
+ # )
@@ -3,8 +3,8 @@ module HiveSQL
3
3
 
4
4
  self.table_name = :Reblogs
5
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
6
+ belongs_to :account_record, foreign_key: :account, class_name: 'Account', primary_key: :name
7
+ belongs_to :author_record, foreign_key: :author, class_name: 'Account', primary_key: :name
8
8
 
9
9
  def comment; Comment::find_by(author: author, permlink: permlink); end
10
10
  end
@@ -14,6 +14,20 @@ module HiveSQL
14
14
 
15
15
  scope :type, lambda { |type| where(type: type) }
16
16
 
17
+ scope :by, lambda {|account_name|
18
+ account_name = [account_name].flatten
19
+ sub_selects = []
20
+ sub_selects << 'tx_id IN (SELECT tx_id FROM TxAccountCreates WHERE creator IN (?))'
21
+ sub_selects << 'tx_id IN (SELECT tx_id FROM TxAccountRecovers WHERE recovery_account IN (?))'
22
+ sub_selects << 'tx_id IN (SELECT tx_id FROM TxAccountUpdates WHERE account IN (?))'
23
+ sub_selects << 'tx_id IN (SELECT tx_id FROM TxAccountWitnessProxies WHERE account IN (?))'
24
+ sub_selects << 'tx_id IN (SELECT tx_id FROM TxAccountWitnessVotes WHERE account IN (?))'
25
+ sub_selects << 'tx_id IN (SELECT tx_id FROM TxClaimRewardBalances WHERE account IN (?))'
26
+ sub_selects << 'tx_id IN (SELECT tx_id FROM TxComments WHERE author IN (?))'
27
+
28
+ where(sub_selects.join(' OR '), *([account_name] * sub_selects.size))
29
+ }
30
+
17
31
  # So you have a Transaction#tx_id and you want to know what the operation was
18
32
  # that lead to it. Well, that's tricky because all of the ops are in their
19
33
  # own tables. This method will (slowly) try to find the appropriate table.
@@ -10,7 +10,7 @@ module HiveSQL
10
10
 
11
11
  ids = active.map do |p|
12
12
  p.id unless AccountWitnessProxy.where(account: p.account).
13
- where.not(proxy: p.Proxy).
13
+ where.not(proxy: p.proxy).
14
14
  where('timestamp > ?', p.timestamp).exists?
15
15
  end
16
16
 
@@ -26,6 +26,6 @@ end
26
26
  # ID: integer,
27
27
  # tx_id: integer,
28
28
  # account: varchar,
29
- # Proxy: varchar,
29
+ # proxy: varchar,
30
30
  # timestamp: datetime
31
31
  # )
@@ -10,6 +10,7 @@ module HiveSQL
10
10
  scope :any_required_posting_auths, lambda { |required_posting_auth|
11
11
  where("? IN(JSON_VALUE(required_posting_auths, '$'))", required_posting_auth)
12
12
  }
13
+ scope :normalized_json, -> { where("ISJSON(json_metadata) > 0") }
13
14
  end
14
15
  end
15
16
  end
@@ -0,0 +1,35 @@
1
+ module HiveSQL
2
+ module Tx
3
+ class ProposalCreate < HiveSQL::SqlBase
4
+ belongs_to :voter_record, foreign_key: :account, primary_key: :voter, class_name: 'HiveSQL::Account'
5
+
6
+ self.table_name = :TxProposalCreates
7
+
8
+ scope :proposal, lambda { |id, invert = false|
9
+ if !!invert
10
+ where("? NOT IN (SELECT value FROM OPENJSON(proposal_ids,'$'))", id)
11
+ else
12
+ where("? IN (SELECT value FROM OPENJSON(proposal_ids,'$'))", id)
13
+ end
14
+ }
15
+
16
+ scope :approve, lambda {|approve = true| where(approve: approve)}
17
+ end
18
+ end
19
+ end
20
+
21
+ # Structure
22
+ #
23
+ # HiveSQL::Tx::ProposalCreate(
24
+ # ID: integer,
25
+ # tx_id: integer,
26
+ # creator: varchar,
27
+ # receiver: varchar,
28
+ # start_date: datetime
29
+ # end_date: datetime
30
+ # daily_pay: money,
31
+ # daily_pay_symbol: boolean,
32
+ # subject: varchar,
33
+ # permlink: varchar,
34
+ # timestamp: datetime
35
+ # )
@@ -1,9 +1,9 @@
1
1
  module HiveSQL
2
2
  module Tx
3
- class UpdateProposalVote < HiveSQL::SqlBase
3
+ class ProposalVoteUpdate < HiveSQL::SqlBase
4
4
  belongs_to :voter_record, foreign_key: :account, primary_key: :voter, class_name: 'HiveSQL::Account'
5
5
 
6
- self.table_name = :TxUpdateProposalVotes
6
+ self.table_name = :TxProposalVoteUpdates
7
7
 
8
8
  scope :proposal, lambda { |id, invert = false|
9
9
  if !!invert
@@ -20,7 +20,7 @@ end
20
20
 
21
21
  # Structure
22
22
  #
23
- # HiveSQL::Tx::Vote(
23
+ # HiveSQL::Tx::ProposalVoteUpdate(
24
24
  # ID: integer,
25
25
  # tx_id: integer,
26
26
  # voter: varchar,
@@ -0,0 +1,23 @@
1
+ module HiveSQL
2
+ module Vo
3
+ class AccountCreated < HiveSQL::SqlBase
4
+
5
+ self.table_name = :VOAccountCreateds
6
+
7
+ end
8
+ end
9
+ end
10
+
11
+ # Structure
12
+ #
13
+ # HiveSQL::Vo::AccountCreated(
14
+ # ID: integer,
15
+ # block_num: integer,
16
+ # timestamp: datetime,
17
+ # new_account_name: varchar,
18
+ # creator: varchar,
19
+ # initial_vesting_shares: money,
20
+ # initial_vesting_shares_symbol: varchar,
21
+ # initial_delegation: money,
22
+ # initial_delegation_symbol: varchar
23
+ # )
@@ -0,0 +1,20 @@
1
+ module HiveSQL
2
+ module Vo
3
+ class ChangedRecoveryAccount < HiveSQL::SqlBase
4
+
5
+ self.table_name = :VOChangedRecoveryAccounts
6
+
7
+ end
8
+ end
9
+ end
10
+
11
+ # Structure
12
+ #
13
+ # HiveSQL::Vo::ChangedRecoveryAccount(
14
+ # ID: integer,
15
+ # block_num: integer,
16
+ # timestamp: datetime,
17
+ # account: varchar,
18
+ # old_recovery_account: varchar,
19
+ # new_recovery_account: varchar
20
+ # )
@@ -0,0 +1,21 @@
1
+ module HiveSQL
2
+ module Vo
3
+ class ClearNullAccountBalance < HiveSQL::SqlBase
4
+
5
+ self.table_name = :VOClearNullAccountBalances
6
+
7
+ def hydrated_total_cleared
8
+ JSON[total_cleared]
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ # Structure
15
+ #
16
+ # HiveSQL::Vo::ClearNullAccountBalance(
17
+ # ID: integer,
18
+ # block_num: integer,
19
+ # timestamp: datetime,
20
+ # total_cleared: json
21
+ # )
@@ -0,0 +1,25 @@
1
+ module HiveSQL
2
+ module Vo
3
+ class CommentsVote < HiveSQL::SqlBase
4
+
5
+ self.table_name = :VOCommentsVotes
6
+
7
+ end
8
+ end
9
+ end
10
+
11
+ # Structure
12
+ #
13
+ # HiveSQL::Vo::CommentsVote(
14
+ # ID: integer,
15
+ # block_num: integer,
16
+ # timestamp: datetime,
17
+ # voter: varchar,
18
+ # author: varchar,
19
+ # permlink: varchar
20
+ # weight: integer,
21
+ # rshares: integer,
22
+ # total_vote_weight: integer,
23
+ # pending_payout: money
24
+ # pending_payout_symbol: varchar
25
+ # )
@@ -14,5 +14,7 @@ end
14
14
  # ID: integer,
15
15
  # block_num: integer,
16
16
  # timestamp: datetime,
17
- # additional_funds: money
17
+ # receiver: varchar
18
+ # payment: money
19
+ # payment_symbol: varchar
18
20
  # )
@@ -1,3 +1,3 @@
1
1
  module HiveSQL
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.3'
3
3
  end
data/lib/hive_sql.rb CHANGED
@@ -11,8 +11,11 @@ require "hive_sql/models/connection"
11
11
  require "hive_sql/models/community"
12
12
  require "hive_sql/models/community_subscriber"
13
13
  require "hive_sql/models/community_role"
14
+ require "hive_sql/models/delegation"
14
15
  require "hive_sql/models/dynamic_global_properties"
15
16
  require "hive_sql/models/follower"
17
+ require "hive_sql/models/proposal"
18
+ require "hive_sql/models/proposal_approval"
16
19
  require "hive_sql/models/reblog"
17
20
  require "hive_sql/models/tag"
18
21
  require "hive_sql/models/token"
@@ -40,7 +43,8 @@ require "hive_sql/models/tx/feed"
40
43
  require "hive_sql/models/tx/limit_order"
41
44
  require "hive_sql/models/tx/pow"
42
45
  require "hive_sql/models/tx/transfer"
43
- require "hive_sql/models/tx/update_proposal_vote"
46
+ require "hive_sql/models/tx/proposal_create"
47
+ require "hive_sql/models/tx/proposal_vote_update"
44
48
  require "hive_sql/models/tx/vote"
45
49
  require "hive_sql/models/tx/withdraw"
46
50
  require "hive_sql/models/tx/withdraw_vesting_route"
@@ -51,10 +55,14 @@ require "hive_sql/models/tx/custom/witness"
51
55
  require "hive_sql/models/tx/custom/reblog"
52
56
  require "hive_sql/models/tx/custom/community"
53
57
 
58
+ require "hive_sql/models/vo/account_created"
54
59
  require "hive_sql/models/vo/author_reward"
60
+ require "hive_sql/models/vo/changed_recovery_account"
61
+ require "hive_sql/models/vo/clear_null_account_balance"
55
62
  require "hive_sql/models/vo/comment_benefactor_reward"
56
63
  require "hive_sql/models/vo/comment_payout_update"
57
64
  require "hive_sql/models/vo/comment_reward"
65
+ require "hive_sql/models/vo/comments_vote"
58
66
  require "hive_sql/models/vo/curation_reward"
59
67
  require "hive_sql/models/vo/fill_convert_request"
60
68
  require "hive_sql/models/vo/fill_order"