Skip to content

Commit

Permalink
Add support for reserved keywords. Allow simple queries. Reorganize code
Browse files Browse the repository at this point in the history
  • Loading branch information
gusbro committed Sep 15, 2021
1 parent 756897e commit 2968fd1
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public interface IODataMap2
{
object GetValue(IOServiceContext serviceContext, RecordEntryRow currentEntry);
string GetName(IOServiceContext serviceContext);
void SetValue(IOServiceContext serviceContext, RecordEntryRow currentEntry, object value);
void SetValue(RecordEntryRow currentEntry, object value);
}


Expand All @@ -129,7 +129,7 @@ public virtual object GetValue(IOServiceContext context, RecordEntryRow currentE
throw new NotImplementedException();
}

public virtual void SetValue(IOServiceContext context, RecordEntryRow currentEntry, object value)
public virtual void SetValue(RecordEntryRow currentEntry, object value)
{
throw new NotImplementedException();
}
Expand Down
48 changes: 38 additions & 10 deletions dotnet/src/dotnetframework/DynService.Dynamo/DynamoDBConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class DynamoDBConnection : ServiceConnection
private readonly string CLIENT_ID = "User Id";
private readonly string CLIENT_SECRET = "password";
private readonly string REGION = "region";
private readonly string LOCAL_URL = "LocalUrl";
private AmazonDynamoDBClient mDynamoDB;
private AmazonDynamoDBConfig mConfig;
private AWSCredentials mCredentials;
private RegionEndpoint mRegion = RegionEndpoint.USEast1;

Expand All @@ -49,9 +51,14 @@ public override string ConnectionString

private void InitializeDBConnection()
{

DbConnectionStringBuilder builder = new DbConnectionStringBuilder(false);
builder.ConnectionString = this.ConnectionString;
mConfig = new AmazonDynamoDBConfig();
string mLocalUrl = null;
if (builder.TryGetValue(LOCAL_URL, out object localUrl))
{
mLocalUrl = localUrl.ToString();
}
if (builder.TryGetValue(CLIENT_ID, out object clientId) && builder.TryGetValue(CLIENT_SECRET, out object clientSecret))
{
mCredentials = new BasicAWSCredentials(clientId.ToString(), clientSecret.ToString());
Expand All @@ -60,12 +67,17 @@ private void InitializeDBConnection()
{
mRegion = RegionEndpoint.GetBySystemName(region.ToString());
}
if (localUrl != null)
mConfig.ServiceURL = mLocalUrl;
else mConfig.RegionEndpoint = mRegion;
}

private bool Initialize()
{
InitializeDBConnection();
State = ConnectionState.Executing;
mDynamoDB = new Amazon.DynamoDBv2.AmazonDynamoDBClient(mCredentials, mRegion);

mDynamoDB = new AmazonDynamoDBClient(mCredentials, mConfig);
return true;
}

Expand Down Expand Up @@ -195,32 +207,48 @@ private static void CreateDynamoQuery(Query query, Dictionary<string, AttributeV
{
dataReader = null;
req = null;
ScanRequest scanReq = null;
QueryRequest queryReq = null;
if (query is Scan)
{
req = new ScanRequest
req = scanReq = new ScanRequest
{
TableName = query.TableName,
ProjectionExpression = String.Join(",", query.Projection),

ProjectionExpression = String.Join(",", query.Projection),
};
if (queryFilters.Length > 0)
{
((ScanRequest)req).FilterExpression = String.Join(" AND ", queryFilters);
((ScanRequest)req).ExpressionAttributeValues = values;
scanReq.FilterExpression = String.Join(" AND ", queryFilters);
scanReq.ExpressionAttributeValues = values;
}
}
else
{
req = new QueryRequest
req = queryReq = new QueryRequest
{
TableName = query.TableName,
KeyConditionExpression = String.Join(" AND ", query.Filters),
ExpressionAttributeValues = values,
ProjectionExpression = String.Join(",", query.Projection),

ProjectionExpression = String.Join(",", query.Projection),
};
}
Dictionary<string, string> expressionAttributeNames = null;
foreach (string mappedName in query.SelectList.Where(selItem => (selItem as DynamoDBMap)?.NeedsAttributeMap == true).Select(selItem => selItem.GetName(NewServiceContext())))
{
expressionAttributeNames = scanReq.ExpressionAttributeNames ?? new Dictionary<string, string>();
string key = $"#{ mappedName }";
string value = mappedName;
expressionAttributeNames.Add(key, value);
}
if(expressionAttributeNames != null)
{
if(scanReq != null)
scanReq.ExpressionAttributeNames = expressionAttributeNames;
else if(queryReq != null)
queryReq.ExpressionAttributeNames = expressionAttributeNames;
}
}
internal static IOServiceContext NewServiceContext() => null;

}
}
64 changes: 27 additions & 37 deletions dotnet/src/dotnetframework/DynService.Dynamo/DynamoDBDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public class DynamoDBDataReader : IDataReader
{
private RequestWrapper mRequest;
private ResponseWrapper mResponse;
private int mCurrentPosition = -1;
private int mCurrentPosition;
private IODataMap2[] selectList;
private DynamoDBRecordEntry currentEntry = null;

private int ItemCount
{
Expand All @@ -36,41 +37,34 @@ private List<Dictionary<string, AttributeValue>> Items
}
}

