25 juni 2009

WMB 6.1 @ Ubuntu 9.04


I have WMB 6.1.0.4 with DB2 9.5.0.4 & MQ 7.0.0.2 running on my Ubuntu 9.04 laptop, this blog posting shows what I have done to get that working.


..... Read more .....

6 juni 2009

pQg - New release, new name


Hi, I have released a new release of my SQL to JDO wrapper for use at Google App Engine. Of course the name changes at every release, it's now pQg, PHP with SQL on Google App Engine.

It is still in an early stage, it's a hobby project.

See it live @ Google App Engine

For now this is the Manual :-)
..... Read more .....

1 juni 2009

How to use pQg


Ok, there are still a lot of bugs and most of the functionality is not yet implemented, but here is a HOW-TO posting about using pQg in your own PHP scripts at GAE.

pQg stands for PHP with SQL on Google App Engine, with pQg you can use SQL statements in PHP at Google App Engine.

..... Read more .....

31 mei 2009

pQg = PHP with SQL on GAE


I have made some progress on my SQL to JDO wrapper for PHP at GAE. There is a new example PHP script with lots of neat SQL statements.

The SQL to JDO wrapper has now a new name. It is pQg. What means PHP with SQL on Google App Engine.

Look at below PHP script. Look at the many SQL statements in it, they are working!

<?php

//
// pQg-Demo.php
//

echo "<html><body><pre>";

// Import the JDO classes for the database

import myDatabase.person;

// The database in use (must be the package for above classes)

$pQg_database = 'myDatabase';

// Include the ROM-PHP-GAE source file.

include "WEB-INF/pQg.inc";

// Start our demo with a empty table.

sql("delete from person");

// Fill the database with the test set.

