Ok, so it's ok to do code like this?
-----------------------------------------------
# establishes the connection
DB = Sequel.connect "sqlite://models/test.db",
:max_connections => 10,
:encoding => 'unicode',
:logger => Logger.new('models/test.log')
# will open a connection, because of the create table in user.rb class
require "models/user.rb"
load "models/user.rb"
DB.disconnect
-----------------------------------------------
-----------------------------------------------
get '/users' do
@users = User.all # open the connection
DB.disconnect # close it
haml :'/user/index'
end
-----------------------------------------------
-----------------------------------------------
class User < Sequel::Model(:users)
set_schema do
primary_key :id
varchar :username
varchar :password
timestamp :created_at
end
if !(table_exists?)
create_table
User.create :username => 'johndoe', :password => '123654',
:created_at => Time.now
User.create :username => 'zehmaneh', :password => '123654',
:created_at => Time.now
end
end
-----------------------------------------------
So probably that's why it's a good idea to use the 'vendor' dir with
the init.rb
file to encapsulate the establishment of the connection there?
On Feb 13, 4:25 am, Jeremy Evans <[email protected]> wrote:
> On Feb 12, 6:29 pm, sohdubom <[email protected]> wrote:
>
> > I saw an old sample tutorial with Sqlite and Sequel and although the
> > sample doesn't really works I got a few doubts:
>
> > 1. The sample use the vendor folder in order to put the init and db
> > files. I took a simpler path put the following code in my main .rb
> > file:
>
> > Sequel.connect "sqlite://models/test.db",
> > :max_connections => 10,
> > :encoding => 'unicode',
> > :logger => Logger.new('models/test.log')
>
> > And after that I load the models like:
>
> > require "models/user.rb"
> > load "models/user.rb"
>
> > It works just fine, but the thing I don't understand is related to the
> > database connection state. With a code like the one above or any
> > other, will Sequel manage the connection open/close state? Or with
> > that code, the connection is always opened?
>
> When you use Sequel.connect, it just establishes the Database object.
> The actual connection to the database isn't made until the first query
> is done (you can use test_connection if you want to establish a
> connection). Once the connection is established, it is left open
> indefinitely, though you can call disconnect on the database to close
> all available connections.
>
> Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---