PxPlugin_DBML /plugins/DBML/register/object.php

Language PHP Lines 391
MD5 Hash 7ac45ad4d6feb16fef8a14a4a8158cea
Repository https://github.com/tomk79/PxPlugin_DBML.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php

/**
 * PX Plugin "DBML"
 * @author Tomoya Koyanagi.
 */
class pxplugin_DBML_register_object{

	private $px;
	private $path_dbml;
	private $database_define;

	/**
	 * コンストラクタ
	 */
	public function __construct($px){
		$this->px = $px;

		$this->path_dbml = $px->get_conf('plugin-DBML.path_dbml');
		$this->parse_dbml();
	}//__construct()

	/**
	 * factory: XML DOM Parser
	 */
	private function factory_dom_parser( $src ){
		$class_name = $this->px->load_px_plugin_class( '/DBML/libs/PxXMLDomParser/PxXMLDomParser.php' );
		$dom_parser = new $class_name( $src, 'bin' );
		return $dom_parser;
	}//factory_dom_parser()

	/**
	 * DBMLファイルをパースして、内容を整理する。
	 */
	private function parse_dbml(){
		$this->database_define = array();
		$this->database_define['tables'] = array();

		$src = $this->px->dbh()->file_get_contents( $this->path_dbml );
		$dom_parser = $this->factory_dom_parser($src);
		$tables = $dom_parser->find('dbml > tables > table');
		foreach($tables as $table){
			$table_info = array();
			$table_info['name']         = $table['attributes']['name']        ;
			$table_info['logical_name'] = $table['attributes']['logical-name'];
			$table_info['skip_create']  = $table['attributes']['skip-create'] ;
			$table_info['columns'] = array();

			//  値のチューニング
			$table_info['name'] = $this->bind_meta_string($table_info['name']);
			$table_info['skip_create'] = $this->judge_boolean($table_info['skip_create']);
			//  / 値のチューニング

			$dom_parser = $this->factory_dom_parser($table['innerHTML']);
			$columns = $dom_parser->find('column');
			foreach($columns as $column){
				$column_info = array();
				$column_info['name']         = $column['attributes']['name']        ;
				$column_info['logical_name'] = $column['attributes']['logical-name'];
				$column_info['type']         = $column['attributes']['type']        ;
				$column_info['size']         = $column['attributes']['size']        ;
				$column_info['not_null']     = $column['attributes']['not-null']    ;
				$column_info['default']      = $column['attributes']['default']     ;
				$column_info['key_type']     = $column['attributes']['key-type']    ;
				$column_info['foreign_key']  = $column['attributes']['foreign-key'] ;
				$column_info['unique']       = $column['attributes']['unique']      ;

				//  値のチューニング
				$column_info['name'] = $this->bind_meta_string($column_info['name']);
				$column_info['size'] = (strlen($column_info['size'])?intval($column_info['size']):null);
				$column_info['unique'] = $this->judge_boolean($column_info['unique']);
				$column_info['not_null'] = $this->judge_boolean($column_info['not_null']);
				$column_info['foreign_key'] = $this->bind_meta_string($column_info['foreign_key']);
				//  / 値のチューニング

				array_push( $table_info['columns'], $column_info );
			}

			array_push( $this->database_define['tables'], $table_info );
		}

		return true;
	}//parse_dbml()

	/**
	 * 埋め込み情報をバインドする
	 */
	private function bind_meta_string( $text ){
		if(!is_string($text)){return $text;}
		$text = preg_replace( '/\{\$prefix\}/' , $this->px->get_conf('dbms.prefix') , $text );
		return $text;
	}//bind_meta_string()

	/**
	 * ブール型となる予定の入力値を評価し、結論を返す
	 */
	private function judge_boolean( $text ){
		switch(strtolower($text)){
			case 'true':
				$text = true; break;
			default:
				$text = false; break;
		}

		return $text;
	}//judge_boolean()

	/**
	 * データベース定義配列を取り出す。
	 */
	public function get_db_definition(){
		return $this->database_define;
	}//get_db_definition()

	/**
	 * テーブル定義配列を取り出す。
	 */
	public function get_table_definition( $table_name ){
		$table_name = $this->bind_meta_string($table_name);
		foreach( $this->database_define['tables'] as $table_info ){
			if($table_info['name'] == $table_name){
				return $table_info;
			}
		}
		return false;
	}//get_table_definition()