$jan = sql("insert into person (firstName, lastName, email, salary )
values ('Jan', 'Modaal', 'j.modaal@het.net', 2500 ) ");

echo "Added Jan to the database with primary key $jan \n";

$john = sql("insert into person (firstName, lastName, email, salary )
values ('John', 'Doe', 'JOHN@GMAIL.COM', 5000 ) ");

echo "Added John to the database with primary key $john \n";

$jane = sql("insert into person (firstName, lastName, email, salary )
values ('Jane', 'Doe', 'JANE@AOL.COM', 4000 ) ");

echo "Added Jane to the database with primary key $jane \n\n";

// Let's see what is in the table right now

print_table();

// getField, get a single field.

$salary = getField(" select salary from person where firstName = 'John' and lastName = 'Doe' ");

echo "The salary of John is $salary \n\n";

// getRecord, retrieve fields from 1 records.

$record = getRecord(" select strtoupper(FiRsTnAmE) as FIRST, strtoupper(LaStNaMe) as LAST
from person where id = $jane ");

echo "Who is number $jane? It is: " . $record['FIRST'] . ' ' . $record['LAST'] . "\n\n";

// Jane is getting a raise.

sql("update person set salary = salary + 1500 where id = $jane");

// Make url's from the emails

$count = sql("update person set email =
'<a href=\"mailto:' . strtolower(email) . '\">' . strtolower(email) . '</a>'
");

echo "Transform emails: Updated $count record(s) \n\n";

// Jan is fired

$count = sql("delete from person where id = $jan");

echo "Fire Jan: Deleted $count record(s) \n\n";

// This is the end result.

print_table();

// Close the Datastore connection (the open is done on first usage)

pQg_close();

echo "</pre></body></html>"

// Print all records in the person table.

function print_table() {

// getRecords, retrieve n records from the database.

$records = getRecords("select * from person order by salary desc");

echo '<p><table border="1">';

// Print the fields name by using the key's from the first record.

echo "<tr>";
foreach($records[0] as $key => $field)
echo "<td> <b> $key </b> </td>";
echo "</tr>";

foreach ($records as $record) {
echo "<tr>";
foreach($record as $field)
echo "<td> $field </td>";
echo "</tr>";
}

echo "</table></p>";
}

?>
Below the output of this script.
Added Jan to the database with primary key 1
Added John to the database with primary key 2
Added Jane to the database with primary key 3

  email     firstName     lastName     salary     id  
  JOHN@GMAIL.COM     John     Doe     5000     2  
  JANE@AOL.COM     Jane     Doe     4000     3  
  j.modaal@het.net     Jan     Modaal     2500     1  


The salary of John is 5000

Who is number 3? It is: JANE DOE

Transform emails: Updated 3 record(s)

Fire Jan: Deleted 1 record(s)

  email     firstName     lastName     salary     id  
  jane@aol.com     Jane     Doe     5500     3  
  john@gmail.com     John     Doe     5000     2  
I will upload this script and a zip files with sources soon to the GAE site for it. First I must look into indexes. It works fine on the development environment. However on GAE it complains that there are no indexes. Somehow those indexes are not auto-generated on my development machine.

For now you can find the sources here

To use this zip file you must first create a PHP enabled GAE project inside Eclipse and then copy the sources from the zip file to the same directory locations as in the zip file. ( Click here for instructions how to setup an PHP enabled GAE project in Eclipse )
..... Read more .....

27 mei 2009

Creating, retrieving, updating & deleting records in PHP @ GAE


Google uses BigTable as the database in Google App Engine (GAE). This article uses the JDO layer with the Datastore Java API to create, retrieve, update & delete Database records in PHP at Google App Engine.

The starting point for this example is an GAE project in Eclipse with PHP, see this blog posting to set that up.

First create a java class for setting up the database connection, save below file as src/database/connection.java in your Eclipse GAE project.
package database;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

public final class connection {
private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");

private connection() {}

public static PersistenceManagerFactory get() {
return pmfInstance;
}
}
Create a Java class with JDO annotations for every database table you want to use, below the one as used in this article, save it as src/database/person.java in your Eclipse GAE project.
package database;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class person {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long person;

@Persistent
private String firstName;

@Persistent
private String lastName;

public Long getPerson() {
return person;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public void setFirstName(String in) {
this.firstName = in;
}

public void setLastName(String in) {
this.lastName = in;
}

}
Above Java code has the same functionality as below SQL code (please make no nasty comments about the java folks)
CREATE TABLE person (
person BIGINT AUTO_INCREMENT ,
firstName TEXT,
lastName TEXT,
PRIMARY KEY (person)
);
Now we are ready to create PHP scripts that uses GAE BigTable as database! Below the PHP source that will be used as a template for every example.
<?php

echo "<pre>";

// Import the java code for setting up a database connection.
import database.connection;

// Import the table definition class
import database.person;

// Setup a database connection.
$connection = connection::get()->getPersistenceManager();

..... example code goes here .....

// Close the database connection.
$connection->close();

echo "</pre>";

?>
Creating records in the database can be done with below PHP code.
$record = new person;

$record->setFirstName('John');
$record->setLastName('Doe');

$connection->makePersistent($record);

$key = $record->getPerson(); // Not the record but the field person that is primary key

echo "Added 'John Doe' with number $key in the database \n";

$record = new person;

$record->setFirstName('Jane');
$record->setLastName('Doe');

$connection->makePersistent($record);

$key = $record->getPerson(); // Not the record but the field person that is primary key

echo "Added 'Jane Doe' with number $key in the database \n";
Retrieving records from the database, that can be done with below PHP script.
$query = $connection->newQuery("select from database.person");
$results = $query->execute();

foreach($results as $record) {
echo "Key: " . $record->getPerson();
echo " Name: " . $record->getFirstName() . " " . $record->getLastName() . "\n";
}
Above examples prints all records from the table, of course you can change that, use something like below
$query = $connection->newQuery("select from person where person == 5");
$query = $connection->newQuery('select from person where lastName <> "Doe"');
A Update example below.
$query = $connection->newQuery('select from database.person where firstName == "John"');
$results = $query->execute();

$record = $results[0];

$record->setFirstName("Lucky");
$record->setLastName("Luke");

$connection->flush{};
The delete example:
$query = $connection->newQuery('select from database.person where firstName == "Jane"');
$results = $query->execute();

$connection->deletePersistentAll($results);

See here for a proof of concept how you can use 'normal' SQL in PHP at GAE

Sources from this article
..... Read more .....

25 mei 2009

Using PHP on GAE


Google is now supporting 2 languages for Google App Engine: Phyton and Java. This article shows how you can use PHP on Google App Engine.

Running PHP on GAE is possible by using Quercus from Caucho, Quercus implements PHP in Java, see below URL's about running Quercus on GAE>
Assuming you have a Google App Engine website running with Java enabled and also localy installed the GAE Java SDK and the Google Eclipse plugin.

Assuming also that you did the Java Getting Started tutorial to get hands-on experience with the GAE Java SDK and the Google Eclipse Plugin.

Then you can also get PHP running on GAE with below steps.

1) Create a Google Web Application inside Eclipse

Optional: De-select GWT on the "New Web Application project" wizzard.

2) Copy resin.jar from Resin 4.0 to WEB-INF/lib

Add resin.jar to the java build path: Right-click on the project-name, select 'properties' then 'Java build path' and the add resin.jar under 'Libraries'

3) Modify war/WEB-INF/web.xml so that it contains (also) below content
<welcome-file-list>
<welcome-file>index.php</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>quercus</servlet-name>
<servlet-class>com.caucho.quercus.servlet.GoogleQuercusServlet<servlet-class>
</servlet>

<servlet-mapping>
<url-pattern>*.php</url-pattern>
<servlet-name>quercus</servlet-name>
</servlet-mapping>
4) Add below lines to war/WEB-INF/appengine-web.xml
<static-files>
<exclude path="/**.php" />
</static-files>

<resource-files>
<include path="/**.php" />
</resource-files>
5) Create the war/index.php file.
<?php 

phpinfo();

?>
6) Run the development server.

