public CacheEntry createCache(Connection con, int revision, Instant date) { int cacheId = con.createQuery("insert into cache (revision, date) values (:revision, :date)") .addParameter("revision", revision) .addParameter("date", date) .executeUpdate() .getKey(int.class); CacheEntry entry = new CacheEntry(); entry.setId(cacheId); entry.setRevision(revision); entry.setDate(date); return entry; }
public IndexEntry createIndex(Connection con, CacheEntry cache, int indexId, int crc, int revision) { int id = con.createQuery("insert into `index` (cache, indexId, crc, revision) values (:cache, :indexId, :crc, :revision)") .addParameter("cache", cache.getId()) .addParameter("indexId", indexId) .addParameter("crc", crc) .addParameter("revision", revision) .executeUpdate() .getKey(int.class); IndexEntry entry = new IndexEntry(); entry.setId(id); entry.setIndexId(indexId); entry.setCrc(crc); entry.setRevision(revision); return entry; }
public ArchiveEntry createArchive(Connection con, IndexEntry index, int archiveId, int nameHash, int crc, int revision, byte[] hash) { if (insertArchive == null) { insertArchive = con.createQuery("insert into archive (archiveId, nameHash, crc, revision, hash) values " + "(:archiveId, :nameHash, :crc, :revision, :hash)"); } int id = insertArchive .addParameter("archiveId", archiveId) .addParameter("nameHash", nameHash) .addParameter("crc", crc) .addParameter("revision", revision) .addParameter("hash", hash) .executeUpdate() .getKey(int.class); ArchiveEntry entry = new ArchiveEntry(); entry.setId(id); entry.setArchiveId(archiveId); entry.setNameHash(nameHash); entry.setCrc(crc); entry.setRevision(revision); entry.setHash(hash); return entry; }
@SuppressWarnings("unchecked") // need to change Convert public <V> V getKey(Class returnType){ final Quirks quirks = this.sql2o.getQuirks(); Object key = getKey(); try { Converter<V> converter = throwIfNull(returnType, quirks.converterOf(returnType)); return converter.convert(key); } catch (ConverterException e) { throw new Sql2oException("Exception occurred while converting value from database to type " + returnType.toString(), e); } }
public static Object save(Object object){ Logger logger = LogManager.getLogger(ActiveRecordBase.class); Connection connection = DB.sql2o.beginTransaction(); Field[] fields = object.getClass().getDeclaredFields(); StringBuffer key = new StringBuffer(); StringBuffer value = new StringBuffer(); String table = Util.getclassName(object.getClass()).toLowerCase(); int i=0; for(Field field :fields){ field.setAccessible(true); key.append(field.getName()); try { String v = null==field.get(object)? "" : field.get(object).toString(); value.append("'"+v+"'"); } catch (IllegalAccessException e) { e.printStackTrace(); } i++; if(i<fields.length){ key.append(","); value.append(","); } } String sql = "insert into "+table + "("+key +") values ("+value +")"; logger.info(Constant.LOG_AC_NAME+" execute sql " + sql); connection.createQuery(sql).executeUpdate().commit(); return connection.getKey(); }
public static Object insert_by_sql(String sql){ Logger logger = LogManager.getLogger(ActiveRecordBase.class); logger.info(Constant.LOG_AC_NAME+" execute sql " + sql); return DB.sql2o.beginTransaction().createQuery(sql).executeUpdate().getKey(); }
public <V> V getKey(Class<V> returnType) { final Quirks quirks = this.sql2o.getQuirks(); Object key = getKey(); try { Converter<V> converter = throwIfNull(returnType, quirks.converterOf(returnType)); return converter.convert(key); } catch (ConverterException e) { throw new Sql2oException("Exception occurred while converting value from database to type " + returnType.toString(), e); } }
@SuppressWarnings("unchecked") // need to change Convert public <V> V getKey(Class returnType){ final Quirks quirks = this.sql2o.getQuirks(); Object key = getKey(); try { Converter<V> converter = throwIfNull(returnType, quirks.converterOf(returnType)); return converter.convert(key); } catch (ConverterException e) { throw new Sql2oException("Exception occurred while converting value from database to type " + returnType.toString(), e); } }
public <S extends Serializable> S save() { QueryMeta queryMeta = SqlBuilder.buildInsertSql(this); Connection con = getConn(); log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql()); log.debug(PARAMETER_PREFIX + " => {}", this); Query query = con.createQuery(queryMeta.getSql()).bind(this); try { S s = (S) query.executeUpdate().getKey(); if (null == Base.connectionThreadLocal.get() && !con.getJdbcConnection().getAutoCommit()) { con.commit(); } return s; } catch (Exception e) { throw new RuntimeException(e); } }
private void insert(final Todo todo) { sql2oHandler.withSql2o(sql2o -> { sql2o.withConnection((connection, argument) -> { String sql = "INSERT INTO todos(description, done) VALUES (:description, false)"; Object key = connection.createQuery(sql) .bind(todo) .executeUpdate() .getKey(); todo.setId(((Number) key).longValue()); }); return todo; }); }
/** * Save a model * * @param model model instance * @param <S> * @return ResultKey */ public <S extends Model> ResultKey save(S model) { List<Object> columnValues = AnimaUtils.toColumnValues(model, true); String sql = this.buildInsertSQL(model, columnValues); Connection conn = getConn(); try { List<Object> params = columnValues.stream() .filter(Objects::nonNull) .collect(toList()); return new ResultKey(conn.createQuery(sql) .withParams(params) .executeUpdate() .getKey()); } finally { this.closeConn(conn); this.clean(conn); } }
public Pizza save(Pizza pizza) { try (Connection con = sql2o.beginTransaction()) { long pizzaId = (long) con.createQuery("INSERT INTO pizza (base_id, name, price) VALUES (:baseId, :name, :price)", true) .addParameter("baseId", pizza.getBase().getId()) .addParameter("name", pizza.getName()) .addParameter("price", pizza.getPrice()) .executeUpdate() .getKey(); Query queryForPt = con.createQuery("INSERT INTO pizza_toppings (pizza_id, toppings_id) VALUES (:pizzaId, :toppingsId)"); pizza.getToppings().forEach(t -> queryForPt .addParameter("pizzaId", pizzaId) .addParameter("toppingsId", t.getId()) .addToBatch()); queryForPt.executeBatch(); con.commit(); } return pizza; }