Skip to content

Commit

Permalink
first go with dm-hibernate-adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mkristian committed Feb 13, 2010
1 parent 91c9d23 commit 631641e
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
jibernate.*
target
65 changes: 30 additions & 35 deletions eventlog.rb
@@ -1,54 +1,49 @@
# Basic requires
require 'rubygems'
require 'java'
require 'jdbc/hsqldb'
require 'jruby/core_ext'
require 'dm-core'

# Our requires
require 'hibernate'

Hibernate.dialect = Hibernate::Dialects::HSQL
Hibernate.current_session_context_class = "thread"

Hibernate.connection_driver_class = "org.hsqldb.jdbcDriver"
Hibernate.connection_url = "jdbc:hsqldb:file:jibernate"
Hibernate.connection_username = "sa"
Hibernate.connection_password = ""
Hibernate.properties["hbm2ddl.auto"] = "update"
require 'lib/dm-hibernate-adapter.rb'
DataMapper.setup(:default, :adapter => "hibernate")

class Event
include DataMapper::Resource

property :id, Serial
property :title, String
property :date, Date

# TODO get this out of the model into the adapter
extend Hibernate::Model
hibernate_attr :id => :long, :title => :string, :date => :date
hibernate!
end

Hibernate.add_model "Event.hbm.xml"

Hibernate.tx do |session|
# Hack for HSQLDB's write delay
session.createSQLQuery("SET WRITE_DELAY FALSE").execute_update
# Hack for HSQLDB's write delay
# session.createSQLQuery("SET WRITE_DELAY FALSE").execute_update

case ARGV[0]
when /store/
# Create event and store it
event = Event.new
event.title = ARGV[1]
event.date = java.util.Date.new

session.save(event)
puts "Stored!"
when /list/
# List all events
list = session.create_query('from Event').list
puts "Listing all events:"
list.each do |evt|
puts <<EOS
case ARGV[0]
when /store/
# Create event and store it
event = Event.new
event.title = ARGV[1]
event.date = java.util.Date.new
event.save
puts "Stored!"
when /list/
list = Event.all
puts "Listing all events:"
list.each do |evt|
puts <<EOS
id: #{evt.id}
title: #{evt.title}
date: #{evt.date}"
date: #{evt.date}
EOS
end
else
puts "Usage:\n\tstore <title>\n\tlist"
end
end
else
puts "Usage:\n\tstore <title>\n\tlist"
end

8 changes: 0 additions & 8 deletions hibernate.rb
@@ -1,12 +1,4 @@
require 'java'
require 'lib/antlr-2.7.6.jar'
require 'lib/commons-collections-3.1.jar'
require 'lib/dom4j-1.6.1.jar'
require 'lib/javassist-3.9.0.GA.jar'
require 'lib/jta-1.1.jar'
require 'lib/slf4j-simple-1.5.8.jar'
require 'lib/slf4j-api-1.5.8.jar'
require 'lib/hibernate3.jar'
require 'stringio'

require 'dialects'
Expand Down
Binary file removed lib/antlr-2.7.6.jar
Binary file not shown.
Binary file removed lib/commons-collections-3.1.jar
Binary file not shown.
59 changes: 59 additions & 0 deletions lib/dm-hibernate-adapter.rb
@@ -0,0 +1,59 @@
require 'hibernate'
module DataMapper
module Adapters
class HibernateAdapter < AbstractAdapter

def initialize(name, options = {})
super
Hibernate.dialect = Hibernate::Dialects::HSQL
Hibernate.current_session_context_class = "thread"

Hibernate.connection_driver_class = "org.hsqldb.jdbcDriver"
Hibernate.connection_url = "jdbc:hsqldb:file:jibernate"
Hibernate.connection_username = "sa"
Hibernate.connection_password = ""
Hibernate.properties["hbm2ddl.auto"] = "update"
end

# @param [Enumerable<Resource>] resources
# The list of resources (model instances) to create
#
# @return [Integer]
# The number of records that were actually saved into the data-store
#
# @api semipublic
def create(resources)
puts "create #{resources.inspect}"
count = 0
Hibernate.tx do |session|
resources.each do |resource|
session.save(resource)
count += 1
end
end
count
end

# @param [Query] query
# the query to match resources in the datastore
#
# @return [Enumerable<Hash>]
# an array of hashes to become resources
#
# @api semipublic
def read(query)
puts "query #{query.inspect}"
result = []
Hibernate.tx do |session|
list = session.create_query("from #{query.model}").list
#TODO maybe there is a direct way to get a ruby array ?
list.each do |resource|
result << resource
end
list
end
result
end
end
end
end
13 changes: 13 additions & 0 deletions lib/dm-hibernate-adapter.rb~
@@ -0,0 +1,13 @@
module DataMapper
module Adapters
class LuceneAdapter < AbstractAdapter

# @param [Enumerable<Resource>] resources
# The list of resources (model instances) to create
#
# @return [Integer]
# The number of records that were actually saved into the data-store
#
# @api semipublic
def create(resources)
end
Binary file removed lib/dom4j-1.6.1.jar
Binary file not shown.
Binary file removed lib/hibernate3.jar
Binary file not shown.
Binary file removed lib/javassist-3.9.0.GA.jar
Binary file not shown.
Binary file removed lib/jta-1.1.jar
Binary file not shown.
Binary file removed lib/slf4j-api-1.5.8.jar
Binary file not shown.
Binary file removed lib/slf4j-simple-1.5.8.jar
Binary file not shown.
117 changes: 117 additions & 0 deletions pom.xml
@@ -0,0 +1,117 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rubygems</groupId>
<artifactId>jibernate</artifactId>
<packaging>gem</packaging>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>rubygems</groupId>
<artifactId>dm-core</artifactId>
<version>0.10.2</version>
<type>gem</type>
</dependency>
<!--
does not load from within maven - wrong classloader !!!
<dependency>
<groupId>rubygems</groupId>
<artifactId>jdbc-hsqldb</artifactId>
<version>1.8.0.7</version>
<type>gem</type>
</dependency>
-->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.1.3</version>
</dependency>
<!-- choose this version which can be downloaded publicly -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>central</id>
<url>http://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>gemcutter</id>
<releases>
<updatePolicy>never</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>http://www.gemcutter.org/gems</url>
<layout>gem</layout>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>jruby-maven-plugin</artifactId>
<version>${jruby.plugins.version}</version>
</plugin>
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>gem-maven-plugin</artifactId>
<version>${jruby.plugins.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<phase>initialize</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>shitty-maven-plugin</artifactId>
<version>1.0-alpha-4-SNAPSHOT</version><executions>
<execution>
<id>it-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<goals>
<goal>clean</goal>
<goal>install</goal>
<goal>test</goal>
</goals>
<configuration>
<parallel>true</parallel>
<threadCount>2</threadCount>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<jruby.plugins.version>0.9.0</jruby.plugins.version>
<!-- this allows to configure it for rails-maven-plugin as well jruby-maven-plugin and gem-maven-plugin -->
<jruby.gem.home>${project.build.directory}/rubygems</jruby.gem.home>
<jruby.gem.path>${project.build.directory}/rubygems</jruby.gem.path>
<jruby.fork>true</jruby.fork>
</properties>

</project>

0 comments on commit 631641e

Please sign in to comment.