Archive for the ‘Code is Poetry’ Category

PHP + ADOdb + Firebird

Friday, July 4th, 2008

Here we go…

First download the latest PHP ADOdb library here: ADOdb Library for PHP.

Install it in proper directories and make sure your Firebird server is running.

- Setup your PHP to support Firebird functions

Edit your php.ini and do this:

  • setup YOUR extension_dir
    extension_dir = “D:\PHP5\ext”
  • Remove the ; of the interbase library
    ;extension=php_ifx.dll
    ;extension=php_imap.dll
    extension=php_interbase.dll
    ;extension=php_ldap.dll
    ;extension=php_mcrypt.dll
    ;extension=php_mhash.dll
  • restart your apache or IIS or whatever

- Setup the database info
<?php
define("DBUSER", "sysdba");
define("DBPASS", "foopassword");
define("DBHOST", 'localhost:C:/your/path/to/the/database.gdb');
?>

!POSSIBLE PROBLEM!: in the host parameter, localhost must be the IP address or hostname of the server where is running the Firebird server.

- Setup the ADOdb
<?php
include("adodb/adodb.inc.php");
// VERY IMPORTANT due Firebird do not send the column names as default
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$DB = &ADONewConnection(’firebird’);
$DB->Connect(DBHOST, DBUSER, DBPASS);
?>

- Testing
<?php
$SQL = "SELECT * FROM TABLENAME ORDER BY DAMNFIREBIRD";
$res = $DB->Execute($SQL);
if ($res->RecordCount() != 0) {
  while(!$res->EOF) {
    echo $res->fields['DAMNFIREBIRD'];
    $res->MoveNext();
  }
}
?>

5° - this shit is working =)

LIMIT in SELECT for Firebird

Friday, July 4th, 2008

The Firebird equivalent for mySQL/PostgreSQL LIMIT statement is:

SELECT FIRST X [SKIP Y] * FROM …. (your query)

Simple, the X is the number of results you want and the Y is how many results you want to “jump”.
The SKIP is optional.
Example:

SELECT FIRST 20 SKIP 40 columnA, columnB FROM TABLENAME …

that’s it.

XML Serialization with C#

Friday, July 4th, 2008

Wanna know a easy way to serialize your class to XML ???
Here we go…

This is the class myXMLSerializer.cs

We gonna need System.IO, System.Text and, of course, System.Xml.Serialization.
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;

I created a sealed class with one static method to make a common XML Serializer for my app, use as you wish.
namespace how2do.XML
{
    public sealed class myXMLSerializer
    {
      public static XmlDocument SerializeObject(Type type, object objToSerialize)
      {

Now we create a MemoryStream because we don’t want a file stream or whatever we see in a lot of examples over the internet, I just wanted to work with string-memory-XmlDocument… (I was so pissed off because I couldn’t find something similar, people assume we are always using .xml files)
        // Serialization
        MemoryStream mStream = new MemoryStream();

Here, the correct parameter shall be: … = new XmlSerializer(typeof(YourClass)); Ok?
        XmlSerializer objSerializer = new XmlSerializer(type);

I got a lot of problems using UTF8 here (to create the stream) and also using UTF8 converting to string
        //must be Encoding.Default due problems with UTF8Enconding which is used to get the XML as string
        XmlTextWriter xmlWriter = new XmlTextWriter(mStream, Encoding.Default);
        objSerializer.Serialize(xmlWriter, objToSerialize);

Now, taking the damn XML as string. Let’s create our beloved XmlDocument.
        XmlDocument xmlObject = new XmlDocument();

I’m using UTF8, don’t remember why… ah! XmlDocument.LoadXml is a pain in the ass, UFT8 works great =)
        UTF8Encoding encoding = new UTF8Encoding();
        mStream = (MemoryStream)xmlWriter.BaseStream;

The trick
        string strXML = encoding.GetString(mStream.ToArray());

Now loading into the XmlDocument using LoadXml(), not Load() because Load() is for files and files sucks.
        xmlObject.LoadXml(strXML);
        return xmlObject;
      }
    }
}

ABOUT THE CLASSES YOU WILL SERIALIZE

Don’t forget to use the XML Attributes into the classes you want to serialize.

  • using System.Xml.Serialization;
  • A parameterless contructor to your class, OR the class simply WON’T serialize
  • [XmlAttribute] and others Attributes
  • [XmlIgnore] in get/set property where can return a class which one you dont want/cannot create a parameterless constructor

If you have some property like this:
public string Name
{
  get { return this.strName; }
}

You must put a empty set otherwise the XML Serializer won’t get it (your class WILL be serialized but without james:
public string Name
{
  get { return this.strName; }
  set { }
}

Using:
XmlDocument xml = myXMLSerializer.SerializeObject(typeof(yourclass), yourinstance));

=)