Lazarus

Programming => Networking and Web Programming => Topic started by: jamhitz on January 30, 2013, 07:51:28 pm

Title: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: jamhitz on January 30, 2013, 07:51:28 pm
Team,

I have been playing around with Brook Framework and I must say I am thoroughly impressed so far!! 8-)

I have a question: Most (all?) of the demos have a SqlLite3 schema and implementation; a number may have PostgreSQL schemas but no implementation; nothing at all for the other DBMS... but of course the whole point of a demo is to pick up from there and figure it out. However, I cannot figure out how to use SqlDB to connect to any other database but SQLLite:

Code: [Select]
BrookSettings.Configuration := 'library=sqldb;driver=pqconnection;host=127.0.0.1;user=USERNAME;password=PASSWORD;database=TESTDB';
I have tried replacing the driver with PQ, Postgres, PostgreSQL, MySQL55, MySQL51.. (of course each to its respective server) nothing works. However dropping a TPQConnection or a TMysql5*conn on a form and setting the proper parameters works fine.

What am I missing?

Thanks.

Kind regars
JamHitz
Title: Re: Brook 1.0 - SQLdb Driver
Post by: Silvio Clécio on January 30, 2013, 07:56:08 pm
Hello friend,

In brokers unit, just declare your connection unit, eg:

https://github.com/silvioprog/brook-site/blob/master/src/release/brokers.pas

Good luck! :)
Title: Re: Brook 1.0 - SQLdb Driver
Post by: Silvio Clécio on January 30, 2013, 07:57:17 pm
... and change your configuration to:

Code: [Select]
BrookSettings.Configuration := 'library=sqldb;driver=postgresql;host=127.0.0.1;user=USERNAME;password=PASSWORD;database=TESTDB';
Title: Re: Brook 1.0 - SQLdb Driver
Post by: jamhitz on January 30, 2013, 08:09:18 pm
@SilvioProg

Eureka!!!! Can you see the smile?   :)  I am intent on writing a small (VEEEEERY basic) 101 tutorial on using  Brook for web programming using FPC/Laz. I hope in this way I will play my little part in making Brook what I  believe it has the potential to become - the number one Web Programming Framework for FPC/Laz. In the meantime I stumbling, falling and finding my way around using the demos and the documentation.

Thank you.

Kind regards
JamHitz
Title: Re: [SOLVED] Brook 1.0 - SQLdb Driver
Post by: Silvio Clécio on January 30, 2013, 08:29:19 pm
Very nice friend! I saw the smile. :)

What the link to the site where you will post? We may publish it in the official community of Lazarus on Facebook, GooglePlus etc.
Title: Re: [SOLVED] Brook 1.0 - SQLdb Driver
Post by: jamhitz on January 30, 2013, 08:37:46 pm
I will most probably post it to the Lazarus wiki and/or on the BrookFramework website.
Title: Re: [SOLVED] Brook 1.0 - SQLdb Driver
Post by: Silvio Clécio on January 30, 2013, 08:39:51 pm
Great!  ;)

It will be posted in future blog of Brook too! :-X
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: jamhitz on January 31, 2013, 05:35:05 pm
I can now be able to connect to a DB. However, attempting to use TBrookTable or TBrookQuery (by alternating the commented lines):

Code: [Select]
procedure TMyAction.Get;
var db: TBrookDatabase;
    q: TBrookQuery;
    t: TBrookTable;
begin
    BrookSettings.Configuration := 'library=sqldb;driver=postgresql;host=SERVER;user=USER;password=PASS;database=db';
    try
        db := TBrookDatabase.Create;
        //q := TBrookQuery.Create(db);
        t := TBrookTable.Create(db, 'gateway');
        try
            WriteLn(t.AsJSON);

            //q.SQL.Text := 'SELECT id,name,description from gateway';
            //q.Open;
            //WriteLn(q.AsJSON);
        except
            on E:Exception do
                Write('ERROR:' + E.Message);
        end;
    finally
        FreeAndNil(db);
        FreeAndnil(q);
        FreeAndNil(t);
    end;

...brings up an error Premature end of script headers: cgi3 .

Ideas?

Thank you.
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: Silvio Clécio on February 02, 2013, 04:38:50 am
Don't use BrookSettings.Configuration in actions, use it on the brokers unit.

You can facilitate decreasing the coding:

...
TMyAction = class(TBrookDBAction)
...

procedure TMyAction.Get;
begin
  Write(Table.Open.AsJSON); // or Table.AsJSON directly
