Skip to content

Commit

Permalink
Optimization regarding lesson control. Dropped a redundant column in …
Browse files Browse the repository at this point in the history
…chapters table, as we can query the max sec in sqlcons table.
  • Loading branch information
Nate Allen committed May 8, 2012
1 parent 92435bd commit 64f804c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Expand Up @@ -15,7 +15,7 @@ def index
def jump_to_lesson
chapter = params[:id]
#Initialize session variables before we direct to that chapter
session[:maxsec] = Chapters.select(:maxlesson).where("id = #{chapter}").first.maxlesson
session[:maxsec] = Sqlcons.maximum(:sec, :conditions => "ch = #{chapter}")#.where("ch = #{chapter}")
session[:maxch] = Chapters.maximum(:id)
session[:tutch] = chapter.to_i
session[:tutsec] = 0
Expand Down
12 changes: 5 additions & 7 deletions app/controllers/sqlcons_controller.rb
Expand Up @@ -6,13 +6,10 @@ class SqlconsController < ApplicationController
# GET /sqlcons
# GET /sqlcons.json
def index
session[:maxsec] = Chapters.select(:maxlesson).where("id = 1").first.maxlesson
session[:maxsec] = Sqlcons.maximum(:sec).where("ch = 1")
#session[:maxsec] = Chapters.select(:maxlesson).where("id = 1").first.maxlesson
session[:tutch] = 1
session[:tutsec] = 1
#respond_to do |format|
# format.html # index.html.erb
# format.json { render :json => @sqlcons }
# end
session[:tutsec] = 1
render :index
end

Expand Down Expand Up @@ -42,7 +39,8 @@ def fetchquery

#Increments session parameters if user wants to advance lesson.
def nextlesson
cur_sec_max = Chapters.select(:maxlesson).where(:id => session[:tutch]).first.maxlesson
cur_sec_max = Sqlcons.maximum(:sec, :conditions => "ch = " + session[:tutch].to_s)
#Chapters.select(:maxlesson).where(:id => session[:tutch]).first.maxlesson
app_ch_max = Chapters.maximum("id")
#If we haven't hit the last lesson in chapter, or last chapter of tutorial
# increment lesson. Otherwise on to the next chapter
Expand Down
9 changes: 9 additions & 0 deletions app/views/sqlcons/tutorials/tut4-2.html
@@ -0,0 +1,9 @@
<h3>Ch4-2 : Count (cont.)</h3>
<p>
To further assist in data analysis, we can employ the <code>group by<code> clause.
<br /> <br />
This addition allows us to "group" aggregate function results together based on values in a column.
<br /><code>select count(*) from students</code>
<br /><code>select count(*) from students where fname like 'S%'</code>
<br / >
</p>
8 changes: 8 additions & 0 deletions db/migrate/20120508230755_dropmaxlesson_from_chapters.rb
@@ -0,0 +1,8 @@
class DropmaxlessonFromChapters < ActiveRecord::Migration
def up
remove_column :chapters, :maxlesson
end

def down
end
end
3 changes: 1 addition & 2 deletions db/schema.rb
Expand Up @@ -11,14 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120414223318) do
ActiveRecord::Schema.define(:version => 20120508230755) do

create_table "chapters", :force => true do |t|
t.string "title"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "description"
t.integer "maxlesson"
end

create_table "dbqs", :force => true do |t|
Expand Down
4 changes: 4 additions & 0 deletions db/seeds.rb
Expand Up @@ -25,6 +25,10 @@

#Chapter 4
Sqlcons.create(ch: 4,sec: 1,regtext: '(select)(.*?)(count)');
Sqlcons.create(ch: 4,sec: 2,regtext: '(select)(.*?)(group by)');
Sqlcons.create(ch: 4,sec: 3,regtext: '(select)(.*?)(having)');
Sqlcons.create(ch: 4,sec: 4,regtext: '(select)(.*?)(max|min)');
Sqlcons.create(ch: 4,sec: 5,regtext: '(select)(.*?)(sum)');

#Chapter description for each chapter.
Chapters.create( title:'Select' , description:'The most basic forms of querying a database', maxlesson:7);
Expand Down

0 comments on commit 64f804c

Please sign in to comment.