Skip to content

Commit

Permalink
Support ST_... for MySql 8.0 where old stuff was removed
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed May 27, 2018
1 parent b5e806f commit 551d4d6
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 290 deletions.
25 changes: 25 additions & 0 deletions dg.Sql.Connector.MsSql/MsSqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,31 @@ public override string func_LENGTH(string value)
return @"LEN(" + value + ")";
}

public override string func_ST_X(string pt)
{
return pt + ".STX";
}

public override string func_ST_Y(string pt)
{
return pt + ".STY";
}

public override string func_ST_Contains(string g1, string g2)
{
return g1 + ".STContains(" + g2 + ")";
}

public override string func_ST_GeomFromText(string text, string srid = null)
{
return "geometry::STGeomFromText(" + PrepareValue(text) + (string.IsNullOrEmpty(srid) ? "" : "," + srid) + ")";
}

public override string func_ST_GeogFromText(string text, string srid = null)
{
return "geography::STGeomFromText(" + PrepareValue(text) + (string.IsNullOrEmpty(srid) ? "" : "," + srid) + ")";
}

public override string type_TINYINT { get { return @"TINYINT"; } }
public override string type_UNSIGNEDTINYINT { get { return @"TINYINT"; } }
public override string type_SMALLINT { get { return @"SMALLINT"; } }
Expand Down
85 changes: 84 additions & 1 deletion dg.Sql.Connector.MySql/MySqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,36 @@ public override string GetVersion()
return _Version;
}

bool? _Is5_0_3OrLater = null;
private bool Is5_0_3OrLater()
{
if (_Is5_0_3OrLater == null)
{
_Is5_0_3OrLater = GetVersion().CompareTo("5.0.3") >= 0;
}
return _Is5_0_3OrLater.Value;
}

bool? _Is5_7OrLater = null;
private bool Is5_7OrLater()
{
if (_Is5_7OrLater == null)
{
_Is5_7OrLater = GetVersion().CompareTo("5.7") >= 0;
}
return _Is5_7OrLater.Value;
}

bool? _Is8_0OrLater = null;
private bool Is8_0OrLater()
{
if (_Is8_0OrLater == null)
{
_Is8_0OrLater = GetVersion().CompareTo("8.0") >= 0;
}
return _Is8_0OrLater.Value;
}

public override bool SupportsSelectPaging()
{
return true;
Expand Down Expand Up @@ -351,7 +381,7 @@ public override int varchar_MAX_VALUE
{
get
{
if (GetVersion().CompareTo("5.0.3") >= 0)
if (Is5_0_3OrLater())
{
return 21845;
}
Expand All @@ -367,6 +397,59 @@ public override string func_UTC_NOW()
return @"UTC_TIMESTAMP()";
}

public override string func_ST_X(string pt)
{
if (Is5_7OrLater())
{
return "ST_X(" + pt + ")";
}
else
{
return "X(" + pt + ")";
}
}

public override string func_ST_Y(string pt)
{
if (Is5_7OrLater())
{
return "ST_Y(" + pt + ")";
}
else
{
return "Y(" + pt + ")";
}
}

public override string func_ST_Contains(string g1, string g2)
{
if (Is5_7OrLater())
{
return "ST_Contains(" + g1 + ", " + g2 + ")";
}
else
{
return "MBRContains(" + g1 + ", " + g2 + ")";
}
}

public override string func_ST_GeomFromText(string text, string srid = null)
{
if (Is5_7OrLater())
{
return "ST_GeomFromText(" + PrepareValue(text) + (string.IsNullOrEmpty(srid) ? "" : "," + srid) + ")";
}
else
{
return "GeomFromText(" + PrepareValue(text) + (string.IsNullOrEmpty(srid) ? "" : "," + srid) + ")";
}
}

public override string func_ST_GeogFromText(string text, string srid = null)
{
return func_ST_GeomFromText(text, srid);
}

public override string type_AUTOINCREMENT { get { return @"AUTO_INCREMENT"; } }
public override string type_AUTOINCREMENT_BIGINT { get { return @"AUTO_INCREMENT"; } }

Expand Down
25 changes: 25 additions & 0 deletions dg.Sql.Connector.PostgreSQL/PostgreSQLConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,31 @@ public override string func_SHA1_Binary(string value)
throw new NotSupportedException("SHA1 is not supported by Postgresql");
}

public override string func_ST_X(string pt)
{
return "ST_X(" + pt + ")";
}

public override string func_ST_Y(string pt)
{
return "ST_Y(" + pt + ")";
}

public override string func_ST_Contains(string g1, string g2)
{
return "ST_Contains(" + g1 + ", " + g2 + ")";
}

public override string func_ST_GeomFromText(string text, string srid = null)
{
return "ST_GeomFromText(" + PrepareValue(text) + (string.IsNullOrEmpty(srid) ? "" : "," + srid) + ")";
}

public override string func_ST_GeogFromText(string text, string srid = null)
{
return "ST_GeogFromText(" + PrepareValue(text) + (string.IsNullOrEmpty(srid) ? "" : "," + srid) + ")";
}

public override string type_AUTOINCREMENT { get { return @"SERIAL"; } }
public override string type_AUTOINCREMENT_BIGINT { get { return @"BIGSERIAL"; } }

Expand Down
25 changes: 25 additions & 0 deletions dg.Sql/Connector/ConnectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,31 @@ public virtual string func_SHA1_Binary(string value)
return @"UNHEX(SHA1(" + value + "))";
}

public virtual string func_ST_X(string pt)
{
return "ST_X(" + pt + ")";
}

public virtual string func_ST_Y(string pt)
{
return "ST_Y(" + pt + ")";
}

