Monday, 11 January 2010

Date (Without Time) In MsSql

Should be something like that:

SELECT dateadd(day, datediff(day, 0, getdate()), 0)

Thursday, 7 January 2010

C++ Header Files With Classes and Makefile

2 good tutorials:

Class code and header files.
Makefile

Updated: And 1 more article about c++ unit test frameworks.

Sunday, 20 December 2009

Date Parsing In C#

DateTime.Parse("Dec 13 18:06");//Results in {06.12.2009 13:18:00}
Why?!

Thursday, 3 December 2009

Virtual Desktop Manager For Windows

I got too accustomed to multiple desktops on my home Ubuntu computer, that working under Windows with many open programs without ability to share them among several desktops made me too uncomfortable. VirtuaWin - The Virtual Desktop Manager was the solution :). Now I am happy again.

Sunday, 15 November 2009

Add List To App.Config

One can add list and not only a list to App.Config file of the .Net project. That can be done by declaring your own configuration element, say a ProductElement

class ProductElement : ConfigurationElement
{
  [ConfigurationProperty("name", IsKey=true, IsRequired=true)]
  public string Name
  {
    get { return this["name"]; }
  }

  [ConfigurationProperty("price")]
  public int Price
  {
    get { return Convert.ToInt32(this["price"]); }
  }
}

class ProductElementCollection : ConfigurationElementCollection
{
  protected override ConfigurationElement CreateNewElement()
  {
    return new ProductElement();
  }

  protected override object GetElementKey(ConfigurationElement element)
  {
    return ((ProductElement)element).Name;
  }
}

class ProductConfigurationSection : ConfigurationSection
{
   [ConfigurationProperty("products")]
   public ProductElementCollection Products
   {
      get { return (ProductElementCollection)this["products"]; }
   }
}

Use it in your app.config like that:

<configuration>
   <configSections>
      <section name="productConfigurationSection"
        type="MyNamespace.ProductConfigurationSection, MyAssembly" 
        requirePermission="false" />
   </configSections>
   <productConfigurationSection>
      <products>
         <add name="car" price="750000" />
         <add name="milk" price="13" />
      </products>
   </productConfigurationSection>
</configuration>

Note that Name is used as a key, so you cannot create 2 Products with the same name. But you can add more fields to the Product, just mark them accordingly. And from code you can access this section with:

ProductConfigurationSection section = (ProductConfigurationSection) ConfigurationManager.GetSection("productConfigurationSection");

Tuesday, 20 October 2009

The Most Intelligent Java IDE — Now Free and Open Source

Hardly anyone will argue, that IntelliJ IDEA is one of the best IDE for Java and Ruby(Rails). And now it is going to be free! Yes, this title is taken from IntelliJ Site. Next version of the product will be free, and it can already be downloaded from the coerced link.

Wednesday, 16 September 2009

Convert Different Audio In Command Line in Ubuntu

Converting different audio formats in Ubuntu is quite an easy task. Just start Soundconverter, or first install it, if you don't have.

sudo apt-get install soundconverter

But I needed to do it in command line. Soundconverter can do it either. (What a good boy!) But you just need to know the mime-type of the output file (simply adding the desired suffix doesn't work). For example, to convert "my_favourite mp3" to wav you can use:

soundconverter -b -m audio/x-wav -s .wav my_favourite.mp3

See the full list of mime types.

For the available options see

soundconverter -h

or

man soundconverter

Sunday, 2 August 2009

Ruby Shoes

I wanted to try ruby Shoes already long ago, but just didn't know what to start with. Until I found a quiteuseful article about making a game on ruby shoes. I tried it and it was unexpectedly easy. Here are some my subjective thoughts about it.

Good things:

It is very easy to study.
One can begin writing using shoes quite quickly and straightforward. No big manuals or hardly understandable api and howtos.
It is cute. :)
Yes, I really liked it. Different objects are created very easy, such as:
Shoes.app { button("Click me!") { alert("Good job.") } }
You also can draw with provided rect, oval or even arrow methods or import an image as a background. Motion ability is also provided:
Shoes.app do #A star that moves after the mouse pointer.
  @shape = star :points => 5
  motion do |left, top|
    @shape.move left, top
  end
end
Unfortunately there were also some disadvantages:
The whole Shoes application should be in Shoes.app block.
Seem to me to be a little bit uncomfortable.
You have to pass everywhere app variable that denotes the Shoes application.
Well, not just everywhere... But if you create your own class, which objects have to be drawn you also have to pass the application variable, or make it global, which is not really good pattern.
class RedRect

  def initialize( app )
    app.fill red
    app.rect :left => 10, :top => 10, :width => 40 
  end
end

Shoes.app do
  RedRect.new( self )
end
Pity, but it is slow.
The resulting snake in the game responds in about 2 seconds. It is a long period of time, especially on higher levels with higher speed.

In conclusion the authors' words about Shoes are right:"Shoes is a tiny graphics toolkit, designed for beginners". It can be used to create quickly a small application, where speed is not essential.

Friday, 31 July 2009

Access DOM Element Within IFrame

Suppose we have a html page with frame or iframe in it and we need to get an element in it. Simple

document.getElementById("myElement");

will not find it, as it is searched only in the main page, where the frame is set. What we should do, is to find the frame first and then look for needed element in the right frame.

frame = document.getElementById("myFrame"); 
frame.contentWindow.getElementById("myElement");

Thursday, 9 July 2009

Concatenate Strings On Group By In Sql

Imagine we have the following table.
CREATE TABLE MyTable (id int, name varchar, value int);

INSERT INTO MyTable (id,name,value) VALUES (1, 'Hello', 4);
INSERT INTO MyTable (id,name,value) VALUES (1, 'World', 8);
INSERT INTO MyTable (id,name,value) VALUES (5, 'Great!', 9);
The result we would like to acquire is:
| id |   name_values    |
+----+------------------+
|  1 | Hello:4; World:8 |
|  5 | Great!:9         |
Names and values are concatenated into strings and grouped by id. We need an aggregate function, that concatenates strings for that. Here are some solutions for different sql databases.
MySql
This case is most easy one. Lucky users already have the GROUP_CONCAT(expr) function. This query should give the answer.
SELECT id, GROUP_CONCAT(name + ':' + value SEPARATOR '; ') AS name_values FROM MyTable GROUP BY id;
PostgreSql
The solution here is a bit more difficult, but nevertheless easy enough. We need to create our own aggregate function and use it in our query.
CREATE AGGREGATE aggr_textcat(
  basetype    = text,
  sfunc       = textcat,
  stype       = text,
  initcond    = ''
);

SELECT id, substring(aggr_textcat(', ' || name || ':' || value) from 2) AS name_values FROM MyTable GROUP BY id;
Here we used already existing function to concatenate text fields textcat, but we could write our own.
MsSql
Since version 2005 it became also possible to write your own aggregate function in MsSql, but here I provide another solution using inner select and xml path.
SELECT id, SUBSTRING((SELECT '; ' + name + ':' + CAST(value AS varchar(MAX)) 
FROM MyTable WHERE (id = Results.id) FOR XML PATH ('')),3,9999) AS name_values
FROM MyTable Results
GROUP BY id