Exemple #1
0
 /**
  *  allows customizing the field sql type using the schema's field hints
  *
  *  @since  10-19-10
  *  @param  string  $field  the field name
  *  @param  MingoSchema $schema the schema for the table         
  *  @return string
  */
 protected function normalizeSqlType(MingoTable $table, $field)
 {
     $ret_str = '';
     $field_instance = $table->getField($field);
     switch ($field_instance->getType()) {
         case MingoField::TYPE_INT:
             if ($field_instance->hasRange()) {
                 $max_size = $this->getMaxSize();
                 // http://help.scibit.com/mascon/masconMySQL_Field_Types.html
                 if ($max_size < 128) {
                     $ret_str = 'TINYINT(4)';
                 } else {
                     if ($max_size < 32768) {
                         $ret_str = 'SMALLINT';
                     } else {
                         if ($max_size < 8388608) {
                             $ret_str = 'MEDIUMINT';
                         } else {
                             if ($max_size < 2147483648.0) {
                                 $ret_str = 'INT(11)';
                             } else {
                                 $ret_str = 'BIGINT';
                             }
                         }
                     }
                 }
                 //if/else if.../else
             } else {
                 $ret_str = 'INT(11)';
             }
             //if/else
             break;
         case MingoField::TYPE_POINT:
             $ret_str = 'POINT';
             break;
         case MingoField::TYPE_BOOL:
             $ret_str = 'TINYINT(4)';
             break;
         case MingoField::TYPE_FLOAT:
             $ret_str = 'FLOAT';
             break;
         case MingoField::TYPE_STR:
         case MingoField::TYPE_LIST:
         case MingoField::TYPE_MAP:
         case MingoField::TYPE_DEFAULT:
         default:
             if ($field_instance->hasRange()) {
                 if ($field_instance->isFixedSize()) {
                     $ret_str = sprintf('CHAR(%s)', $field_instance->getMaxSize());
                 } else {
                     $ret_str = sprintf('VARCHAR(%s)', $field_instance->getMaxSize());
                 }
                 //if/else
             } else {
                 $ret_str = 'VARCHAR(100)';
             }
             //if/else
             break;
     }
     //switch
     return $ret_str;
 }
Exemple #2
0
 /**
  *  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;
 }
 /**
  *  allows customizing the field sql type using the schema's field hints
  *
  *  @link http://www.postgresql.org/docs/8.1/static/datatype.html   
  *  @param  \MingoTable $table   
  *  @param  string  $field  the field name
  *  @return string
  */
 protected function normalizeSqlType(MingoTable $table, $field)
 {
     $ret_str = '';
     $field_instance = $table->getField($field);
     switch ($field_instance->getType()) {
         case MingoField::TYPE_INT:
             $ret_str = 'INTEGER';
             break;
         case MingoField::TYPE_BOOL:
             $ret_str = 'SMALLINT';
             break;
         case MingoField::TYPE_FLOAT:
             $ret_str = 'REAL';
             break;
         case MingoField::TYPE_POINT:
             $ret_str = 'POINT';
             break;
         case MingoField::TYPE_STR:
         case MingoField::TYPE_LIST:
         case MingoField::TYPE_MAP:
         case MingoField::TYPE_DEFAULT:
         default:
             if ($field_instance->hasRange()) {
                 if ($field_instance->isFixedSize()) {
                     $ret_str = sprintf('CHAR(%s)', $field_instance->getMaxSize());
                 } else {
                     $ret_str = sprintf('VARCHAR(%s)', $field_instance->getMaxSize());
                 }
                 //if/else
             } else {
                 $ret_str = 'VARCHAR(100)';
             }
             //if/else
             break;
     }
     //switch
     return $ret_str;
 }
Exemple #4
0
 /**
  *  using the $table normalize the fields to make sure the values are what they should be
  *  to query against the table   
  *
  *  @since  5-7-11
  *  @param  MingoTable  $table   
  */
 public function normalizeFields(MingoTable $table)
 {
     foreach ($this->field_map as $name => $val_list) {
         if ($table->hasField($name)) {
             $field = $table->getField($name);
             foreach (array_keys($val_list) as $key) {
                 $val_list[$key] = $field->normalizeInVal($val_list[$key]);
             }
             //foreach
         }
         //if
     }
     //foreach
 }