<?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>blog.autopsy.se &#187; MySQL</title>
	<atom:link href="http://blog.autopsy.se/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.autopsy.se</link>
	<description>basically; it sucks. everything sucks. but.. in a good way..</description>
	<lastBuildDate>Mon, 02 Aug 2010 16:14:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Reading config from database sucks!</title>
		<link>http://blog.autopsy.se/2009/01/reading-config-from-database-sucks/</link>
		<comments>http://blog.autopsy.se/2009/01/reading-config-from-database-sucks/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 19:16:24 +0000</pubDate>
		<dc:creator>ube</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code Poetry]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.autopsy.se/?p=30</guid>
		<description><![CDATA[I'm working on a new site and I wanted an easy way to read config for a user from the database. Earlier I've stored all the config variables in the user table, but this leaves a problem when developing; adding a new parameter means that you'll have to add a new entry in the user [...]]]></description>
			<content:encoded><![CDATA[<p>I'm working on a new site and I wanted an easy way to read config for a user from the database. Earlier I've stored all the config variables in the user table, but this leaves a problem when developing; adding a new parameter means that you'll have to add a new entry in the user table. So, why not use one table for config and then have a default value stored in the database as user 0? <span id="more-30"></span></p>
<pre class="mysql"> <span style="color: #993333; font-weight: bold;">EXPLAIN</span> config;
+<span style="color: #808080; font-style: italic;">-------------+--------------+------+-----+---------+-------+</span>
| <span style="color: #993333; font-weight: bold;">FIELD</span>       | Type         | <span style="color: #aa3399; font-weight: bold;">NULL</span> | Key | <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> | Extra |
+<span style="color: #808080; font-style: italic;">-------------+--------------+------+-----+---------+-------+</span>
| configUser  | <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span>      | NO   |     | <span style="color: #cc66cc;">0</span>       |       |
| configVar   | <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> | NO   |     | <span style="color: #aa3399; font-weight: bold;">NULL</span>    |       |
| configValue | <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> | NO   |     | <span style="color: #aa3399; font-weight: bold;">NULL</span>    |       |
+<span style="color: #808080; font-style: italic;">-------------+--------------+------+-----+---------+-------+</span>
&nbsp;
 <span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> config;
+<span style="color: #808080; font-style: italic;">------------+----------------+-------------+</span>
| configUser | configVar      | configValue |
+<span style="color: #808080; font-style: italic;">------------+----------------+-------------+</span>
|          <span style="color: #cc66cc;">0</span> | streamsPerPage | <span style="color: #cc66cc;">30</span>          |
|          <span style="color: #cc66cc;">1</span> | streamsPerPage | <span style="color: #cc66cc;">10</span>          |
+<span style="color: #808080; font-style: italic;">------------+----------------+-------------+</span></pre>
<p>The problem this is when you're trying to get the config value from the database for a user and the value is set, if you don't want to run two queries that is:</p>
<pre class="mysql"><span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> config <span style="color: #993333; font-weight: bold;">WHERE</span> configUser = <span style="color: #cc66cc;">0</span>;
<span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> config <span style="color: #993333; font-weight: bold;">WHERE</span> configUser = <span style="color: #cc66cc;">4384</span>;</pre>
<pre class="php"><span style="color: #000000; font-weight: bold;">function</span> getConfig<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$uid</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">$ids</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;0&quot;</span>, =&amp;gt; <span style="color: #cc66cc;">0</span>, <span style="color: #ff0000;">&quot;1&quot;</span>, <span style="color: #0000ff;">$uid</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">$ids</span> = <a href="http://www.php.net/array_unique"><span style="color: #000066;">array_unique</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ids</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$ids</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$id</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   <span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">&quot;select * from config where configUser = $id&quot;</span>;
   <span style="color: #0000ff;">$data</span> = executeSql<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$q</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
     <span style="color: #0000ff;">$return</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$q</span><span style="color: #66cc66;">&#91;</span>configVar<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$q</span><span style="color: #66cc66;">&#91;</span>configValue<span style="color: #66cc66;">&#93;</span>;
   <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$return</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>Could be used, but you do want a more elegant way, right?</p>
<pre class="mysql"><span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> config <span style="color: #993333; font-weight: bold;">WHERE</span> configUser = <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> max<span style="color: #66cc66;">&#40;</span>configUser<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> config <span style="color: #993333; font-weight: bold;">WHERE</span> configUser = <span style="color: #cc66cc;">4384</span> <span style="color: #993333; font-weight: bold;">OR</span> configUser = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>Could also work, but sub-queries are CPU hoggers. So what I came up with (with some help from my friends):</p>
<pre class="mysql"><span style="color: #993333; font-weight: bold;">SELECT</span>
 config.configVar,
 IFNULL<span style="color: #66cc66;">&#40;</span>userconfig.configvalue, config.configvalue<span style="color: #66cc66;">&#41;</span> AS configValue
<span style="color: #993333; font-weight: bold;">FROM</span>
 config <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> config as userconfig
    on
     <span style="color: #66cc66;">&#40;</span>userconfig.configVar = config.configVar <span style="color: #993333; font-weight: bold;">AND</span> userconfig.configuser=<span style="color: #cc66cc;">4384</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #993333; font-weight: bold;">WHERE</span>
    config.configuser=<span style="color: #cc66cc;">0</span><span style="color: #ff0000;">&quot;;</span></pre>
<p>This gives us the following code:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">function</span> getConfig<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$uid</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">&quot;select
                 config.configVar,
                 ifnull(userconfig.configvalue, config.configvalue) as configValue
          from
                 config
          left join
                 config as userconfig on (userconfig.configVar = config.configVar and userconfig.configuser=$uid)
          where
                 config.configuser=0&quot;</span>;
  <span style="color: #0000ff;">$data</span> = executeSql<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$q</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$return</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$q</span><span style="color: #66cc66;">&#91;</span>configVar<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$q</span><span style="color: #66cc66;">&#91;</span>configValue<span style="color: #66cc66;">&#93;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$return</span>;
<span style="color: #66cc66;">&#125;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.autopsy.se/2009/01/reading-config-from-database-sucks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
