-
Notifications
You must be signed in to change notification settings - Fork 0
/
thingy.rb
132 lines (108 loc) · 3.44 KB
/
thingy.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require 'set'
require 'json'
require_relative 'fields.rb'
include Fields
require_relative 'table.rb'
include TableDefintion
require_relative 'tableDiff.rb'
require_relative 'migration.rb'
require_relative 'platforms.rb'
require_relative 'schema.rb'
require_relative 'query.rb'
class User < Table
attr_accessor :table
def create
self.name = "User"
@table[:profile] = ForeignKeyField.new(reference: "Profile")
@table[:username] = CharField.new(max_length: 128, constraints: [Unique.new])
@table[:password] = CharField.new(max_length: 255)
@table[:bio] = TextField.new(constraints: [Nullable.new])
end
end
class User2 < Table
attr_accessor :table
def create
self.name = "User"
@table[:profile] = ForeignKeyField.new(reference: "Profile")
@table[:username] = CharField.new(max_length: 255)
@table[:password] = CharField.new(max_length: 255, constraints: [Nullable.new])
@table[:phone_number] = IntField.new(field_type: IntTypes::Big, constraints: [Unique.new])
end
end
class Post < Table
attr_accessor :table
def create
self.name = "Post"
@table[:user] = ForeignKeyField.new(reference: "User")
@table[:title] = CharField.new(max_length: 500)
@table[:text] = TextField.new(constraints: [Nullable.new])
end
end
class Profile < Table
attr_accessor :table
def create
self.name = "Profile"
@table[:user] = ForeignKeyField.new(reference: "User")
@table[:title] = CharField.new(max_length: 500)
@table[:text] = TextField.new(constraints: [Nullable.new])
end
end
class Message < Table
attr_accessor :table
def create
self.name = "Message"
@table[:sender] = ForeignKeyField.new(reference: "User", back_ref: "sent_message")
@table[:text] = TextField.new
@table[:receiver] = ForeignKeyField.new(reference: "User", back_ref: "received_message")
end
end
dbAuth = DBAuth.new("localhost", 5432, "orm_test", "postgres", "root")
DBConn.create(dbAuth)
Users = User2.new
Profiles = Profile.new
Posts = Post.new
Messages = Message.new
schema = Schema.new([])
schema.register(Users)
schema.register(Profiles)
schema.register(Posts)
schema.register(Messages)
schema.initialize_model
usr_migrations = Migrations.new("usr")
usr_migrations.migrate(dbAuth, schema, Platforms::POSTGRES)
# phone_nums = [93777809, 93971805, 95277105]
# usernames = ["ameershah", "ameerwasi", "ameerwasi001"]
# passwords = ["mx12345678", "mio90xa11", "wasiameer"]
# i = 0
# while i < 3
# user = Users.init
# profile = Profiles.init
# profile[:user] = user
# profile[:title] = "My Title #{i+1}"
# profile[:text] = "A description #{i+1}"
# user[:profile] = profile
# user[:username] = usernames[i]
# user[:password] = passwords[i]
# user[:phone_number] = phone_nums[i]
# user.save
# i+=1
# end
# user = Users.get({id: 1})
# post = Posts.init
# post[:user] = user
# post[:title] = "1st user, first post"
# post[:text] = "Text about this post from first user"
# post.save
# message = Messages.init
# message[:sender] = 2
# message[:receiver] = 3
# message[:text] = "From 2 to 3, message 4"
# message.save
# print Users.model, "\n"
# print Profiles.model, "\n"
# print Posts.model, "\n"
# print Messages.model, "\n"
messages = Users.get({id: 3})[:posts].first[:user][:profile][:user][:sent_messages].order_by(:id)
for message in messages
print message[:receiver], "\n"
end