end;
...
initialization
  TMyAction.Register('gateway', '*');
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: jamhitz on February 02, 2013, 11:55:05 am
@Silvio

I've created a brand new project that uses TBrookDBAction instead of TBrookAction, but I still get the same error. Here is the broker:

Code: [Select]
unit brokers007;

{$mode objfpc}{$H+}

interface

uses
  BrookFCLCGIBroker, BrookUtils, BrookSQLdbBroker, PQConnection;

implementation

initialization
BrookSettings.Charset := 'library=sqldb;driver=postgresql;host=127.0.0.1;user=USERNAME;password=PASSWORD;database=TESTDB';
BrookSettings.Page404 := '404.html';
BrookSettings.Page500 := '500.html';
end.

..and here is the action:

Code: [Select]
unit umain007;

{$mode objfpc}{$H+}

interface

uses
  BrookDBAction, fpJSON;

type
  TMyAction = class(TBrookDBAction)
  public
      procedure Get; override;
  end;

implementation

{ TMyAction }

procedure TMyAction.Get;
begin
    Write(Table.Open.AsJSON);
end;

initialization
  TMyAction.Register('gateway','*');

end.

See the attachment for the response.

JamHitz
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: Silvio Clécio on February 05, 2013, 07:02:38 pm
Hello, sorry for my delay.

So, try to run the demo "\repository\git\brookframework\demos\simple\helloworld\cgi\cgi1.lpi". I need to find out if the error is in your DB or in your HTTP server.
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: jamhitz on February 06, 2013, 10:54:24 pm
Hi

I have tested MOST of the demos in the /simple, /http and /others sub-directories.  They all work as expected. It is those in the /db sub-directory that are a problem.

OpenSuse 12/Lazarus 1.1/fpc 2.7/PostgreSQL 9.1/MySQL 5.5

JamHitz
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: Silvio Clécio on February 07, 2013, 12:16:19 am
You can connect using pure SQLdb? Take a test, add a SQLConnector1 in a form and try to connect.
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: jamhitz on February 07, 2013, 05:27:05 am
Yes, a regular SQLdb-based desktop application works just fine accessing the very same database with the very same credentials.

In fact CGI applications based of fpWeb and FreeSpider are accessing the database ok.
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: Silvio Clécio on February 07, 2013, 11:40:56 am
I don't know what can be, because I could not reproduce the error.

Download this demo:

http://www.sendspace.com/file/wkajve

And follow the steps below:

1. # zypper install sqlite3-devel ;
2. Open and run the project "project1.lpi";
3. Call http://localhost in your browser;

This allow you to set a break point and debug your application, running the it step by step with F8.
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: jamhitz on February 07, 2013, 04:03:15 pm
Thank you. I can see some progress. When I compile the program and run it off the terminal, my browser is able to get the JSON object fetched from the database (I tested the DB using Database Desktop).

However, I cannot run the app from my IDE, since it is attempting to bind to Port 80, and my Lazarus does not run as root. How do I change the port on which the app binds so that I can debug the code by stepping using a regular user account?

JamHitz
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: Silvio Clécio on February 07, 2013, 05:52:49 pm
Hm... without error in the embedded server, we will not need to debug with this technique. :)

Are you using apache? Port 80?

Execute the commands below:

Code: [Select]
# zypper install postgresql-devel
# zypper install sqlite3-devel

It will install client libs of your database servers.
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: jamhitz on February 08, 2013, 10:34:56 am
Apache is installed, the *-devel packages are installed; and of course when testing the project, Apache is off, and the Firewall is off. But, because I am running this as a regular user, I cannot access port 80 (ports < 1024) when running from the Lazarus IDE. However, if I run the compiled program as root, I can then access my application on port 80.

What I need to do is to change the port to something like 8080 so that I can be able to debug the program.

I know I can set re-routing in the iptables but I was hoping to not have to do this.

Thank you.
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: Silvio Clécio on February 13, 2013, 04:02:37 pm
Hello,

I think that I found your problem.

See this picture:

http://imagebin.org/246565

So, use BrookSettings.Configuration instead BrookSettings.Charset. :)
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: jamhitz on February 15, 2013, 10:16:13 pm
Hello Silvio

You are right. I think I trusted Code Assist a little too much to counter-check.  It works ok now. I'll finally get the tutorial going. Thank you.
Title: Re: Brook 1.0 - SQLdb Driver [RE-OPENED]
Post by: Silvio Clécio on February 18, 2013, 02:14:03 am
Nice! :)
TinyPortal © 2005-2018