Permalink
Browse files

Creating new db abstraction.

_why wrote an ORM, long ago. It had all kinds of crazy stuff in it. Now, it's been replaced with a nice little key-value-store. The preferences has been updated to take advantage of it.

"if you program and want any longevity to your work, make a game. all else recycles, but people rewrite architectures to keep games alive."
  • Loading branch information...
1 parent fc77808 commit 6f61aea17ec5f376247ac48148405844e0588593 @steveklabnik steveklabnik committed Sep 5, 2011
Showing with 167 additions and 1,406 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 Gemfile
  3. +4 −0 Gemfile.lock
  4. +4 −1 Rakefile
  5. +1 −3 app/boot.rb
  6. +0 −65 app/db/connection_pool.rb
  7. +0 −9 app/db/core_ext.rb
  8. +0 −119 app/db/database.rb
  9. +0 −354 app/db/dataset.rb
  10. +0 −78 app/db/http.rb
  11. +0 −235 app/db/model.rb
  12. +0 −161 app/db/schema.rb
  13. +0 −21 app/db/sequel.rb
  14. +0 −112 app/db/sqlite.rb
  15. +0 −231 app/db/table.rb
  16. +1 −17 app/ui/tabs/home.rb
  17. +7 −0 lib/all.rb
  18. +32 −0 lib/database.rb
  19. +22 −0 lib/preferences.rb
  20. +52 −0 lib/web/hacker.rb
  21. +25 −0 spec/lib/database_spec.rb
  22. +17 −0 spec/lib/preferences_spec.rb
