001.
<?
002.
# **********************************************************
003.
# database
class
( 2009/09/14 )
004.
# キャラクタセット変換を利用する場合は、
005.
# 本体で以下を実行して下さい
006.
# 1) mb_language(
"ja"
);
007.
# 2) mb_internal_encoding(
"UTF-8"
);
008.
# または
009.
# 2') mb_internal_encoding(
"EUC-JP"
);
010.
# **********************************************************
011.
class
Oracle {
012.
013.
var
$Connect
;
014.
var
$Result
;
015.
016.
var
$nField
;
017.
var
$nRow
;
018.
019.
var
$Debug
;
020.
var
$Error
;
021.
var
$Work
;
022.
023.
var
$Cn
;
024.
var
$Rs
;
025.
var
$ConnectionString
;
026.
027.
# **********************************************************
028.
# Microsoft の仕様そのままで良いので、
029.
# ドライバ指定も可能
030.
# **********************************************************
031.
function
Oracle(
$Server
=
'default'
,
$User
=
'default'
,
$Password
=
'default'
) {
032.
033.
$Server
=
$Server
==
'default'
?
$GLOBALS
[
'conf_db_host'
] :
$Server
;
034.
$User
=
$User
==
'default'
?
$GLOBALS
[
'conf_db_user'
] :
$User
;
035.
$Password
=
$Password
==
'default'
?
$GLOBALS
[
'conf_db_pass'
] :
$Password
;
036.
037.
$this
->Cn =
new
COM(
"ADODB.Connection"
);
038.
$this
->Cn->CursorLocation = 3;
039.
$this
->Rs =
new
COM(
"ADODB.Recordset"
);
040.
041.
$ConnectionString
=
"Provider=MSDASQL;"
;
042.
$ConnectionString
.=
"DSN=$Server;"
;
043.
$ConnectionString
.=
"UID=$User;"
;
044.
$ConnectionString
.=
"PWD=$Password;"
;
045.
046.
$this
->Cn->Open(
$ConnectionString
);
047.
if
(
$GLOBALS
[
'conf_db_connect_action'
] !=
''
) {
048.
$this
->Cn->Execute(
$GLOBALS
[
'conf_db_connect_action'
] );
049.
}
050.
051.
$this
->Debug = FALSE;
052.
}
053.
054.
# **********************************************************
055.
# 接続解除
056.
# **********************************************************
057.
function
Close( ) {
058.
059.
@
$this
->Cn->Close();
060.
061.
}
062.
063.
# **********************************************************
064.
# select の実行
065.
# **********************************************************
066.
function
Query(
$SqlQuery
) {
067.
068.
069.
070.
if
(
$GLOBALS
[
'conf_db_charset'
] !=
''
) {
071.
$SqlQuery
= mb_convert_encoding(
072.
$SqlQuery
,
073.
$GLOBALS
[
'conf_db_charset'
],
074.
$GLOBALS
[
'conf_client_charset'
]
075.
);
076.
}
077.
078.
079.
if
(
$this
->Rs->State >= 1 ) {
080.
$this
->Rs->Close();
081.
}
082.
083.
$this
->Rs->Open(
$SqlQuery
,
$this
->Cn );
084.
085.
$ret
= !
$this
->Rs->EOF;
086.
087.
return
$ret
;
088.
}
089.
090.
# **********************************************************
091.
# データの読み出し
092.
# PHP で使う一般的な仕様に合わせて、連想配列化
093.
# **********************************************************
094.
function
Fetch(
$Result
) {
095.
096.
097.
$ret
=
array
();
098.
for
(
$i
= 0;
$i
<
$this
->Rs->Fields->
count
;
$i
++ ) {
099.
100.
if
(
$this
->Rs->Fields[
$i
]->type == 7 ) {
101.
102.
if
(
substr
(phpversion(),0,1) ==
'4'
) {
103.
$test
=
date
(
"H:i:s"
,
$this
->Rs->Fields[
$i
]->value );
104.
if
(
$test
==
"00:00:00"
) {
105.
106.
$ret
[
$i
] =
date
(
107.
"Y/m/d"
,
108.
$this
->Rs->Fields[
$i
]->value
109.
);
110.
$ret
[
$this
->Rs->Fields[
$i
]->name] =
date
(
111.
"Y/m/d"
,
112.
$this
->Rs->Fields[
$i
]->value
113.
);
114.
}
115.
else
{
116.
$ret
[
$i
] =
date
(
117.
"Y/m/d H:i:s"
,
118.
$this
->Rs->Fields[
$i
]->value
119.
);
120.
$ret
[
$this
->Rs->Fields[
$i
]->name] =
date
(
121.
"Y/m/d H:i:s"
,
122.
$this
->Rs->Fields[
$i
]->value
123.
);
124.
}
125.
}
126.
127.
else
{
128.
$ret
[
$i
] =
129.
$this
->Rs->Fields[
$i
]->value;
130.
$ret
[
$this
->Rs->Fields[
$i
]->name] =
131.
$this
->Rs->Fields[
$i
]->value;
132.
}
133.
}
134.
else
{
135.
$ret
[
$i
] =
136.
$this
->Rs->Fields[
$i
]->value;
137.
$ret
[
$this
->Rs->Fields[
$i
]->name] =
138.
$this
->Rs->Fields[
$i
]->value;
139.
}
140.
}
141.
142.
143.
144.
if
(
$GLOBALS
[
'conf_db_charset'
] !=
''
) {
145.
146.
if
(
$ret
) {
147.
$ret2
=
array
();
148.
while
(list(
$Key
,
$Value
) = @each(
$ret
)) {
149.
$ret2
[
$Key
] = mb_convert_encoding(
150.
$Value
,
151.
$GLOBALS
[
'conf_client_charset'
],
152.
$GLOBALS
[
'conf_db_charset'
]
153.
);
154.
$Key2
= mb_convert_encoding(
155.
$Key
,
156.
$GLOBALS
[
'conf_client_charset'
],
157.
$GLOBALS
[
'conf_db_charset'
]
158.
);
159.
$ret2
[
$Key2
] = mb_convert_encoding(
160.
$Value
,
161.
$GLOBALS
[
'conf_client_charset'
],
162.
$GLOBALS
[
'conf_db_charset'
]
163.
);
164.
}
165.
}
166.
167.
}
168.
else
{
169.
$ret2
=
$ret
;
170.
}
171.
172.
return
$ret2
;
173.
}
174.
175.
# **********************************************************
176.
# フィールド名を取得
177.
# **********************************************************
178.
function
FieldName(
$idx
) {
179.
$ret
=
$this
->Rs->Fields[
$idx
]->name;
180.
if
(
$GLOBALS
[
'conf_db_charset'
] !=
''
) {
181.
$ret
= mb_convert_encoding(
182.
$ret
,
183.
$GLOBALS
[
'conf_client_charset'
],
184.
$GLOBALS
[
'conf_db_charset'
]
185.
);
186.
}
187.
return
$ret
;
188.
}
189.
190.
# **********************************************************
191.
# 外部から使うループを想定した SQL 実行
192.
# **********************************************************
193.
function
QueryEx(
$SqlQuery
=
''
) {
194.
195.
196.
if
(
$SqlQuery
!=
''
) {
197.
if
(
$this
->Debug ) {
198.
print
"<TABLE border=0 cellpadding=5>"
;
199.
print
"<TH align=left bgcolor=skyblue><pre>"
.
200.
$this
->Arrange(
$SqlQuery
) .
"</pre></TD>"
;
201.
print
"</TABLE>"
;
202.
}
203.
204.
205.
if
(
substr
(
$SqlQuery
, 0, 7 ) ==
"COLUMNS"
) {
206.
$table_name
=
Explode
(
" "
,
$SqlQuery
);
207.
$this
->Rs =
$this
->Cn->OpenSchema( 4 );
208.
if
(
$GLOBALS
[
'conf_db_charset'
] !=
''
) {
209.
$tbl
= mb_convert_encoding(
210.
$table_name
[1],
211.
$GLOBALS
[
'conf_db_charset'
],
212.
$GLOBALS
[
'conf_client_charset'
]
213.
);
214.
}
215.
else
{
216.
$tbl
=
$table_name
[1];
217.
}
218.
$this
->Rs->Filter =
"TABLE_NAME = '"
.
$tbl
.
"'"
;
219.
$this
->Rs->Sort =
"ORDINAL_POSITION"
;
220.
}
221.
222.
else
{
223.
$this
->Result =
$this
->Query(
$SqlQuery
);
224.
if
( !
$this
->Result ) {
225.
return
FALSE;
226.
}
227.
}
228.
229.
230.
$this
->nField =
$this
->Rs->Fields->
count
;
231.
$this
->nRow =
$this
->Rs->RecordCount;
232.
233.
return
$this
->Fetch (
$this
->Result );
234.
}
235.
236.
else
{
237.
$this
->Rs->MoveNext();
238.
if
(
$this
->Rs->EOF ) {
239.
return
FALSE;
240.
}
241.
return
$this
->Fetch (
$this
->Result );
242.
}
243.
244.
}
245.
246.
# **********************************************************
247.
# レコードセットを返さない SQL の実行
248.
# **********************************************************
249.
function
Execute(
$SqlExec
) {
250.
251.
if
(
$this
->Debug ) {
252.
print
"<TABLE border=0 cellpadding=5>"
;
253.
print
"<TH align=left bgcolor=skyblue><pre>"
.
254.
$this
->Arrange(
$SqlExec
) .
"</pre></TD>"
;
255.
print
"</TABLE>"
;
256.
}
257.
258.
259.
260.
if
(
$GLOBALS
[
'conf_db_charset'
] !=
''
) {
261.
$SqlExec
= mb_convert_encoding(
262.
$SqlExec
,
263.
$GLOBALS
[
'conf_db_charset'
],
264.
$GLOBALS
[
'conf_client_charset'
]
265.
);
266.
}
267.
268.
$this
->Cn->Execute(
$SqlExec
);
269.
270.
271.
$ret
= TRUE;
272.
273.
return
$ret
;
274.
}
275.
276.
277.
# **********************************************************
278.
# 内部関数
279.
# **********************************************************
280.
function
Arrange(
$target
) {
281.
$ret
=
str_replace
(
','
,
"\n\t,"
,
$target
);
282.
$ret
=
str_replace
(
'set '
,
"set\n\t"
,
$ret
);
283.
$ret
=
str_replace
(
'where '
,
"\nwhere "
,
$ret
);
284.
return
"\n$ret"
;
285.
}
286.
}
287.
?>