private void CheckCurrentPosition()
{
if (currentEntry == null)
throw new ServiceException(ServiceError.RecordNotFound);
}

public DynamoDBDataReader(ServiceCursorDef cursorDef, RequestWrapper request, IDataParameterCollection parameters)
{
Query query = cursorDef.Query as Query;
selectList = query.SelectList;
mRequest = request;
mResponse = mRequest.Read();
mCurrentPosition = -1;
}

public object this[string name]
{
get
{
if (mCurrentPosition >= 0 && mCurrentPosition < ItemCount)
{
return Items[mCurrentPosition][name].S;
}
throw new ArgumentOutOfRangeException(nameof(name));

throw new NotImplementedException();
}
}

public object this[int i]
{
get
{
if (mCurrentPosition >= 0 && mCurrentPosition < ItemCount)
{
int j = 0;
foreach (var col in Items[mCurrentPosition])
{
if (j == i)
return col.Value.S;
}
}
throw new ArgumentOutOfRangeException(nameof(i));
throw new NotImplementedException();
}
}

Expand Down Expand Up @@ -178,7 +172,7 @@ public double GetDouble(int i)

public Type GetFieldType(int i)
{
throw new NotImplementedException();
return selectList[i].GetValue(DynamoDBConnection.NewServiceContext(), currentEntry).GetType();
}

public float GetFloat(int i)
Expand Down Expand Up @@ -214,14 +208,11 @@ public string GetName(int i)

public int GetOrdinal(string name)
{
int j = 0;
foreach (var col in Items[mCurrentPosition])
{
if (col.Key.ToLower() == name.ToLower())
return j;
j++;
}
throw new ArgumentOutOfRangeException(nameof(name));
CheckCurrentPosition();
int ordinal = currentEntry.CurrentRow.ToList().FindIndex(col => col.Key.ToLower() == name.ToLower());
if (ordinal == -1)
throw new ArgumentOutOfRangeException(nameof(name));
else return ordinal;
}

public DataTable GetSchemaTable()
Expand Down Expand Up @@ -260,7 +251,7 @@ public object GetValue(int i)

private AttributeValue GetAttValue(int i)
{
return (AttributeValue)selectList[i].GetValue(NewServiceContext(), new DynamoDBRecordEntry(Items[mCurrentPosition]));
return (AttributeValue)selectList[i].GetValue(DynamoDBConnection.NewServiceContext(), currentEntry);
}

public int GetValues(object[] values)
Expand All @@ -281,12 +272,15 @@ public bool IsDBNull(int i)
public bool NextResult()
{
mCurrentPosition++;
return (mCurrentPosition < ItemCount);
currentEntry = (mCurrentPosition < ItemCount) ? new DynamoDBRecordEntry(Items[mCurrentPosition]) : null;
return currentEntry != null;
}

public bool Read()
{
if (mCurrentPosition == ItemCount && mCurrentPosition > 0)
{
if (NextResult())
return true;
else if (mCurrentPosition > 0 && mResponse.LastEvaluatedKey?.Count > 0)
{
mResponse = mRequest.Read(mResponse.LastEvaluatedKey);
/*
Expand All @@ -295,15 +289,11 @@ public bool Read()
* The result set contains the last_evaluated_key field. If more data is available for the operation,
* this key contains information about the last evaluated key. Otherwise, the key remains empty.
* */
mCurrentPosition = -1;
return NextResult();
}
mCurrentPosition++;
return (mCurrentPosition < ItemCount);
return false;
}
internal IOServiceContext NewServiceContext()
{
return null;
}

}

public class GxDynamoDBCacheDataReader : GxCacheDataReader
Expand Down
10 changes: 6 additions & 4 deletions dotnet/src/dotnetframework/DynService.Dynamo/DynamoDBMaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ namespace GeneXus.Data.Dynamo
{
public class DynamoDBMap : Map
{

public DynamoDBMap(string name): base(name)
{
internal bool NeedsAttributeMap { get; private set; }
public DynamoDBMap(string name): base(RemoveSharp(name))
{
NeedsAttributeMap = name.StartsWith("#");
}
private static string RemoveSharp(string name) => name.StartsWith("#") ? name.Substring(1) : name;

public override object GetValue(IOServiceContext context, RecordEntryRow currentEntry)
{
Expand All @@ -25,7 +27,7 @@ public override object GetValue(IOServiceContext context, RecordEntryRow current
return val;
}

public override void SetValue(IOServiceContext context, RecordEntryRow currentEntry, object value)
public override void SetValue(RecordEntryRow currentEntry, object value)
{
throw new NotImplementedException();
}
Expand Down

0 comments on commit 2968fd1

Please sign in to comment.