#php #php #string
/##
 #  文字列を全て半角に
 #  @param  string $str 文字列
 #  @return string $str 半角の文字列
 # /
function toHankaku($str) {
    $str= mb_convert_kana($str,"rnask");
    return $str;
}
#php #php

$_GET、 $_POST そして $_COOKIE の内容をまとめた連想配列です。

もう少し説明を加えると、

In the beginning of execution, $_REQUEST is a clone of $_GET. $_POST is then merged into the array, overwriting keys if they exist in both $_GET and $_POST. Finally, $_COOKIE is merged into the array, again overwriting old values.

まずは$_GETのクローンでそこに$_POSTをマージする感じで、同じキーのものがあれば上書きされます。同じくその後$_COOKIEもマージします。 なので本当にPOSTで受取るものは$_REQUESTに書き換えてはいけないと思いますね。

#mysql #sql

1000件くらいのinsert文を用意して、それをコピって一気にmysqlのコマンドラインに貼りつけたらうまく行かなかったんです。sql文に文法的間違いがなくてもエラーとなったり、表示が崩れたりしました。数件なら多分まだいいんですけど、さすが1000件だと(コマンドラインに貼り付けるやり方では)処理が追えない気がします。

それらのsql文をファイルに保存し実行すれば問題なかったので、以下がそのコマンドです。

mysql> source file_name
--あるいは
mysql> \. file_name

また下記文を書けば進捗情報が見れるとMySQLのレファレンスで書かれてますが、時間の関係上今回は試してませんでした。

SELECT '<info_to_display>' AS ' ';

参考: http://dev.mysql.com/doc/refman/5.0/en/batch-commands.html

#mysql #sqlite #transaction

autocommitの値を持ちましてMySQLでのトランザクションを制御できます。

--暗黙(自動)コミットを無効に
SET AUTOCOMMIT = 0;
--トランザクション開始
BEGIN;

--SQL文をながす
UPDATE...
INSERT...

--コミット
COMMIT;
--あるいはロールバック
ROLLBACK;

--暗黙(自動)コミットを有効に
SET AUTOCOMMIT = 1;

--暗黙(自動)コミットのステータスを確認
SELECT @@AUTOCOMMIT;

DataSource Beanを定義する際applicationContext.xmlの書き方と、Javaで呼び出すコードのメモです。ずいぶん昔のメモです。。

こんなjdbc.propertiesファイルがクラスパスにあるとします。

jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:~/test
jdbc.username=sa
jdbc.password=

そしてSpringのapplicationContext.xmlにはこう書きます。

<context:property-placeholder location="jdbc.properties"/>

<bean id="dataSource"
	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="${jdbc.driverClassName}" />
	<property name="url" value="${jdbc.url}" />
	<property name="username" value="${jdbc.username}" />
	<property name="password" value="${jdbc.password}" />
</bean>


<bean id="accountDao" class="test.dao.AccountDao">
	<property name="dataSource" ref="dataSource" />
</bean>

Javaでの呼び出し:

ApplicationContext context = new ClassPathXmlApplicationContext(
		"applicationContext.xml");

AccountDao accountDao = (AccountDao) context.getBean("accountDao");

// other code...

一つ以上の項目がPKとなったケースでHibernateはどう設定すればいいのかを説明します。 ずいぶん昔のメモになります。

こんなテーブルがあるとします。

create table Account (
	code varchar(255) not null,
	number integer not null,
	description varchar(255),
	primary key (code, number)
);

PKは’code’と’number’です。

方法は三つあります

まずPKを表すクラスを作ります。PKクラスは以下の条件を満足しなければなりません。

  • publicクラス
  • デフォルトのコンストラクタ
  • シリアライズを実装
  • hashCode()とequals()を実装

そしてエンティティクラスは以下の三つの方法のうちどれかで実装します。

  • PKクラスを@Embeddableアノテーションで記述し、エンティティクラスのプロパティとして書いて@Idとマーク
  • PKクラスをエンティティクラスにプロパティとして書いて@EmbeddableIdとマーク
  • PKを表す全ての項目をエンティティクラスのプロパティとして登録し@Idとマーク

それぞれのソースコードをリストします。

一番目@Embeddable

ここはAccountとそのPKを表すクラスAccountPkを作成しました。 メリットはPKクラスを再利用できるところです。 もっとも自然的なアプローチだそうです。

package sample.annotations;

import java.io.Serializable;

import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Account {
	private String description;
	private AccountPk id;

	public Account(String description) {
		this.description = description;
	}

	protected Account() {
	}

	@Id
	public AccountPk getId() {
		return this.id;
	}

	public String getDescription() {
		return this.description;
	}

	public void setId(AccountPk id) {
		this.id = id;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Embeddable
	public static class AccountPk implements Serializable{
		private String code;
		private Integer number;

		public AccountPk() {
		}

		public String getCode() {
			return this.code;
		}

		public Integer getNumber() {
			return this.number;
		}

		public void setNumber(Integer number) {
			this.number = number;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public int hashCode() {
			int hashCode = 0;
			if (code != null)
				hashCode ^= code.hashCode();
			if (number != null)
				hashCode ^= number.hashCode();
			return hashCode;
		}

		public boolean equals(Object obj) {
			if (!(obj instanceof AccountPk))
				return false;
			AccountPk target = (AccountPk) obj;
			return ((this.code == null) ? (target.code == null) : this.code
					.equals(target.code))
					&& ((this.number == null) ? (target.number == null)
							: this.number.equals(target.number));
		}
	}
}

検証するクラスを作成しました。

package sample.annotations;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import sample.annotations.Account.AccountPk;

public class TestAccount {
	public static void main(String** args) {
		SessionFactory sessionFactory = new AnnotationConfiguration()
				.configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();

		Account account = new Account("This is the first type.");

		// construct pk value
		AccountPk accountPk = new AccountPk();
		accountPk.setCode("kinopyo001");
		accountPk.setNumber(12345);
		// set pk
		account.setId(accountPk);

		// save
		session.save(account);
		session.getTransaction().commit();
		System.out.println("Commit");

		// load
		List list = session.createQuery("from Account").list();
		Iterator i = list.iterator();
		while (i.hasNext()) {
			Account a = (Account) i.next();
			System.out.println("code: " + a.getId().getCode() + ", number: "
					+ a.getId().getNumber() + "  Description: "
					+ a.getDescription());
		}
		session.close();

	}
}

設定が正しければこんなログが出るはずです。

Commit
code: kinopyo001, number: 12345  Description: This is the first type.

二番目@EmbeddableId(抜粋)

@EmbeddedId
public AccountPk getId() {
	return this.id;
}

三番目@IdClass(抜粋)

@Entity
@IdClass(Account.AccountPk.class)
public class Account {

	private String description;
	private String code;
	private Integer number;

	public Account(String description) {
		this.description = description;
	}

	protected Account() {
	}

	@Id
	public String getCode() {
		return this.code;
	}

	@Id
	public Integer getNumber() {
		return this.number;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setNumber(Integer number) {
		this.number = number;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public static class AccountPk {
	// ...
}