7) Surf to: http://localhost:8080/

..... Read more .....

Using the GAE log with PHP


With a very small java source you can use the GAE log with PHP. You can see this log on the console tab in Eclipse, or with the GAE Dashboard. It is also possible to download the logs from GAE with the appcfg.sh script.

This article shows you how you can use this log in your PHP scripts running on Google App Engine.

The usage in your PHP scripts is:
logInfo("I just want to say Hello!");
logWarning("Not good, xxx is not supported");
logSevere("Big big troubles");
First add resin.jar to the build path for your GAE project in Eclipse with properties -> Java build path -> Libraries

Below the Java code, save it as src/gae/php_log.java in your GAE project in Eclipse.
package gae;

import java.util.logging.Logger;
import com.caucho.quercus.module.AbstractQuercusModule;

public class php_log extends AbstractQuercusModule {

private static final Logger log = Logger.getLogger(php_log.class.getName());

public void logInfo(String message) { log.info(message); }
public void logWarning(String message) { log.warning(message); }
public void logSevere(String message) { log.severe(message); }

}
To make Quercus aware of your new log fuctions, add below line to the file src/META-INF/services/com.caucho.quercus.QuercusModule (create this file if it not yet exists)
 gae.php_log
To activate the INFO severity (default it is at WARNING severity), add below line to the war/WEB-INF/logging.properties file.
 gae.php_log.level=INFO
See quercus: java and php integration for more information how to write your own PHP functions with Java for Quercus PHP on GAE.
..... Read more .....

Sending email with PHP at GAE

Currently sending email from PHP scripts on Google App Engine is not working. Most likely this will be possible in the future again when the nice folks of Caucho adjust Quercus to the GAE sandbox.

This article shows how to use a small Java program for sending email from PHP at Google App Engine.

With using a small java source you can send email from your PHP scripts at GAE with below line:
 gaeMail('me@gmail,com', 'someone@inter.net', 'Subject', 'Hi, how are you?');
First add resin.jar to the build path for your GAE project in Eclipse with properties -> Java build path -> Libraries

Below the Java code, save it as src/gae/email.java in your GAE project in Eclipse.
package gae;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.caucho.quercus.module.AbstractQuercusModule;

public class email extends AbstractQuercusModule {

public void gaeMail(String from, String to, String subject, String body)

throws java.io.IOException {

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(body);
Transport.send(msg);
} catch (AddressException e) {
// ...
} catch (MessagingException e) {
// ...
}

}

}
To make Quercus aware of your new email fuction, add below line to the file src/META-INF/services/com.caucho.quercus.QuercusModule (create this file if it not yet exists)
 gae.email
See quercus: java and php integration for more information how to write your own PHP functions with Java for Quercus PHP on GAE.
..... Read more .....

24 mei 2009

SQL to JDO for PHP at GAE


Started a project that makes it possible to use 'normal' SQL like below in PHP scripts at Google App Engine.
sql("insert into demo (field1, field2) values(1, 'Yes it works')");

$rows = sql("select demo, field1, field2 from table order by demo desc");

sql("update demo set field1 = $field1 where demo = $demo");

sql("delete from demo where demo = $demo");

There is now a new version, see below blog postings about it.

pQg- Use SQL in PHP scripts at GAE

How to use pQg
..... Read more .....