/ Execute an SQL command. */ /
| 1861 | /* Execute an SQL command. */ |
| 1862 | /***********************************************************************/ |
| 1863 | bool ODBConn::ExecSQLcommand(char *sql) |
| 1864 | { |
| 1865 | char cmd[16]; |
| 1866 | bool b, rcd = false; |
| 1867 | UINT txn = 0; |
| 1868 | PGLOBAL& g = m_G; |
| 1869 | SWORD ncol = 0; |
| 1870 | SQLLEN afrw; |
| 1871 | RETCODE rc; |
| 1872 | HSTMT hstmt; |
| 1873 | |
| 1874 | try { |
| 1875 | b = FALSE; |
| 1876 | |
| 1877 | // Check whether we should use transaction |
| 1878 | if (sscanf(sql, " %15s ", cmd) == 1) { |
| 1879 | if (!stricmp(cmd, "INSERT") || !stricmp(cmd, "UPDATE") || |
| 1880 | !stricmp(cmd, "DELETE") || !stricmp(cmd, "REPLACE")) { |
| 1881 | // Does the data source support transactions |
| 1882 | rc = SQLGetInfo(m_hdbc, SQL_TXN_CAPABLE, &txn, 0, NULL); |
| 1883 | |
| 1884 | if (Check(rc) && txn != SQL_TC_NONE) { |
| 1885 | rc = SQLSetConnectAttr(m_hdbc, SQL_ATTR_AUTOCOMMIT, |
| 1886 | SQL_AUTOCOMMIT_OFF, SQL_IS_UINTEGER); |
| 1887 | |
| 1888 | if (!Check(rc)) |
| 1889 | ThrowDBX(SQL_INVALID_HANDLE, "SQLSetConnectAttr"); |
| 1890 | |
| 1891 | m_Transact = TRUE; |
| 1892 | } // endif txn |
| 1893 | |
| 1894 | } // endif cmd |
| 1895 | |
| 1896 | } // endif sql |
| 1897 | |
| 1898 | // Allocate the statement handle |
| 1899 | rc = SQLAllocStmt(m_hdbc, &hstmt); |
| 1900 | |
| 1901 | if (!Check(rc)) |
| 1902 | ThrowDBX(SQL_INVALID_HANDLE, "SQLAllocStmt"); |
| 1903 | |
| 1904 | OnSetOptions(hstmt); |
| 1905 | b = true; |
| 1906 | |
| 1907 | if (trace(1)) |
| 1908 | htrc("ExecSQLcommand hstmt=%p %.64s\n", hstmt, sql); |
| 1909 | |
| 1910 | // Proceed with command execution |
| 1911 | do { |
| 1912 | rc = SQLExecDirect(hstmt, (PUCHAR)sql, SQL_NTS); |
| 1913 | } while (rc == SQL_STILL_EXECUTING); |
| 1914 | |
| 1915 | if (!Check(rc)) |
| 1916 | ThrowDBX(rc, "SQLExecDirect", hstmt); |
| 1917 | |
| 1918 | // Check whether this is a query returning a result set |
| 1919 | if (!Check(rc = SQLNumResultCols(hstmt, &ncol))) |
| 1920 | ThrowDBX(rc, "SQLNumResultCols", hstmt); |
no test coverage detected