	/**
	 * テーブルのレコード数を数える
	 */
	public function get_count_table_rows( $table_name ){
		$table_name = $this->bind_meta_string($table_name);

		ob_start();?>
SELECT count(*) AS count FROM :D:table_name;
<?php
		$sql = @ob_get_clean();

		$bind_data = array(
			'table_name'=>$table_name,
		);
		$sql = $this->px->dbh()->bind( $sql , $bind_data );
		$res = $this->px->dbh()->send_query( $sql );
		if( !$res ){
			return false;
		}
		$value = $this->px->dbh()->get_results();

		return intval($value[0]['count']);
	}//get_count_table_rows()

	/**
	 * テーブルに行を追加する
	 */
	public function insert( $table_name, $row ){
		$table_name = $this->bind_meta_string($table_name);

		$table_definition = $this->get_table_definition($table_name);
		$ary_key = array();
		$ary_val = array();
		$bind_data = array();
		foreach( $table_definition['columns'] as $column_info ){
			//  シリアル型(=auto inclement)は、指定できない。
			if($column_info['type'] == 'series'){
				continue;
			}

			//  VARCHARマスターキーを自動生成
			if( $column_info['key_type'] == 'primary' && $column_info['type'] == 'varchar' && is_null( $row[$column_info['name']] ) ){
				$row[$column_info['name']] = uniqid();
			}

			//  メタ文字の接頭辞を決める
			$type_prefix = ':S:';
			switch($column_info['type']){
				case 'int':
				case 'serial':
					$type_prefix = ':N:';
					break;
				default:
					$type_prefix = ':S:';
			}

			//  NOT NULL の処理
			if( $column_info['not_null'] && is_null($row[$column_info['name']])){
				if( !is_null($column_info['default']) ){
					$row[$column_info['name']] = $column_info['default'];
				}else{
					//  NOT NULL でかつ DEFAULT が指定されていないカラムに
					//  NULL を入れようとした場合は、続行不能。
					return false;
				}
			}

			//  DATETIME型の "NOW" を処理。
			if( $column_info['type'] == 'datetime' && strtolower($row[$column_info['name']]) == 'now' ){
				$row[$column_info['name']] = date('Y-m-d H:i:s');
			}

			//  要素をセット
			$bind_data[$column_info['name']] = $row[$column_info['name']];
			array_push($ary_key,$column_info['name']);
			array_push($ary_val,$type_prefix.$column_info['name']);

		}

		$sql = '';
		$sql .= 'INSERT ';
		$sql .= 'INTO '.$this->px->dbh()->bind( ':D:table_name' , array('table_name'=>$table_name) ).' ';
		$sql .= '('.implode(',',$ary_key).') ';
		$sql .= 'VALUES ('.implode(',',$ary_val).')';
		$sql .= ';';

		$sql = $this->px->dbh()->bind( $sql , $bind_data );
		$res = $this->px->dbh()->send_query( $sql );
		if( !$res ){
			return false;
		}

		return true;
	}//insert()

	/**
	 * テーブルの行を書き換える
	 */
	public function update( $table_name, $conditions, $row ){
		$table_name = $this->bind_meta_string($table_name);

		$table_definition = $this->get_table_definition($table_name);
		$ary_conditions = array();
		$ary_key_val = array();
		$bind_data = array();
		foreach( $table_definition['columns'] as $column_info ){
			//  メタ文字の接頭辞を決める
			$type_prefix = ':S:';
			switch($column_info['type']){
				case 'int':
				case 'serial':
					$type_prefix = ':N:';
					break;
				default:
					$type_prefix = ':S:';
			}

			//  DATETIME型の "NOW" を処理。
			if( $column_info['type'] == 'datetime' ){
				if( strtolower($row[$column_info['name']]) == 'now' ){ $row[$column_info['name']] = date('Y-m-d H:i:s'); }
				if( strtolower($conditions[$column_info['name']]) == 'now' ){ $conditions[$column_info['name']] = date('Y-m-d H:i:s'); }
			}

			//  要素をセット
			if( array_key_exists( $column_info['name'], $row ) ){
				$bind_data['VALUES_'.$column_info['name']] = $row[$column_info['name']];
				array_push($ary_key_val, $column_info['name'].'='.$type_prefix.'VALUES_'.$column_info['name']);
			}
			if( array_key_exists( $column_info['name'], $conditions ) ){
				array_push($ary_conditions, $column_info['name'].'='.$type_prefix.'CONDITIONS_'.$column_info['name']);
			}

		}
		//  条件式のバインドデータを作成
		foreach( $conditions as $key=>$val ){
			$bind_data['CONDITIONS_'.$key] = $val;
		}
		$sql = '';
		$sql .= 'UPDATE ';
		$sql .= ''.$this->px->dbh()->bind( ':D:table_name' , array('table_name'=>$table_name) ).' ';
		if(count($ary_key_val)){
			$sql .= 'SET ';
			$sql .= ''.implode(', ',$ary_key_val).' ';
		}
		if(count($ary_conditions)){
			$sql .= 'WHERE ';
			$sql .= ''.implode(' AND ',$ary_conditions).' ';
		}
		$sql .= ';';

		$sql = $this->px->dbh()->bind( $sql , $bind_data );
		$res = $this->px->dbh()->send_query( $sql );
		if( !$res ){
			return false;
		}

		return true;
	}//update()