View
@@ -1,2 +1,3 @@
*~
root/Home/*
+*.swp
View
@@ -4,3 +4,4 @@ gem "cucumber"
gem "hpricot"
gem "rspec"
gem "rake"
+gem "sqlite3-ruby"
View
@@ -22,6 +22,9 @@ GEM
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
+ sqlite3 (1.3.4)
+ sqlite3-ruby (1.3.3)
+ sqlite3 (>= 1.3.3)
term-ansicolor (1.0.6)
PLATFORMS
@@ -32,3 +35,4 @@ DEPENDENCIES
hpricot
rake
rspec
+ sqlite3-ruby
View
@@ -1,9 +1,12 @@
require 'cucumber'
require 'cucumber/rake/task'
+require 'rspec/core/rake_task'
Cucumber::Rake::Task.new(:features) do |t|
t.cucumber_opts = "--format pretty"
end
-task :default => :features
+RSpec::Core::RakeTask.new(:spec)
+
+task :default => [:spec, :features]
View
@@ -15,8 +15,6 @@ def HH.anonymous_binding
require 'lib/all'
require 'app/syntax/markup'
-require 'app/db/sequel'
-
require 'app/ui/lessons'
require 'app/ui/widgets'
require 'app/ui/completion'
@@ -26,7 +24,7 @@ def HH.anonymous_binding
if HH::PREFS['first_run'].nil?
File.open(File.join(HH::USER, "Hello World.rb"), "w") do |f|
f << 'alert "Hello, world!"'
- end
+ end
#the first_run pref will get set by the tour notice in app/ui/mainwindow
end
View
@@ -1,65 +0,0 @@
-require 'thread'
-
-module HH::Sequel
- class ConnectionPool
- attr_reader :max_size, :mutex, :conn_maker
- attr_reader :available_connections, :allocated, :created_count
-
- def initialize(max_size = 4, &block)
- @max_size = max_size
- @mutex = Mutex.new
- @conn_maker = block
-
- @available_connections = []
- @allocated = {}
- @created_count = 0
- end
-
- def size
- @created_count
- end
-
- def hold
- t = Thread.current
- if (conn = owned_connection(t))
- return yield(conn)
- end
- while !(conn = acquire(t))
- sleep 0.001
- end
- begin
- yield conn
- ensure
- release(t)
- end
- end
-
- def owned_connection(thread)
- @mutex.synchronize {@allocated[thread]}
- end
-
- def acquire(thread)
- @mutex.synchronize do
- @allocated[thread] ||= available
- end
- end
-
- def available
- @available_connections.pop || make_new
- end
-
- def make_new
- if @created_count < @max_size
- @created_count += 1
- @conn_maker.call
- end
- end
-
- def release(thread)
- @mutex.synchronize do
- @available_connections << @allocated[thread]
- @allocated.delete(thread)
- end
- end
- end
-end
View
@@ -1,9 +0,0 @@
-# Time extensions.
-class Time
- SQL_FORMAT = "TIMESTAMP '%Y-%m-%d %H:%M:%S'".freeze
-
- # Formats the Time object as an SQL TIMESTAMP.
- def to_sql_timestamp
- strftime(SQL_FORMAT)
- end
-end
View
@@ -1,119 +0,0 @@
-require 'uri'
-
-require 'app/db/schema'
-
-module HH::Sequel
- # A Database object represents a virtual connection to a database.
- # The Database class is meant to be subclassed by database adapters in order
- # to provide the functionality needed for executing queries.
- class Database
- # Constructs a new instance of a database connection with the specified
- # options hash.
- #
- # Sequel::Database is an abstract class that is not useful by itself.
- def initialize(opts = {})
- @opts = opts
- end
-
- # Returns a new dataset with the from method invoked.
- def from(*args); dataset.from(*args); end
-
- # Returns a new dataset with the select method invoked.
- def select(*args); dataset.select(*args); end
-
- # Returns a new dataset with the from parameter set. For example,
- # db[:posts].each {|p| alert p[:title]}
- def [](table)
- dataset.from(table)
- end
-
- # call-seq:
- # db.execute(sql)
- # db << sql
- #
- # Executes an sql query.
- def <<(sql)
- execute(sql)
- end
-
- # Returns a literal SQL representation of a value. This method is usually
- # overriden in database adapters.
- def literal(v)
- case v
- when String then "'%s'" % v
- else v.to_s
- end
- end
-
- # Creates a table. The easiest way to use this method is to provide a
- # block:
- # DB.create_table :posts do
- # primary_key :id, :serial
- # column :title, :text
- # column :content, :text
- # index :title
- # end
- def create_table(name, columns = nil, indexes = nil, &block)
- if block
- schema = Schema.new
- schema.create_table(name, &block)
- schema.create(self)
- else
- execute Schema.create_table_sql(name, columns, indexes)
- end
- end
-
- # Drops a table.
- def drop_table(name)
- execute Schema.drop_table_sql(name)
- end
-
- # Performs a brute-force check for the existance of a table. This method is
- # usually overriden in descendants.
- def table_exists?(name)
- from(name).first && true
- rescue
- false
- end
-
- @@adapters = Hash.new
-
- # Sets the adapter scheme for the database class. Call this method in
- # descendnants of Database to allow connection using a URL. For example:
- # class DB2::Database < Sequel::Database
- # set_adapter_scheme :db2
- # ...
- # end
- def self.set_adapter_scheme(scheme)
- @@adapters[scheme.to_sym] = self
- end
-
- # Converts a uri to an options hash. These options are then passed
- # to a newly created database object.
- def self.uri_to_options(uri)
- {
- :user => uri.user,
- :password => uri.password,
- :host => uri.host,
- :port => uri.port,
- :database => (uri.path =~ /\/(.*)/) && ($1)
- }
- end
-
- # call-seq:
- # Sequel::Database.connect(conn_string)
- # Sequel.connect(conn_string)
- #
- # Creates a new database object based on the supplied connection string.
- # The specified scheme determines the database class used, and the rest
- # of the string specifies the connection options. For example:
- # DB = Sequel.connect('sqlite:///blog.db')
- def self.connect(conn_string)
- uri = URI.parse(conn_string)
- c = @@adapters[uri.scheme.to_sym]
- raise "Invalid database scheme" unless c
- c.new(c.uri_to_options(uri))
- end
- end
-end
-
Oops, something went wrong.

0 comments on commit 6f61aea

Please sign in to comment.