Atomicy operation in Couchbase 3 with java v2 driver - couchbase

I am trying to use the counter function but always it returns method not matched. So, I have the following questions :
1) How you initialize the counter name (like customerID) ?
2) When I am running mbacket.counter("customerID",1,1); I am getting counter not matched.
I am using couchbase 3 with java driver version 2.
BR/Antonis

Related

Passing C++ variable in MySQL Statement using MySQL Driver in MySQL 8.0+

I have looked through Stack Overflow's plethora of answers and questions but they are all for older versions of MySQL. I have also scoured the bowls of the internet for an answer to this and tried numerous different methods to no avail. So the question, how do I use a C++ variable in a MySQL query.
For instance:
pstmt = con->prepareStatement("SELECT balance FROM accounts WHERE id = [C++ Var]");
Where [C++ Var] would be the C++ variable. I am not sure if it is a good idea to use older methods from MySQL 5 in MySQL 8 or not. I think the most effective way would be to use the SET #var_name = data method, but I cannot find any way to implement that in C++. Currently I am using MySQL/C++ Connector 8.0 with the driver. Thank You!
I don't see why you need to use a MySQL #xxx variable here.
Using the old MySQL C++ API (which seems to be modeled after the Java JDBC API) prepareStatment call should look like:
pstmt = con->prepareStatement("SELECT balance FROM accounts WHERE id = ?");
And later on you should use something like
pstmt->setInt(1, userId); //where userId is a C++ int
ResultSet res* = pstmt->executeQuery()
Using the newer C++ API (X Dev API) the calls would look similar to the following:
Table accounts = db.getTable("accounts");
auto query = accounts.select("balance").where("id = :account_id");
RowResult res = query.bind("account_id", account_id).execute(); // where account_id is the name of an int variable.

DotNetCore | The 'MySQLNumberTypeMapping' does not support value conversions

We are using Entity framework (Code First) to read a Table in MySQL using Asp.NET core, and facing below exception while reading the table:
Error Message:
The 'MySQLNumberTypeMapping' does not support value conversions. Support for value conversions typically requires changes in the database provider.
Query:
var existing = (from x in db.loyaltyCards select x).OrderByDescending(x=>x.CardNumber).FirstOfDefault();

Couchbase N1QL Java SDK Pagination and Sorting issue

