Class: Surfer::Support

Inherits:
Object
  • Object
show all
Defined in:
lib/surfer/support.rb

Instance Method Summary collapse

Instance Method Details

#create_field(argv) ⇒ Object

Parsing command line argument for table creating.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/surfer/support.rb', line 30

def create_field argv
	i = 3
	str = ""
	str = " id    INTEGER   AUTO_INCREMENT   PRIMARY KEY, "
	begin
		att = argv[i]	
		str << att
		str << "   "
		i = i.to_i + 2
		att1 = get_data_type(argv[i])
		str << att1
		if i.to_i != argv.length.to_i-1
			str << ","
			str << "\n"
			i = i.to_i + 2
		else 
			return str
		end
	end while i < argv.length.to_i
end

#create_table(argv) ⇒ Object

Table creation query.



53
54
55
56
57
# File 'lib/surfer/support.rb', line 53

def create_table argv 
	query = "CREATE TABLE #{get_pluralize(argv[2])}( #{create_field(argv)} )"
	puts query
	return query
end

#generate_dindex(tab_name, argv) ⇒ Object

# Generating the index deletion command.



209
210
211
212
213
# File 'lib/surfer/support.rb', line 209

def generate_dindex tab_name,argv
	query = " ALTER TABLE #{tab_name} DROP INDEX #{argv[argv.keys[0]]} "
	puts query
	return query
end

#generate_index(tab_name, argv) ⇒ Object

Generating the index command.



202
203
204
205
206
# File 'lib/surfer/support.rb', line 202

def generate_index tab_name, argv
	query = " CREATE INDEX #{argv[argv.keys[0]]}  ON #{tab_name} (#{argv[argv.keys[1]]}) "
	puts query
	return query
end

#generate_insert(tab_name, argv) ⇒ Object

Parsing insert command attribute.



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
# File 'lib/surfer/support.rb', line 61

def generate_insert tab_name, argv

			# Attribute of table.
	attribute = ""
	attribute << "id"
	i = 0
	argv.each do |k,v|
		if i == 0 and argv.length != 0
		 	attribute << ","
		 end
		attribute << k
		i = i.to_i + 1
		if i != argv.length
			attribute << ","
		end
	end
			# Values of the Attributes.
	val = []
	val << "NULL"
	i = 0
	if argv.length != 0
		begin
			val << "\"#{argv[argv.keys[i]]}\""
			i = i.to_i + 1 		
		end while i < argv.length.to_i	
	end		

	if argv.length == 0
		query = " INSERT INTO #{tab_name}(#{attribute}) VALUES (#{val * ""}) "
		return query
	else
		query = " INSERT INTO #{tab_name}(#{attribute}) VALUES (#{val * ","}) "
		return query
	end
end

#generate_remove(tab_name, argv) ⇒ Object

Parsing coditional parameter for delete command.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/surfer/support.rb', line 99

def generate_remove tab_name, argv
	condition = ""
	condition << "( "
	i = 0
	begin
		if  "condition" != argv.keys[i] 
			condition << argv.keys[i] 
			condition << "="
			condition << "\"#{argv[argv.keys[i]]}\"" 
			condition << " "
			i = i.to_i + 1
		else
			condition << "\"#{argv[argv.keys[i]]}\"" 
			condition << " "
			i = i.to_i + 1
		end	
	end while i < argv.length
	condition << ")"
	query = " DELETE FROM #{tab_name} WHERE#{condition} "
	#puts query
	return query
end

#generate_update(tab_name, argv, cond_argv) ⇒ Object

Parsing attributes and values for update command.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/surfer/support.rb', line 124

def generate_update tab_name, argv, cond_argv

# Parsing updatable attribute
	attribute = ""
	i = 0
	begin
		attribute << argv.keys[i]
		attribute << " = "
		attribute << "\"#{argv[argv.keys[i]]}\""
		i = i.to_i + 1
		if i != argv.length
			attribute << ","
		end
	end while i < argv.length


# Parsing conditional hashing argument.	
	condition = ""
	condition << "( "
	i = 0
	begin
		if "condition" != cond_argv.keys[i] 
			condition << cond_argv.keys[i] 
			condition << "="
			condition << "\"#{cond_argv[cond_argv.keys[i]]}\"" 
			condition << " "
			i = i.to_i + 1
		else
			condition << "\"#{cond_argv[cond_argv.keys[i]]}\"" 
			condition << " "
			i = i.to_i + 1
		end	
	end while i < cond_argv.length
	condition << ")"

	query = " UPDATE #{tab_name} SET #{attribute} WHERE #{condition} "
	puts query
	return query
end

#generate_where(tab_name, argv) ⇒ Object

Parsing attribute for where clouse.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/surfer/support.rb', line 166

def generate_where tab_name, argv #, att_argv


			# Parsing conditional hashing argument.	
	condition = ""
	condition << "( "
	i = 0
	begin
		if "condition" != argv.keys[i] 
			condition << argv.keys[i] 
			condition << "="
			condition << "\"#{argv[argv.keys[i]]}\""
			condition << " "
			i = i.to_i + 1
		else
			condition << "\"#{argv[argv.keys[i]]}\""
			condition << " "
			i = i.to_i + 1
		end	
	end while i < argv.length
	condition << ")"


	#if att_argv.length == 0
		query = " SELECT * FROM #{tab_name} WHERE #{condition} "
		puts query
		return query
	#else
		#query = " SELECT #{att_argv * ","} FROM #{tab_name} WHERE #{condition} "	
		#puts query
		#return query
	#end
end

#get_data_type(key) ⇒ Object

Getting sql specific datatype.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/surfer/support.rb', line 15

def get_data_type key
	data_type = Hash.new 
	data_type["int"] = "INTEGER"
	data_type["mobile"] = "BIGINT(20)"
	data_type["string"] = "VARCHAR(50)"
	data_type["float"] = "INTEGER(20,5)" 
	data_type["integer"] = "INTEGER" 
	data_type["text"] = "TEXT" 
	data_type["date"] = "DATE" 
	 
	tdata = data_type[key]
end

#get_pluralize(name) ⇒ Object

Plural method for pluralization purpose.



6
7
8
9
10
11
# File 'lib/surfer/support.rb', line 6

def get_pluralize name
	until name.empty? 
		 str = name.downcase.concat('s')
		return str
	end
end