public virtual string func_ST_Contains(string g1, string g2)
{
return "ST_Contains(" + g1 + ", " + g2 + ")";
}

public virtual string func_ST_GeomFromText(string text, string srid = null)
{
return "ST_GeomFromText(" + PrepareValue(text) + (string.IsNullOrEmpty(srid) ? "" : "," + srid) + ")";
}

public virtual string func_ST_GeogFromText(string text, string srid = null)
{
return "ST_GeogFromText(" + PrepareValue(text) + (string.IsNullOrEmpty(srid) ? "" : "," + srid) + ")";
}

public virtual string type_AUTOINCREMENT { get { return @"AUTOINCREMENT"; } }
public virtual string type_AUTOINCREMENT_BIGINT { get { return @"AUTOINCREMENT"; } }

Expand Down
10 changes: 10 additions & 0 deletions dg.Sql/Sql/Phrases/PhraseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,11 @@ public static ST_X ST_X(IPhrase phrase)
return new ST_X(phrase);
}

public static ST_X ST_X(ValueWrapper value)
{
return new ST_X(value);
}

public static ST_Y ST_Y(string tableName, string columnName)
{
return new ST_Y(tableName, columnName);
Expand All @@ -1286,6 +1291,11 @@ public static ST_Y ST_Y(IPhrase phrase)
{
return new ST_Y(phrase);
}

public static ST_Y ST_Y(ValueWrapper value)
{
return new ST_Y(value);
}

#endregion

Expand Down
59 changes: 17 additions & 42 deletions dg.Sql/Sql/Phrases/Spatial/GeographyContains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,79 +114,54 @@ public GeographyContains(

public string BuildPhrase(ConnectorBase conn, Query relatedQuery = null)
{
StringBuilder sb = new StringBuilder();

if (conn.TYPE == ConnectorBase.SqlServiceType.MSSQL)
{
}
else
{
if (conn.TYPE == ConnectorBase.SqlServiceType.POSTGRESQL)
{
sb.Append(@"ST_Contains(");
}
else // MYSQL
{
sb.Append(@"MBRContains(");
}
}

StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();

if (OuterValueType == ValueObjectType.ColumnName)
{
if (OuterTableName != null && OuterTableName.Length > 0)
{
sb.Append(conn.WrapFieldName(OuterTableName));
sb.Append(".");
sb1.Append(conn.WrapFieldName(OuterTableName));
sb1.Append(".");
}
sb.Append(conn.WrapFieldName(OuterValue.ToString()));
sb1.Append(conn.WrapFieldName(OuterValue.ToString()));
}
else if (OuterValueType == ValueObjectType.Value)
{
if (OuterValue is Geometry)
{
((Geometry)OuterValue).BuildValue(sb, conn);
((Geometry)OuterValue).BuildValue(sb1, conn);
}
else
{
sb.Append(conn.PrepareValue(OuterValue, relatedQuery));
sb1.Append(conn.PrepareValue(OuterValue, relatedQuery));
}
}
else sb.Append(OuterValue);

if (conn.TYPE == ConnectorBase.SqlServiceType.MSSQL)
{
sb.Append(@".STContains(");
}
else // MYSQL, PostgreSQL
{
sb.Append(@",");
}

else sb1.Append(OuterValue);

if (InnerValueType == ValueObjectType.ColumnName)
{
if (InnerTableName != null && InnerTableName.Length > 0)
{
sb.Append(conn.WrapFieldName(InnerTableName));
sb.Append(".");
sb2.Append(conn.WrapFieldName(InnerTableName));
sb2.Append(".");
}
sb.Append(conn.WrapFieldName(InnerValue.ToString()));
sb2.Append(conn.WrapFieldName(InnerValue.ToString()));
}
else if (InnerValueType == ValueObjectType.Value)
{
if (InnerValue is Geometry)
{
((Geometry)InnerValue).BuildValue(sb, conn);
((Geometry)InnerValue).BuildValue(sb2, conn);
}
else
{
sb.Append(conn.PrepareValue(InnerValue, relatedQuery));
sb2.Append(conn.PrepareValue(InnerValue, relatedQuery));
}
}
else sb.Append(InnerValue);

sb.Append(@")");
else sb2.Append(InnerValue);

return sb.ToString();
return conn.func_ST_Contains(sb1.ToString(), sb2.ToString());
}
}
}
22 changes: 3 additions & 19 deletions dg.Sql/Sql/Phrases/Spatial/GeographyDistance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public string BuildPhrase(ConnectorBase conn, Query relatedQuery = null)
GenerateXY(conn, To, out tx, out ty);

StringBuilder sb = new StringBuilder();
sb.Append(@"12742.0*ASIN(SQRT(POWER(SIN(((");
sb.Append(@"12742.0 * ASIN(SQRT(POWER(SIN(((");
sb.Append(fx);
sb.Append(@")-(");
sb.Append(tx);
Expand Down Expand Up @@ -69,24 +69,8 @@ private static void GenerateXY(
pt = conn.WrapFieldName(point.PointColumnName);
}

if (conn.TYPE == ConnectorBase.SqlServiceType.MSSQL)
{
x = pt + @".STX";
y = pt + @".STY";
}
else
{
if (conn.TYPE == ConnectorBase.SqlServiceType.POSTGRESQL)
{
x = @"ST_X(" + pt + @")";
y = @"ST_Y(" + pt + @")";
}
else // MYSQL
{
x = @"X(" + pt + @")";
y = @"Y(" + pt + @")";
}
}
x = conn.func_ST_X(pt);
y = conn.func_ST_Y(pt);
}
else
{
Expand Down
Loading

0 comments on commit 551d4d6

Please sign in to comment.