/dao/suspence_dao.rb
Ruby | 64 lines | 41 code | 6 blank | 17 comment | 6 complexity | a581f02790593f918dc31b2725e97cd3 MD5 | raw file
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, version 3 of the License.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- #
- # Copyright: (C) 2004-2010 Rafal Kotusiewicz aka 'jjhop'. All rights reserved.
- #
- # http://bitbucket.org/jjhop/mycash/overview/
- # http://bitbucket.org/jjhop/mycash/wiki/Home
- #
-
- require 'rubygems'
- require File.join(File.dirname(__FILE__), '../model/suspence')
-
- module MyCashDAO
-
- class SuspenceDAO
- def initialize(dbh)
- if dbh.nil?
- raise 'Polaczenie z baza musi byc prawidlowe'
- end
- @dbh = dbh
- end
-
- def find_by_id(id)
- begin
- if id == -1
- return
- end
- r = @dbh.get_first_row("SELECT * FROM suspences WHERE id=:id", 'id' => id)
- return MyCashModel::Suspence.create_from_hash(r)
- rescue Exception => ex
- raise DAOException, ex
- end
- end
-
- def save_or_update(suspence)
- unless MyCashModel::Suspence == suspence.class
- raise DAOException,
- 'Argumentem metody MyCashDAO::SuspenceDAO#save_or_update musi' +
- ' by? prawid?owy obiekt klasy MyCashModel::Suspence'
- end
- if suspence.new?
- @dbh.execute(
- "INSERT INTO suspences(id, created_at, amount, what) VALUES(?,?,?,?)",
- nil, suspence.created_at || Time.new, suspence.amount, suspence.what)
- else
- @dbh.execute( "UPDATE suspences SET amount=?, what=? WHERE id=?",
- suspence.amount, suspence.what, suspence.suspence_id)
- end
- end
-
- def delete_by_id(id)
- @dbh.execute("DELETE FROM suspences WHERE id=:id", 'id' => id)
- end
- end
- end