/** * insert into an index table * * @param \MingoTable $table the master table, not the index table * @param integer $_id the unique id * @param array $map the key/value pairs found in $table's body field * @param \MingoIndex $index the index * @return boolean */ protected function insertIndex(MingoTable $table, $_id, array $map, MingoIndex $index) { // canary if (empty($_id)) { throw new InvalidArgumentException('empty _id key'); } //if $ret_bool = false; $field_name_list = array(); $field_bind_list = array(); $val_list = array(); foreach ($index->getFieldNames() as $field) { // canary, ignore values that don't exist in the map if (!isset($map[$field])) { continue; } //if // canary, you can't index an array if (is_array($map[$field])) { throw new RuntimeException('you cannot index an array field'); } //if list($sql_name, $sql_val, $sql_bind) = $this->normalizeInsertSql($table->getField($field), $map[$field]); $field_name_list[] = $sql_name; $val_list[] = $sql_val; $field_bind_list[] = $sql_bind; } //foreach if (!empty($field_name_list)) { list($sql_name, $sql_val, $sql_bind) = $this->normalizeInsertSql($table->getField('_id'), $_id); $field_name_list[] = $sql_name; $val_list[] = $sql_val; $field_bind_list[] = $sql_bind; $index_table = $this->getIndexTableName($table, $index); $query = sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->normalizeTableSql($index_table), join(',', $field_name_list), join(',', $field_bind_list)); $ret_bool = $this->getQuery($query, $val_list); } //if return $ret_bool; }
/** * this is here because each rdbms is a little different in how they create indexes * * it is not in the _setIndex because _setIndex creates a table for the index, and * we need a way to set an index on an existing table * * @link http://www.postgresql.org/docs/8.2/static/sql-createindex.html * @param string $table * @param \MingoIndex $index * @return boolean */ protected function createIndex($table, MingoIndex $index, array $options = array()) { $query = sprintf('CREATE %sINDEX %s_%s ON %s USING BTREE (%s)', empty($options['unique']) ? '' : 'UNIQUE ', $table, $index->getName(), $this->normalizeTableSQL($table), join(',', $index->getFieldNames())); return $this->getQuery($query); }
/** * this is here because each rdbms is a little different in how they create indexes * * it is not in the _setIndex because _setIndex creates a table for the index, and * we need a way to set an index on an existing table * * why, oh why? http://stackoverflow.com/questions/1676448/ * * @param string $table * @param \MingoIndex $index * @return boolean */ protected function createIndex($table, MingoIndex $index) { // SQLite has a different index creation syntax... // http://www.sqlite.org/lang_createindex.html // create index [if not exists] name ON table_name (col_one[,col...]) $query = sprintf('CREATE INDEX IF NOT EXISTS %s_%s ON %s (%s)', $table, $index->getName(), $this->normalizeTableSQL($table), join(',', $index->getFieldNames())); return $this->getQuery($query); }