I have started using couchbase recently. I am using Spring-Data couchbase for inserting Java POJOs in Couchbase. Since, Pagination and Sorting is not supported by Spring Data Couchbase project, I have tried to use couchbase java client 2.2.0-dp2.
I have inserted 8 users where ids ranging from 1 to 8.
I have wrote following code to apply pagination and sorting.
public void test() {
int offset = 5 * (1 - 1);
Statement statement = select("*").from("test").where(x("_class").eq(s("com.test.rest.entity.User"))).orderBy(Sort.asc("id")).limit(5).offset(offset);
log.info(statement.toString());
Iterator<QueryRow> result = bucket.query(Query.simple(statement)).rows();
while(result.hasNext()) {
QueryRow doc = result.next();
log.info("Document:: " + doc.value());
}
}
However, I am seeing result as below. It should be test1 to test5, though users being selected randomly. Can someone help me with that?
Document:: {“test":{"createdAt":1.443420400374E12,"firstname":"test5","_class":"com.test.rest.entity.User","type":"User","lastname":"test5"}}
Document:: {“test":{"createdAt":1.443420708495E12,"firstname":"test8","_class":"com.test.rest.entity.User","type":"User","lastname":"test8"}}
Document:: {“test:{"createdAt":1.443420386638E12,"firstname":"test2","_class":"com.test.rest.entity.User","type":"User","lastname":"test2"}}
Document:: {“test":{"createdAt":1.443420704104E12,"firstname":"test7","_class":"com.test.rest.entity.User","type":"User","lastname":"test7"}}
Document:: {“test":{"createdAt":1.443420379712E12,"firstname":"test1","_class":"com.test.rest.entity.User","type":"User","lastname":"test1"}}
It should be test1 to test5, though users being selected randomly
Looks like you are expecting to sort by first name, since id is generated by CB.
Try replacing "id" with "firstname", e.g.:
Statement statement = select("*").from("test").where(x("_class")
.eq(s("com.test.rest.entity.User")))
.orderBy(Sort.asc("firstname")).limit(5).offset(offset);
NOTE: I suspect that field name "id" collides with the metadata's id (hence not returned in your json result). Try naming it differently like "_id", "id#" etc.
After going through Spring Data Couchbase codebase, I figure out the way they are querying the n1ql. By default select(*) doesn't select id because id is not part of document. So,
N1QL Statement:
SELECT META(`test`).id AS _ID, META(`test`).cas AS _CAS, `test`.* FROM `test` WHERE `_class` = "com.test.rest.entity.User";
Couchbase Java Client code:
Statement statement = select("META(`test`).id AS _ID, META(`test`).cas AS _CAS, `test`.*").from("test").where(x("_class").eq(s("com.test.rest.entity.User"))).orderBy(Sort.asc("_ID")).limit(5).offset(offset);
NOTE: orderBy(Sort.asc("_ID")) is not required. I just kept it for sample.

getExportedKeys() in JDBC returns empty set in mysql

I am using getExportedKeys JDBC method. It is working fine in Oracle but in MySQL it is returning empty set. I tried using useInformationSchema=true, still it returns empty set.
Let me know whether getTables,getExportedKeys,getImportedKeys works for MySQL, sql , DB2 , Postgres database.
This depends on the driver and DataSource that you use.
Some DataSorce implementation provide option to enable disable DatabaseMetadata for better performance.
For example: Refer to the below information from link
https://docs.oracle.com/cd/E21764_01/web.1111/e13753/db2.htm#JDBCD153
--->>CatalogOptions
Determines which type of metadata information is included in result sets when an application calls DatabaseMetaData methods.
Valid Values: 0 | 2 | 6 and the default value is 2.
If 0, result sets do not contain synonyms.
If 2, result sets contain synonyms that are returned from the following DatabaseMetaData methods: getColumns(), getExportedKeys(), getFunctionColumns(), getFunctions(), getImportedKeys(), getIndexInfo(), getPrimaryKeys(), getProcedureColumns(), and getProcedures().
If 6, a hint is provided to the driver to emulate getColumns() calls using the ResultSetMetaData object instead of querying database catalogs for column information. Result sets contain synonyms. Using emulation can improve performance because the SQL statement that is formulated by the emulation is less complex than the SQL statement that is formulated using getColumns(). The argument to getColumns() must evaluate to a single table. If it does not, because of a wildcard or null value, for example, the driver reverts to the default behavior for getColumns() calls. <<<------

Obtaining a row number of a given row from MySQL via JPA

I'm executing the following SQL in JPA 2.0 that retrieves the row number of a given row.
SELECT
rownum
FROM
(SELECT
#rownum:=#rownum+1 AS rownum,
tbl.zone_id
FROM
zone_table tbl,
(SELECT
#rownum:=0)t
ORDER BY
tbl.zone_id DESC)t
WHERE
zone_id=?
I'm using the following method in JPA to execute this native SQL query.
Object object = entityManager.createNativeQuery("aboveQuery")
.setParameter("zone_id", id).getSingleResult();
object.toString() returns like
10.0
11.0
12.0
13.0
and so on based on the id supplied. So, this value can't be parsed to java.lang.Long. I expect it to display a value without a decimal point. Why does this query produce this result. It is more related to MySQL rather than JPA.
I'm using MySQL 5.6.11
EDIT:
I can't verify what datatype it returns in MySQL, since it returns the result with no decimal point in MySQL (the primary key which is taken into account here has a type of BIGINT).
When I tried this query in PHP, it also produced the result with no decimal point. So, I may believe it has to do something with JPA.
It is most likely returning a BigDecimal which is the standard type for database NUMBER types. If you want a Long just cast it to Number and call longValue().
((Number)result).toLong()
If it is a String for some reason then just use,
new BigDecimal((String)result).toLong()