Skip to content
Permalink
Browse files
Add support for SQL Server in query and the PN (#1212)
  • Loading branch information
LadyCailin committed Jun 20, 2020
1 parent e6fe6c2 commit bbd3adac5eac2482db9f940d8ef0041aa95b1457
Showing 13 changed files with 731 additions and 20 deletions.
25 pom.xml
@@ -257,6 +257,17 @@
<version>9.1-901-1.jdbc4</version>
</dependency>

<!-- MSSQL -->
<dependency>
<!-- MIT -->
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<!-- NOTE: If you change this version, you also have to change the install-mssql-auth command to
download the corresponding version of the mssql-auth dll. This will also require users to re-install
it, so an upgrade routine will need to be released as well. -->
<version>8.2.2.jre8</version>
</dependency>

<!-- Yaml parsing -->
<dependency>
<!-- Apache 2.0 -->
@@ -579,6 +590,7 @@
<include>org.threeten:threetenbp:jar:*</include>
<include>io.gsonfire:gson-fire:jar:*</include>
<include>com.squareup.okhttp:logging-interceptor:jar:*</include>
<include>com.microsoft.sqlserver:mssql-jdbc:jar:*</include>
</includes>
</artifactSet>
<relocations>
@@ -694,10 +706,6 @@
<exclude>com.google.common.collect.*</exclude>
</excludes>
</relocation>
<relocation>
<pattern>com.microsoft</pattern>
<shadedPattern>com.laytonsmith.libs.com.microsoft</shadedPattern>
</relocation>
<relocation>
<pattern>com.squareup.okhttp</pattern>
<shadedPattern>com.laytonsmith.libs.com.squareup.okhttp</shadedPattern>
@@ -928,6 +936,15 @@
<exclude>META-INF/**</exclude>
</excludes>
</filter>
<filter>
<artifact>com.microsoft.sqlserver:mssql-jdbc:jar:*</artifact>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
@@ -129,7 +129,7 @@ public static String GetLineEnding() {
*/
@SuppressWarnings("checkstyle:localvariablename")
public static long GetMyPid() throws UnsupportedOperationException {
if(JavaVersion.getMajorVersion() >= 9) {
if(JavaVersion.GetMajorVersion() >= 9) {
try {
// Since we compile against Java 8, we can't directly use the class, and have to use reflection.
// If Java 8 support is dropped, this can be uncommented and simplified.
@@ -127,6 +127,16 @@ public static InputStream GetInputStream(String contents, String encoding) throw
return new ByteArrayInputStream(contents.getBytes(encoding));
}

/**
* Returns an InputStream for a given byte array (ByteArrayInputStream).
*
* @param bytes The bytes to wrap in an InputStream
* @return An InputStream wrapping the bytes
*/
public static InputStream GetInputStream(byte[] bytes) {
return new ByteArrayInputStream(bytes);
}

/**
* Returns System.out, but wrapped in a UTF-8 capable output stream. This is required, because higher order
* characters cannot print by default in Java.
@@ -5,7 +5,11 @@
*/
public class JavaVersion {

public static int getMajorVersion() {
/**
* Gets the major version of the running JVM, for instance 6 for "1.6" or 8 for "1.8" or 11 for "11".
* @return
*/
public static int GetMajorVersion() {
String version = System.getProperty("java.version");
if(version.startsWith("1.")) {
version = version.substring(2, 3);
@@ -18,4 +22,26 @@ public static int getMajorVersion() {
return Integer.parseInt(version);
}

/**
* Returns the bit depth of the currently running JVM, NOT the OS bit depth.
* @return Currently, either 32 or 64, could be different in future versions.
* @throws UnsupportedOperationException If the value returned by the "os.arch" system property was unexpected.
* If this happens, a new JVM type has been released, and this function needs to be updated.
*/
public static int GetJVMBitDepth() {
// Note that despite the name, this is not the architecture of the operating system,
// it is in fact the JVM architecture. On a 32 bit system, this will always be 32 bit,
// but it's possible to install a 32 bit JVM on a 64 bit system.
String arch = System.getProperty("os.arch");
switch(arch) {
case "x86":
return 32;
case "amd64":
case "x64":
return 64;
default:
throw new UnsupportedOperationException("JVM bit depth could not be determined.");
}
}

}
@@ -192,7 +192,8 @@ public GroupData(String name) {
}

/**
* Sets the sort order. If the sort order is the same, it is alphabetical.
* Sets the sort order. If the sort order is the same, it is alphabetical. Note that the default
* sort order is 100.
* @param sort
* @return
*/
@@ -141,6 +141,9 @@ public static HTTPResponse GetPage(URL url, HTTPMethod method, Map<String, List<
public static HTTPResponse GetPage(URL url, RequestSettings settings) throws SocketTimeoutException, IOException {
// If SAFE_WRITE is set, there's no reason to do the download given we know it will fail later, so let's fail
// fast, and do the check here
if(settings == null) {
settings = new RequestSettings();
}
if(settings.getDownloadTo() != null && settings.getDownloadStrategy() == FileWriteMode.SAFE_WRITE) {
if(settings.getDownloadTo().exists()) {
throw new IOException("Refusing to download file, destination path already exists ["
@@ -17,6 +17,7 @@
import com.laytonsmith.PureUtilities.JavaVersion;
import com.laytonsmith.PureUtilities.MapBuilder;
import com.laytonsmith.PureUtilities.TermColors;
import com.laytonsmith.PureUtilities.Web.WebUtility;
import com.laytonsmith.PureUtilities.XMLDocument;
import com.laytonsmith.abstraction.Implementation;
import com.laytonsmith.abstraction.StaticLayer;
@@ -62,6 +63,7 @@
import java.awt.HeadlessException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -166,7 +168,8 @@ public static CmdlineToolCollection GetCommandLineTools() {
NewMode.class,
NewTypeMode.class,
JavaVersionMode.class,
EditPrefsMode.class
EditPrefsMode.class,
InstallMSSQLAuthMode.class
};

/**
@@ -744,7 +747,7 @@ public ArgumentParser getArgumentParser() {
@Override
public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exception {
String args = "";
if(JavaVersion.getMajorVersion() > 8) {
if(JavaVersion.GetMajorVersion() > 8) {
// Need to add the --add-opens values. The values live in interpreter-helper/modules
String modules = Static.GetStringResource("/interpreter-helpers/modules");
modules = modules.replaceAll("(.*)\n", "--add-opens $1=ALL-UNNAMED ");
@@ -1703,7 +1706,7 @@ public ArgumentParser getArgumentParser() {

@Override
public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exception {
System.out.println(JavaVersion.getMajorVersion());
System.out.println(JavaVersion.GetMajorVersion());
System.exit(0);
}

@@ -1830,4 +1833,76 @@ public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exce
}

}

@tool(value = "install-mssql-auth")
public static class InstallMSSQLAuthMode extends AbstractCommandLineTool {

@Override
public boolean startupExtensionManager() {
return false;
}

@Override
public ArgumentParser getArgumentParser() {
return ArgumentParser.GetParser().addDescription("Downloads the correct MSSQL JDBC Auth dll and places it"
+ " in the C:\\Program Files\\MethodScript folder. It is required that MethodScript be generally"
+ " installed first. This command is only useable on Windows.");
}

@Override
public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exception {
if(OSUtils.GetOS() != OSUtils.OS.WINDOWS) {
System.err.println("Can only run this command on Windows.");
System.exit(1);
}
if(!MethodScriptFileLocations.getDefault().getWindowsNativeDirectory().exists()) {
System.err.println("MethodScript must be installed using install-cmdline before running this command.");
System.exit(1);
}
try {
switch(JavaVersion.GetJVMBitDepth()) {
case 32:
doDownload(32);
return;
case 64:
doDownload(64);
return;
}
} catch (UnsupportedOperationException ex) {
//
}
System.err.println("Could not determine JVM bit depth. Please manually download the MSSQL JDBC Auth dll,"
+ " and place it on your system path.");
System.exit(1);
}

private void doDownload(int bitdepth) throws IOException {
String url;
String fname;
switch(bitdepth) {
case 32:
url = "https://repo1.maven.org/maven2/com/microsoft/sqlserver/mssql-jdbc_auth/8.2.2.x86/mssql-jdbc_auth-8.2.2.x86.dll";
fname = "mssql-jdbc_auth-8.2.2.x86.dll";
break;
case 64:
url = "https://repo1.maven.org/maven2/com/microsoft/sqlserver/mssql-jdbc_auth/8.2.2.x64/mssql-jdbc_auth-8.2.2.x64.dll";
fname = "mssql-jdbc_auth-8.2.2.x64.dll";
break;
default:
throw new Error();
}
byte[] dll = WebUtility.GetPageContentsBinary(url);
File to = new File(new File("C:\\Program Files\\MethodScript").getAbsoluteFile(), fname).getAbsoluteFile();
try {
FileUtil.write(dll, to, com.laytonsmith.PureUtilities.Common.FileWriteMode.OVERWRITE, false);
} catch (FileNotFoundException ex) {
System.err.println("Could not write file to " + to.getAbsolutePath()
+ ". Did you run this as Administrator?");
System.exit(1);
}
System.out.println(url + " downloaded to " + to);
System.exit(0);
}

}
}
@@ -1,5 +1,6 @@
package com.laytonsmith.core;

import com.laytonsmith.PureUtilities.Common.OSUtils;
import com.laytonsmith.PureUtilities.Common.StringUtils;
import com.laytonsmith.PureUtilities.FileLocations;
import com.laytonsmith.abstraction.Implementation;
@@ -217,4 +218,35 @@ public File getExtensionDirectory(Extension ext) {
public File getTelemetryConfigFile() {
return new File(getPreferencesDirectory(), "telemetry.ini");
}

/**
* Gets the native executable folder for a Windows install. Note that if the user has
* not run install-cmdline, this will not necessarily exist.
* @return
*/
public File getWindowsNativeDirectory() {
return new File("C:\\Program Files\\MethodScript");
}

/**
* Gets the native executable folder for a Linux install. Note that in general, binaries are at the root of this
* folder, and no other files exist. Configuration files and the like live in /etc or something, so this
* method is provided for completeness, but is probably not particularly useful.
* @return
*/
public File getLinuxNativeDirectory() {
return new File("/usr/local/bin/");
}

/**
* Returns the native directory (where the native binary wrappers live).
* @return
*/
public File getNativeDirectory() {
if(OSUtils.GetOS() == OSUtils.OS.WINDOWS) {
return getWindowsNativeDirectory();
} else {
return getLinuxNativeDirectory();
}
}
}
@@ -201,7 +201,11 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
} else if(params[i].isInstanceOf(CDouble.TYPE)) {
ps.setDouble(i + 1, (Double) ArgumentValidation.getDouble(params[i], t));
} else if(params[i].isInstanceOf(CString.TYPE)) {
ps.setString(i + 1, (String) params[i].val());
if(type == Types.NCHAR || type == Types.NVARCHAR || type == Types.LONGNVARCHAR) {
ps.setNString(i + 1, (String) params[i].val());
} else {
ps.setString(i + 1, (String) params[i].val());
}
} else if(params[i].isInstanceOf(CByteArray.TYPE)) {
ps.setBytes(i + 1, ((CByteArray) params[i]).asByteArrayCopy());
} else if(params[i].isInstanceOf(CBoolean.TYPE)) {
@@ -241,6 +245,11 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
case Types.NUMERIC:
value = new CDouble(rs.getDouble(i), t);
break;
case Types.NCHAR:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
value = new CString(rs.getNString(i), t);
break;
case Types.VARCHAR:
case Types.CHAR:
case Types.LONGVARCHAR:
@@ -252,6 +261,7 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
case Types.LONGVARBINARY:
value = CByteArray.wrap(rs.getBytes(i), t);
break;

case Types.DATE:
case Types.TIME:
case Types.TIMESTAMP:

0 comments on commit bbd3ada

Please sign in to comment.