28 if (!array_key_exists(
'multi', $options)) {
29 $options[
'multi'] = FALSE;
32 if (!$options[
'multi']) {
33 $records = array($records);
37 $insert = db_insert(
"chado.$table");
38 $fields_arr = array();
39 foreach ($records[0] as $field => $val) {
40 $fields_arr[] = $field;
42 $insert->fields($fields_arr);
45 $table_desc = chado_get_schema($table);
49 $primary = !empty($table_desc[
'primary key'][0]) ? $table_desc[
'primary key'][0] : NULL;
50 foreach ($records as $values) {
53 $insert_values = array();
55 foreach ($values as $field => $value) {
56 if (is_array($value)) {
57 $results = chado_schema_get_foreign_key($table_desc, $field, $value);
58 if (count($results) != 1) {
62 $insert_values[$field] = $results[0];
66 $insert_values[$field] = $value;
71 if (array_key_exists(
'unique keys', $table_desc)) {
72 $unique = $table_desc[
'unique keys'];
74 $unique[
'primary'] = array($primary);
77 foreach ($unique as $fields) {
78 $u_cols = array($primary);
80 foreach ($fields as $field) {
82 if (!array_key_exists($field, $insert_values)) {
83 if (array_key_exists(
'default', $table_desc[
'fields'][$field])) {
84 $u_vals[$field] = $table_desc[
'fields'][$field][
'default'];
88 $u_vals[$field] = $insert_values[$field];
92 if (!empty($u_vals) and ($results = chado_select_record($table, $u_cols, $u_vals))) {
96 if (!$options[
'multi'] and count($results) == 1) {
97 chado_update_record($table, $u_vals, $insert_values);
98 return $results[0]->{$primary};
103 if (!$exists and $options[
'multi']) {
104 $insert->values($insert_values);
108 if (!$options[
'multi']) {
110 return $results[0]->{$primary};
112 $new_record = chado_insert_record($table, $insert_values, $options);
113 if (!empty($new_record[$primary])) {
114 return $new_record[$primary];
121 if ($options[
'multi'] and $has_fields) {
143 if (!isset($options[
'devel'])) {
144 $options[
'devel'] = FALSE;
152 foreach (array_keys($record_groups) as $table) {
153 $current_records = &$record_groups[$table];
154 if (empty($current_records)) {
155 unset($record_groups[$table]);
158 $schemas[$table] = chado_get_schema($table);
159 if (!$schemas[$table]) {
163 if (!empty($options[
'fk_overrides'][$table])) {
164 foreach ($options[
'fk_overrides'][$table] as $fk_type => $props) {
165 $schemas[$table][
'foreign keys'][$fk_type] = $props;
169 $inserts[$table] = db_insert(
"chado.$table");
170 $fields_arr = array();
171 foreach (current($current_records) as $field => $val) {
172 if ($field !=
'#fk') {
173 $fields_arr[] = $field;
176 foreach ($val as $fk_type => $fk_val) {
177 $fields_arr[] = key($schemas[$table][
'foreign keys'][$fk_type][
'columns']);
181 $inserts[$table]->fields($fields_arr);
182 $primarys[$table] = !empty($schemas[$table][
'primary key'][0]) ? $schemas[$table][
'primary key'][0] : NULL;
183 $fks[$table] = array();
186 if ($options[
'devel']) {
187 $start_time = microtime(TRUE);
188 print_r(
"\nstart time: " . date(
'r') .
"\n");
198 foreach ($record_groups as $table => $records) {
199 $schema = &$schemas[$table];
200 $primary = &$primarys[$table];
201 $fks_to_populate = array();
204 if ($options[
'devel']) {
205 $start_unique = microtime(TRUE);
207 foreach ($records as $id => $values) {
208 $unique = !empty($schema[
'unique keys']) ? $schema[
'unique keys'] : array();
209 if (!empty($values[$primary])) {
210 $unique[
'primary'] = array($primary);
215 if (!empty($values[
'#fk'])) {
216 foreach ($values[
'#fk'] as $fk_type => $foreign_record_id) {
217 $fk_table = $schema[
'foreign keys'][$fk_type][
'table'];
218 $col_name = key($schema[
'foreign keys'][$fk_type][
'columns']);
219 $values[$col_name] = $fks[$fk_table][$foreign_record_id];
221 unset($values[
'#fk']);
224 foreach ($unique as $fields) {
225 $u_cols = array(
'*');
227 foreach ($fields as $field) {
228 if (!array_key_exists($field, $values)) {
229 if (array_key_exists(
'default', $schema[
'fields'][$field])) {
230 $u_vals[$field] = $schema[
'fields'][$field][
'default'];
234 $u_vals[$field] = $values[$field];
238 $start_query = microtime(TRUE);
239 $results = chado_select_record($table, $u_cols, $u_vals);
240 if ($options[
'devel']) {
241 $times[
'unique_query'] += (microtime(TRUE) - $start_query);
243 if ($results and isset($primary)) {
245 if (count($results) == 1) {
246 if (!empty($values[$primary])) {
247 unset($values[$primary]);
249 foreach ($values as $field => $val) {
250 if ($val == current($results)->{$field}) {
251 unset($values[$field]);
254 if (!empty($values)) {
255 $fks[$table][$id] = chado_update_record($table, $u_vals, $values, array(
'return_record' => TRUE))->{$primary};
259 $fks[$table][$id] = current($results)->{$primary};
265 $inserts[$table]->values($values);
267 $fks_to_populate[] = $id;
271 if ($options[
'devel']) {
272 $times[
'unique'] += (microtime(TRUE) - $start_unique);
273 $start_insert = microtime(TRUE);
277 if ($table ==
'featureloc') {
278 $previous = chado_set_active(
'chado');
279 $last_id = $inserts[$table]->execute();
280 chado_set_active($previous);
284 $last_id = $inserts[$table]->execute();
286 catch (Exception $ex) {
288 throw new Exception($ex);
291 $fks[$table] += array_combine($fks_to_populate, range($last_id - count($fks_to_populate) + 1, $last_id));
294 if ($options[
'devel']) {
295 $times[
'insert'] += (microtime(TRUE) - $start_insert);
299 if ($options[
'devel']) {
300 $times[
'total'] = microtime(TRUE) - $start_time;
301 print_r(
"\nend time: " . date(
'r') .
"\n");
302 $times[
'other'] = $times[
'total'] - $times[
'unique'] - $times[
'insert'];
303 $times[
'unique_other'] = $times[
'unique'] - $times[
'unique_query'];
304 print_r(
"timing breakdown:\n");
308 if (!empty($options[
'entities'])) {
311 'prefix' => ($options[
'entities'][
'prefix'] ?? NULL),
315 if (!empty($options[
'fks'])) {
316 return $fks[$options[
'fks']];
334 if (!empty($options[
'multi'])) {
335 $bundle = tripal_load_bundle_entity(array(
'label' => $bundle_name));
336 if (!isset($bundle)) {
341 $entity_insert = db_insert(
'tripal_entity')
342 ->fields(array(
'type',
'bundle',
'term_id',
'title',
'created',
'changed'));
344 foreach ($vals as $title => $id) {
345 if (empty(chado_get_record_entity_by_bundle($bundle, $id))) {
346 if (!empty($options[
'prefix'])) {
347 $title = $options[
'prefix'] . $title;
349 $entity_insert->values(array(
350 'type' =>
'TripalEntity',
351 'bundle' =>
'bio_data_' . $bundle->id,
352 'term_id' => $bundle->term_id,
360 $entity_id = $entity_insert->execute() - $entity_count + 1;
362 $bio_data_insert = db_insert(
'chado_bio_data_' . $bundle->id)
363 ->fields(array(
'entity_id',
'record_id'));
364 foreach ($vals as $id) {
365 if (empty(chado_get_record_entity_by_bundle($bundle, $id))) {
366 $bio_data_insert->values(array(
367 'entity_id' => $entity_id,
371 $entity =
new stdClass();
372 $entity->id = $entity_id;
373 $entity->bundle =
'bio_data_' . $bundle->id;
374 $alias = str_replace(
' ',
'', $bundle->term->name) .
'/' . $entity_id;
375 $controller = entity_get_controller(
'TripalEntity');
376 $controller->setAlias($entity, $alias);
377 $controller->resetCache();
382 $bio_data_insert->execute();
385 $bundle = tripal_load_bundle_entity(array(
'label' => $bundle_name));
387 if (!isset($bundle)) {
391 if (empty(chado_get_record_entity_by_bundle($bundle, $vals[1]))) {
392 $entity_id = db_insert(
'tripal_entity')
394 'type' =>
'TripalEntity',
395 'bundle' =>
'bio_data_' . $bundle->id,
396 'term_id' => $bundle->term_id,
403 db_insert(
'chado_bio_data_' . $bundle->id)
405 'entity_id' => $entity_id,
406 'record_id' => $vals[1],
410 $entity = tripal_load_entity(
'TripalEntity', array($entity_id))[$entity_id];
411 $controller = entity_get_controller(
'TripalEntity');
412 $controller->setAlias($entity);
413 $controller->resetCache();
434 $prop_table = $base_table .
'prop';
435 $cvterm = array(
'name' => $name);
437 if (array_key_exists(
'prop_table', $options)) {
438 $prop_table = $options[
'prop_table'];
441 if (array_key_exists(
'cv', $options)) {
442 $cvterm[
'cv_id'] = array(
443 'name' => $options[
'cv'],
447 $prop_query = chado_select_record($prop_table, array($prop_table .
'_id'), array(
448 'type_id' => $cvterm,
449 $base_table .
'_id' => $id,
451 return !empty($prop_query);
468 $schema = chado_get_schema($table);
469 if (empty($schema)) {
470 $schema = drupal_get_schema($table);
471 if (empty($schema)) {
476 foreach ($schema[
'fields'] as $name => $info) {
497 $parts = explode(
' ', $org_name);
500 $query = db_select(
'chado.organismprop',
'op');
501 $query->join(
'chado.organism',
'o',
'o.organism_id = op.organism_id');
502 $query->fields(
'op', array(
'value'));
504 $query->condition(
'o.genus', $genus);
506 $query = $query->execute();
507 $result = $query->fetchObject()->value ?? NULL;
508 if (!empty($result)) {
523 $file = file_load($fid) ?? NULL;
526 'project_id' => $state[
'ids'][
'project_id'],
528 'value' => file_create_url($file->uri),
529 'rank' => $state[
'file_rank'],
531 db_insert(
'tpps_project_file_managed')
533 'project_id' => $state[
'ids'][
'project_id'],
537 $state[
'file_rank']++;
540 if (!empty($state[
'revised_files'][$fid]) and ($file = file_load($state[
'revised_files'][$fid]))) {
541 $fid = $state[
'revised_files'][$fid];
543 'project_id' => $state[
'ids'][
'project_id'],
545 'value' => file_create_url($file->uri),
546 'rank' => $state[
'file_rank'],
548 db_insert(
'tpps_project_file_managed')
550 'project_id' => $state[
'ids'][
'project_id'],
554 $state[
'file_rank']++;
tpps_add_project_file(array &$state, &$fid)
tpps_chado_insert_record($table, $records, array $options=array())
tpps_chado_insert_multi(array $record_groups, array $options=array())
tpps_tripal_entity_publish($bundle_name, array $vals, array $options=array())
tpps_chado_prop_exists($base_table, $id, $name, array $options=array())
tpps_get_taxon($org_name, $rank)
tpps_load_cvterm($term, array $options=array(), $version=NULL, $refresh_cache=FALSE)
tpps_table_columns($table)
tpps_ncbi_get_taxonomy($organism, $rank)