parsav  Diff

Differences From Artifact [af6b4187ca]:

To Artifact [35848d4bf0]:


   300    300   				$1::bigint, case when $2::text = '' then null else $2::text end,
   301    301   				$3::text, $4::text, 
   302    302   				now(), now(), array[]::bigint[], array[]::bigint[]
   303    303   			) returning id
   304    304   		]]; -- TODO array handling
   305    305   	};
   306    306   
          307  +	post_destroy_prepare = {
          308  +		params = {uint64}, cmd = true, sql = [[
          309  +			update parsav_posts set
          310  +				parent = (select parent from parsav_posts where id = $1::bigint limit 1)
          311  +			where parent = $1::bigint
          312  +		]]
          313  +	};
          314  +
          315  +	post_destroy = {
          316  +		params = {uint64}, cmd = true, sql = [[
          317  +			delete from parsav_posts where id = $1::bigint
          318  +		]]
          319  +	};
          320  +	
   307    321   	post_fetch = {
   308    322   		params = {uint64}, sql = [[
   309    323   			select a.origin is null,
   310    324   				p.id, p.author, p.subject, p.acl, p.body,
   311    325   				extract(epoch from p.posted    )::bigint,
   312    326   				extract(epoch from p.discovered)::bigint,
   313    327   				extract(epoch from p.edited    )::bigint,
................................................................................
   773    787   			end
   774    788   		end
   775    789   	end
   776    790   
   777    791   	return powers
   778    792   end
   779    793   
          794  +
          795  +local txdo = terra(src: &lib.store.source)
          796  +	var res = lib.pq.PQexec([&lib.pq.PGconn](src.handle), 'begin')
          797  +	if lib.pq.PQresultStatus(res) == lib.pq.PGRES_COMMAND_OK then
          798  +		lib.dbg('beginning pgsql transaction')
          799  +		return true
          800  +	else
          801  +		lib.warn('backend pgsql - failed to begin transaction: \n', lib.pq.PQresultErrorMessage(res))
          802  +		return false
          803  +	end
          804  +end
          805  +
          806  +local txdone = terra(src: &lib.store.source)
          807  +	var res = lib.pq.PQexec([&lib.pq.PGconn](src.handle), 'end')
          808  +	if lib.pq.PQresultStatus(res) == lib.pq.PGRES_COMMAND_OK then
          809  +		lib.dbg('completing pgsql transaction')
          810  +		return true
          811  +	else
          812  +		lib.warn('backend pgsql - failed to complete transaction: \n', lib.pq.PQresultErrorMessage(res))
          813  +		return false
          814  +	end
          815  +end
          816  +
   780    817   local b = `lib.store.backend {
   781    818   	id = "pgsql";
   782    819   	open = [terra(src: &lib.store.source): &opaque
   783    820   		lib.report('connecting to postgres database: ', src.string.ptr)
   784    821   		var [con] = lib.pq.PQconnectdb(src.string.ptr)
   785    822   		if lib.pq.PQstatus(con) ~= lib.pq.CONNECTION_OK then
   786    823   			lib.warn('postgres backend connection failed')
................................................................................
   803    840   			return nil
   804    841   		end
   805    842   
   806    843   		return con
   807    844   	end];
   808    845   
   809    846   	close = [terra(src: &lib.store.source) lib.pq.PQfinish([&lib.pq.PGconn](src.handle)) end];
          847  +
          848  +	tx_enter = txdo, tx_complete = txdone;
   810    849   
   811    850   	conprep = [terra(src: &lib.store.source, mode: lib.store.prepmode.t)
   812    851   		var [con] = [&lib.pq.PGconn](src.handle)
   813    852   		if mode == lib.store.prepmode.full then [prep]
   814    853   		elseif mode == lib.store.prepmode.conf or
   815    854   		       mode == lib.store.prepmode.admin then 
   816    855   			queries.conf_get.prep(con)
................................................................................
   837    876   		if lib.pq.PQresultStatus(res) == lib.pq.PGRES_COMMAND_OK then
   838    877   			lib.report('successfully wiped out everything parsav-related in database')
   839    878   			return true
   840    879   		else
   841    880   			lib.warn('backend pgsql - failed to obliterate database: \n', lib.pq.PQresultErrorMessage(res))
   842    881   			return false
   843    882   		end
   844         -	end];
   845         -
   846         -	tx_enter = [terra(src: &lib.store.source)
   847         -		var res = lib.pq.PQexec([&lib.pq.PGconn](src.handle), 'begin')
   848         -		if lib.pq.PQresultStatus(res) == lib.pq.PGRES_COMMAND_OK then
   849         -			lib.dbg('beginning pgsql transaction')
   850         -			return true
   851         -		else
   852         -			lib.warn('backend pgsql - failed to begin transaction: \n', lib.pq.PQresultErrorMessage(res))
   853         -			return false
   854         -		end
   855         -	end];
   856         -
   857         -	tx_complete = [terra(src: &lib.store.source)
   858         -		var res = lib.pq.PQexec([&lib.pq.PGconn](src.handle), 'end')
   859         -		if lib.pq.PQresultStatus(res) == lib.pq.PGRES_COMMAND_OK then
   860         -			lib.dbg('completing pgsql transaction')
   861         -			return true
   862         -		else
   863         -			lib.warn('backend pgsql - failed to complete transaction: \n', lib.pq.PQresultErrorMessage(res))
   864         -			return false
   865         -		end
   866    883   	end];
   867    884   
   868    885   	conf_get = [terra(src: &lib.store.source, key: rawstring)
   869    886   		var r = queries.conf_get.exec(src, key)
   870    887   		if r.sz == 0 then return [lib.mem.ptr(int8)] { ptr = nil, ct = 0 } else
   871    888   			defer r:free()
   872    889   			return r:String(0,0)
................................................................................
  1014   1031   	): uint64
  1015   1032   		var r = queries.post_create.exec(src,post.author,post.subject,post.acl,post.body) 
  1016   1033   		if r.sz == 0 then return 0 end
  1017   1034   		defer r:free()
  1018   1035   		var id = r:int(uint64,0,0)
  1019   1036   		return id
  1020   1037   	end];
         1038  +
         1039  +	post_destroy = [terra(
         1040  +		src: &lib.store.source,
         1041  +		post: uint64
         1042  +	): {}
         1043  +		txdo(src)
         1044  +			queries.post_destroy_prepare.exec(src, post)
         1045  +			queries.post_destroy.exec(src, post)
         1046  +		txdone(src)
         1047  +	end];
  1021   1048   
  1022   1049   	post_fetch = [terra(
  1023   1050   		src: &lib.store.source,
  1024   1051   		post: uint64
  1025   1052   	): lib.mem.ptr(lib.store.post)
  1026   1053   		var r = queries.post_fetch.exec(src, post)
  1027   1054   		if r.sz == 0 then return [lib.mem.ptr(lib.store.post)].null() end