2 * Copyright (C) 2012 Openismus GmbH
4 * This file is part of GWT-Glom.
6 * GWT-Glom is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
11 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with GWT-Glom. If not, see <http://www.gnu.org/licenses/>.
20 package org.glom.web.server;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.net.ServerSocket;
27 import java.sql.Connection;
28 import java.sql.DriverManager;
29 import java.sql.SQLException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Locale;
34 import java.util.Map.Entry;
35 import java.util.Properties;
37 import org.apache.commons.lang3.StringUtils;
38 import org.glom.web.server.libglom.Document;
39 import org.glom.web.shared.DataItem;
40 import org.glom.web.shared.libglom.Field;
41 import org.jooq.InsertResultStep;
42 import org.jooq.InsertSetStep;
43 import org.jooq.Record;
44 import org.jooq.SQLDialect;
45 import org.jooq.Table;
46 import org.jooq.exception.DataAccessException;
47 import org.jooq.impl.Factory;
49 import com.google.common.io.Files;
50 import com.ibm.icu.text.NumberFormat;
53 * @author Murray Cumming <murrayc@murrayc.com>
56 public class SelfHoster {
57 // private String tempFilepathDir = "";
58 private boolean selfHostingActive = false;
59 private Document document = null;
60 private String username = "";
61 private String password = "";
63 SelfHoster(final Document document) {
64 this.document = document;
67 private static final int PORT_POSTGRESQL_SELF_HOSTED_START = 5433;
68 private static final int PORT_POSTGRESQL_SELF_HOSTED_END = 5500;
70 private static final String DEFAULT_CONFIG_PG_HBA_LOCAL_8p4 = "# TYPE DATABASE USER CIDR-ADDRESS METHOD\n"
72 + "# local is for Unix domain socket connections only\n"
73 + "# trust allows connection from the current PC without a password:\n"
74 + "local all all trust\n"
75 + "local all all ident\n"
76 + "local all all md5\n"
78 + "# TCP connections from the same computer, with a password:\n"
79 + "host all all 127.0.0.1 255.255.255.255 md5\n"
80 + "# IPv6 local connections:\n"
81 + "host all all ::1/128 md5\n";
83 private static final String DEFAULT_CONFIG_PG_IDENT = "";
84 private static final String FILENAME_DATA = "data";
86 public boolean createAndSelfHostFromExample(final Document.HostingMode hostingMode) {
88 if (!createAndSelfHostNewEmpty(hostingMode)) {
89 // std::cerr << G_STRFUNC << ": test_create_and_selfhost_new_empty() failed." << std::endl;
93 final boolean recreated = recreateDatabaseFromDocument(); /* TODO: Progress callback */
106 * @param subDirectoryPath
109 private boolean createAndSelfHostNewEmpty(final Document.HostingMode hostingMode) {
110 if (hostingMode != Document.HostingMode.HOSTING_MODE_POSTGRES_SELF) {
111 // TODO: std::cerr << G_STRFUNC << ": This test function does not support the specified hosting_mode: " <<
112 // hosting_mode << std::endl;
116 // Save a copy, specifying the path to file in a directory:
117 // For instance, /tmp/testglom/testglom.glom");
118 final String tempFilename = "testglom";
119 final File tempFolder = Files.createTempDir();
120 final File tempDir = new File(tempFolder, tempFilename);
122 final String tempDirPath = tempDir.getPath();
123 final String tempFilePath = tempDirPath + File.separator + tempFilename;
124 final File file = new File(tempFilePath);
126 // Make sure that the file does not exist yet:
131 // Save the example as a real file:
132 document.setFileURI(file.getPath());
134 document.setHostingMode(hostingMode);
135 document.setIsExampleFile(false);
136 final boolean saved = document.save();
138 System.out.println("createAndSelfHostNewEmpty(): Document.save() failed.");
139 return false; // TODO: Delete the directory.
142 // We must specify a default username and password:
143 final String user = "glom_default_developer_user";
144 final String password = "glom_default_developer_password";
146 // Create the self-hosting files:
147 if (!initialize(user, password)) {
148 System.out.println("createAndSelfHostNewEmpty(): initialize failed.");
149 // TODO: Delete directory.
152 // Check that it really created some files:
153 if (!tempDir.exists()) {
154 System.out.println("createAndSelfHostNewEmpty(): tempDir does not exist.");
155 // TODO: Delete directory.
158 return selfHost(user, password);
167 private boolean selfHost(final String user, final String password) {
168 // TODO: m_network_shared = network_shared;
170 if (getSelfHostingActive()) {
171 // TODO: std::cerr << G_STRFUNC << ": Already started." << std::endl;
172 return false; // STARTUPERROR_NONE; //Just do it once.
175 final String dbDirData = getSelfHostingDataPath(false);
176 if (StringUtils.isEmpty(dbDirData) || !fileExists(dbDirData)) {
178 * final String dbDirBackup = dbDir + File.separator + FILENAME_BACKUP;
180 * if(fileExists(dbDirBackup)) { //TODO: std::cerr << G_STRFUNC <<
181 * ": There is no data, but there is backup data." << std::endl; //Let the caller convert the backup to real
182 * data and then try again: return false; // STARTUPERROR_FAILED_NO_DATA_HAS_BACKUP_DATA; } else {
184 // TODO: std::cerr << "ConnectionPool::create_self_hosting(): The data sub-directory could not be found." <<
185 // dbdir_data_uri << std::endl;
186 return false; // STARTUPERROR_FAILED_NO_DATA;
190 final int availablePort = discoverFirstFreePort(PORT_POSTGRESQL_SELF_HOSTED_START,
191 PORT_POSTGRESQL_SELF_HOSTED_END);
192 // std::cout << "debug: " << G_STRFUNC << ":() : debug: Available port for self-hosting: " << available_port <<
194 if (availablePort == 0) {
195 // TODO: Use a return enum or exception so we can tell the user about this:
196 // TODO: std::cerr << G_STRFUNC << ": No port was available between " << PORT_POSTGRESQL_SELF_HOSTED_START
197 // << " and " << PORT_POSTGRESQL_SELF_HOSTED_END << std::endl;
198 return false; // STARTUPERROR_FAILED_UNKNOWN_REASON;
201 final NumberFormat format = NumberFormat.getInstance(Locale.US);
202 format.setGroupingUsed(false); // TODO: Does this change it system-wide?
203 final String portAsText = format.format(availablePort);
205 // -D specifies the data directory.
206 // -c config_file= specifies the configuration file
207 // -k specifies a directory to use for the socket. This must be writable by us.
208 // Make sure to use double quotes for the executable path, because the
209 // CreateProcess() API used on Windows does not support single quotes.
210 final String dbDir = getSelfHostingPath("", false);
211 final String dbDirConfig = getSelfHostingPath("config", false);
212 final String dbDirHba = dbDirConfig + File.separator + "pg_hba.conf";
213 final String dbDirIdent = dbDirConfig + File.separator + "pg_ident.conf";
214 final String dbDirPid = getSelfHostingPath("pid", false);
216 // Note that postgres returns this error if we split the arguments more,
217 // for instance splitting -D and dbDirData into separate strings:
218 // too many command-line arguments (first is "(null)")
219 // Note: If we use "-D " instead of "-D" then the initdb seems to make the space part of the filepath,
220 // though that does not happen with the normal command line.
221 // However, we must have a space after -k.
222 // Also, the c hba_file=path argument must be split after -c, or postgres will get a " hba_file" configuration
223 // parameter instead of "hba_file".
224 final String commandPathStart = getPathToPostgresExecutable("postgres");
225 if (StringUtils.isEmpty(commandPathStart)) {
226 System.out.println("selfHost(): getPathToPostgresExecutable(postgres) failed.");
229 final ProcessBuilder commandPostgresStart = new ProcessBuilder(commandPathStart, "-D" + shellQuote(dbDirData),
230 "-p", portAsText, "-i", // Equivalent to -h "*", which in turn is equivalent
232 // listen_addresses in postgresql.conf. Listen to all IP addresses,
233 // so any client can connect (with a username+password)
234 "-c", "hba_file=" + shellQuote(dbDirHba), "-c", "ident_file=" + shellQuote(dbDirIdent), "-k"
235 + shellQuote(dbDir), "--external_pid_file=" + shellQuote(dbDirPid));
236 // std::cout << G_STRFUNC << ": debug: " << command_postgres_start << std::endl;
238 // Make sure to use double quotes for the executable path, because the
239 // CreateProcess() API used on Windows does not support single quotes.
241 // Note that postgres returns this error if we split the arguments more,
242 // for instance splitting -D and dbDirData into separate strings:
243 // too many command-line arguments (first is "(null)")
244 // Note: If we use "-D " instead of "-D" then the initdb seems to make the space part of the filepath,
245 // though that does not happen with the normal command line.
246 final String commandPathCheck = getPathToPostgresExecutable("pg_ctl");
247 if (StringUtils.isEmpty(commandPathCheck)) {
248 System.out.println("selfHost(): getPathToPostgresExecutable(pg_ctl) failed.");
251 final ProcessBuilder commandCheckPostgresHasStarted = new ProcessBuilder(commandPathCheck, "status", "-D"
252 + shellQuote(dbDirData));
254 // For postgres 8.1, this is "postmaster is running".
255 // For postgres 8.2, this is "server is running".
256 // This is a big hack that we should avoid. murrayc.
258 // pg_ctl actually seems to return a 0 result code for "is running" and a 1 for not running, at least with
260 // so maybe we can avoid this in future.
261 // Please do test it with your postgres version, using "echo $?" to see the result code of the last command.
262 final String secondCommandSuccessText = "is running"; // TODO: This is not a stable API. Also, watch out for
265 // The first command does not return, but the second command can check whether it succeeded:
267 final boolean result = executeCommandLineAndWaitUntilSecondCommandReturnsSuccess(commandPostgresStart,
268 commandCheckPostgresHasStarted, secondCommandSuccessText);
270 System.out.println("selfHost(): Error while attempting to self-host a database.");
271 return false; // STARTUPERROR_FAILED_UNKNOWN_REASON;
274 // Remember the port for later:
275 document.setConnectionPort(availablePort);
277 // Check that we can really connect:
279 // pg_ctl sometimes reports success before it is really ready to let us connect,
280 // so in this case we can just keep trying until it works, for a while:
281 for (int i = 0; i < 10; i++) {
285 } catch (InterruptedException e) {
286 // TODO Auto-generated catch block
290 final String dbName = document.getConnectionDatabase();
291 document.setConnectionDatabase(""); // We have not created the database yet.
293 //Check that we can connect:
294 final Connection connection = createConnection();
295 document.setConnectionDatabase(dbName);
296 if (connection != null) {
297 //Close the connection:
300 } catch (SQLException e) {
301 // TODO Auto-generated catch block
305 return true; // STARTUPERROR_NONE;
309 .println("selfHost(): Waiting and retrying the connection due to suspected too-early success of pg_ctl. retries="
313 System.out.println("selfHost(): Test connection failed after multiple retries.");
321 private String shellQuote(final String str) {
322 // TODO: If we add the quotes then they seem to be used as part of the path, though that is not a problem with
323 // the normal command line.
327 // return "'" + str + "'";
330 private String getSelfHostingPath(final String subpath, final boolean create) {
331 final String dbDir = document.getSelfHostedDirectoryPath();
332 if (StringUtils.isEmpty(subpath)) {
336 final String dbDirData = dbDir + File.separator + subpath;
337 final File file = new File(dbDirData);
339 // Return the path regardless of whether it exists:
344 if (!file.exists()) {
346 Files.createParentDirs(file);
347 } catch (final IOException e) {
348 // TODO Auto-generated catch block
361 private String getSelfHostingDataPath(final boolean create) {
362 return getSelfHostingPath(FILENAME_DATA, create);
365 private boolean executeCommandLineAndWait(final ProcessBuilder command) {
367 command.redirectErrorStream(true);
369 // Run the first command, and wait for it to return:
370 Process process = null;
372 process = command.start();
373 } catch (final IOException e) {
374 // TODO Auto-generated catch block
379 // final InputStream stderr = process.getInputStream();
380 // final InputStreamReader isr = new InputStreamReader(stderr);
381 // final BufferedReader br = new BufferedReader(isr);
382 // String output = "";
385 * try { //TODO: readLine() can hang, waiting for an end of line that never comes. while ((line = br.readLine())
386 * != null) { output += line + "\n"; } } catch (final IOException e1) { e1.printStackTrace(); return false; }
391 result = process.waitFor();
392 } catch (final InterruptedException e) {
393 // TODO Auto-generated catch block
399 System.out.println("executeCommandLineAndWait(): Command failed: " + command.command().toString());
400 // System.out.print(" Output: " + output);
407 private boolean executeCommandLineAndWaitUntilSecondCommandReturnsSuccess(final ProcessBuilder command,
408 final ProcessBuilder commandSecond, final String secondCommandSuccessText) {
409 command.redirectErrorStream(true);
411 // Run the first command, and do not wait for it to return:
412 // Process process = null;
416 } catch (final IOException e) {
417 // TODO Auto-generated catch block
422 // final InputStream stderr = process.getInputStream();
423 // final InputStreamReader isr = new InputStreamReader(stderr);
424 // final BufferedReader br = new BufferedReader(isr);
427 * We do not wait, because this (postgres, for instance), does not return: final int result = process.waitFor();
428 * if (result != 0) { // TODO: Warn. return false; }
431 // Now run the second command, usually to verify that the first command has really done its work:
432 // We run this repeatedly until it succeeds, to show that the first command has finished.
433 boolean result = false;
435 result = executeCommandLineAndWait(commandSecond);
437 System.out.println("executeCommandLineAndWait(): second command succeeded.");
442 } catch (InterruptedException e) {
443 // TODO Auto-generated catch block
448 System.out.println("executeCommandLineAndWait(): Trying the second command again.");
452 // Try to get the output:
454 * if (!result) { String output = ""; /* String line; try { // TODO: readLine() can hang, waiting for an end of
455 * line that never comes. while ((line = br.readLine()) != null) { output += line + "\n";
456 * System.out.println(line); } } catch (final IOException e1) { // TODO Auto-generated catch block
457 * e1.printStackTrace(); return false; }
460 // System.out.println(" Output of first command: " + output);
461 // System.out.println(" first command: " + command.command().toString());
462 // System.out.println(" second command: " + commandSecond.command().toString());
470 private static String getPathToPostgresExecutable(final String string) {
471 final List<String> dirPaths = new ArrayList<String>();
472 dirPaths.add("/usr/bin");
473 dirPaths.add("/usr/lib/postgresql/9.1/bin");
474 dirPaths.add("/usr/lib/postgresql/9.0/bin");
475 dirPaths.add("/usr/lib/postgresql/8.4/bin");
477 for (String dir : dirPaths) {
478 final String path = dir + File.separator + string;
479 if (fileExistsAndIsExecutable(path)) {
491 private static boolean fileExistsAndIsExecutable(String path) {
492 final File file = new File(path);
493 if (!file.exists()) {
497 if (!file.canExecute()) {
509 private static int discoverFirstFreePort(final int start, final int end) {
510 for (int port = start; port <= end; ++port) {
512 final ServerSocket socket = new ServerSocket(port);
514 // If the instantiation succeeded then the port was free:
515 final int result = socket.getLocalPort(); // This must equal port.
518 } catch (final IOException ex) {
519 continue; // try next port
530 private static boolean fileExists(final String filePath) {
531 final File file = new File(filePath);
532 return file.exists();
538 private boolean getSelfHostingActive() {
539 return selfHostingActive;
546 private boolean initialize(final String initialUsername, final String initialPassword) {
547 if (!initializeConfFiles()) {
548 System.out.println("initialize(): initializeConfFiles() failed.");
552 // initdb creates a new postgres database cluster:
554 // Get file:// URI for the tmp/ directory:
555 File filePwFile = null;
557 filePwFile = File.createTempFile("glom_initdb_pwfile", "");
558 } catch (final IOException e) {
559 // TODO Auto-generated catch block
562 final String tempPwFile = filePwFile.getPath();
564 final boolean pwfileCreationSucceeded = createTextFile(tempPwFile, initialPassword);
565 if (!pwfileCreationSucceeded) {
566 System.out.println("initialize(): createTextFile() failed.");
570 // Make sure to use double quotes for the executable path, because the
571 // CreateProcess() API used on Windows does not support single quotes.
572 final String dbDirData = getSelfHostingDataPath(false /* create */);
574 // Note that initdb returns this error if we split the arguments more,
575 // for instance splitting -D and dbDirData into separate strings:
576 // too many command-line arguments (first is "(null)")
577 // TODO: If we quote tempPwFile then initdb says that it cannot find it.
578 // Note: If we use "-D " instead of "-D" then the initdb seems to make the space part of the filepath,
579 // though that does not happen with the normal command line.
580 boolean result = false;
581 final String commandPath = getPathToPostgresExecutable("initdb");
582 if (StringUtils.isEmpty(commandPath)) {
583 System.out.println("initialize(): getPathToPostgresExecutable(initdb) failed.");
585 final ProcessBuilder commandInitdb = new ProcessBuilder(commandPath, "-D" + shellQuote(dbDirData), "-U",
586 initialUsername, "--pwfile=" + tempPwFile);
588 // Note that --pwfile takes the password from the first line of a file. It's an alternative to supplying it
590 // prompted on stdin.
591 result = executeCommandLineAndWait(commandInitdb);
594 // Of course, we don't want this to stay around. It would be a security risk.
595 final File fileTempPwFile = new File(tempPwFile);
596 if (!fileTempPwFile.delete()) {
597 System.out.println("initialize(): Failed to delete the password file.");
601 System.out.println("initialize(): Error while attempting to create self-hosting database.");
605 // Save the username and password for later;
606 this.username = initialUsername;
607 this.password = initialPassword;
609 return result; // ? INITERROR_NONE : INITERROR_COULD_NOT_START_SERVER;
613 private boolean initializeConfFiles() {
614 final String dataDirPath = document.getSelfHostedDirectoryPath();
616 final String dbDirConfig = dataDirPath + File.separator + "config";
617 // String defaultConfContents = "";
619 // Choose the configuration contents based on the postgresql version
620 // and whether we want to be network-shared:
621 // final float postgresqlVersion = 9.0f; //TODO: get_postgresql_utils_version_as_number(slot_progress);
622 // final boolean networkShared = true;
623 // std::cout << "DEBUG: postgresql_version=" << postgresql_version << std::endl;
625 // TODO: Support the other configurations, as in libglom.
626 final String defaultConfContents = DEFAULT_CONFIG_PG_HBA_LOCAL_8p4;
628 // std::cout << "DEBUG: default_conf_contents=" << default_conf_contents << std::endl;
630 final boolean hbaConfCreationSucceeded = createTextFile(dbDirConfig + File.separator + "pg_hba.conf",
631 defaultConfContents);
632 if (!hbaConfCreationSucceeded) {
633 System.out.println("initialize(): createTextFile() failed.");
637 final boolean identConfCreationSucceeded = createTextFile(dbDirConfig + File.separator + "pg_ident.conf",
638 DEFAULT_CONFIG_PG_IDENT);
639 if (!identConfCreationSucceeded) {
640 System.out.println("initialize(): createTextFile() failed.");
652 private static boolean createTextFile(final String path, final String contents) {
653 final File file = new File(path);
654 final File parent = file.getParentFile();
655 if (parent == null) {
656 System.out.println("initialize(): getParentFile() failed.");
662 file.createNewFile();
663 } catch (final IOException e) {
664 // TODO Auto-generated catch block
669 FileOutputStream output = null;
671 output = new FileOutputStream(file);
672 } catch (final FileNotFoundException e) {
673 // TODO Auto-generated catch block
679 output.write(contents.getBytes());
680 } catch (final IOException e) {
681 // TODO Auto-generated catch block
684 //TODO: Avoid the duplicate close() here.
687 } catch (IOException e1) {
688 // TODO Auto-generated catch block
689 e1.printStackTrace();
697 } catch (IOException e) {
698 // TODO Auto-generated catch block
709 private boolean recreateDatabaseFromDocument() {
710 // Check whether the database exists already.
711 final String dbName = document.getConnectionDatabase();
712 if (StringUtils.isEmpty(dbName)) {
716 document.setConnectionDatabase(dbName);
717 Connection connection = createConnection();
718 if (connection != null) {
719 // Connection to the database succeeded, so the database
723 } catch (final SQLException e) {
724 // TODO Auto-generated catch block
730 // Create the database:
732 document.setConnectionDatabase("");
734 connection = createConnection();
735 if (connection == null) {
736 System.out.println("recreatedDatabase(): createConnection() failed, before creating the database.");
740 final boolean dbCreated = createDatabase(connection, dbName);
748 // Check that we can connect:
751 } catch (final SQLException e) {
752 // TODO Auto-generated catch block
757 document.setConnectionDatabase(dbName);
758 connection = createConnection();
759 if (connection == null) {
760 System.out.println("recreatedDatabase(): createConnection() failed, after creating the database.");
766 // Create each table:
767 final List<String> tables = document.getTableNames();
768 for (final String tableName : tables) {
770 // Create SQL to describe all fields in this table:
771 final List<Field> fields = document.getTableFields(tableName);
774 final boolean tableCreationSucceeded = createTable(connection, document, tableName, fields);
776 if (!tableCreationSucceeded) {
777 // TODO: std::cerr << G_STRFUNC << ": CREATE TABLE failed with the newly-created database." <<
783 // Note that create_database() has already called add_standard_tables() and add_standard_groups(document).
785 // Add groups from the document:
787 if (!addGroupsFromDocument(document)) {
788 // TODO: std::cerr << G_STRFUNC << ": add_groups_from_document() failed." << std::endl;
792 // Set table privileges, using the groups we just added:
794 if (!setTablePrivilegesGroupsFromDocument(document)) {
795 // TODO: std::cerr << G_STRFUNC << ": set_table_privileges_groups_from_document() failed." << std::endl;
799 for (final String tableName : tables) {
800 // Add any example data to the table:
806 final boolean tableInsertSucceeded = insertExampleData(connection, document, tableName);
808 if (!tableInsertSucceeded) {
809 // TODO: std::cerr << G_STRFUNC << ": INSERT of example data failed with the newly-created database." <<
814 // catch(final std::exception& ex)
816 // std::cerr << G_STRFUNC << ": exception: " << ex.what() << std::endl;
822 return true; // All tables created successfully.
827 * @throws SQLException
829 private Connection createConnection() {
830 final Properties connectionProps = new Properties();
831 connectionProps.put("user", this.username);
832 connectionProps.put("password", this.password);
834 String jdbcURL = "jdbc:postgresql://" + document.getConnectionServer() + ":" + document.getConnectionPort();
835 String db = document.getConnectionDatabase();
836 if (StringUtils.isEmpty(db)) {
837 // Use the default PostgreSQL database, because ComboPooledDataSource.connect() fails otherwise.
840 jdbcURL += "/" + db; // TODO: Quote the database name?
842 Connection conn = null;
844 //TODO: Remove these debug prints when we figure out why getConnection sometimes hangs.
845 //System.out.println("debug: SelfHoster.createConnection(): before createConnection()");
846 DriverManager.setLoginTimeout(10);
847 conn = DriverManager.getConnection(jdbcURL + "/", connectionProps);
848 //System.out.println("debug: SelfHoster.createConnection(): before createConnection()");
849 } catch (final SQLException e) {
860 private void progress() {
861 // TODO Auto-generated method stub
870 private boolean insertExampleData(final Connection connection, final Document document, final String tableName) {
872 final Factory factory = new Factory(connection, SQLDialect.POSTGRES);
873 final Table<Record> table = Factory.tableByName(tableName);
875 final List<Map<String, DataItem>> exampleRows = document.getExampleRows(tableName);
876 for (final Map<String, DataItem> row : exampleRows) {
877 InsertSetStep<Record> insertStep = factory.insertInto(table);
879 for (final Entry<String, DataItem> entry : row.entrySet()) {
880 final String fieldName = entry.getKey();
881 final DataItem value = entry.getValue();
886 final Field field = document.getField(tableName, fieldName);
891 final org.jooq.Field<Object> jooqField = Factory.fieldByName(field.getName());
892 if (jooqField == null) {
896 final Object fieldValue = value.getValue(field.getGlomType());
897 insertStep = insertStep.set(jooqField, fieldValue);
900 if (!(insertStep instanceof InsertResultStep<?>)) {
904 // We suppress the warning because we _do_ check the cast above.
905 @SuppressWarnings("unchecked")
906 final InsertResultStep<Record> insertResultStep = (InsertResultStep<Record>) insertStep;
909 insertResultStep.fetchOne();
910 } catch (final DataAccessException e) {
911 // e.printStackTrace();
914 // TODO: Check that it worked.
924 private boolean setTablePrivilegesGroupsFromDocument(final Document document2) {
925 // TODO Auto-generated method stub
933 private boolean addGroupsFromDocument(final Document document2) {
934 // TODO Auto-generated method stub
944 private boolean createTable(final Connection connection, final Document document, final String tableName,
945 final List<Field> fields) {
946 boolean tableCreationSucceeded = false;
949 * TODO: //Create the standard field too: //(We don't actually use this yet) if(std::find_if(fields.begin(),
950 * fields.end(), predicate_FieldHasName<Field>(GLOM_STANDARD_FIELD_LOCK)) == fields.end()) { sharedptr<Field>
951 * field = sharedptr<Field>::create(); field->set_name(GLOM_STANDARD_FIELD_LOCK);
952 * field->set_glom_type(Field::TYPE_TEXT); fields.push_back(field); }
955 // Create SQL to describe all fields in this table:
956 String sqlFields = "";
957 for (final Field field : fields) {
958 // Create SQL to describe this field:
959 String sqlFieldDescription = escapeSqlId(field.getName()) + " " + field.getSqlType();
961 if (field.getPrimaryKey()) {
962 sqlFieldDescription += " NOT NULL PRIMARY KEY";
966 if (!StringUtils.isEmpty(sqlFields)) {
970 sqlFields += sqlFieldDescription;
973 if (StringUtils.isEmpty(sqlFields)) {
974 // TODO: std::cerr << G_STRFUNC << ": sql_fields is empty." << std::endl;
977 // Actually create the table
978 final String query = "CREATE TABLE " + escapeSqlId(tableName) + " (" + sqlFields + ");";
979 final Factory factory = new Factory(connection, SQLDialect.POSTGRES);
980 factory.execute(query);
981 tableCreationSucceeded = true;
982 if (!tableCreationSucceeded) {
983 System.out.println("recreatedDatabase(): CREATE TABLE() failed.");
986 return tableCreationSucceeded;
993 private String escapeSqlId(final String name) {
995 return "\"" + name + "\"";
1001 private static boolean createDatabase(final Connection connection, final String databaseName) {
1003 final String query = "CREATE DATABASE \"" + databaseName + "\""; // TODO: Escaping.
1004 final Factory factory = new Factory(connection, SQLDialect.POSTGRES);
1006 factory.execute(query);
1014 public boolean cleanup() {
1015 boolean result = true;
1018 if ((document != null) && (document.getConnectionPort() != 0)) {
1019 final String dbDirData = getSelfHostingDataPath(false);
1021 // -D specifies the data directory.
1022 // -c config_file= specifies the configuration file
1023 // -k specifies a directory to use for the socket. This must be writable by us.
1024 // We use "-m fast" instead of the default "-m smart" because that waits for clients to disconnect (and
1025 // sometimes never succeeds).
1026 // TODO: Warn about connected clients on other computers? Warn those other users?
1027 // Make sure to use double quotes for the executable path, because the
1028 // CreateProcess() API used on Windows does not support single quotes.
1029 final String commandPath = getPathToPostgresExecutable("pg_ctl");
1030 if (StringUtils.isEmpty(commandPath)) {
1031 System.out.println("cleanup(): getPathToPostgresExecutable(pg_ctl) failed.");
1033 final ProcessBuilder commandPostgresStop = new ProcessBuilder(commandPath,
1034 "-D" + shellQuote(dbDirData), "stop", "-m", "fast");
1035 result = executeCommandLineAndWait(commandPostgresStop);
1037 System.out.println("cleanup(): Failed to stop the PostgreSQL server.");
1041 document.setConnectionPort(0);
1044 // Delete the files:
1045 final String selfhostingPath = getSelfHostingPath("", false);
1046 final File fileSelfHosting = new File(selfhostingPath);
1047 fileSelfHosting.delete();
1049 final String docPath = document.getFileURI();
1050 final File fileDoc = new File(docPath);