Code Monkey home page Code Monkey logo

Comments (15)

ujjwalguptaofficial avatar ujjwalguptaofficial commented on August 25, 2024

Query 1

Select * from Table_Name where Column1=value1 and (Column2=value2 or Column3=value3);

Jsstore

select({
    from: table_name,
    where: [{
        column1: value1
    }, {
        column2: value2,
        or: {
            column3: value3
        }
    }]
})

here is idbstudio link - https://ujjwalguptaofficial.github.io/idbstudio/?db=Demo&query=select(%7B%0A%20%20%20%20from%3A%20%22Customers%22%2C%0A%20%20%20%20where%3A%20%5B%7B%0A%20%20%20%20%20%20%20%20country%3A%20'Mexico'%2C%0A%20%20%20%20%7D%2C%20%7B%0A%20%20%20%20%20%20%20%20customerId%3A%202%2C%0A%20%20%20%20%20%20%20%20or%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20customerId%3A%2080%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%7D%5D%0A%7D)%3B

from jsstore.

xuyongweb avatar xuyongweb commented on August 25, 2024

Thanks,
Select * from Table_Name where Column1=value1 and ((Column2=value2 and Column4=value4) or (Column3=value3 and Column5=value5));

`
select({
from: table_name,

where: [{

    column1: value1,

}, {

    column2: value2,

    Column4=value4,

    or: {

        column3: value3,

       Column5=value5

    }
}]

})
`

Is it correct to write this way?

from jsstore.

ujjwalguptaofficial avatar ujjwalguptaofficial commented on August 25, 2024

yes this is correct.

from jsstore.

xuyongweb avatar xuyongweb commented on August 25, 2024

Okay, thank you. I'll test it and get back to you later

from jsstore.

ujjwalguptaofficial avatar ujjwalguptaofficial commented on August 25, 2024

Take a look at this link - https://jsstore.net/tutorial/adv-sql-example/

from jsstore.

xuyongweb avatar xuyongweb commented on August 25, 2024
Select * from Products where supplierId = 1 and((categoryId = 1 and price = 18) or(categoryId = 2 and price = 39));
select({
    from: "Products",
    where: [{
        supplierId: 1,
    }, {
        categoryId: 1,
        price: 18,
        or: {
            categoryId: 2,
            price: 39,

        }

    }]
});

https://ujjwalguptaofficial.github.io/idbstudio/?db=Demo&query=select(%7B%0D%0A%20%20%20%20from%3A%20%22Products%22%2C%0D%0A%20%20%20%20where%3A%20%5B%7B%0D%0A%20%20%20%20%20%20%20%20supplierId%3A%201%2C%0D%0A%20%20%20%20%7D%2C%20%7B%0D%0A%20%20%20%20%20%20%20%20categoryId%3A%201%2C%0D%0A%20%20%20%20%20%20%20%20price%3A%2018%2C%0D%0A%20%20%20%20%20%20%20%20or%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20categoryId%3A%202%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20price%3A%2039%2C%0D%0A%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20%7D%5D%0D%0A%7D)%3B

The result is incorrect!

from jsstore.

xuyongweb avatar xuyongweb commented on August 25, 2024

Are you still there? @ujjwalguptaofficial

from jsstore.

ujjwalguptaofficial avatar ujjwalguptaofficial commented on August 25, 2024

from jsstore.

ujjwalguptaofficial avatar ujjwalguptaofficial commented on August 25, 2024

Try this -

select({
    from: "Products",
    where: [{
        supplierId: 1,
    }, {
        categoryId: 1,
        price: 18,
    }, {
        or: {
            categoryId: 2,
            price: 39,
        }
    }]
});

Treat every parenthis in sql as object query here.

https://ujjwalguptaofficial.github.io/idbstudio/?db=Demo&query=select(%7B%0D%0A%20%20%20%20from%3A%20%22Products%22%2C%0D%0A%20%20%20%20where%3A%20%5B%7B%0D%0A%20%20%20%20%20%20%20%20supplierId%3A%201%2C%0D%0A%20%20%20%20%7D%2C%20%7B%0D%0A%20%20%20%20%20%20%20%20categoryId%3A%201%2C%0D%0A%20%20%20%20%20%20%20%20price%3A%2018%2C%0D%0A%20%20%20%20%7D%2C%20%7B%0D%0A%20%20%20%20%20%20%20%20or%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20categoryId%3A%202%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20price%3A%2039%2C%0D%0A%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%5D%0D%0A%7D)%3B

from jsstore.

xuyongweb avatar xuyongweb commented on August 25, 2024

Thanks! Problem solved

from jsstore.

ujjwalguptaofficial avatar ujjwalguptaofficial commented on August 25, 2024