	/**
	 * テーブルから行を取得する
	 */
	public function select( $table_name, $conditions, $limit = null, $offset = 0 ){
		$table_name = $this->bind_meta_string($table_name);

		$offset = intval( $offset );
		if( !is_null( $limit ) ){
			$limit = intval( $limit );
		}

		$table_definition = $this->get_table_definition($table_name);
		$ary_conditions = array();
		$bind_data = array();
		foreach( $table_definition['columns'] as $column_info ){
			if( !array_key_exists( $column_info['name'], $conditions ) ){ continue; }

			//  メタ文字の接頭辞を決める
			$type_prefix = ':S:';
			switch($column_info['type']){
				case 'int':
				case 'serial':
					$type_prefix = ':N:';
					break;
				default:
					$type_prefix = ':S:';
			}

			//  要素をセット
			$bind_data['CONDITIONS_'.$column_info['name']] = $conditions[$column_info['name']];
			array_push($ary_conditions,$column_info['name'].'='.$type_prefix.'CONDITIONS_'.$column_info['name']);

		}

		$sql = '';
		$sql .= 'SELECT * ';
		$sql .= 'FROM '.$this->px->dbh()->bind( ':D:table_name' , array('table_name'=>$table_name) ).' ';
		if(count($ary_conditions)){
			$sql .= 'WHERE ';
			$sql .= ''.implode(' AND ',$ary_conditions).' ';
		}
		if( $limit > 0 ){
			$sql .= $this->px->dbh()->mk_sql_limit($limit, $offset);
		}
		$sql .= ';';

		$sql = $this->px->dbh()->bind( $sql , $bind_data );
		$res = $this->px->dbh()->send_query( $sql );
		if( !$res ){
			return false;
		}

		$value = $this->px->dbh()->get_results();

		return $value;
	}//select()

	/**
	 * テーブルの行を削除する
	 */
	public function delete( $table_name, $conditions ){
		$table_name = $this->bind_meta_string($table_name);

		$table_definition = $this->get_table_definition($table_name);
		$ary_conditions = array();
		$bind_data = array();
		foreach( $table_definition['columns'] as $column_info ){
			if( !array_key_exists( $column_info['name'], $conditions ) ){ continue; }

			//  メタ文字の接頭辞を決める
			$type_prefix = ':S:';
			switch($column_info['type']){
				case 'int':
				case 'serial':
					$type_prefix = ':N:';
					break;
				default:
					$type_prefix = ':S:';
			}

			//  要素をセット
			$bind_data['CONDITIONS_'.$column_info['name']] = $conditions[$column_info['name']];
			array_push($ary_conditions,$column_info['name'].'='.$type_prefix.'CONDITIONS_'.$column_info['name']);

		}

		$sql = '';
		$sql .= 'DELETE ';
		$sql .= 'FROM '.$this->px->dbh()->bind( ':D:table_name' , array('table_name'=>$table_name) ).' ';
		$sql .= 'WHERE ';
		$sql .= ''.implode(' AND ',$ary_conditions).' ';
		$sql .= ';';

		$sql = $this->px->dbh()->bind( $sql , $bind_data );
		$res = $this->px->dbh()->send_query( $sql );
		if( !$res ){
			return false;
		}

		return true;
	}//delete()

}

?>
Back to Top