00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBSqlBuilderInsert extends DBSqlBuilderBase {
00009 protected function get_sql_template() {
00010 return 'INSERT%delayed%ignore INTO %!table (%fields) %!values_or_select';
00011 }
00012
00013 protected function get_substitutes() {
00014 $value_or_select = $this->get_select(Arr::get_item($this->params, 'select', null));
00015 if (empty($value_or_select)) {
00016 $value_or_select = $this->get_values($this->fields, $this->query->get_table());
00017 }
00018 $ret = array(
00019 '%fields' => $this->get_fieldnames($this->fields, $this->query->get_table()),
00020 '%!table' => $this->get_table($this->query->get_table()),
00021 '%!values_or_select' => $value_or_select,
00022 '%delayed' => $this->get_feature_sql($this->params, 'delayed', 'DELAYED'),
00023 '%ignore' => $this->get_feature_sql($this->params, 'ignore', 'IGNORE'),
00024 );
00025 return $ret;
00026 }
00027
00028 protected function get_fieldnames($arr_fields, IDBTable $table) {
00029 $conn = $table->get_table_driver();
00030 $ret = array();
00031 foreach($arr_fields as $key => $field) {
00032 if (!is_numeric($key)) {
00033
00034 $ret[] = DB::escape_database_entity($key, $conn, IDBDriver::FIELD);
00035 }
00036 else {
00037
00038 $ret[] = DB::escape_database_entity($field, $conn, IDBDriver::FIELD);
00039 }
00040 }
00041 return implode(', ', $ret);
00042 }
00043
00044 protected function get_values($arr_fields, IDBTable $table) {
00045 $values = array();
00046 foreach($arr_fields as $column => $value) {
00047 $values[] = DB::format($value, $table, $column);
00048 }
00049 $values = 'VALUES (' . implode(', ', $values) . ')';
00050 return $values;
00051 }
00052
00053 protected function get_select($query) {
00054 if ($query) {
00055 return $query->get_sql();
00056 }
00057 return '';
00058 }
00059
00060 protected function get_table(IDBTable $table) {
00061 return $table->get_table_name_escaped();
00062 }
00063 }