Repository: realm/realm-swift
File: src/tightdb/objc/TDBTransaction.mm (Objective-C++)

Code before:

    TDBTable* table = [[TDBTable alloc] _initRaw];
    if (TIGHTDB_UNLIKELY(!table))
        return nil;
    TIGHTDB_EXCEPTION_HANDLER_CORE_EXCEPTION(
        tightdb::TableRef table_2 = m_group->get_table(ObjcStringAccessor(name));
        [table setNativeTable:table_2.get()];
    )
    [table setParent:self];
    [table setReadOnly:m_read_only];
    return table;
}

// FIXME: Check that the specified class derives from Table.
-(id)createTableWithName:(NSString*)name asTableClass:(__unsafe_unretained Class)class_obj
{
    if ([name length] == 0) {
       @throw [NSException exceptionWithName:@"tightdb:table_name_exception"
                                      reason:@"Name must be a non-empty NSString"
                                    userInfo:nil];
    }

    if (m_read_only) {
        @throw [NSException exceptionWithName:@"tightdb:core_read_only_exception"
                                       reason:@"Transaction is read-only."
                                     userInfo:nil];
    }

    if ([self hasTableWithName:name]) {
        @throw [NSException exceptionWithName:@"tightdb:table_with_name_already_exists"
                                       reason:[NSString stringWithFormat:@"A table with the name '%@' already exists in the context.", name]
                                     userInfo:nil];
    }

    TDBTable* table = [[class_obj alloc] _initRaw];
    if (TIGHTDB_UNLIKELY(!table))
        return nil;
    bool was_created;
    TIGHTDB_EXCEPTION_HANDLER_CORE_EXCEPTION(
        tightdb::TableRef table_2 = m_group->get_table(ObjcStringAccessor(name), was_created);
        [table setNativeTable:table_2.get()];)
    [table setParent:self];
    [table setReadOnly:m_read_only];
    if (was_created) {
        if (![table _addColumns])
            return nil;
    }
    else {
        if (![table _checkType])
            return nil;
    }

Review comment (bug):
Cool.

On Tue, Apr 15, 2014 at 9:24 PM, Fiel Guhit notifications@github.comwrote:

> In src/tightdb/objc/TDBTransaction.mm:
> 
> > @@ -174,6 +174,24 @@ -(TDBTable _)createTableWithName:(NSString_)name
> >      return table;
> >  }
> > 
> > +-(TDBTable _)createTableWithName:(NSString_)name columns:(NSArray*)columns
> > +{
> > -    TDBTable\* table = [self createTableWithName:name];
> >   +
> > -    //Set columns
> > -    tightdb::TableRef nativeTable = [table getNativeTable].get_table_ref(
> >   );
> > -    if (!set_columns(nativeTable, columns)) {
> > -        // Parsing the schema failed
> > -        //TODO: More detailed error msg in exception
> > -        NSException\* exception = [NSException exceptionWithName:@"tightdb:invalid_columns"
> > -                                                         reason:@"The supplied list of columns was invalid"
> > -                                                       userInfo:[NSMutableDictionary dictionary]];
> > -        [exception raise];
> 
> Sorry just saw them now. I will apply your suggestions in another PR and
> copy you. Fiel
> ... <#14566d9067f7853b_>
> On Tue, Apr 15, 2014 at 12:20 PM, Kristian Spangsege <
> notifications@github.com> wrote: In src/tightdb/objc/TDBTransaction.mm: >
> @@ -174,6 +174,24 @@ -(TDBTable _)createTableWithName:(NSString_)name >
> return table; > } > > +-(TDBTable _)createTableWithName:(NSString_)name
> columns:(NSArray_)columns > +{ > + TDBTable_ table = [self
> createTableWithName:name]; > + > + //Set columns > + tightdb::TableRef
> nativeTable = [table getNativeTable].get_table_ref(); > + if
> (!set_columns(nativeTable, columns)) { > + // Parsing the schema failed > +
> //TODO: More detailed error msg in exception > + NSException\* exception =
> [NSException exceptionWithName:@"tightdb:invalid_columns" > + reason:@"The
> supplied list of columns was invalid" > + userInfo:[NSMutableDictionary
> dictionary]]; > + [exception raise]; Did you see my last two messages? --
> Reply to this email directly or view it on GitHub<
> https://github.com/Tightdb/tightdb_objc/pull/177/files#r11652504> .
> 
> ## 
> 
> Reply to this email directly or view it on GitHubhttps://github.com/Tightdb/tightdb_objc/pull/177/files#r11652698
> .


Code after:

    TDBTable* table = [[TDBTable alloc] _initRaw];
    if (TIGHTDB_UNLIKELY(!table))
        return nil;
    TIGHTDB_EXCEPTION_HANDLER_CORE_EXCEPTION(
        tightdb::TableRef table_2 = m_group->get_table(ObjcStringAccessor(name));
        [table setNativeTable:table_2.get()];
    )
    [table setParent:self];
    [table setReadOnly:m_read_only];
    return table;
}

-(TDBTable *)createTableWithName:(NSString*)name columns:(NSArray*)columns
{
    TDBTable* table = [self createTableWithName:name];
    
    //Set columns
    tightdb::TableRef nativeTable = [table getNativeTable].get_table_ref();
    if (!set_columns(nativeTable, columns)) {
        // Parsing the schema failed
        //TODO: More detailed error msg in exception
        NSException* exception = [NSException exceptionWithName:@"tightdb:invalid_columns"
                                                         reason:@"The supplied list of columns was invalid"
                                                       userInfo:[NSMutableDictionary dictionary]];
        [exception raise];
    }
    
    return table;
}

// FIXME: Check that the specified class derives from Table.
-(id)createTableWithName:(NSString*)name asTableClass:(__unsafe_unretained Class)class_obj
{
    if ([name length] == 0) {
       @throw [NSException exceptionWithName:@"tightdb:table_name_exception"
                                      reason:@"Name must be a non-empty NSString"
                                    userInfo:nil];
    }

    if (m_read_only) {
        @throw [NSException exceptionWithName:@"tightdb:core_read_only_exception"
                                       reason:@"Transaction is read-only."
                                     userInfo:nil];
    }

    if ([self hasTableWithName:name]) {
        @throw [NSException exceptionWithName:@"tightdb:table_with_name_already_exists"
                                       reason:[NSString stringWithFormat:@"A table with the name '%@' already exists in the context.", name]
                                     userInfo:nil];
    }