<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Software Warlock &#187; ORM</title>
	<atom:link href="http://softwareblog.morlok.net/tag/orm/feed/" rel="self" type="application/rss+xml" />
	<link>http://softwareblog.morlok.net</link>
	<description></description>
	<lastBuildDate>Sat, 24 Dec 2011 16:06:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>DataMapper defaults to socket; causes error</title>
		<link>http://softwareblog.morlok.net/2010/12/11/datamapper-defaults-to-socket-causes-error/</link>
		<comments>http://softwareblog.morlok.net/2010/12/11/datamapper-defaults-to-socket-causes-error/#comments</comments>
		<pubDate>Sun, 12 Dec 2010 02:08:22 +0000</pubDate>
		<dc:creator>Warlock</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[DataMapper]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://softwareblog.morlok.net/?p=409</guid>
		<description><![CDATA[Just started migrating a Ruby project I&#8217;m working on to DataMapper and I found an interesting error. DataMapper.setup(:default, ENV['DATABASE_URL']) Where my DATABASE_URL was set as an environment variable to something along the lines of the following: DATABASE_URL=mysql://username:password@localhost:3306/database_name The key point &#8230;<p class="read-more"><a href="http://softwareblog.morlok.net/2010/12/11/datamapper-defaults-to-socket-causes-error/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Just started migrating a Ruby project I&#8217;m working on to DataMapper and I found an interesting error.</p>
<p><tt><br />
DataMapper.setup(:default, ENV['DATABASE_URL'])<br />
</tt></p>
<p>
Where my DATABASE_URL was set as an environment variable to something along the lines of the following:
</p>
<p><tt><br />
DATABASE_URL=mysql://username:password@localhost:3306/database_name<br />
</tt></p>
<p>The key point to note is that I&#8217;m trying to point it to TCP port 3306 on my local machine. When I&#8217;d trying to load up a model object and hit the database, I got the following error:</p>
<p><tt><br />
DataObjects::SQLError: Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)<br />
</tt></pre>
<p>Full stack trace:</p>
<pre>
DataObjects::SQLError: Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)
	/Library/Ruby/Gems/1.8/gems/data_objects-0.10.2/lib/data_objects/connection.rb:76:in `initialize'
	/Library/Ruby/Gems/1.8/gems/data_objects-0.10.2/lib/data_objects/connection.rb:76:in `send'
	/Library/Ruby/Gems/1.8/gems/data_objects-0.10.2/lib/data_objects/connection.rb:76:in `__new'
	/Library/Ruby/Gems/1.8/gems/data_objects-0.10.2/lib/data_objects/pooling.rb:177:in `new'
	/Library/Ruby/Gems/1.8/gems/data_objects-0.10.2/lib/data_objects/pooling.rb:172:in `synchronize'
	/Library/Ruby/Gems/1.8/gems/data_objects-0.10.2/lib/data_objects/pooling.rb:172:in `new'
	/Library/Ruby/Gems/1.8/gems/data_objects-0.10.2/lib/data_objects/pooling.rb:119:in `new'
	/Library/Ruby/Gems/1.8/gems/data_objects-0.10.2/lib/data_objects/connection.rb:65:in `new'
	/Library/Ruby/Gems/1.8/gems/dm-do-adapter-1.0.2/lib/dm-do-adapter/adapter.rb:235:in `open_connection'
	/Library/Ruby/Gems/1.8/gems/dm-do-adapter-1.0.2/lib/dm-do-adapter/adapter.rb:260:in `with_connection'
	/Library/Ruby/Gems/1.8/gems/dm-do-adapter-1.0.2/lib/dm-do-adapter/adapter.rb:138:in `read'
	/Library/Ruby/Gems/1.8/gems/dm-core-1.0.2/lib/dm-core/repository.rb:162:in `read'
	/Library/Ruby/Gems/1.8/gems/dm-core-1.0.2/lib/dm-core/model.rb:379:in `first'
	./routes/services.rb:11:in `GET /v1/:choice_id'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:1032:in `call'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:1032:in `compile!'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:620:in `instance_eval'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:620:in `route_eval'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:604:in `route!'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:656:in `process_route'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:653:in `catch'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:653:in `process_route'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:603:in `route!'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:602:in `each'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:602:in `route!'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:741:in `dispatch!'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:530:in `call!'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:706:in `instance_eval'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:706:in `invoke'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:706:in `catch'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:706:in `invoke'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:530:in `call!'
	/Library/Ruby/Gems/1.8/gems/sinatra-1.1.0/lib/sinatra/base.rb:516:in `call'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/showexceptions.rb:24:in `call'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/lint.rb:48:in `_call'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/lint.rb:36:in `call'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/showexceptions.rb:24:in `call'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/commonlogger.rb:18:in `call'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/content_length.rb:13:in `call'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/chunked.rb:15:in `call'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/handler/mongrel.rb:67:in `process'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/handler/mongrel.rb:38:in `run'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/server.rb:213:in `start'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/server.rb:100:in `start'
	/Library/Ruby/Gems/1.8/gems/rack-1.2.1/bin/rackup:4
	/usr/bin/rackup:19:in `load'
	/usr/bin/rackup:19
</pre>
<p>Despite the fact that I was telling it to go after a TCP port, it was attempting to connect to MySQL via a socket (the reason it was getting the error is it was looking for the socket at /opt/local/var/run/mysql5/mysqld.sock and mine was configured /tmp/mysql.sock &mdash; but that's not the point, it shouldn't have been looking there to begin with</p>
<p>Through trial and error I found that it appears that DataMapper automatically falls back to socket based communication with MySQL when you specify localhost. I was able to correct the error by specifying an IP address instead:</p>
<p><tt><br />
DATABASE_URL=mysql://username:password@127.0.0.1:3306/database_name<br />
</tt></p>
]]></content:encoded>
			<wfw:commentRss>http://softwareblog.morlok.net/2010/12/11/datamapper-defaults-to-socket-causes-error/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Mapping Enums to custom strings in NHibernate</title>
		<link>http://softwareblog.morlok.net/2009/07/02/mapping-enums-to-custom-strings-in-nhibernate/</link>
		<comments>http://softwareblog.morlok.net/2009/07/02/mapping-enums-to-custom-strings-in-nhibernate/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 03:16:09 +0000</pubDate>
		<dc:creator>Warlock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Enum]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Object-relational mapping]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://softwareblog.morlok.net/?p=294</guid>
		<description><![CDATA[Many times when working with a legacy relation model and a newly developed C# object model that sits on top of the relational model, you may need to map enum values to/from arbitrary strings in the database using NHibernate. For &#8230;<p class="read-more"><a href="http://softwareblog.morlok.net/2009/07/02/mapping-enums-to-custom-strings-in-nhibernate/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>
Many times when working with a legacy relation model and a newly developed C# object model that sits on top of the relational model, you may need to map enum values to/from arbitrary strings in the database using NHibernate.
</p>
<p>
For example, suppose you have a a work order object that three possible states: request, approved, denied.  To represent this, you might use the following enum:
</p>
<pre name="code" class="c#">
enum WorkRequestState
{
    Request,
    Approved,
    Denied
};
</pre>
<p>But let&#8217;s assume that the legacy relational model has represented these values as strings: <tt>REQ</tt>, <tt>APR</tt>, <tt>DEN</tt>.  You can&#8217;t change the values for the relational model, and you certainly don&#8217;t want to use these values in your enum because they are not as readable as the values shown above.</p>
<p>By default, NHibernate will automatically convert you enum to a string value that matches the name of the enum values if the table field a string, or it will convert to the integer value for the enum options if the table field is an integer.</p>
<p>To do the above custom mapping we must implement a custom NHibernate type.</p>
<p>First, create a new class that inherits <tt>NHibernate.Type.EnumStringType</tt>.  In your constructor, pass the type of the enum you are handling, and the maximum number of characters that the enum values will be to the base class constructor:</p>
<pre name="code" class="c#">
class WorkRequestStateEnumStringType : NHibernate.Type.EnumStringType
{
    public WorkRequestStateEnumStringType()
        : base( typeof(WorkRequestState), 3)
    {
    }
    ...
}
</pre>
<p>Next, override the <tt>GetValue(...)</tt> method to map the enum value to the equivalent string.</p>
<pre name="code" class="c#">
public override object GetValue(object enm)
{
	if( null == enm )
		return String.Empty;

	switch( (WorkRequestState)enm )
	{
		case WorkRequestState.Request	: return "REQ";
		case WorkRequestState.Approved	: return "APR";
		case WorkRequestState.Denied	: return "DEN";
		default : throw new ArgumentException("Invalid WorkRequestState.");
	}
}
</pre>
<p>Override the <tt>GetInstance(...)</tt> method to map a string value to the equivalent enum value:</p>
<pre name="code" class="c#">
public override object GetInstance(object code)
{
	code = code.ToUpper();

	if( "REQ".Equals(code) )
		return WorkRequestState.Request;
	else if( "APR".Equals(code) )
		return WorkRequestState.Approved;
	else if( "DEN".Equals(code) )
		return WorkRequestState.Denied;

	throw new ArgumentException(
		"Cannot convert code '" + code + "' to WorkRequestState.");
}
</pre>
<p>Finally, in the NHibernate mapping file, specify the property&#8217;s type as the derived <tt>EnumStringType</tt> class.</p>
<pre name="code" class="xml">
&lt;class name="WorkRequest" table="work_request_table"&gt;
  &lt;property
    name="Status"
    column="status_field"
    type="my.namespace. WorkRequestStateEnumStringType, MyAssembly" /&gt;
  ...
&lt;class&gt;
</pre>
<p>Note that when specifying this type, you must use the <a href="http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx">assembly-qualified name</a> because the NHibernate code doing the mapping is in a separate assembly from the type you created.</p>
<p>This information has been derived from Jeremy Miller&#8217;s original post <a href="http://codebetter.com/blogs/jeremy.miller/archive/2006/02/20/138732.aspx">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://softwareblog.morlok.net/2009/07/02/mapping-enums-to-custom-strings-in-nhibernate/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

