use of org.jkiss.dbeaver.model.sql.SQLDialect in project dbeaver by serge-rider.
the class BaseSQLDialog method createSQLPanel.
protected Composite createSQLPanel(Composite parent) {
Composite panel = UIUtils.createPlaceholder(parent, 1);
panel.setLayoutData(new GridData(GridData.FILL_BOTH));
if (isLabelVisible()) {
UIUtils.createControlLabel(panel, "SQL Preview");
}
// new Label(panel, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Composite editorPH = new Composite(panel, SWT.BORDER);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.verticalIndent = 3;
gd.horizontalSpan = 1;
gd.minimumHeight = 100;
gd.minimumWidth = 100;
editorPH.setLayoutData(gd);
editorPH.setLayout(new FillLayout());
sqlViewer = new SQLEditorBase() {
@NotNull
@Override
protected SQLDialect getSQLDialect() {
return BaseSQLDialog.this.getSQLDialect();
}
@Override
public DBCExecutionContext getExecutionContext() {
return BaseSQLDialog.this.getExecutionContext();
}
};
updateSQL();
sqlViewer.createPartControl(editorPH);
if (isWordWrap()) {
Object text = sqlViewer.getAdapter(Control.class);
if (text instanceof StyledText) {
((StyledText) text).setWordWrap(true);
}
}
sqlViewer.reloadSyntaxRules();
parent.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
sqlViewer.dispose();
}
});
return panel;
}
use of org.jkiss.dbeaver.model.sql.SQLDialect in project dbeaver by dbeaver.
the class BaseSQLDialog method createSQLPanel.
protected Composite createSQLPanel(Composite parent) {
Composite panel = UIUtils.createPlaceholder(parent, 1);
panel.setLayoutData(new GridData(GridData.FILL_BOTH));
if (isLabelVisible()) {
UIUtils.createControlLabel(panel, "SQL Preview");
}
// new Label(panel, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Composite editorPH = new Composite(panel, SWT.BORDER);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.verticalIndent = 3;
gd.horizontalSpan = 1;
gd.minimumHeight = 100;
gd.minimumWidth = 100;
editorPH.setLayoutData(gd);
editorPH.setLayout(new FillLayout());
sqlViewer = new SQLEditorBase() {
@NotNull
@Override
public SQLDialect getSQLDialect() {
return BaseSQLDialog.this.getSQLDialect();
}
@Override
public DBCExecutionContext getExecutionContext() {
return BaseSQLDialog.this.getExecutionContext();
}
};
updateSQL();
sqlViewer.createPartControl(editorPH);
if (isWordWrap()) {
Object text = sqlViewer.getAdapter(Control.class);
if (text instanceof StyledText) {
((StyledText) text).setWordWrap(true);
}
}
sqlViewer.reloadSyntaxRules();
parent.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
sqlViewer.dispose();
}
});
return panel;
}
use of org.jkiss.dbeaver.model.sql.SQLDialect in project dbeaver by dbeaver.
the class SQLContextInformer method searchInformation.
public void searchInformation(IRegion region) {
ITextViewer textViewer = editor.getTextViewer();
final DBCExecutionContext executionContext = editor.getExecutionContext();
if (region == null || textViewer == null || executionContext == null) {
return;
}
IDocument document = textViewer.getDocument();
if (document == null) {
return;
}
SQLWordPartDetector wordDetector = new SQLWordPartDetector(document, syntaxManager, region.getOffset());
wordRegion = wordDetector.detectIdentifier(document, region);
if (wordRegion.word.length() == 0) {
return;
}
String fullName = wordRegion.identifier;
String tableName = wordRegion.word;
boolean caseSensitive = false;
if (wordDetector.isQuoted(tableName)) {
tableName = DBUtils.getUnQuotedIdentifier(tableName, syntaxManager.getQuoteStrings());
caseSensitive = true;
}
String[] containerNames = null;
if (!CommonUtils.equalObjects(fullName, tableName)) {
int divPos = fullName.indexOf(syntaxManager.getStructSeparator());
if (divPos != -1) {
String[] parts = wordDetector.splitIdentifier(fullName);
tableName = parts[parts.length - 1];
containerNames = ArrayUtils.remove(String.class, parts, parts.length - 1);
for (int i = 0; i < containerNames.length; i++) {
if (wordDetector.isQuoted(containerNames[i])) {
containerNames[i] = DBUtils.getUnQuotedIdentifier(containerNames[i], syntaxManager.getQuoteStrings());
}
containerNames[i] = DBObjectNameCaseTransformer.transformName(editor.getDataSource(), containerNames[i]);
}
if (wordDetector.isQuoted(tableName)) {
tableName = DBUtils.getUnQuotedIdentifier(tableName, syntaxManager.getQuoteStrings());
}
} else {
// Full name could be quoted
if (wordDetector.isQuoted(fullName)) {
String unquotedName = DBUtils.getUnQuotedIdentifier(tableName, syntaxManager.getQuoteStrings());
if (unquotedName.equals(tableName)) {
caseSensitive = true;
}
}
}
}
final SQLDialect dialect = syntaxManager.getDialect();
keywordType = dialect.getKeywordType(fullName);
if (keywordType == DBPKeywordType.KEYWORD && region.getLength() > 1) {
// It is a keyword = let's use whole selection
try {
fullName = document.get(region.getOffset(), region.getLength());
} catch (BadLocationException e) {
log.warn(e);
}
}
keywords = new String[] { fullName };
if (keywordType == DBPKeywordType.KEYWORD || keywordType == DBPKeywordType.FUNCTION) {
// Skip keywords
return;
}
DBSStructureAssistant structureAssistant = DBUtils.getAdapter(DBSStructureAssistant.class, editor.getDataSource());
if (structureAssistant == null) {
return;
}
final Map<String, ObjectLookupCache> contextCache = getLinksCache();
if (contextCache == null) {
return;
}
ObjectLookupCache tlc = contextCache.get(fullName);
if (tlc == null) {
// Start new word finder job
tlc = new ObjectLookupCache();
contextCache.put(fullName, tlc);
TablesFinderJob job = new TablesFinderJob(executionContext, structureAssistant, containerNames, tableName, caseSensitive, tlc);
job.schedule();
}
if (tlc.loading) {
// Wait for 1000ms maximum
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// interrupted - just go further
break;
}
if (!tlc.loading) {
break;
}
Display.getCurrent().readAndDispatch();
}
}
if (!tlc.loading) {
synchronized (this) {
objectReferences = tlc.references;
}
}
}
use of org.jkiss.dbeaver.model.sql.SQLDialect in project dbeaver by serge-rider.
the class JDBCTable method updateData.
// //////////////////////////////////////////////////////////////////
// Update
@NotNull
@Override
public ExecuteBatch updateData(@NotNull DBCSession session, @NotNull final DBSAttributeBase[] updateAttributes, @NotNull final DBSAttributeBase[] keyAttributes, @Nullable DBDDataReceiver keysReceiver, @NotNull final DBCExecutionSource source) throws DBCException {
if (useUpsert(session)) {
return insertData(session, ArrayUtils.concatArrays(updateAttributes, keyAttributes), keysReceiver, source);
}
readRequiredMeta(session.getProgressMonitor());
DBSAttributeBase[] attributes = ArrayUtils.concatArrays(updateAttributes, keyAttributes);
return new ExecuteBatchImpl(attributes, keysReceiver, false) {
@NotNull
@Override
protected DBCStatement prepareStatement(@NotNull DBCSession session, DBDValueHandler[] handlers, Object[] attributeValues, Map<String, Object> options) throws DBCException {
String tableAlias = null;
SQLDialect dialect = session.getDataSource().getSQLDialect();
if (dialect.supportsAliasInUpdate()) {
tableAlias = DEFAULT_TABLE_ALIAS;
}
// Make query
StringBuilder query = new StringBuilder();
String tableName = DBUtils.getEntityScriptName(JDBCTable.this, options);
query.append("UPDATE ").append(tableName);
if (tableAlias != null) {
query.append(' ').append(tableAlias);
}
// $NON-NLS-1$ //$NON-NLS-2$
query.append("\n\tSET ");
boolean hasKey = false;
for (int i = 0; i < updateAttributes.length; i++) {
DBSAttributeBase attribute = updateAttributes[i];
// $NON-NLS-1$
if (hasKey)
query.append(",");
hasKey = true;
if (tableAlias != null) {
query.append(tableAlias).append(dialect.getStructSeparator());
}
// $NON-NLS-1$
query.append(getAttributeName(attribute)).append("=");
DBDValueHandler valueHandler = handlers[i];
if (valueHandler instanceof DBDValueBinder) {
query.append(((DBDValueBinder) valueHandler).makeQueryBind(attribute, attributeValues[i]));
} else {
// $NON-NLS-1$
query.append("?");
}
}
if (keyAttributes.length > 0) {
// $NON-NLS-1$
query.append("\n\tWHERE ");
hasKey = false;
for (int i = 0; i < keyAttributes.length; i++) {
DBSAttributeBase attribute = keyAttributes[i];
// $NON-NLS-1$
if (hasKey)
query.append(" AND ");
hasKey = true;
appendAttributeCriteria(tableAlias, dialect, query, attribute, attributeValues[updateAttributes.length + i]);
}
}
// Execute
DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, keysReceiver != null);
dbStat.setStatementSource(source);
return dbStat;
}
@Override
protected void bindStatement(@NotNull DBDValueHandler[] handlers, @NotNull DBCStatement statement, Object[] attributeValues) throws DBCException {
int paramIndex = 0;
for (int k = 0; k < handlers.length; k++) {
DBSAttributeBase attribute = attributes[k];
if (k >= updateAttributes.length && DBUtils.isNullValue(attributeValues[k])) {
// Skip NULL criteria binding
continue;
}
handlers[k].bindValueObject(statement.getSession(), statement, attribute, paramIndex++, attributeValues[k]);
}
}
};
}
use of org.jkiss.dbeaver.model.sql.SQLDialect in project dbeaver by serge-rider.
the class JDBCTable method deleteData.
// //////////////////////////////////////////////////////////////////
// Delete
@NotNull
@Override
public ExecuteBatch deleteData(@NotNull DBCSession session, @NotNull final DBSAttributeBase[] keyAttributes, @NotNull final DBCExecutionSource source) throws DBCException {
readRequiredMeta(session.getProgressMonitor());
return new ExecuteBatchImpl(keyAttributes, null, false) {
@NotNull
@Override
protected DBCStatement prepareStatement(@NotNull DBCSession session, DBDValueHandler[] handlers, Object[] attributeValues, Map<String, Object> options) throws DBCException {
String tableAlias = null;
SQLDialect dialect = session.getDataSource().getSQLDialect();
if (dialect.supportsAliasInUpdate()) {
tableAlias = DEFAULT_TABLE_ALIAS;
}
// Make query
StringBuilder query = new StringBuilder();
String tableName = DBUtils.getEntityScriptName(JDBCTable.this, options);
query.append("DELETE FROM ").append(tableName);
if (tableAlias != null) {
query.append(' ').append(tableAlias);
}
if (keyAttributes.length > 0) {
// $NON-NLS-1$ //$NON-NLS-2$
query.append("\n\tWHERE ");
boolean hasKey = false;
for (int i = 0; i < keyAttributes.length; i++) {
// $NON-NLS-1$
if (hasKey)
query.append(" AND ");
hasKey = true;
appendAttributeCriteria(tableAlias, dialect, query, keyAttributes[i], attributeValues[i]);
}
}
// Execute
DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, false);
dbStat.setStatementSource(source);
return dbStat;
}
@Override
protected void bindStatement(@NotNull DBDValueHandler[] handlers, @NotNull DBCStatement statement, Object[] attributeValues) throws DBCException {
int paramIndex = 0;
for (int k = 0; k < handlers.length; k++) {
DBSAttributeBase attribute = attributes[k];
if (DBUtils.isNullValue(attributeValues[k])) {
// Skip NULL criteria binding
continue;
}
handlers[k].bindValueObject(statement.getSession(), statement, attribute, paramIndex++, attributeValues[k]);
}
}
};
}
Aggregations