-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Using object literals to append rows to tables #42
Merged
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4ec71b2
initial implementation of appendRow using NSArray
kneth f6c410c
A few extra unit tests
kneth e7ff823
Support for float and double
kneth 304a4f4
Support for DateTime
kneth c8fda43
Support for binary
kneth 2c82801
Forgot file
kneth e73fe89
Merge branch 'master' of github.com:Tightdb/tightdb_objc into feature…
kneth d5507c8
Subtable support in appendRow
kneth a1b9ea3
Adding partial support for Mixed in appendRow
kneth 09c9bfe
Check if array has the correct number of elements
kneth 6cca3ed
WTF: @YES and @NO are chars
kneth bbbe36b
appendRow: Support binary in mixed columns
kneth f20dd40
adding NSDate support in appendRow
kneth 378f1f6
adding NSData support in appendRow
kneth dd85b44
Merge branch 'master' of github.com:Tightdb/tightdb_objc into feature…
kneth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/************************************************************************* | ||
* | ||
* TIGHTDB CONFIDENTIAL | ||
* __________________ | ||
* | ||
* [2011] - [2014] TightDB Inc | ||
* All Rights Reserved. | ||
* | ||
* NOTICE: All information contained herein is, and remains | ||
* the property of TightDB Incorporated and its suppliers, | ||
* if any. The intellectual and technical concepts contained | ||
* herein are proprietary to TightDB Incorporated | ||
* and its suppliers and may be covered by U.S. and Foreign Patents, | ||
* patents in process, and are protected by trade secret or copyright law. | ||
* Dissemination of this information or reproduction of this material | ||
* is strictly forbidden unless prior written permission is obtained | ||
* from TightDB Incorporated. | ||
* | ||
**************************************************************************/ | ||
|
||
#ifndef TIGHTDB_OBJC_SUPPORT_H | ||
#define TIGHTDB_OBJC_SUPPORT_H | ||
|
||
#import <Foundation/Foundation.h> | ||
#include <tightdb/descriptor.hpp> | ||
|
||
BOOL verify_row(const tightdb::Descriptor& descr, NSArray * data); | ||
BOOL insert_row(size_t ndx, tightdb::Table& table, NSArray * data); | ||
#endif /* TIGHTDB_OBJC_SUPPORT_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
#include <tightdb/descriptor.hpp> | ||
|
||
#import "table.h" | ||
|
||
using namespace tightdb; | ||
|
||
BOOL verify_row(const Descriptor& descr, NSArray * data) | ||
{ | ||
NSEnumerator *enumerator = [data objectEnumerator]; | ||
id obj; | ||
|
||
/* type encodings: http://nshipster.com/type-encodings/ */ | ||
size_t col_ndx = 0; | ||
while (obj = [enumerator nextObject]) { | ||
DataType type = descr.get_column_type(col_ndx); | ||
switch (type) { | ||
case type_String: | ||
if (![obj isKindOfClass:[NSString class]]) | ||
return NO; | ||
break; | ||
case type_Bool: | ||
if ([obj isKindOfClass:[NSNumber class]]) { | ||
const char * data_type = [obj objCType]; | ||
const char dt = data_type[0]; | ||
if (dt != 'B') | ||
return NO; | ||
} | ||
break; | ||
case type_DateTime: | ||
if ([obj isKindOfClass:[NSNumber class]]) { | ||
const char * data_type = [obj objCType]; | ||
const char dt = data_type[0]; | ||
/* time_t is an integer */ | ||
if (dt == 'i' || dt == 's' || dt == 'l' || dt == 'q' || | ||
dt == 'I' || dt == 'S' || dt == 'L' || dt == 'Q') | ||
break; | ||
else { | ||
return NO; | ||
} | ||
} | ||
else { | ||
return NO; | ||
} | ||
break; | ||
case type_Int: | ||
if ([obj isKindOfClass:[NSNumber class]]) { | ||
const char * data_type = [obj objCType]; | ||
const char dt = data_type[0]; | ||
/* FIXME: what about: 'c', 'C' */ | ||
if (dt == 'i' || dt == 's' || dt == 'l' || dt == 'q' || | ||
dt == 'I' || dt == 'S' || dt == 'L' || dt == 'Q') | ||
break; | ||
else | ||
return NO; | ||
} | ||
else { | ||
return NO; | ||
} | ||
break; | ||
case type_Float: | ||
if ([obj isKindOfClass:[NSNumber class]]) { | ||
const char * data_type = [obj objCType]; | ||
const char dt = data_type[0]; | ||
/* FIXME: what about: 'c', 'C' */ | ||
if (dt == 'i' || dt == 's' || dt == 'l' || dt == 'q' || | ||
dt == 'I' || dt == 'S' || dt == 'L' || dt == 'Q' || | ||
dt == 'f') | ||
break; | ||
else | ||
return NO; | ||
} | ||
else | ||
return NO; | ||
break; /* FIXME: remove */ | ||
case type_Double: | ||
if ([obj isKindOfClass:[NSNumber class]]) { | ||
const char * data_type = [obj objCType]; | ||
const char dt = data_type[0]; | ||
/* FIXME: what about: 'c', 'C' */ | ||
if (dt == 'i' || dt == 's' || dt == 'l' || dt == 'q' || | ||
dt == 'I' || dt == 'S' || dt == 'L' || dt == 'Q' || | ||
dt == 'f' || dt == 'd') | ||
break; | ||
else | ||
return NO; | ||
} | ||
else | ||
return NO; | ||
break; /* FIXME: remove */ | ||
case type_Binary: | ||
if (![obj isKindOfClass:[TightdbBinary class]]) | ||
return NO; | ||
break; | ||
case type_Mixed: | ||
break; /* everything goes */ | ||
case type_Table: | ||
if ([obj isKindOfClass:[NSArray class]]) { | ||
if ([obj count] == 0) | ||
break; /* empty subtable */ | ||
id subobj; | ||
ConstDescriptorRef subdescr = descr.get_subdescriptor(col_ndx); | ||
NSEnumerator *subenumerator = [obj objectEnumerator]; | ||
while (subobj = [subenumerator nextObject]) { | ||
if (![subobj isKindOfClass:[NSArray class]]) | ||
return NO; | ||
if (!verify_row(*subdescr, (NSArray *)subobj)) | ||
return NO; | ||
} | ||
} | ||
else { | ||
return NO; | ||
} | ||
break; | ||
|
||
} | ||
++col_ndx; | ||
} | ||
return YES; | ||
} | ||
|
||
BOOL insert_row(size_t row_ndx, tightdb::Table& table, NSArray * data) | ||
{ | ||
/* | ||
Assumption: | ||
- data has been validated by verify_row | ||
*/ | ||
|
||
NSEnumerator *enumerator = [data objectEnumerator]; | ||
id obj; | ||
|
||
/* FIXME: handling of tightdb exceptions => return NO */ | ||
size_t col_ndx = 0; | ||
while (obj = [enumerator nextObject]) { | ||
DataType type = table.get_column_type(col_ndx); | ||
switch (type) { | ||
case type_Bool: | ||
table.insert_bool(col_ndx, row_ndx, bool([obj boolValue])); | ||
break; | ||
case type_DateTime: | ||
table.insert_datetime(col_ndx, row_ndx, time_t([obj longValue])); | ||
break; | ||
case type_Int: | ||
table.insert_int(col_ndx, row_ndx, int64_t([obj longValue])); | ||
break; | ||
case type_Float: | ||
table.insert_float(col_ndx, row_ndx, float([obj floatValue])); | ||
break; | ||
case type_Double: | ||
table.insert_double(col_ndx, row_ndx, double([obj doubleValue])); | ||
break; | ||
case type_String: | ||
{ | ||
StringData sd([obj UTF8String]); | ||
table.insert_string(col_ndx, row_ndx, sd); | ||
} | ||
break; | ||
case type_Binary: | ||
{ | ||
BinaryData bd([obj getData], [obj getSize]); | ||
table.insert_binary(col_ndx, row_ndx, bd); | ||
} | ||
break; | ||
case type_Table: | ||
if ([obj count]) { | ||
table.clear_subtable(col_ndx, row_ndx); | ||
} | ||
else { | ||
// Clear sub-table to prepare for new values | ||
table.clear_subtable(col_ndx, row_ndx); | ||
table.insert_subtable(col_ndx, row_ndx); | ||
TableRef subtable = table.get_subtable(col_ndx, row_ndx); | ||
NSEnumerator *subenumerator = [obj objectEnumerator]; | ||
id subobj; | ||
size_t subrow_ndx = 0; | ||
while (subobj = [subenumerator nextObject]) { | ||
if (!insert_row(subrow_ndx, *subtable, subobj)) | ||
return NO; | ||
++subrow_ndx; | ||
} | ||
} | ||
break; | ||
} | ||
++col_ndx; | ||
} | ||
table.insert_done(); | ||
|
||
/* FIXME: subtables */ | ||
|
||
return YES; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To quote a conversation we had earlier with @kspangsege and @kneth :
The general paradigm I use is
The effect is that the compiler will warn of any missing cases, and if
any new cases are added (to core) after the extension has been
compiled, the extension will duly fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is nothing common to do after "ok:", I would definitely 'return result' in the 'case' instead of 'goto ok'.