Nice :). I am also working on making documentation better 🎉 .

from jsstore.

xuyongweb avatar xuyongweb commented on August 25, 2024
// 新增
connection.insert = async function(body) {
	// if(body['into']== 'Members') console.log('开始插入',body)
  // console.log('开始插入',body)
  if (!isOpen()) await openDb();
  //获取表格字段
  let tableItems = initTable.filter(el => {
    return el.name == body['into']
  })
  // console.log('tableItems',tableItems)
  const tableItem = {...tableItems[0]['columns']}
  Object.keys(tableItem).forEach(mkk => {
	// 如果是主键自增字段,删除数据,让他自增长
    if ('autoIncrement' in tableItem[mkk] && tableItem[mkk]['autoIncrement']) {
      delete tableItem[mkk];
	  return;
    }
	//如果设置了联合索引,删除字段不做处理
	if ('keyPath' in tableItem[mkk] ) {
		delete tableItem[mkk];
		return;
	}
  })
  
  let sqlStr = ``;
  Object.keys(body).forEach(key => {
    if (key == 'into') {
      if ('upsert' in body && body['upsert']) {
        // 如果存在旧数据与新数据冲突,删除旧数据插入新数据
        sqlStr += `REPLACE INTO ${body[key]} `
      } else {
        // 与主键冲突的数据会忽略插入
        sqlStr += `INSERT IGNORE INTO ${body[key]} `
      }
    }
    if (key == 'values') {
      let keys = "";
      let values = "";
      if (Array.isArray(body[key])) {
        Object.keys(tableItem).forEach(m => {
          keys += m + ',';
        })
        keys = keys.replace(/^(\s|,)+|(\s|,)+$/g, '');
		
        body[key].forEach(n => {
          values += `(`;
          Object.keys(tableItem).forEach(mk => {
            if (mk in n) {
              if (tableItem[mk]['dataType'] == 'number') {
                if (Number(n[mk]) == 0 || Number(n[mk])) {
                  values += `'${ Number(n[mk])}',`;
                } else {
                  values += '-1,';
                }
              } else {
                values += `'${ n[mk]? sqliteEscape(n[mk]) : ""}',`;
              }
            } else {
              if (tableItem[mk]['dataType'] == 'number') {
                values += '-1,';
              } else {
                values += '"",';
              }
            }
          })
          values = values.replace(/^(\s|,)+|(\s|,)+$/g, '');
          values += `),`;
        })
        // console.log('值',values)
        keys = keys.replace(/^(\s|,)+|(\s|,)+$/g, '');
        values = values.replace(/^(\s|,)+|(\s|,)+$/g, '');
        sqlStr += `(${keys}) VALUES ${values}`;
      } else {
        // console.log('进入222')
        Object.keys(body[key]).forEach(s => {
          keys += s + ',';
          // values+=body[key][s]+',';
          if (typeof body[key][s] == 'string') {
            values += `'${sqliteEscape(body[key][s]) }',`;
          } else {
            values += `${body[key][s]},`;
          }

        });
        keys = keys.replace(/^(\s|,)+|(\s|,)+$/g, '');
        values = values.replace(/^(\s|,)+|(\s|,)+$/g, '');
        sqlStr += `(${keys}) VALUES(${values})`;
      }

    }
  })
  // if(body['into']== 'Members') console.log('新增sql语句',sqlStr)
  // console.log('新增sql语句',sqlStr)
  return new Promise((resolve, reject) => {
    plus.sqlite.executeSql({
      name: DbName,
      sql: sqlStr,
      success: async (e) => {
        // console.log('插入成功...' + sqlStr)
        if (body['return']) {
          // console.log(body['into'])
          let rowId = await getLastId(body['into']);
          resolve(rowId);
        } else {
          resolve(e);
        }

      },
      fail(e) {
        console.log('e----', e)
        console.log('sqlStr', sqlStr)
        reject(e);
      }
    })
  })
}

To be compatible with sqlite, I loop through the query objects of the jsstore and concatenate them into sqlite syntax. Would it be easier to implement using sqlweb?

from jsstore.

ujjwalguptaofficial avatar ujjwalguptaofficial commented on August 25, 2024

No, I think this is fine. In SqlWeb - syntax might be little different but feel free to experiment it.

from jsstore.

xuyongweb avatar xuyongweb commented on August 25, 2024

Because the way to loop jsstore query objects and concatenate them into sqlite syntax is a bit complicated, if sqlweb could pass in the sqlite syntax directly, then I wouldn't have to loop concatenate syntax。

from jsstore.

xuyongweb avatar xuyongweb commented on August 25, 2024

One more question, is batch updating with a transaction faster than direct updating?

from jsstore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.